raspberry.patch 2.9 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704737057370673707737087370973710737117371273713737147371573716737177371873719737207372173722737237372473725737267372773728737297373073731737327373373734737357373673737737387373973740737417374273743737447374573746737477374873749737507375173752737537375473755737567375773758737597376073761737627376373764737657376673767737687376973770737717377273773737747377573776737777377873779737807378173782737837378473785737867378773788737897379073791737927379373794737957379673797737987379973800738017380273803738047380573806738077380873809738107381173812738137381473815738167381773818738197382073821738227382373824738257382673827738287382973830738317383273833738347383573836738377383873839738407384173842738437384473845738467384773848738497385073851738527385373854738557385673857738587385973860738617386273863738647386573866738677386873869738707387173872738737387473875738767387773878738797388073881738827388373884738857388673887738887388973890738917389273893738947389573896738977389873899739007390173902739037390473905739067390773908739097391073911739127391373914739157391673917739187391973920739217392273923739247392573926739277392873929739307393173932739337393473935739367393773938739397394073941739427394373944739457394673947739487394973950739517395273953739547395573956739577395873959739607396173962739637396473965739667396773968739697397073971739727397373974739757397673977739787397973980739817398273983739847398573986739877398873989739907399173992739937399473995739967399773998739997400074001740027400374004740057400674007740087400974010740117401274013740147401574016740177401874019740207402174022740237402474025740267402774028740297403074031740327403374034740357403674037740387403974040740417404274043740447404574046740477404874049740507405174052740537405474055740567405774058740597406074061740627406374064740657406674067740687406974070740717407274073740747407574076740777407874079740807408174082740837408474085740867408774088740897409074091740927409374094740957409674097740987409974100741017410274103741047410574106741077410874109741107411174112741137411474115741167411774118741197412074121741227412374124741257412674127741287412974130741317413274133741347413574136741377413874139741407414174142741437414474145741467414774148741497415074151741527415374154741557415674157741587415974160741617416274163741647416574166741677416874169741707417174172741737417474175741767417774178741797418074181741827418374184741857418674187741887418974190741917419274193741947419574196741977419874199742007420174202742037420474205742067420774208742097421074211742127421374214742157421674217742187421974220742217422274223742247422574226742277422874229742307423174232742337423474235742367423774238742397424074241742427424374244742457424674247742487424974250742517425274253742547425574256742577425874259742607426174262742637426474265742667426774268742697427074271742727427374274742757427674277742787427974280742817428274283742847428574286742877428874289742907429174292742937429474295742967429774298742997430074301743027430374304743057430674307743087430974310743117431274313743147431574316743177431874319743207432174322743237432474325743267432774328743297433074331743327433374334743357433674337743387433974340743417434274343743447434574346743477434874349743507435174352743537435474355743567435774358743597436074361743627436374364743657436674367743687436974370743717437274373743747437574376743777437874379743807438174382743837438474385743867438774388743897439074391743927439374394743957439674397743987439974400744017440274403744047440574406744077440874409744107441174412744137441474415744167441774418744197442074421744227442374424744257442674427744287442974430744317443274433744347443574436744377443874439744407444174442744437444474445744467444774448744497445074451744527445374454744557445674457744587445974460744617446274463744647446574466744677446874469744707447174472744737447474475744767447774478744797448074481744827448374484744857448674487744887448974490744917449274493744947449574496744977449874499745007450174502745037450474505745067450774508745097451074511745127451374514745157451674517745187451974520745217452274523745247452574526745277452874529745307453174532745337453474535745367453774538745397454074541745427454374544745457454674547745487454974550745517455274553745547455574556745577455874559745607456174562745637456474565745667456774568745697457074571745727457374574745757457674577745787457974580745817458274583745847458574586745877458874589745907459174592745937459474595745967459774598745997460074601746027460374604746057460674607746087460974610746117461274613746147461574616746177461874619746207462174622746237462474625746267462774628746297463074631746327463374634746357463674637746387463974640746417464274643746447464574646746477464874649746507465174652746537465474655746567465774658746597466074661746627466374664746657466674667746687466974670746717467274673746747467574676746777467874679746807468174682746837468474685746867468774688746897469074691746927469374694746957469674697746987469974700747017470274703747047470574706747077470874709747107471174712747137471474715747167471774718747197472074721747227472374724747257472674727747287472974730747317473274733747347473574736747377473874739747407474174742747437474474745747467474774748747497475074751747527475374754747557475674757747587475974760747617476274763747647476574766747677476874769747707477174772747737477474775747767477774778747797478074781747827478374784747857478674787747887478974790747917479274793747947479574796747977479874799748007480174802748037480474805748067480774808748097481074811748127481374814748157481674817748187481974820748217482274823748247482574826748277482874829748307483174832748337483474835748367483774838748397484074841748427484374844748457484674847748487484974850748517485274853748547485574856748577485874859748607486174862748637486474865748667486774868748697487074871748727487374874748757487674877748787487974880748817488274883748847488574886748877488874889748907489174892748937489474895748967489774898748997490074901749027490374904749057490674907749087490974910749117491274913749147491574916749177491874919749207492174922749237492474925749267492774928749297493074931749327493374934749357493674937749387493974940749417494274943749447494574946749477494874949749507495174952749537495474955749567495774958749597496074961749627496374964749657496674967749687496974970749717497274973749747497574976749777497874979749807498174982749837498474985749867498774988749897499074991749927499374994749957499674997749987499975000750017500275003750047500575006750077500875009750107501175012750137501475015750167501775018750197502075021750227502375024750257502675027750287502975030750317503275033750347503575036750377503875039750407504175042750437504475045750467504775048750497505075051750527505375054750557505675057750587505975060750617506275063750647506575066750677506875069750707507175072750737507475075750767507775078750797508075081750827508375084750857508675087750887508975090750917509275093750947509575096750977509875099751007510175102751037510475105751067510775108751097511075111751127511375114751157511675117751187511975120751217512275123751247512575126751277512875129751307513175132751337513475135751367513775138751397514075141751427514375144751457514675147751487514975150751517515275153751547515575156751577515875159751607516175162751637516475165751667516775168751697517075171751727517375174751757517675177751787517975180751817518275183751847518575186751877518875189751907519175192751937519475195751967519775198751997520075201752027520375204752057520675207752087520975210752117521275213752147521575216752177521875219752207522175222752237522475225752267522775228752297523075231752327523375234752357523675237752387523975240752417524275243752447524575246752477524875249752507525175252752537525475255752567525775258752597526075261752627526375264752657526675267752687526975270752717527275273752747527575276752777527875279752807528175282752837528475285752867528775288752897529075291752927529375294752957529675297752987529975300753017530275303753047530575306753077530875309753107531175312753137531475315753167531775318753197532075321753227532375324753257532675327753287532975330753317533275333753347533575336753377533875339753407534175342753437534475345753467534775348753497535075351753527535375354753557535675357753587535975360753617536275363753647536575366753677536875369753707537175372753737537475375753767537775378753797538075381753827538375384753857538675387753887538975390753917539275393753947539575396753977539875399754007540175402754037540475405754067540775408754097541075411754127541375414754157541675417754187541975420754217542275423754247542575426754277542875429754307543175432754337543475435754367543775438754397544075441754427544375444754457544675447754487544975450754517545275453754547545575456754577545875459754607546175462754637546475465754667546775468754697547075471754727547375474754757547675477754787547975480754817548275483754847548575486754877548875489754907549175492754937549475495754967549775498754997550075501755027550375504755057550675507755087550975510755117551275513755147551575516755177551875519755207552175522755237552475525755267552775528755297553075531755327553375534755357553675537755387553975540755417554275543755447554575546755477554875549755507555175552755537555475555755567555775558755597556075561755627556375564755657556675567755687556975570755717557275573755747557575576755777557875579755807558175582755837558475585755867558775588755897559075591755927559375594755957559675597755987559975600756017560275603756047560575606756077560875609756107561175612756137561475615756167561775618756197562075621756227562375624756257562675627756287562975630756317563275633756347563575636756377563875639756407564175642756437564475645756467564775648756497565075651756527565375654756557565675657756587565975660756617566275663756647566575666756677566875669756707567175672756737567475675756767567775678756797568075681756827568375684756857568675687756887568975690756917569275693756947569575696756977569875699757007570175702757037570475705757067570775708757097571075711757127571375714757157571675717757187571975720757217572275723757247572575726757277572875729757307573175732757337573475735757367573775738757397574075741757427574375744757457574675747757487574975750757517575275753757547575575756757577575875759757607576175762757637576475765757667576775768757697577075771757727577375774757757577675777757787577975780757817578275783757847578575786757877578875789757907579175792757937579475795757967579775798757997580075801758027580375804758057580675807758087580975810758117581275813758147581575816758177581875819758207582175822758237582475825758267582775828758297583075831758327583375834758357583675837758387583975840758417584275843758447584575846758477584875849758507585175852758537585475855758567585775858758597586075861758627586375864758657586675867758687586975870758717587275873758747587575876758777587875879758807588175882758837588475885758867588775888758897589075891758927589375894758957589675897758987589975900759017590275903759047590575906759077590875909759107591175912759137591475915759167591775918759197592075921759227592375924759257592675927759287592975930759317593275933759347593575936759377593875939759407594175942759437594475945759467594775948759497595075951759527595375954759557595675957759587595975960759617596275963759647596575966759677596875969759707597175972759737597475975759767597775978759797598075981759827598375984759857598675987759887598975990759917599275993759947599575996759977599875999760007600176002760037600476005760067600776008760097601076011760127601376014760157601676017760187601976020760217602276023760247602576026760277602876029760307603176032760337603476035760367603776038760397604076041760427604376044760457604676047760487604976050760517605276053760547605576056760577605876059760607606176062760637606476065760667606776068760697607076071760727607376074760757607676077760787607976080760817608276083760847608576086760877608876089760907609176092760937609476095760967609776098760997610076101761027610376104761057610676107761087610976110761117611276113761147611576116761177611876119761207612176122761237612476125761267612776128761297613076131761327613376134761357613676137761387613976140761417614276143761447614576146761477614876149761507615176152761537615476155761567615776158761597616076161761627616376164761657616676167761687616976170761717617276173761747617576176761777617876179761807618176182761837618476185761867618776188761897619076191761927619376194761957619676197761987619976200762017620276203762047620576206762077620876209762107621176212762137621476215762167621776218762197622076221762227622376224762257622676227762287622976230762317623276233762347623576236762377623876239762407624176242762437624476245762467624776248762497625076251762527625376254762557625676257762587625976260762617626276263762647626576266762677626876269762707627176272762737627476275762767627776278762797628076281762827628376284762857628676287762887628976290762917629276293762947629576296762977629876299763007630176302763037630476305763067630776308763097631076311763127631376314763157631676317763187631976320763217632276323763247632576326763277632876329763307633176332763337633476335763367633776338763397634076341763427634376344763457634676347763487634976350763517635276353763547635576356763577635876359763607636176362763637636476365763667636776368763697637076371763727637376374763757637676377763787637976380763817638276383763847638576386763877638876389763907639176392763937639476395763967639776398763997640076401764027640376404764057640676407764087640976410764117641276413764147641576416764177641876419764207642176422764237642476425764267642776428764297643076431764327643376434764357643676437764387643976440764417644276443764447644576446764477644876449764507645176452764537645476455764567645776458764597646076461764627646376464764657646676467764687646976470764717647276473764747647576476764777647876479764807648176482764837648476485764867648776488764897649076491764927649376494764957649676497764987649976500765017650276503765047650576506765077650876509765107651176512765137651476515765167651776518765197652076521765227652376524765257652676527765287652976530765317653276533765347653576536765377653876539765407654176542765437654476545765467654776548765497655076551765527655376554765557655676557765587655976560765617656276563765647656576566765677656876569765707657176572765737657476575765767657776578765797658076581765827658376584765857658676587765887658976590765917659276593765947659576596765977659876599766007660176602766037660476605766067660776608766097661076611766127661376614766157661676617766187661976620766217662276623766247662576626766277662876629766307663176632766337663476635766367663776638766397664076641766427664376644766457664676647766487664976650766517665276653766547665576656766577665876659766607666176662766637666476665766667666776668766697667076671766727667376674766757667676677766787667976680766817668276683766847668576686766877668876689766907669176692766937669476695766967669776698766997670076701767027670376704767057670676707767087670976710767117671276713767147671576716767177671876719767207672176722767237672476725767267672776728767297673076731767327673376734767357673676737767387673976740767417674276743767447674576746767477674876749767507675176752767537675476755767567675776758767597676076761767627676376764767657676676767767687676976770767717677276773767747677576776767777677876779767807678176782767837678476785767867678776788767897679076791767927679376794767957679676797767987679976800768017680276803768047680576806768077680876809768107681176812768137681476815768167681776818768197682076821768227682376824768257682676827768287682976830768317683276833768347683576836768377683876839768407684176842768437684476845768467684776848768497685076851768527685376854768557685676857768587685976860768617686276863768647686576866768677686876869768707687176872768737687476875768767687776878768797688076881768827688376884768857688676887768887688976890768917689276893768947689576896768977689876899769007690176902769037690476905769067690776908769097691076911769127691376914769157691676917769187691976920769217692276923769247692576926769277692876929769307693176932769337693476935769367693776938769397694076941769427694376944769457694676947769487694976950769517695276953769547695576956769577695876959769607696176962769637696476965769667696776968769697697076971769727697376974769757697676977769787697976980769817698276983769847698576986769877698876989769907699176992769937699476995769967699776998769997700077001770027700377004770057700677007770087700977010770117701277013770147701577016770177701877019770207702177022770237702477025770267702777028770297703077031770327703377034770357703677037770387703977040770417704277043770447704577046770477704877049770507705177052770537705477055770567705777058770597706077061770627706377064770657706677067770687706977070770717707277073770747707577076770777707877079770807708177082770837708477085770867708777088770897709077091770927709377094770957709677097770987709977100771017710277103771047710577106771077710877109771107711177112771137711477115771167711777118771197712077121771227712377124771257712677127771287712977130771317713277133771347713577136771377713877139771407714177142771437714477145771467714777148771497715077151771527715377154771557715677157771587715977160771617716277163771647716577166771677716877169771707717177172771737717477175771767717777178771797718077181771827718377184771857718677187771887718977190771917719277193771947719577196771977719877199772007720177202772037720477205772067720777208772097721077211772127721377214772157721677217772187721977220772217722277223772247722577226772277722877229772307723177232772337723477235772367723777238772397724077241772427724377244772457724677247772487724977250772517725277253772547725577256772577725877259772607726177262772637726477265772667726777268772697727077271772727727377274772757727677277772787727977280772817728277283772847728577286772877728877289772907729177292772937729477295772967729777298772997730077301773027730377304773057730677307773087730977310773117731277313773147731577316773177731877319773207732177322773237732477325773267732777328773297733077331773327733377334773357733677337773387733977340773417734277343773447734577346773477734877349773507735177352773537735477355773567735777358773597736077361773627736377364773657736677367773687736977370773717737277373773747737577376773777737877379773807738177382773837738477385773867738777388773897739077391773927739377394773957739677397773987739977400774017740277403774047740577406774077740877409774107741177412774137741477415774167741777418774197742077421774227742377424774257742677427774287742977430774317743277433774347743577436774377743877439774407744177442774437744477445774467744777448774497745077451774527745377454774557745677457774587745977460774617746277463774647746577466774677746877469774707747177472774737747477475774767747777478774797748077481774827748377484774857748677487774887748977490774917749277493774947749577496774977749877499775007750177502775037750477505775067750777508775097751077511775127751377514775157751677517775187751977520775217752277523775247752577526775277752877529775307753177532775337753477535775367753777538775397754077541775427754377544775457754677547775487754977550775517755277553775547755577556775577755877559775607756177562775637756477565775667756777568775697757077571775727757377574775757757677577775787757977580775817758277583775847758577586775877758877589775907759177592775937759477595775967759777598775997760077601776027760377604776057760677607776087760977610776117761277613776147761577616776177761877619776207762177622776237762477625776267762777628776297763077631776327763377634776357763677637776387763977640776417764277643776447764577646776477764877649776507765177652776537765477655776567765777658776597766077661776627766377664776657766677667776687766977670776717767277673776747767577676776777767877679776807768177682776837768477685776867768777688776897769077691776927769377694776957769677697776987769977700777017770277703777047770577706777077770877709777107771177712777137771477715777167771777718777197772077721777227772377724777257772677727777287772977730777317773277733777347773577736777377773877739777407774177742777437774477745777467774777748777497775077751777527775377754777557775677757777587775977760777617776277763777647776577766777677776877769777707777177772777737777477775777767777777778777797778077781777827778377784777857778677787777887778977790777917779277793777947779577796777977779877799778007780177802778037780477805778067780777808778097781077811778127781377814778157781677817778187781977820778217782277823778247782577826778277782877829778307783177832778337783477835778367783777838778397784077841778427784377844778457784677847778487784977850778517785277853778547785577856778577785877859778607786177862778637786477865778667786777868778697787077871778727787377874778757787677877778787787977880778817788277883778847788577886778877788877889778907789177892778937789477895778967789777898778997790077901779027790377904779057790677907779087790977910779117791277913779147791577916779177791877919779207792177922779237792477925779267792777928779297793077931779327793377934779357793677937779387793977940779417794277943779447794577946779477794877949779507795177952779537795477955779567795777958779597796077961779627796377964779657796677967779687796977970779717797277973779747797577976779777797877979779807798177982779837798477985779867798777988779897799077991779927799377994779957799677997779987799978000780017800278003780047800578006780077800878009780107801178012780137801478015780167801778018780197802078021780227802378024780257802678027780287802978030780317803278033780347803578036780377803878039780407804178042780437804478045780467804778048780497805078051780527805378054780557805678057780587805978060780617806278063780647806578066780677806878069780707807178072780737807478075780767807778078780797808078081780827808378084780857808678087780887808978090780917809278093780947809578096780977809878099781007810178102781037810478105781067810778108781097811078111781127811378114781157811678117781187811978120781217812278123781247812578126781277812878129781307813178132781337813478135781367813778138781397814078141781427814378144781457814678147781487814978150781517815278153781547815578156781577815878159781607816178162781637816478165781667816778168781697817078171781727817378174781757817678177781787817978180781817818278183781847818578186781877818878189781907819178192781937819478195781967819778198781997820078201782027820378204782057820678207782087820978210782117821278213782147821578216782177821878219782207822178222782237822478225782267822778228782297823078231782327823378234782357823678237782387823978240782417824278243782447824578246782477824878249782507825178252782537825478255782567825778258782597826078261782627826378264782657826678267782687826978270782717827278273782747827578276782777827878279782807828178282782837828478285782867828778288782897829078291782927829378294782957829678297782987829978300783017830278303783047830578306783077830878309783107831178312783137831478315783167831778318783197832078321783227832378324783257832678327783287832978330783317833278333783347833578336783377833878339783407834178342783437834478345783467834778348783497835078351783527835378354783557835678357783587835978360783617836278363783647836578366783677836878369783707837178372783737837478375783767837778378783797838078381783827838378384783857838678387783887838978390783917839278393783947839578396783977839878399784007840178402784037840478405784067840778408784097841078411784127841378414784157841678417784187841978420784217842278423784247842578426784277842878429784307843178432784337843478435784367843778438784397844078441784427844378444784457844678447784487844978450784517845278453784547845578456784577845878459784607846178462784637846478465784667846778468784697847078471784727847378474784757847678477784787847978480784817848278483784847848578486784877848878489784907849178492784937849478495784967849778498784997850078501785027850378504785057850678507785087850978510785117851278513785147851578516785177851878519785207852178522785237852478525785267852778528785297853078531785327853378534785357853678537785387853978540785417854278543785447854578546785477854878549785507855178552785537855478555785567855778558785597856078561785627856378564785657856678567785687856978570785717857278573785747857578576785777857878579785807858178582785837858478585785867858778588785897859078591785927859378594785957859678597785987859978600786017860278603786047860578606786077860878609786107861178612786137861478615786167861778618786197862078621786227862378624786257862678627786287862978630786317863278633786347863578636786377863878639786407864178642786437864478645786467864778648786497865078651786527865378654786557865678657786587865978660786617866278663786647866578666786677866878669786707867178672786737867478675786767867778678786797868078681786827868378684786857868678687786887868978690786917869278693786947869578696786977869878699787007870178702787037870478705787067870778708787097871078711787127871378714787157871678717787187871978720787217872278723787247872578726787277872878729787307873178732787337873478735787367873778738787397874078741787427874378744787457874678747787487874978750787517875278753787547875578756787577875878759787607876178762787637876478765787667876778768787697877078771787727877378774787757877678777787787877978780787817878278783787847878578786787877878878789787907879178792787937879478795787967879778798787997880078801788027880378804788057880678807788087880978810788117881278813788147881578816788177881878819788207882178822788237882478825788267882778828788297883078831788327883378834788357883678837788387883978840788417884278843788447884578846788477884878849788507885178852788537885478855788567885778858788597886078861788627886378864788657886678867788687886978870788717887278873788747887578876788777887878879788807888178882788837888478885788867888778888788897889078891788927889378894788957889678897788987889978900789017890278903789047890578906789077890878909789107891178912789137891478915789167891778918789197892078921789227892378924789257892678927789287892978930789317893278933789347893578936789377893878939789407894178942789437894478945789467894778948789497895078951789527895378954789557895678957789587895978960789617896278963789647896578966789677896878969789707897178972789737897478975789767897778978789797898078981789827898378984789857898678987789887898978990789917899278993789947899578996789977899878999790007900179002790037900479005790067900779008790097901079011790127901379014790157901679017790187901979020790217902279023790247902579026790277902879029790307903179032790337903479035790367903779038790397904079041790427904379044790457904679047790487904979050790517905279053790547905579056790577905879059790607906179062790637906479065790667906779068790697907079071790727907379074790757907679077790787907979080790817908279083790847908579086790877908879089790907909179092790937909479095790967909779098790997910079101791027910379104791057910679107791087910979110791117911279113791147911579116791177911879119791207912179122791237912479125791267912779128791297913079131791327913379134791357913679137791387913979140791417914279143791447914579146791477914879149791507915179152791537915479155791567915779158791597916079161791627916379164791657916679167791687916979170791717917279173791747917579176791777917879179791807918179182791837918479185791867918779188791897919079191791927919379194791957919679197791987919979200792017920279203792047920579206792077920879209792107921179212792137921479215792167921779218792197922079221792227922379224792257922679227792287922979230792317923279233792347923579236792377923879239792407924179242792437924479245792467924779248792497925079251792527925379254792557925679257792587925979260792617926279263792647926579266792677926879269792707927179272792737927479275792767927779278792797928079281792827928379284792857928679287792887928979290792917929279293792947929579296792977929879299793007930179302793037930479305793067930779308793097931079311793127931379314793157931679317793187931979320793217932279323793247932579326793277932879329793307933179332793337933479335793367933779338793397934079341793427934379344793457934679347793487934979350793517935279353793547935579356793577935879359793607936179362793637936479365793667936779368793697937079371793727937379374793757937679377793787937979380793817938279383793847938579386793877938879389793907939179392793937939479395793967939779398793997940079401794027940379404794057940679407794087940979410794117941279413794147941579416794177941879419794207942179422794237942479425794267942779428794297943079431794327943379434794357943679437794387943979440794417944279443794447944579446794477944879449794507945179452794537945479455794567945779458794597946079461794627946379464794657946679467794687946979470794717947279473794747947579476794777947879479794807948179482794837948479485794867948779488794897949079491794927949379494794957949679497794987949979500795017950279503795047950579506795077950879509795107951179512795137951479515795167951779518795197952079521795227952379524795257952679527795287952979530795317953279533795347953579536795377953879539795407954179542795437954479545795467954779548795497955079551795527955379554795557955679557795587955979560795617956279563795647956579566795677956879569795707957179572795737957479575795767957779578795797958079581795827958379584795857958679587795887958979590795917959279593795947959579596795977959879599796007960179602796037960479605796067960779608796097961079611796127961379614796157961679617796187961979620796217962279623796247962579626796277962879629796307963179632796337963479635796367963779638796397964079641796427964379644796457964679647796487964979650796517965279653796547965579656796577965879659796607966179662796637966479665796667966779668796697967079671796727967379674796757967679677796787967979680796817968279683796847968579686796877968879689796907969179692796937969479695796967969779698796997970079701797027970379704797057970679707797087970979710797117971279713797147971579716797177971879719797207972179722797237972479725797267972779728797297973079731797327973379734797357973679737797387973979740797417974279743797447974579746797477974879749797507975179752797537975479755797567975779758797597976079761797627976379764797657976679767797687976979770797717977279773797747977579776797777977879779797807978179782797837978479785797867978779788797897979079791797927979379794797957979679797797987979979800798017980279803798047980579806798077980879809798107981179812798137981479815798167981779818798197982079821798227982379824798257982679827798287982979830798317983279833798347983579836798377983879839798407984179842798437984479845798467984779848798497985079851798527985379854798557985679857798587985979860798617986279863798647986579866798677986879869798707987179872798737987479875798767987779878798797988079881798827988379884798857988679887798887988979890798917989279893798947989579896798977989879899799007990179902799037990479905799067990779908799097991079911799127991379914799157991679917799187991979920799217992279923799247992579926799277992879929799307993179932799337993479935799367993779938799397994079941799427994379944799457994679947799487994979950799517995279953799547995579956799577995879959799607996179962799637996479965799667996779968799697997079971799727997379974799757997679977799787997979980799817998279983799847998579986799877998879989799907999179992799937999479995799967999779998799998000080001800028000380004800058000680007800088000980010800118001280013800148001580016800178001880019800208002180022800238002480025800268002780028800298003080031800328003380034800358003680037800388003980040800418004280043800448004580046800478004880049800508005180052800538005480055800568005780058800598006080061800628006380064800658006680067800688006980070800718007280073800748007580076800778007880079800808008180082800838008480085800868008780088800898009080091800928009380094800958009680097800988009980100801018010280103801048010580106801078010880109801108011180112801138011480115801168011780118801198012080121801228012380124801258012680127801288012980130801318013280133801348013580136801378013880139801408014180142801438014480145801468014780148801498015080151801528015380154801558015680157801588015980160801618016280163801648016580166801678016880169801708017180172801738017480175801768017780178801798018080181801828018380184801858018680187801888018980190801918019280193801948019580196801978019880199802008020180202802038020480205802068020780208802098021080211802128021380214802158021680217802188021980220802218022280223802248022580226802278022880229802308023180232802338023480235802368023780238802398024080241802428024380244802458024680247802488024980250802518025280253802548025580256802578025880259802608026180262802638026480265802668026780268802698027080271802728027380274802758027680277802788027980280802818028280283802848028580286802878028880289802908029180292802938029480295802968029780298802998030080301803028030380304803058030680307803088030980310803118031280313803148031580316803178031880319803208032180322803238032480325803268032780328803298033080331803328033380334803358033680337803388033980340803418034280343803448034580346803478034880349803508035180352803538035480355803568035780358803598036080361803628036380364803658036680367803688036980370803718037280373803748037580376803778037880379803808038180382803838038480385803868038780388803898039080391803928039380394803958039680397803988039980400804018040280403804048040580406804078040880409804108041180412804138041480415804168041780418804198042080421804228042380424804258042680427804288042980430804318043280433804348043580436804378043880439804408044180442804438044480445804468044780448804498045080451804528045380454804558045680457804588045980460804618046280463804648046580466804678046880469804708047180472804738047480475804768047780478804798048080481804828048380484804858048680487804888048980490804918049280493804948049580496804978049880499805008050180502805038050480505805068050780508805098051080511805128051380514805158051680517805188051980520805218052280523805248052580526805278052880529805308053180532805338053480535805368053780538805398054080541805428054380544805458054680547805488054980550805518055280553805548055580556805578055880559805608056180562805638056480565805668056780568805698057080571805728057380574805758057680577805788057980580805818058280583805848058580586805878058880589805908059180592805938059480595805968059780598805998060080601806028060380604806058060680607806088060980610806118061280613806148061580616806178061880619806208062180622806238062480625806268062780628806298063080631806328063380634806358063680637806388063980640806418064280643806448064580646806478064880649806508065180652806538065480655806568065780658806598066080661806628066380664806658066680667806688066980670806718067280673806748067580676806778067880679806808068180682806838068480685806868068780688806898069080691806928069380694806958069680697806988069980700807018070280703807048070580706807078070880709807108071180712807138071480715807168071780718807198072080721807228072380724807258072680727807288072980730807318073280733807348073580736807378073880739807408074180742807438074480745807468074780748807498075080751807528075380754807558075680757807588075980760807618076280763807648076580766807678076880769807708077180772807738077480775807768077780778807798078080781807828078380784807858078680787807888078980790807918079280793807948079580796807978079880799808008080180802808038080480805808068080780808808098081080811808128081380814808158081680817808188081980820808218082280823808248082580826808278082880829808308083180832808338083480835808368083780838808398084080841808428084380844808458084680847808488084980850808518085280853808548085580856808578085880859808608086180862808638086480865808668086780868808698087080871808728087380874808758087680877808788087980880808818088280883808848088580886808878088880889808908089180892808938089480895808968089780898808998090080901809028090380904809058090680907809088090980910809118091280913809148091580916809178091880919809208092180922809238092480925809268092780928809298093080931809328093380934809358093680937809388093980940809418094280943809448094580946809478094880949809508095180952809538095480955809568095780958809598096080961809628096380964809658096680967809688096980970809718097280973809748097580976809778097880979809808098180982809838098480985809868098780988809898099080991809928099380994809958099680997809988099981000810018100281003810048100581006810078100881009810108101181012810138101481015810168101781018810198102081021810228102381024810258102681027810288102981030810318103281033810348103581036810378103881039810408104181042810438104481045810468104781048810498105081051810528105381054810558105681057810588105981060810618106281063810648106581066810678106881069810708107181072810738107481075810768107781078810798108081081810828108381084810858108681087810888108981090810918109281093810948109581096810978109881099811008110181102811038110481105811068110781108811098111081111811128111381114811158111681117811188111981120811218112281123811248112581126811278112881129811308113181132811338113481135811368113781138811398114081141811428114381144811458114681147811488114981150811518115281153811548115581156811578115881159811608116181162811638116481165811668116781168811698117081171811728117381174811758117681177811788117981180811818118281183811848118581186811878118881189811908119181192811938119481195811968119781198811998120081201812028120381204812058120681207812088120981210812118121281213812148121581216812178121881219812208122181222812238122481225812268122781228812298123081231812328123381234812358123681237812388123981240812418124281243812448124581246812478124881249812508125181252812538125481255812568125781258812598126081261812628126381264812658126681267812688126981270812718127281273812748127581276812778127881279812808128181282812838128481285812868128781288812898129081291812928129381294812958129681297812988129981300813018130281303813048130581306813078130881309813108131181312813138131481315813168131781318813198132081321813228132381324813258132681327813288132981330813318133281333813348133581336813378133881339813408134181342813438134481345813468134781348813498135081351813528135381354813558135681357813588135981360813618136281363813648136581366813678136881369813708137181372813738137481375813768137781378813798138081381813828138381384813858138681387813888138981390813918139281393813948139581396813978139881399814008140181402814038140481405814068140781408814098141081411814128141381414814158141681417814188141981420814218142281423814248142581426814278142881429814308143181432814338143481435814368143781438814398144081441814428144381444814458144681447814488144981450814518145281453814548145581456814578145881459814608146181462814638146481465814668146781468814698147081471814728147381474814758147681477814788147981480814818148281483814848148581486814878148881489814908149181492814938149481495814968149781498814998150081501815028150381504815058150681507815088150981510815118151281513815148151581516815178151881519815208152181522815238152481525815268152781528815298153081531815328153381534815358153681537815388153981540815418154281543815448154581546815478154881549815508155181552815538155481555815568155781558815598156081561815628156381564815658156681567815688156981570815718157281573815748157581576815778157881579815808158181582815838158481585815868158781588815898159081591815928159381594815958159681597815988159981600816018160281603816048160581606816078160881609816108161181612816138161481615816168161781618816198162081621816228162381624816258162681627816288162981630816318163281633816348163581636816378163881639816408164181642816438164481645816468164781648816498165081651816528165381654816558165681657816588165981660816618166281663816648166581666816678166881669816708167181672816738167481675816768167781678816798168081681816828168381684816858168681687816888168981690816918169281693816948169581696816978169881699817008170181702817038170481705817068170781708817098171081711817128171381714817158171681717817188171981720817218172281723817248172581726817278172881729817308173181732817338173481735817368173781738817398174081741817428174381744817458174681747817488174981750817518175281753817548175581756817578175881759817608176181762817638176481765817668176781768817698177081771817728177381774817758177681777817788177981780817818178281783817848178581786817878178881789817908179181792817938179481795817968179781798817998180081801818028180381804818058180681807818088180981810818118181281813818148181581816818178181881819818208182181822818238182481825818268182781828818298183081831818328183381834818358183681837818388183981840818418184281843818448184581846818478184881849818508185181852818538185481855818568185781858818598186081861818628186381864818658186681867818688186981870818718187281873818748187581876818778187881879818808188181882818838188481885818868188781888818898189081891818928189381894818958189681897818988189981900819018190281903819048190581906819078190881909819108191181912819138191481915819168191781918819198192081921819228192381924819258192681927819288192981930819318193281933819348193581936819378193881939819408194181942819438194481945819468194781948819498195081951819528195381954819558195681957819588195981960819618196281963819648196581966819678196881969819708197181972819738197481975819768197781978819798198081981819828198381984819858198681987819888198981990819918199281993819948199581996819978199881999820008200182002820038200482005820068200782008820098201082011820128201382014820158201682017820188201982020820218202282023820248202582026820278202882029820308203182032820338203482035820368203782038820398204082041820428204382044820458204682047820488204982050820518205282053820548205582056820578205882059820608206182062820638206482065820668206782068820698207082071820728207382074820758207682077820788207982080820818208282083820848208582086820878208882089820908209182092820938209482095820968209782098820998210082101821028210382104821058210682107821088210982110821118211282113821148211582116821178211882119821208212182122821238212482125821268212782128821298213082131821328213382134821358213682137821388213982140821418214282143821448214582146821478214882149821508215182152821538215482155821568215782158821598216082161821628216382164821658216682167821688216982170821718217282173821748217582176821778217882179821808218182182821838218482185821868218782188821898219082191821928219382194821958219682197821988219982200822018220282203822048220582206822078220882209822108221182212822138221482215822168221782218822198222082221822228222382224822258222682227822288222982230822318223282233822348223582236822378223882239822408224182242822438224482245822468224782248822498225082251822528225382254822558225682257822588225982260822618226282263822648226582266822678226882269822708227182272822738227482275822768227782278822798228082281822828228382284822858228682287822888228982290822918229282293822948229582296822978229882299823008230182302823038230482305823068230782308823098231082311823128231382314823158231682317823188231982320823218232282323823248232582326823278232882329823308233182332823338233482335823368233782338823398234082341823428234382344823458234682347823488234982350823518235282353823548235582356823578235882359823608236182362823638236482365823668236782368823698237082371823728237382374823758237682377823788237982380823818238282383823848238582386823878238882389823908239182392823938239482395823968239782398823998240082401824028240382404824058240682407824088240982410824118241282413824148241582416824178241882419824208242182422824238242482425824268242782428824298243082431824328243382434824358243682437824388243982440824418244282443824448244582446824478244882449824508245182452824538245482455824568245782458824598246082461824628246382464824658246682467824688246982470824718247282473824748247582476824778247882479824808248182482824838248482485824868248782488824898249082491824928249382494824958249682497824988249982500825018250282503825048250582506825078250882509825108251182512825138251482515825168251782518825198252082521825228252382524825258252682527825288252982530825318253282533825348253582536825378253882539825408254182542825438254482545825468254782548825498255082551825528255382554825558255682557825588255982560825618256282563825648256582566825678256882569825708257182572825738257482575825768257782578825798258082581825828258382584825858258682587825888258982590825918259282593825948259582596825978259882599826008260182602826038260482605826068260782608826098261082611826128261382614826158261682617826188261982620826218262282623826248262582626826278262882629826308263182632826338263482635826368263782638826398264082641826428264382644826458264682647826488264982650826518265282653826548265582656826578265882659826608266182662826638266482665826668266782668826698267082671826728267382674826758267682677826788267982680826818268282683826848268582686826878268882689826908269182692826938269482695826968269782698826998270082701827028270382704827058270682707827088270982710827118271282713827148271582716827178271882719827208272182722827238272482725827268272782728827298273082731827328273382734827358273682737827388273982740827418274282743827448274582746827478274882749827508275182752827538275482755827568275782758827598276082761827628276382764827658276682767827688276982770827718277282773827748277582776827778277882779827808278182782827838278482785827868278782788827898279082791827928279382794827958279682797827988279982800828018280282803828048280582806828078280882809828108281182812828138281482815828168281782818828198282082821828228282382824828258282682827828288282982830828318283282833828348283582836828378283882839828408284182842828438284482845828468284782848828498285082851828528285382854828558285682857828588285982860828618286282863828648286582866828678286882869828708287182872828738287482875828768287782878828798288082881828828288382884828858288682887828888288982890828918289282893828948289582896828978289882899829008290182902829038290482905829068290782908829098291082911829128291382914829158291682917829188291982920829218292282923829248292582926829278292882929829308293182932829338293482935829368293782938829398294082941829428294382944829458294682947829488294982950829518295282953829548295582956829578295882959829608296182962829638296482965829668296782968829698297082971829728297382974829758297682977829788297982980829818298282983829848298582986829878298882989829908299182992829938299482995829968299782998829998300083001830028300383004830058300683007830088300983010830118301283013830148301583016830178301883019830208302183022830238302483025830268302783028830298303083031830328303383034830358303683037830388303983040830418304283043830448304583046830478304883049830508305183052830538305483055830568305783058830598306083061830628306383064830658306683067830688306983070830718307283073830748307583076830778307883079830808308183082830838308483085830868308783088830898309083091830928309383094830958309683097830988309983100831018310283103831048310583106831078310883109831108311183112831138311483115831168311783118831198312083121831228312383124831258312683127831288312983130831318313283133831348313583136831378313883139831408314183142831438314483145831468314783148831498315083151831528315383154831558315683157831588315983160831618316283163831648316583166831678316883169831708317183172831738317483175831768317783178831798318083181831828318383184831858318683187831888318983190831918319283193831948319583196831978319883199832008320183202832038320483205832068320783208832098321083211832128321383214832158321683217832188321983220832218322283223832248322583226832278322883229832308323183232832338323483235832368323783238832398324083241832428324383244832458324683247832488324983250832518325283253832548325583256832578325883259832608326183262832638326483265832668326783268832698327083271832728327383274832758327683277832788327983280832818328283283832848328583286832878328883289832908329183292832938329483295832968329783298832998330083301833028330383304833058330683307833088330983310833118331283313833148331583316833178331883319833208332183322833238332483325833268332783328833298333083331833328333383334833358333683337833388333983340833418334283343833448334583346833478334883349833508335183352833538335483355833568335783358833598336083361833628336383364833658336683367833688336983370833718337283373833748337583376833778337883379833808338183382833838338483385833868338783388833898339083391833928339383394833958339683397833988339983400834018340283403834048340583406834078340883409834108341183412834138341483415834168341783418834198342083421834228342383424834258342683427834288342983430834318343283433834348343583436834378343883439834408344183442834438344483445834468344783448834498345083451834528345383454834558345683457834588345983460834618346283463834648346583466834678346883469834708347183472834738347483475834768347783478834798348083481834828348383484834858348683487834888348983490834918349283493834948349583496834978349883499835008350183502835038350483505835068350783508835098351083511835128351383514835158351683517835188351983520835218352283523835248352583526835278352883529835308353183532835338353483535835368353783538835398354083541835428354383544835458354683547835488354983550835518355283553835548355583556835578355883559835608356183562835638356483565835668356783568835698357083571835728357383574835758357683577835788357983580835818358283583835848358583586835878358883589835908359183592835938359483595835968359783598835998360083601836028360383604836058360683607836088360983610836118361283613836148361583616836178361883619836208362183622836238362483625836268362783628836298363083631836328363383634836358363683637836388363983640836418364283643836448364583646836478364883649836508365183652836538365483655836568365783658836598366083661836628366383664836658366683667836688366983670836718367283673836748367583676836778367883679836808368183682836838368483685836868368783688836898369083691836928369383694836958369683697836988369983700837018370283703837048370583706837078370883709837108371183712837138371483715837168371783718837198372083721837228372383724837258372683727837288372983730837318373283733837348373583736837378373883739837408374183742837438374483745837468374783748837498375083751837528375383754837558375683757837588375983760837618376283763837648376583766837678376883769837708377183772837738377483775837768377783778837798378083781837828378383784837858378683787837888378983790837918379283793837948379583796837978379883799838008380183802838038380483805838068380783808838098381083811838128381383814838158381683817838188381983820838218382283823838248382583826838278382883829838308383183832838338383483835838368383783838838398384083841838428384383844838458384683847838488384983850838518385283853838548385583856838578385883859838608386183862838638386483865838668386783868838698387083871838728387383874838758387683877838788387983880838818388283883838848388583886838878388883889838908389183892838938389483895838968389783898838998390083901839028390383904839058390683907839088390983910839118391283913839148391583916839178391883919839208392183922839238392483925839268392783928839298393083931839328393383934839358393683937839388393983940839418394283943839448394583946839478394883949839508395183952839538395483955839568395783958839598396083961839628396383964839658396683967839688396983970839718397283973839748397583976839778397883979839808398183982839838398483985839868398783988839898399083991839928399383994839958399683997839988399984000840018400284003840048400584006840078400884009840108401184012840138401484015840168401784018840198402084021840228402384024840258402684027840288402984030840318403284033840348403584036840378403884039840408404184042840438404484045840468404784048840498405084051840528405384054840558405684057840588405984060840618406284063840648406584066840678406884069840708407184072840738407484075840768407784078840798408084081840828408384084840858408684087840888408984090840918409284093840948409584096840978409884099841008410184102841038410484105841068410784108841098411084111841128411384114841158411684117841188411984120841218412284123841248412584126841278412884129841308413184132841338413484135841368413784138841398414084141841428414384144841458414684147841488414984150841518415284153841548415584156841578415884159841608416184162841638416484165841668416784168841698417084171841728417384174841758417684177841788417984180841818418284183841848418584186841878418884189841908419184192841938419484195841968419784198841998420084201842028420384204842058420684207842088420984210842118421284213842148421584216842178421884219842208422184222842238422484225842268422784228842298423084231842328423384234842358423684237842388423984240842418424284243842448424584246842478424884249842508425184252842538425484255842568425784258842598426084261842628426384264842658426684267842688426984270842718427284273842748427584276842778427884279842808428184282842838428484285842868428784288842898429084291842928429384294842958429684297842988429984300843018430284303843048430584306843078430884309843108431184312843138431484315843168431784318843198432084321843228432384324843258432684327843288432984330843318433284333843348433584336843378433884339843408434184342843438434484345843468434784348843498435084351843528435384354843558435684357843588435984360843618436284363843648436584366843678436884369843708437184372843738437484375843768437784378843798438084381843828438384384843858438684387843888438984390843918439284393843948439584396843978439884399844008440184402844038440484405844068440784408844098441084411844128441384414844158441684417844188441984420844218442284423844248442584426844278442884429844308443184432844338443484435844368443784438844398444084441844428444384444844458444684447844488444984450844518445284453844548445584456844578445884459844608446184462844638446484465844668446784468844698447084471844728447384474844758447684477844788447984480844818448284483844848448584486844878448884489844908449184492844938449484495844968449784498844998450084501845028450384504845058450684507845088450984510845118451284513845148451584516845178451884519845208452184522845238452484525845268452784528845298453084531845328453384534845358453684537845388453984540845418454284543845448454584546845478454884549845508455184552845538455484555845568455784558845598456084561845628456384564845658456684567845688456984570845718457284573845748457584576845778457884579845808458184582845838458484585845868458784588845898459084591845928459384594845958459684597845988459984600846018460284603846048460584606846078460884609846108461184612846138461484615846168461784618846198462084621846228462384624846258462684627846288462984630846318463284633846348463584636846378463884639846408464184642846438464484645846468464784648846498465084651846528465384654846558465684657846588465984660846618466284663846648466584666846678466884669846708467184672846738467484675846768467784678846798468084681846828468384684846858468684687846888468984690846918469284693846948469584696846978469884699847008470184702847038470484705847068470784708847098471084711847128471384714847158471684717847188471984720847218472284723847248472584726847278472884729847308473184732847338473484735847368473784738847398474084741847428474384744847458474684747847488474984750847518475284753847548475584756847578475884759847608476184762847638476484765847668476784768847698477084771847728477384774847758477684777847788477984780847818478284783847848478584786847878478884789847908479184792847938479484795847968479784798847998480084801848028480384804848058480684807848088480984810848118481284813848148481584816848178481884819848208482184822848238482484825848268482784828848298483084831848328483384834848358483684837848388483984840848418484284843848448484584846848478484884849848508485184852848538485484855848568485784858848598486084861848628486384864848658486684867848688486984870848718487284873848748487584876848778487884879848808488184882848838488484885848868488784888848898489084891848928489384894848958489684897848988489984900849018490284903849048490584906849078490884909849108491184912849138491484915849168491784918849198492084921849228492384924849258492684927849288492984930849318493284933849348493584936849378493884939849408494184942849438494484945849468494784948849498495084951849528495384954849558495684957849588495984960849618496284963849648496584966849678496884969849708497184972849738497484975849768497784978849798498084981849828498384984849858498684987849888498984990849918499284993849948499584996849978499884999850008500185002850038500485005850068500785008850098501085011850128501385014850158501685017850188501985020850218502285023850248502585026850278502885029850308503185032850338503485035850368503785038850398504085041850428504385044850458504685047850488504985050850518505285053850548505585056850578505885059850608506185062850638506485065850668506785068850698507085071850728507385074850758507685077850788507985080850818508285083850848508585086850878508885089850908509185092850938509485095850968509785098850998510085101851028510385104851058510685107851088510985110851118511285113851148511585116851178511885119851208512185122851238512485125851268512785128851298513085131851328513385134851358513685137851388513985140851418514285143851448514585146851478514885149851508515185152851538515485155851568515785158851598516085161851628516385164851658516685167851688516985170851718517285173851748517585176851778517885179851808518185182851838518485185851868518785188851898519085191851928519385194851958519685197851988519985200852018520285203852048520585206852078520885209852108521185212852138521485215852168521785218852198522085221852228522385224852258522685227852288522985230852318523285233852348523585236852378523885239852408524185242852438524485245852468524785248852498525085251852528525385254852558525685257852588525985260852618526285263852648526585266852678526885269852708527185272852738527485275852768527785278852798528085281852828528385284852858528685287852888528985290852918529285293852948529585296852978529885299853008530185302853038530485305853068530785308853098531085311853128531385314853158531685317853188531985320853218532285323853248532585326853278532885329853308533185332853338533485335853368533785338853398534085341853428534385344853458534685347853488534985350853518535285353853548535585356853578535885359853608536185362853638536485365853668536785368853698537085371853728537385374853758537685377853788537985380853818538285383853848538585386853878538885389853908539185392853938539485395853968539785398853998540085401854028540385404854058540685407854088540985410854118541285413854148541585416854178541885419854208542185422854238542485425854268542785428854298543085431854328543385434854358543685437854388543985440854418544285443854448544585446854478544885449854508545185452854538545485455854568545785458854598546085461854628546385464854658546685467854688546985470854718547285473854748547585476854778547885479854808548185482854838548485485854868548785488854898549085491854928549385494854958549685497854988549985500855018550285503855048550585506855078550885509855108551185512855138551485515855168551785518855198552085521855228552385524855258552685527855288552985530855318553285533855348553585536855378553885539855408554185542855438554485545855468554785548855498555085551855528555385554855558555685557855588555985560855618556285563855648556585566855678556885569855708557185572855738557485575855768557785578855798558085581855828558385584855858558685587855888558985590855918559285593855948559585596855978559885599856008560185602856038560485605856068560785608856098561085611856128561385614856158561685617856188561985620856218562285623856248562585626856278562885629856308563185632856338563485635856368563785638856398564085641856428564385644856458564685647856488564985650856518565285653856548565585656856578565885659856608566185662856638566485665856668566785668856698567085671856728567385674856758567685677856788567985680856818568285683856848568585686856878568885689856908569185692856938569485695856968569785698856998570085701857028570385704857058570685707857088570985710857118571285713857148571585716857178571885719857208572185722857238572485725857268572785728857298573085731857328573385734857358573685737857388573985740857418574285743857448574585746857478574885749857508575185752857538575485755857568575785758857598576085761857628576385764857658576685767857688576985770857718577285773857748577585776857778577885779857808578185782857838578485785857868578785788857898579085791857928579385794857958579685797857988579985800858018580285803858048580585806858078580885809858108581185812858138581485815858168581785818858198582085821858228582385824858258582685827858288582985830858318583285833858348583585836858378583885839858408584185842858438584485845858468584785848858498585085851858528585385854858558585685857858588585985860858618586285863858648586585866858678586885869858708587185872858738587485875858768587785878858798588085881858828588385884858858588685887858888588985890858918589285893858948589585896858978589885899859008590185902859038590485905859068590785908859098591085911859128591385914859158591685917859188591985920859218592285923859248592585926859278592885929859308593185932859338593485935859368593785938859398594085941859428594385944859458594685947859488594985950859518595285953859548595585956859578595885959859608596185962859638596485965859668596785968859698597085971859728597385974859758597685977859788597985980859818598285983859848598585986859878598885989859908599185992859938599485995859968599785998859998600086001860028600386004860058600686007860088600986010860118601286013860148601586016860178601886019860208602186022860238602486025860268602786028860298603086031860328603386034860358603686037860388603986040860418604286043860448604586046860478604886049860508605186052860538605486055860568605786058860598606086061860628606386064860658606686067860688606986070860718607286073860748607586076860778607886079860808608186082860838608486085860868608786088860898609086091860928609386094860958609686097860988609986100861018610286103861048610586106861078610886109861108611186112861138611486115861168611786118861198612086121861228612386124861258612686127861288612986130861318613286133861348613586136861378613886139861408614186142861438614486145861468614786148861498615086151861528615386154861558615686157861588615986160861618616286163861648616586166861678616886169861708617186172861738617486175861768617786178861798618086181861828618386184861858618686187861888618986190861918619286193861948619586196861978619886199862008620186202862038620486205862068620786208862098621086211862128621386214862158621686217862188621986220862218622286223862248622586226862278622886229862308623186232862338623486235862368623786238862398624086241862428624386244862458624686247862488624986250862518625286253862548625586256862578625886259862608626186262862638626486265862668626786268862698627086271862728627386274862758627686277862788627986280862818628286283862848628586286862878628886289862908629186292862938629486295862968629786298862998630086301863028630386304863058630686307863088630986310863118631286313863148631586316863178631886319863208632186322863238632486325863268632786328863298633086331863328633386334863358633686337863388633986340863418634286343863448634586346863478634886349863508635186352863538635486355863568635786358863598636086361863628636386364863658636686367863688636986370863718637286373863748637586376863778637886379863808638186382863838638486385863868638786388863898639086391863928639386394863958639686397863988639986400864018640286403864048640586406864078640886409864108641186412864138641486415864168641786418864198642086421864228642386424864258642686427864288642986430864318643286433864348643586436864378643886439864408644186442864438644486445864468644786448864498645086451864528645386454864558645686457864588645986460864618646286463864648646586466864678646886469864708647186472864738647486475864768647786478864798648086481864828648386484864858648686487864888648986490864918649286493864948649586496864978649886499865008650186502865038650486505865068650786508865098651086511865128651386514865158651686517865188651986520865218652286523865248652586526865278652886529865308653186532865338653486535865368653786538865398654086541865428654386544865458654686547865488654986550865518655286553865548655586556865578655886559865608656186562865638656486565865668656786568865698657086571865728657386574865758657686577865788657986580865818658286583865848658586586865878658886589865908659186592865938659486595865968659786598865998660086601866028660386604866058660686607866088660986610866118661286613866148661586616866178661886619866208662186622866238662486625866268662786628866298663086631866328663386634866358663686637866388663986640866418664286643866448664586646866478664886649866508665186652866538665486655866568665786658866598666086661866628666386664866658666686667866688666986670866718667286673866748667586676866778667886679866808668186682866838668486685866868668786688866898669086691866928669386694866958669686697866988669986700867018670286703867048670586706867078670886709867108671186712867138671486715867168671786718867198672086721867228672386724867258672686727867288672986730867318673286733867348673586736867378673886739867408674186742867438674486745867468674786748867498675086751867528675386754867558675686757867588675986760867618676286763867648676586766867678676886769867708677186772867738677486775867768677786778867798678086781867828678386784867858678686787867888678986790867918679286793867948679586796867978679886799868008680186802868038680486805868068680786808868098681086811868128681386814868158681686817868188681986820868218682286823868248682586826868278682886829868308683186832868338683486835868368683786838868398684086841868428684386844868458684686847868488684986850868518685286853868548685586856868578685886859868608686186862868638686486865868668686786868868698687086871868728687386874868758687686877868788687986880868818688286883868848688586886868878688886889868908689186892868938689486895868968689786898868998690086901869028690386904869058690686907869088690986910869118691286913869148691586916869178691886919869208692186922869238692486925869268692786928869298693086931869328693386934869358693686937869388693986940869418694286943869448694586946869478694886949869508695186952869538695486955869568695786958869598696086961869628696386964869658696686967869688696986970869718697286973869748697586976869778697886979869808698186982869838698486985869868698786988869898699086991869928699386994869958699686997869988699987000870018700287003870048700587006870078700887009870108701187012870138701487015870168701787018870198702087021870228702387024870258702687027870288702987030870318703287033870348703587036870378703887039870408704187042870438704487045870468704787048870498705087051870528705387054870558705687057870588705987060870618706287063870648706587066870678706887069870708707187072870738707487075870768707787078870798708087081870828708387084870858708687087870888708987090870918709287093870948709587096870978709887099871008710187102871038710487105871068710787108871098711087111871128711387114871158711687117871188711987120871218712287123871248712587126871278712887129871308713187132871338713487135871368713787138871398714087141871428714387144871458714687147871488714987150871518715287153871548715587156871578715887159871608716187162871638716487165871668716787168871698717087171871728717387174871758717687177871788717987180871818718287183871848718587186871878718887189871908719187192871938719487195871968719787198871998720087201872028720387204872058720687207872088720987210872118721287213872148721587216872178721887219872208722187222872238722487225872268722787228872298723087231872328723387234872358723687237872388723987240872418724287243872448724587246872478724887249872508725187252872538725487255872568725787258872598726087261872628726387264872658726687267872688726987270872718727287273872748727587276872778727887279872808728187282872838728487285872868728787288872898729087291872928729387294872958729687297872988729987300873018730287303873048730587306873078730887309873108731187312873138731487315873168731787318873198732087321873228732387324873258732687327873288732987330873318733287333873348733587336873378733887339873408734187342873438734487345873468734787348873498735087351873528735387354873558735687357873588735987360873618736287363873648736587366873678736887369873708737187372873738737487375873768737787378873798738087381873828738387384873858738687387873888738987390873918739287393873948739587396873978739887399874008740187402874038740487405874068740787408874098741087411874128741387414874158741687417874188741987420874218742287423874248742587426874278742887429874308743187432874338743487435874368743787438874398744087441874428744387444874458744687447874488744987450874518745287453874548745587456874578745887459874608746187462874638746487465874668746787468874698747087471874728747387474874758747687477874788747987480874818748287483874848748587486874878748887489874908749187492874938749487495874968749787498874998750087501875028750387504875058750687507875088750987510875118751287513875148751587516875178751887519875208752187522875238752487525875268752787528875298753087531875328753387534875358753687537875388753987540875418754287543875448754587546875478754887549875508755187552875538755487555875568755787558875598756087561875628756387564875658756687567875688756987570875718757287573875748757587576875778757887579875808758187582875838758487585875868758787588875898759087591875928759387594875958759687597875988759987600876018760287603876048760587606876078760887609876108761187612876138761487615876168761787618876198762087621876228762387624876258762687627876288762987630876318763287633876348763587636876378763887639876408764187642876438764487645876468764787648876498765087651876528765387654876558765687657876588765987660876618766287663876648766587666876678766887669876708767187672876738767487675876768767787678876798768087681876828768387684876858768687687876888768987690876918769287693876948769587696876978769887699877008770187702877038770487705877068770787708877098771087711877128771387714877158771687717877188771987720877218772287723877248772587726877278772887729877308773187732877338773487735877368773787738877398774087741877428774387744877458774687747877488774987750877518775287753877548775587756877578775887759877608776187762877638776487765877668776787768877698777087771877728777387774877758777687777877788777987780877818778287783877848778587786877878778887789877908779187792877938779487795877968779787798877998780087801878028780387804878058780687807878088780987810878118781287813878148781587816878178781887819878208782187822878238782487825878268782787828878298783087831878328783387834878358783687837878388783987840878418784287843878448784587846878478784887849878508785187852878538785487855878568785787858878598786087861878628786387864878658786687867878688786987870878718787287873878748787587876878778787887879878808788187882878838788487885878868788787888878898789087891878928789387894878958789687897878988789987900879018790287903879048790587906879078790887909879108791187912879138791487915879168791787918879198792087921879228792387924879258792687927879288792987930879318793287933879348793587936879378793887939879408794187942879438794487945879468794787948879498795087951879528795387954879558795687957879588795987960879618796287963879648796587966879678796887969879708797187972879738797487975879768797787978879798798087981879828798387984879858798687987879888798987990879918799287993879948799587996879978799887999880008800188002880038800488005880068800788008880098801088011880128801388014880158801688017880188801988020880218802288023880248802588026880278802888029880308803188032880338803488035880368803788038880398804088041880428804388044880458804688047880488804988050880518805288053880548805588056880578805888059880608806188062880638806488065880668806788068880698807088071880728807388074880758807688077880788807988080880818808288083880848808588086880878808888089880908809188092880938809488095880968809788098880998810088101881028810388104881058810688107881088810988110881118811288113881148811588116881178811888119881208812188122881238812488125881268812788128881298813088131881328813388134881358813688137881388813988140881418814288143881448814588146881478814888149881508815188152881538815488155881568815788158881598816088161881628816388164881658816688167881688816988170881718817288173881748817588176881778817888179881808818188182881838818488185881868818788188881898819088191881928819388194881958819688197881988819988200882018820288203882048820588206882078820888209882108821188212882138821488215882168821788218882198822088221882228822388224882258822688227882288822988230882318823288233882348823588236882378823888239882408824188242882438824488245882468824788248882498825088251882528825388254882558825688257882588825988260882618826288263882648826588266882678826888269882708827188272882738827488275882768827788278882798828088281882828828388284882858828688287882888828988290882918829288293882948829588296882978829888299883008830188302883038830488305883068830788308883098831088311883128831388314883158831688317883188831988320883218832288323883248832588326883278832888329883308833188332883338833488335883368833788338883398834088341883428834388344883458834688347883488834988350883518835288353883548835588356883578835888359883608836188362883638836488365883668836788368883698837088371883728837388374883758837688377883788837988380883818838288383883848838588386883878838888389883908839188392883938839488395883968839788398883998840088401884028840388404884058840688407884088840988410884118841288413884148841588416884178841888419884208842188422884238842488425884268842788428884298843088431884328843388434884358843688437884388843988440884418844288443884448844588446884478844888449884508845188452884538845488455884568845788458884598846088461884628846388464884658846688467884688846988470884718847288473884748847588476884778847888479884808848188482884838848488485884868848788488884898849088491884928849388494884958849688497884988849988500885018850288503885048850588506885078850888509885108851188512885138851488515885168851788518885198852088521885228852388524885258852688527885288852988530885318853288533885348853588536885378853888539885408854188542885438854488545885468854788548885498855088551885528855388554885558855688557885588855988560885618856288563885648856588566885678856888569885708857188572885738857488575885768857788578885798858088581885828858388584885858858688587885888858988590885918859288593885948859588596885978859888599886008860188602886038860488605886068860788608886098861088611886128861388614886158861688617886188861988620886218862288623886248862588626886278862888629886308863188632886338863488635886368863788638886398864088641886428864388644886458864688647886488864988650886518865288653886548865588656886578865888659886608866188662886638866488665886668866788668886698867088671886728867388674886758867688677886788867988680886818868288683886848868588686886878868888689886908869188692886938869488695886968869788698886998870088701887028870388704887058870688707887088870988710887118871288713887148871588716887178871888719887208872188722887238872488725887268872788728887298873088731887328873388734887358873688737887388873988740887418874288743887448874588746887478874888749887508875188752887538875488755887568875788758887598876088761887628876388764887658876688767887688876988770887718877288773887748877588776887778877888779887808878188782887838878488785887868878788788887898879088791887928879388794887958879688797887988879988800888018880288803888048880588806888078880888809888108881188812888138881488815888168881788818888198882088821888228882388824888258882688827888288882988830888318883288833888348883588836888378883888839888408884188842888438884488845888468884788848888498885088851888528885388854888558885688857888588885988860888618886288863888648886588866888678886888869888708887188872888738887488875888768887788878888798888088881888828888388884888858888688887888888888988890888918889288893888948889588896888978889888899889008890188902889038890488905889068890788908889098891088911889128891388914889158891688917889188891988920889218892288923889248892588926889278892888929889308893188932889338893488935889368893788938889398894088941889428894388944889458894688947889488894988950889518895288953889548895588956889578895888959889608896188962889638896488965889668896788968889698897088971889728897388974889758897688977889788897988980889818898288983889848898588986889878898888989889908899188992889938899488995889968899788998889998900089001890028900389004890058900689007890088900989010890118901289013890148901589016890178901889019890208902189022890238902489025890268902789028890298903089031890328903389034890358903689037890388903989040890418904289043890448904589046890478904889049890508905189052890538905489055890568905789058890598906089061890628906389064890658906689067890688906989070890718907289073890748907589076890778907889079890808908189082890838908489085890868908789088890898909089091890928909389094890958909689097890988909989100891018910289103891048910589106891078910889109891108911189112891138911489115891168911789118891198912089121891228912389124891258912689127891288912989130891318913289133891348913589136891378913889139891408914189142891438914489145891468914789148891498915089151891528915389154891558915689157891588915989160891618916289163891648916589166891678916889169891708917189172891738917489175891768917789178891798918089181891828918389184891858918689187891888918989190891918919289193891948919589196891978919889199892008920189202892038920489205892068920789208892098921089211892128921389214892158921689217892188921989220892218922289223892248922589226892278922889229892308923189232892338923489235892368923789238892398924089241892428924389244892458924689247892488924989250892518925289253892548925589256892578925889259892608926189262892638926489265892668926789268892698927089271892728927389274892758927689277892788927989280892818928289283892848928589286892878928889289892908929189292892938929489295892968929789298892998930089301893028930389304893058930689307893088930989310893118931289313893148931589316893178931889319893208932189322893238932489325893268932789328893298933089331893328933389334893358933689337893388933989340893418934289343893448934589346893478934889349893508935189352893538935489355893568935789358893598936089361893628936389364893658936689367893688936989370893718937289373893748937589376893778937889379893808938189382893838938489385893868938789388893898939089391893928939389394893958939689397893988939989400894018940289403894048940589406894078940889409894108941189412894138941489415894168941789418894198942089421894228942389424894258942689427894288942989430894318943289433894348943589436894378943889439894408944189442894438944489445894468944789448894498945089451894528945389454894558945689457894588945989460894618946289463894648946589466894678946889469894708947189472894738947489475894768947789478894798948089481894828948389484894858948689487894888948989490894918949289493894948949589496894978949889499895008950189502895038950489505895068950789508895098951089511895128951389514895158951689517895188951989520895218952289523895248952589526895278952889529895308953189532895338953489535895368953789538895398954089541895428954389544895458954689547895488954989550895518955289553895548955589556895578955889559895608956189562895638956489565895668956789568895698957089571895728957389574895758957689577895788957989580895818958289583895848958589586895878958889589895908959189592895938959489595895968959789598895998960089601896028960389604896058960689607896088960989610896118961289613896148961589616896178961889619896208962189622896238962489625896268962789628896298963089631896328963389634896358963689637896388963989640896418964289643896448964589646896478964889649896508965189652896538965489655896568965789658896598966089661896628966389664896658966689667896688966989670896718967289673896748967589676896778967889679896808968189682896838968489685896868968789688896898969089691896928969389694896958969689697896988969989700897018970289703897048970589706897078970889709897108971189712897138971489715897168971789718897198972089721897228972389724897258972689727897288972989730897318973289733897348973589736897378973889739897408974189742897438974489745897468974789748897498975089751897528975389754897558975689757897588975989760897618976289763897648976589766897678976889769897708977189772897738977489775897768977789778897798978089781897828978389784897858978689787897888978989790897918979289793897948979589796897978979889799898008980189802898038980489805898068980789808898098981089811898128981389814898158981689817898188981989820898218982289823898248982589826898278982889829898308983189832898338983489835898368983789838898398984089841898428984389844898458984689847898488984989850898518985289853898548985589856898578985889859898608986189862898638986489865898668986789868898698987089871898728987389874898758987689877898788987989880898818988289883898848988589886898878988889889898908989189892898938989489895898968989789898898998990089901899028990389904899058990689907899088990989910899118991289913899148991589916899178991889919899208992189922899238992489925899268992789928899298993089931899328993389934899358993689937899388993989940899418994289943899448994589946899478994889949899508995189952899538995489955899568995789958899598996089961899628996389964899658996689967899688996989970899718997289973899748997589976899778997889979899808998189982899838998489985899868998789988899898999089991899928999389994899958999689997899988999990000900019000290003900049000590006900079000890009900109001190012900139001490015900169001790018900199002090021900229002390024900259002690027900289002990030900319003290033900349003590036900379003890039900409004190042900439004490045900469004790048900499005090051900529005390054900559005690057900589005990060900619006290063900649006590066900679006890069900709007190072900739007490075900769007790078900799008090081900829008390084900859008690087900889008990090900919009290093900949009590096900979009890099901009010190102901039010490105901069010790108901099011090111901129011390114901159011690117901189011990120901219012290123901249012590126901279012890129901309013190132901339013490135901369013790138901399014090141901429014390144901459014690147901489014990150901519015290153901549015590156901579015890159901609016190162901639016490165901669016790168901699017090171901729017390174901759017690177901789017990180901819018290183901849018590186901879018890189901909019190192901939019490195901969019790198901999020090201902029020390204902059020690207902089020990210902119021290213902149021590216902179021890219902209022190222902239022490225902269022790228902299023090231902329023390234902359023690237902389023990240902419024290243902449024590246902479024890249902509025190252902539025490255902569025790258902599026090261902629026390264902659026690267902689026990270902719027290273902749027590276902779027890279902809028190282902839028490285902869028790288902899029090291902929029390294902959029690297902989029990300903019030290303903049030590306903079030890309903109031190312903139031490315903169031790318903199032090321903229032390324903259032690327903289032990330903319033290333903349033590336903379033890339903409034190342903439034490345903469034790348903499035090351903529035390354903559035690357903589035990360903619036290363903649036590366903679036890369903709037190372903739037490375903769037790378903799038090381903829038390384903859038690387903889038990390903919039290393903949039590396903979039890399904009040190402904039040490405904069040790408904099041090411904129041390414904159041690417904189041990420904219042290423904249042590426904279042890429904309043190432904339043490435904369043790438904399044090441904429044390444904459044690447904489044990450904519045290453904549045590456904579045890459904609046190462904639046490465904669046790468904699047090471904729047390474904759047690477904789047990480904819048290483904849048590486904879048890489904909049190492904939049490495904969049790498904999050090501905029050390504905059050690507905089050990510905119051290513905149051590516905179051890519905209052190522905239052490525905269052790528905299053090531905329053390534905359053690537905389053990540905419054290543905449054590546905479054890549905509055190552905539055490555905569055790558905599056090561905629056390564905659056690567905689056990570905719057290573905749057590576905779057890579905809058190582905839058490585905869058790588905899059090591905929059390594905959059690597905989059990600906019060290603906049060590606906079060890609906109061190612906139061490615906169061790618906199062090621906229062390624906259062690627906289062990630906319063290633906349063590636906379063890639906409064190642906439064490645906469064790648906499065090651906529065390654906559065690657906589065990660906619066290663906649066590666906679066890669906709067190672906739067490675906769067790678906799068090681906829068390684906859068690687906889068990690906919069290693906949069590696906979069890699907009070190702907039070490705907069070790708907099071090711907129071390714907159071690717907189071990720907219072290723907249072590726907279072890729907309073190732907339073490735907369073790738907399074090741907429074390744907459074690747907489074990750907519075290753907549075590756907579075890759907609076190762907639076490765907669076790768907699077090771907729077390774907759077690777907789077990780907819078290783907849078590786907879078890789907909079190792907939079490795907969079790798907999080090801908029080390804908059080690807908089080990810908119081290813908149081590816908179081890819908209082190822908239082490825908269082790828908299083090831908329083390834908359083690837908389083990840908419084290843908449084590846908479084890849908509085190852908539085490855908569085790858908599086090861908629086390864908659086690867908689086990870908719087290873908749087590876908779087890879908809088190882908839088490885908869088790888908899089090891908929089390894908959089690897908989089990900909019090290903909049090590906909079090890909909109091190912909139091490915909169091790918909199092090921909229092390924909259092690927909289092990930909319093290933909349093590936909379093890939909409094190942909439094490945909469094790948909499095090951909529095390954909559095690957909589095990960909619096290963909649096590966909679096890969909709097190972909739097490975909769097790978909799098090981909829098390984909859098690987909889098990990909919099290993909949099590996909979099890999910009100191002910039100491005910069100791008910099101091011910129101391014910159101691017910189101991020910219102291023910249102591026910279102891029910309103191032910339103491035910369103791038910399104091041910429104391044910459104691047910489104991050910519105291053910549105591056910579105891059910609106191062910639106491065910669106791068910699107091071910729107391074910759107691077910789107991080910819108291083910849108591086910879108891089910909109191092910939109491095910969109791098910999110091101911029110391104911059110691107911089110991110911119111291113911149111591116911179111891119911209112191122911239112491125911269112791128911299113091131911329113391134911359113691137911389113991140911419114291143911449114591146911479114891149911509115191152911539115491155911569115791158911599116091161911629116391164911659116691167911689116991170911719117291173911749117591176911779117891179911809118191182911839118491185911869118791188911899119091191911929119391194911959119691197911989119991200912019120291203912049120591206912079120891209912109121191212912139121491215912169121791218912199122091221912229122391224912259122691227912289122991230912319123291233912349123591236912379123891239912409124191242912439124491245912469124791248912499125091251912529125391254912559125691257912589125991260912619126291263912649126591266912679126891269912709127191272912739127491275912769127791278912799128091281912829128391284912859128691287912889128991290912919129291293912949129591296912979129891299913009130191302913039130491305913069130791308913099131091311913129131391314913159131691317913189131991320913219132291323913249132591326913279132891329913309133191332913339133491335913369133791338913399134091341913429134391344913459134691347913489134991350913519135291353913549135591356913579135891359913609136191362913639136491365913669136791368913699137091371913729137391374913759137691377913789137991380913819138291383913849138591386913879138891389913909139191392913939139491395913969139791398913999140091401914029140391404914059140691407914089140991410914119141291413914149141591416914179141891419914209142191422914239142491425914269142791428914299143091431914329143391434914359143691437914389143991440914419144291443914449144591446914479144891449914509145191452914539145491455914569145791458914599146091461914629146391464914659146691467914689146991470914719147291473914749147591476914779147891479914809148191482914839148491485914869148791488914899149091491914929149391494914959149691497914989149991500915019150291503915049150591506915079150891509915109151191512915139151491515915169151791518915199152091521915229152391524915259152691527915289152991530915319153291533915349153591536915379153891539915409154191542915439154491545915469154791548915499155091551915529155391554915559155691557915589155991560915619156291563915649156591566915679156891569915709157191572915739157491575915769157791578915799158091581915829158391584915859158691587915889158991590915919159291593915949159591596915979159891599916009160191602916039160491605916069160791608916099161091611916129161391614916159161691617916189161991620916219162291623916249162591626916279162891629916309163191632916339163491635916369163791638916399164091641916429164391644916459164691647916489164991650916519165291653916549165591656916579165891659916609166191662916639166491665916669166791668916699167091671916729167391674916759167691677916789167991680916819168291683916849168591686916879168891689916909169191692916939169491695916969169791698916999170091701917029170391704917059170691707917089170991710917119171291713917149171591716917179171891719917209172191722917239172491725917269172791728917299173091731917329173391734917359173691737917389173991740917419174291743917449174591746917479174891749917509175191752917539175491755917569175791758917599176091761917629176391764917659176691767917689176991770917719177291773917749177591776917779177891779917809178191782917839178491785917869178791788917899179091791917929179391794917959179691797917989179991800918019180291803918049180591806918079180891809918109181191812918139181491815918169181791818918199182091821918229182391824918259182691827918289182991830918319183291833918349183591836918379183891839918409184191842918439184491845918469184791848918499185091851918529185391854918559185691857918589185991860918619186291863918649186591866918679186891869918709187191872918739187491875918769187791878918799188091881918829188391884918859188691887918889188991890918919189291893918949189591896918979189891899919009190191902919039190491905919069190791908919099191091911919129191391914919159191691917919189191991920919219192291923919249192591926919279192891929919309193191932919339193491935919369193791938919399194091941919429194391944919459194691947919489194991950919519195291953919549195591956919579195891959919609196191962919639196491965919669196791968919699197091971919729197391974919759197691977919789197991980919819198291983919849198591986919879198891989919909199191992919939199491995919969199791998919999200092001920029200392004920059200692007920089200992010920119201292013920149201592016920179201892019920209202192022920239202492025920269202792028920299203092031920329203392034920359203692037920389203992040920419204292043920449204592046920479204892049920509205192052920539205492055920569205792058920599206092061920629206392064920659206692067920689206992070920719207292073920749207592076920779207892079920809208192082920839208492085920869208792088920899209092091920929209392094920959209692097920989209992100921019210292103921049210592106921079210892109921109211192112921139211492115921169211792118921199212092121921229212392124921259212692127921289212992130921319213292133921349213592136921379213892139921409214192142921439214492145921469214792148921499215092151921529215392154921559215692157921589215992160921619216292163921649216592166921679216892169921709217192172921739217492175921769217792178921799218092181921829218392184921859218692187921889218992190921919219292193921949219592196921979219892199922009220192202922039220492205922069220792208922099221092211922129221392214922159221692217922189221992220922219222292223922249222592226922279222892229922309223192232922339223492235922369223792238922399224092241922429224392244922459224692247922489224992250922519225292253922549225592256922579225892259922609226192262922639226492265922669226792268922699227092271922729227392274922759227692277922789227992280922819228292283922849228592286922879228892289922909229192292922939229492295922969229792298922999230092301923029230392304923059230692307923089230992310923119231292313923149231592316923179231892319923209232192322923239232492325923269232792328923299233092331923329233392334923359233692337923389233992340923419234292343923449234592346923479234892349923509235192352923539235492355923569235792358923599236092361923629236392364923659236692367923689236992370923719237292373923749237592376923779237892379923809238192382923839238492385923869238792388923899239092391923929239392394923959239692397923989239992400924019240292403924049240592406924079240892409924109241192412924139241492415924169241792418924199242092421924229242392424924259242692427924289242992430924319243292433924349243592436924379243892439924409244192442924439244492445924469244792448924499245092451924529245392454924559245692457924589245992460924619246292463924649246592466924679246892469924709247192472924739247492475924769247792478924799248092481924829248392484924859248692487924889248992490924919249292493924949249592496924979249892499925009250192502925039250492505925069250792508925099251092511925129251392514925159251692517925189251992520925219252292523925249252592526925279252892529925309253192532925339253492535925369253792538925399254092541925429254392544925459254692547925489254992550925519255292553925549255592556925579255892559925609256192562925639256492565925669256792568925699257092571925729257392574925759257692577925789257992580925819258292583925849258592586925879258892589925909259192592925939259492595925969259792598925999260092601926029260392604926059260692607926089260992610926119261292613926149261592616926179261892619926209262192622926239262492625926269262792628926299263092631926329263392634926359263692637926389263992640926419264292643926449264592646926479264892649926509265192652926539265492655926569265792658926599266092661926629266392664926659266692667926689266992670926719267292673926749267592676926779267892679926809268192682926839268492685926869268792688926899269092691926929269392694926959269692697926989269992700927019270292703927049270592706927079270892709927109271192712927139271492715927169271792718927199272092721927229272392724927259272692727927289272992730927319273292733927349273592736927379273892739927409274192742927439274492745927469274792748927499275092751927529275392754927559275692757927589275992760927619276292763927649276592766927679276892769927709277192772927739277492775927769277792778927799278092781927829278392784927859278692787927889278992790927919279292793927949279592796927979279892799928009280192802928039280492805928069280792808928099281092811928129281392814928159281692817928189281992820928219282292823928249282592826928279282892829928309283192832928339283492835928369283792838928399284092841928429284392844928459284692847928489284992850928519285292853928549285592856928579285892859928609286192862928639286492865928669286792868928699287092871928729287392874928759287692877928789287992880928819288292883928849288592886928879288892889928909289192892928939289492895928969289792898928999290092901929029290392904929059290692907929089290992910929119291292913929149291592916929179291892919929209292192922929239292492925929269292792928929299293092931929329293392934929359293692937929389293992940929419294292943929449294592946929479294892949929509295192952929539295492955929569295792958929599296092961929629296392964929659296692967929689296992970929719297292973929749297592976929779297892979929809298192982929839298492985929869298792988929899299092991929929299392994929959299692997929989299993000930019300293003930049300593006930079300893009930109301193012930139301493015930169301793018930199302093021930229302393024930259302693027930289302993030930319303293033930349303593036930379303893039930409304193042930439304493045930469304793048930499305093051930529305393054930559305693057930589305993060930619306293063930649306593066930679306893069930709307193072930739307493075930769307793078930799308093081930829308393084930859308693087930889308993090930919309293093930949309593096930979309893099931009310193102931039310493105931069310793108931099311093111931129311393114931159311693117931189311993120931219312293123931249312593126931279312893129931309313193132931339313493135931369313793138931399314093141931429314393144931459314693147931489314993150931519315293153931549315593156931579315893159931609316193162931639316493165931669316793168931699317093171931729317393174931759317693177931789317993180931819318293183931849318593186931879318893189931909319193192931939319493195931969319793198931999320093201932029320393204932059320693207932089320993210932119321293213932149321593216932179321893219932209322193222932239322493225932269322793228932299323093231932329323393234932359323693237932389323993240932419324293243932449324593246932479324893249932509325193252932539325493255932569325793258932599326093261932629326393264932659326693267932689326993270932719327293273932749327593276932779327893279932809328193282932839328493285932869328793288932899329093291932929329393294932959329693297932989329993300933019330293303933049330593306933079330893309933109331193312933139331493315933169331793318933199332093321933229332393324933259332693327933289332993330933319333293333933349333593336933379333893339933409334193342933439334493345933469334793348933499335093351933529335393354933559335693357933589335993360933619336293363933649336593366933679336893369933709337193372933739337493375933769337793378933799338093381933829338393384933859338693387933889338993390933919339293393933949339593396933979339893399934009340193402934039340493405934069340793408934099341093411934129341393414934159341693417934189341993420934219342293423934249342593426934279342893429934309343193432934339343493435934369343793438934399344093441934429344393444934459344693447934489344993450934519345293453934549345593456934579345893459934609346193462934639346493465934669346793468934699347093471934729347393474934759347693477934789347993480934819348293483934849348593486934879348893489934909349193492934939349493495934969349793498934999350093501935029350393504935059350693507935089350993510935119351293513935149351593516935179351893519935209352193522935239352493525935269352793528935299353093531935329353393534935359353693537935389353993540935419354293543935449354593546935479354893549935509355193552935539355493555935569355793558935599356093561935629356393564935659356693567935689356993570935719357293573935749357593576935779357893579935809358193582935839358493585935869358793588935899359093591935929359393594935959359693597935989359993600936019360293603936049360593606936079360893609936109361193612936139361493615936169361793618936199362093621936229362393624936259362693627936289362993630936319363293633936349363593636936379363893639936409364193642936439364493645936469364793648936499365093651936529365393654936559365693657936589365993660936619366293663936649366593666936679366893669936709367193672936739367493675936769367793678936799368093681936829368393684936859368693687936889368993690936919369293693936949369593696936979369893699937009370193702937039370493705937069370793708937099371093711937129371393714937159371693717937189371993720937219372293723937249372593726937279372893729937309373193732937339373493735937369373793738937399374093741937429374393744937459374693747937489374993750937519375293753937549375593756937579375893759937609376193762937639376493765937669376793768937699377093771937729377393774937759377693777937789377993780937819378293783937849378593786937879378893789937909379193792937939379493795937969379793798937999380093801938029380393804938059380693807938089380993810938119381293813938149381593816938179381893819938209382193822938239382493825938269382793828938299383093831938329383393834938359383693837938389383993840938419384293843938449384593846938479384893849938509385193852938539385493855938569385793858938599386093861938629386393864938659386693867938689386993870938719387293873938749387593876938779387893879938809388193882938839388493885938869388793888938899389093891938929389393894938959389693897938989389993900939019390293903939049390593906939079390893909939109391193912939139391493915939169391793918939199392093921939229392393924939259392693927939289392993930939319393293933939349393593936939379393893939939409394193942939439394493945939469394793948939499395093951939529395393954939559395693957939589395993960939619396293963939649396593966939679396893969939709397193972939739397493975939769397793978939799398093981939829398393984939859398693987939889398993990939919399293993939949399593996939979399893999940009400194002940039400494005940069400794008940099401094011940129401394014940159401694017940189401994020940219402294023940249402594026940279402894029940309403194032940339403494035940369403794038940399404094041940429404394044940459404694047940489404994050940519405294053940549405594056940579405894059940609406194062940639406494065940669406794068940699407094071940729407394074940759407694077940789407994080940819408294083940849408594086940879408894089940909409194092940939409494095940969409794098940999410094101941029410394104941059410694107941089410994110941119411294113941149411594116941179411894119941209412194122941239412494125941269412794128941299413094131941329413394134941359413694137941389413994140941419414294143941449414594146941479414894149941509415194152941539415494155941569415794158941599416094161941629416394164941659416694167941689416994170941719417294173941749417594176941779417894179941809418194182941839418494185941869418794188941899419094191941929419394194941959419694197941989419994200942019420294203942049420594206942079420894209942109421194212942139421494215942169421794218942199422094221942229422394224942259422694227942289422994230942319423294233942349423594236942379423894239942409424194242942439424494245942469424794248942499425094251942529425394254942559425694257942589425994260942619426294263942649426594266942679426894269942709427194272942739427494275942769427794278942799428094281942829428394284942859428694287942889428994290942919429294293942949429594296942979429894299943009430194302943039430494305943069430794308943099431094311943129431394314943159431694317943189431994320943219432294323943249432594326943279432894329943309433194332943339433494335943369433794338943399434094341943429434394344943459434694347943489434994350943519435294353943549435594356943579435894359943609436194362943639436494365943669436794368943699437094371943729437394374943759437694377943789437994380943819438294383943849438594386943879438894389943909439194392943939439494395943969439794398943999440094401944029440394404944059440694407944089440994410944119441294413944149441594416944179441894419944209442194422944239442494425944269442794428944299443094431944329443394434944359443694437944389443994440944419444294443944449444594446944479444894449944509445194452944539445494455944569445794458944599446094461944629446394464944659446694467944689446994470944719447294473944749447594476944779447894479944809448194482944839448494485944869448794488944899449094491944929449394494944959449694497944989449994500945019450294503945049450594506945079450894509945109451194512945139451494515945169451794518945199452094521945229452394524945259452694527945289452994530945319453294533945349453594536945379453894539945409454194542945439454494545945469454794548945499455094551945529455394554945559455694557945589455994560945619456294563945649456594566945679456894569945709457194572945739457494575945769457794578945799458094581945829458394584945859458694587945889458994590945919459294593945949459594596945979459894599946009460194602946039460494605946069460794608946099461094611946129461394614946159461694617946189461994620946219462294623946249462594626946279462894629946309463194632946339463494635946369463794638946399464094641946429464394644946459464694647946489464994650946519465294653946549465594656946579465894659946609466194662946639466494665946669466794668946699467094671946729467394674946759467694677946789467994680946819468294683946849468594686946879468894689946909469194692946939469494695946969469794698946999470094701947029470394704947059470694707947089470994710947119471294713947149471594716947179471894719947209472194722947239472494725947269472794728947299473094731947329473394734947359473694737947389473994740947419474294743947449474594746947479474894749947509475194752947539475494755947569475794758947599476094761947629476394764947659476694767947689476994770947719477294773947749477594776947779477894779947809478194782947839478494785947869478794788947899479094791947929479394794947959479694797947989479994800948019480294803948049480594806948079480894809948109481194812948139481494815948169481794818948199482094821948229482394824948259482694827948289482994830948319483294833948349483594836948379483894839948409484194842948439484494845948469484794848948499485094851948529485394854948559485694857948589485994860948619486294863948649486594866948679486894869948709487194872948739487494875948769487794878948799488094881948829488394884948859488694887948889488994890948919489294893948949489594896948979489894899949009490194902949039490494905949069490794908949099491094911949129491394914949159491694917949189491994920949219492294923949249492594926949279492894929949309493194932949339493494935949369493794938949399494094941949429494394944949459494694947949489494994950949519495294953949549495594956949579495894959949609496194962949639496494965949669496794968949699497094971949729497394974949759497694977949789497994980949819498294983949849498594986949879498894989949909499194992949939499494995949969499794998949999500095001950029500395004950059500695007950089500995010950119501295013950149501595016950179501895019950209502195022950239502495025950269502795028950299503095031950329503395034950359503695037950389503995040950419504295043950449504595046950479504895049950509505195052950539505495055950569505795058950599506095061950629506395064950659506695067950689506995070950719507295073950749507595076950779507895079950809508195082950839508495085950869508795088950899509095091950929509395094950959509695097950989509995100951019510295103951049510595106951079510895109951109511195112951139511495115951169511795118951199512095121951229512395124951259512695127951289512995130951319513295133951349513595136951379513895139951409514195142951439514495145951469514795148951499515095151951529515395154951559515695157951589515995160951619516295163951649516595166951679516895169951709517195172951739517495175951769517795178951799518095181951829518395184951859518695187951889518995190951919519295193951949519595196951979519895199952009520195202952039520495205952069520795208952099521095211952129521395214952159521695217952189521995220952219522295223952249522595226952279522895229952309523195232952339523495235952369523795238952399524095241952429524395244952459524695247952489524995250952519525295253952549525595256952579525895259952609526195262952639526495265952669526795268952699527095271952729527395274952759527695277952789527995280952819528295283952849528595286952879528895289952909529195292952939529495295952969529795298952999530095301953029530395304953059530695307953089530995310953119531295313953149531595316953179531895319953209532195322953239532495325953269532795328953299533095331953329533395334953359533695337953389533995340953419534295343953449534595346953479534895349953509535195352953539535495355953569535795358953599536095361953629536395364953659536695367953689536995370953719537295373953749537595376953779537895379953809538195382953839538495385953869538795388953899539095391953929539395394953959539695397953989539995400954019540295403954049540595406954079540895409954109541195412954139541495415954169541795418954199542095421954229542395424954259542695427954289542995430954319543295433954349543595436954379543895439954409544195442954439544495445954469544795448954499545095451954529545395454954559545695457954589545995460954619546295463954649546595466954679546895469954709547195472954739547495475954769547795478954799548095481954829548395484954859548695487954889548995490954919549295493954949549595496954979549895499955009550195502955039550495505955069550795508955099551095511955129551395514955159551695517955189551995520955219552295523955249552595526955279552895529955309553195532955339553495535955369553795538955399554095541955429554395544955459554695547955489554995550955519555295553955549555595556955579555895559955609556195562955639556495565955669556795568955699557095571955729557395574955759557695577955789557995580955819558295583955849558595586955879558895589955909559195592955939559495595955969559795598955999560095601956029560395604956059560695607956089560995610956119561295613956149561595616956179561895619956209562195622956239562495625956269562795628956299563095631956329563395634956359563695637956389563995640956419564295643956449564595646956479564895649956509565195652956539565495655956569565795658956599566095661956629566395664956659566695667956689566995670956719567295673956749567595676956779567895679956809568195682956839568495685956869568795688956899569095691956929569395694956959569695697956989569995700957019570295703957049570595706957079570895709957109571195712957139571495715957169571795718957199572095721957229572395724957259572695727957289572995730957319573295733957349573595736957379573895739957409574195742957439574495745957469574795748957499575095751957529575395754957559575695757957589575995760957619576295763957649576595766957679576895769957709577195772957739577495775957769577795778957799578095781957829578395784957859578695787957889578995790957919579295793957949579595796957979579895799958009580195802958039580495805958069580795808958099581095811958129581395814958159581695817958189581995820958219582295823958249582595826958279582895829958309583195832958339583495835958369583795838958399584095841958429584395844958459584695847958489584995850958519585295853958549585595856958579585895859958609586195862958639586495865958669586795868958699587095871958729587395874958759587695877958789587995880958819588295883958849588595886958879588895889958909589195892958939589495895958969589795898958999590095901959029590395904959059590695907959089590995910959119591295913959149591595916959179591895919959209592195922959239592495925959269592795928959299593095931959329593395934959359593695937959389593995940959419594295943959449594595946959479594895949959509595195952959539595495955959569595795958959599596095961959629596395964959659596695967959689596995970959719597295973959749597595976959779597895979959809598195982959839598495985959869598795988959899599095991959929599395994959959599695997959989599996000960019600296003960049600596006960079600896009960109601196012960139601496015960169601796018960199602096021960229602396024960259602696027960289602996030960319603296033960349603596036960379603896039960409604196042960439604496045960469604796048960499605096051960529605396054960559605696057960589605996060960619606296063960649606596066960679606896069960709607196072960739607496075960769607796078960799608096081960829608396084960859608696087960889608996090960919609296093960949609596096960979609896099961009610196102961039610496105961069610796108961099611096111961129611396114961159611696117961189611996120961219612296123961249612596126961279612896129961309613196132961339613496135961369613796138961399614096141961429614396144961459614696147961489614996150961519615296153961549615596156961579615896159961609616196162961639616496165961669616796168961699617096171961729617396174961759617696177961789617996180961819618296183961849618596186961879618896189961909619196192961939619496195961969619796198961999620096201962029620396204962059620696207962089620996210962119621296213962149621596216962179621896219962209622196222962239622496225962269622796228962299623096231962329623396234962359623696237962389623996240962419624296243962449624596246962479624896249962509625196252962539625496255962569625796258962599626096261962629626396264962659626696267962689626996270962719627296273962749627596276962779627896279962809628196282962839628496285962869628796288962899629096291962929629396294962959629696297962989629996300963019630296303963049630596306963079630896309963109631196312963139631496315963169631796318963199632096321963229632396324963259632696327963289632996330963319633296333963349633596336963379633896339963409634196342963439634496345963469634796348963499635096351963529635396354963559635696357963589635996360963619636296363963649636596366963679636896369963709637196372963739637496375963769637796378963799638096381963829638396384963859638696387963889638996390963919639296393963949639596396963979639896399964009640196402964039640496405964069640796408964099641096411964129641396414964159641696417964189641996420964219642296423964249642596426964279642896429964309643196432964339643496435964369643796438964399644096441964429644396444964459644696447964489644996450964519645296453964549645596456964579645896459964609646196462964639646496465964669646796468964699647096471964729647396474964759647696477964789647996480964819648296483964849648596486964879648896489964909649196492964939649496495964969649796498964999650096501965029650396504965059650696507965089650996510965119651296513965149651596516965179651896519965209652196522965239652496525965269652796528965299653096531965329653396534965359653696537965389653996540965419654296543965449654596546965479654896549965509655196552965539655496555965569655796558965599656096561965629656396564965659656696567965689656996570965719657296573965749657596576965779657896579965809658196582965839658496585965869658796588965899659096591965929659396594965959659696597965989659996600966019660296603966049660596606966079660896609966109661196612966139661496615966169661796618966199662096621966229662396624966259662696627966289662996630966319663296633966349663596636966379663896639966409664196642966439664496645966469664796648966499665096651966529665396654966559665696657966589665996660966619666296663966649666596666966679666896669966709667196672966739667496675966769667796678966799668096681966829668396684966859668696687966889668996690966919669296693966949669596696966979669896699967009670196702967039670496705967069670796708967099671096711967129671396714967159671696717967189671996720967219672296723967249672596726967279672896729967309673196732967339673496735967369673796738967399674096741967429674396744967459674696747967489674996750967519675296753967549675596756967579675896759967609676196762967639676496765967669676796768967699677096771967729677396774967759677696777967789677996780967819678296783967849678596786967879678896789967909679196792967939679496795967969679796798967999680096801968029680396804968059680696807968089680996810968119681296813968149681596816968179681896819968209682196822968239682496825968269682796828968299683096831968329683396834968359683696837968389683996840968419684296843968449684596846968479684896849968509685196852968539685496855968569685796858968599686096861968629686396864968659686696867968689686996870968719687296873968749687596876968779687896879968809688196882968839688496885968869688796888968899689096891968929689396894968959689696897968989689996900969019690296903969049690596906969079690896909969109691196912969139691496915969169691796918969199692096921969229692396924969259692696927969289692996930969319693296933969349693596936969379693896939969409694196942969439694496945969469694796948969499695096951969529695396954969559695696957969589695996960969619696296963969649696596966969679696896969969709697196972969739697496975969769697796978969799698096981969829698396984969859698696987969889698996990969919699296993969949699596996969979699896999970009700197002970039700497005970069700797008970099701097011970129701397014970159701697017970189701997020970219702297023970249702597026970279702897029970309703197032970339703497035970369703797038970399704097041970429704397044970459704697047970489704997050970519705297053970549705597056970579705897059970609706197062970639706497065970669706797068970699707097071970729707397074970759707697077970789707997080970819708297083970849708597086970879708897089970909709197092970939709497095970969709797098970999710097101971029710397104971059710697107971089710997110971119711297113971149711597116971179711897119971209712197122971239712497125971269712797128971299713097131971329713397134971359713697137971389713997140971419714297143971449714597146971479714897149971509715197152971539715497155971569715797158971599716097161971629716397164971659716697167971689716997170971719717297173971749717597176971779717897179971809718197182971839718497185971869718797188971899719097191971929719397194971959719697197971989719997200972019720297203972049720597206972079720897209972109721197212972139721497215972169721797218972199722097221972229722397224972259722697227972289722997230972319723297233972349723597236972379723897239972409724197242972439724497245972469724797248972499725097251972529725397254972559725697257972589725997260972619726297263972649726597266972679726897269972709727197272972739727497275972769727797278972799728097281972829728397284972859728697287972889728997290972919729297293972949729597296972979729897299973009730197302973039730497305973069730797308973099731097311973129731397314973159731697317973189731997320973219732297323973249732597326973279732897329973309733197332973339733497335973369733797338973399734097341973429734397344973459734697347973489734997350973519735297353973549735597356973579735897359973609736197362973639736497365973669736797368973699737097371973729737397374973759737697377973789737997380973819738297383973849738597386973879738897389973909739197392973939739497395973969739797398973999740097401974029740397404974059740697407974089740997410974119741297413974149741597416974179741897419974209742197422974239742497425974269742797428974299743097431974329743397434974359743697437974389743997440974419744297443974449744597446974479744897449974509745197452974539745497455974569745797458974599746097461974629746397464974659746697467974689746997470974719747297473974749747597476974779747897479974809748197482974839748497485974869748797488974899749097491974929749397494974959749697497974989749997500975019750297503975049750597506975079750897509975109751197512975139751497515975169751797518975199752097521975229752397524975259752697527975289752997530975319753297533975349753597536975379753897539975409754197542975439754497545975469754797548975499755097551975529755397554975559755697557975589755997560975619756297563975649756597566975679756897569975709757197572975739757497575975769757797578975799758097581975829758397584975859758697587975889758997590975919759297593975949759597596975979759897599976009760197602976039760497605976069760797608976099761097611976129761397614976159761697617976189761997620976219762297623976249762597626976279762897629976309763197632976339763497635976369763797638976399764097641976429764397644976459764697647976489764997650976519765297653976549765597656976579765897659976609766197662976639766497665976669766797668976699767097671976729767397674976759767697677976789767997680976819768297683976849768597686976879768897689976909769197692976939769497695976969769797698976999770097701977029770397704977059770697707977089770997710977119771297713977149771597716977179771897719977209772197722977239772497725977269772797728977299773097731977329773397734977359773697737977389773997740977419774297743977449774597746977479774897749977509775197752977539775497755977569775797758977599776097761977629776397764977659776697767977689776997770977719777297773977749777597776977779777897779977809778197782977839778497785977869778797788977899779097791977929779397794977959779697797977989779997800978019780297803978049780597806978079780897809978109781197812978139781497815978169781797818978199782097821978229782397824978259782697827978289782997830978319783297833978349783597836978379783897839978409784197842978439784497845978469784797848978499785097851978529785397854978559785697857978589785997860978619786297863978649786597866978679786897869978709787197872978739787497875978769787797878978799788097881978829788397884978859788697887978889788997890978919789297893978949789597896978979789897899979009790197902979039790497905979069790797908979099791097911979129791397914979159791697917979189791997920979219792297923979249792597926979279792897929979309793197932979339793497935979369793797938979399794097941979429794397944979459794697947979489794997950979519795297953979549795597956979579795897959979609796197962979639796497965979669796797968979699797097971979729797397974979759797697977979789797997980979819798297983979849798597986979879798897989979909799197992979939799497995979969799797998979999800098001980029800398004980059800698007980089800998010980119801298013980149801598016980179801898019980209802198022980239802498025980269802798028980299803098031980329803398034980359803698037980389803998040980419804298043980449804598046980479804898049980509805198052980539805498055980569805798058980599806098061980629806398064980659806698067980689806998070980719807298073980749807598076980779807898079980809808198082980839808498085980869808798088980899809098091980929809398094980959809698097980989809998100981019810298103981049810598106981079810898109981109811198112981139811498115981169811798118981199812098121981229812398124981259812698127981289812998130981319813298133981349813598136981379813898139981409814198142981439814498145981469814798148981499815098151981529815398154981559815698157981589815998160981619816298163981649816598166981679816898169981709817198172981739817498175981769817798178981799818098181981829818398184981859818698187981889818998190981919819298193981949819598196981979819898199982009820198202982039820498205982069820798208982099821098211982129821398214982159821698217982189821998220982219822298223982249822598226982279822898229982309823198232982339823498235982369823798238982399824098241982429824398244982459824698247982489824998250982519825298253982549825598256982579825898259982609826198262982639826498265982669826798268982699827098271982729827398274982759827698277982789827998280982819828298283982849828598286982879828898289982909829198292982939829498295982969829798298982999830098301983029830398304983059830698307983089830998310983119831298313983149831598316983179831898319983209832198322983239832498325983269832798328983299833098331983329833398334983359833698337983389833998340983419834298343983449834598346983479834898349983509835198352983539835498355983569835798358983599836098361983629836398364983659836698367983689836998370983719837298373983749837598376983779837898379983809838198382983839838498385983869838798388983899839098391983929839398394983959839698397983989839998400984019840298403984049840598406984079840898409984109841198412984139841498415984169841798418984199842098421984229842398424984259842698427984289842998430984319843298433984349843598436984379843898439984409844198442984439844498445984469844798448984499845098451984529845398454984559845698457984589845998460984619846298463984649846598466984679846898469984709847198472984739847498475984769847798478984799848098481984829848398484984859848698487984889848998490984919849298493984949849598496984979849898499985009850198502985039850498505985069850798508985099851098511985129851398514985159851698517985189851998520985219852298523985249852598526985279852898529985309853198532985339853498535985369853798538985399854098541985429854398544985459854698547985489854998550985519855298553985549855598556985579855898559985609856198562985639856498565985669856798568985699857098571985729857398574985759857698577985789857998580985819858298583985849858598586985879858898589985909859198592985939859498595985969859798598985999860098601986029860398604986059860698607986089860998610986119861298613986149861598616986179861898619986209862198622986239862498625986269862798628986299863098631986329863398634986359863698637986389863998640986419864298643986449864598646986479864898649986509865198652986539865498655986569865798658986599866098661986629866398664986659866698667986689866998670986719867298673986749867598676986779867898679986809868198682986839868498685986869868798688986899869098691986929869398694986959869698697986989869998700987019870298703987049870598706987079870898709987109871198712987139871498715987169871798718987199872098721987229872398724987259872698727987289872998730987319873298733987349873598736987379873898739987409874198742987439874498745987469874798748987499875098751987529875398754987559875698757987589875998760987619876298763987649876598766987679876898769987709877198772987739877498775987769877798778987799878098781987829878398784987859878698787987889878998790987919879298793987949879598796987979879898799988009880198802988039880498805988069880798808988099881098811988129881398814988159881698817988189881998820988219882298823988249882598826988279882898829988309883198832988339883498835988369883798838988399884098841988429884398844988459884698847988489884998850988519885298853988549885598856988579885898859988609886198862988639886498865988669886798868988699887098871988729887398874988759887698877988789887998880988819888298883988849888598886988879888898889988909889198892988939889498895988969889798898988999890098901989029890398904989059890698907989089890998910989119891298913989149891598916989179891898919989209892198922989239892498925989269892798928989299893098931989329893398934989359893698937989389893998940989419894298943989449894598946989479894898949989509895198952989539895498955989569895798958989599896098961989629896398964989659896698967989689896998970989719897298973989749897598976989779897898979989809898198982989839898498985989869898798988989899899098991989929899398994989959899698997989989899999000990019900299003990049900599006990079900899009990109901199012990139901499015990169901799018990199902099021990229902399024990259902699027990289902999030990319903299033990349903599036990379903899039990409904199042990439904499045990469904799048990499905099051990529905399054990559905699057990589905999060990619906299063990649906599066990679906899069990709907199072990739907499075990769907799078990799908099081990829908399084990859908699087990889908999090990919909299093990949909599096990979909899099991009910199102991039910499105991069910799108991099911099111991129911399114991159911699117991189911999120991219912299123991249912599126991279912899129991309913199132991339913499135991369913799138991399914099141991429914399144991459914699147991489914999150991519915299153991549915599156991579915899159991609916199162991639916499165991669916799168991699917099171991729917399174991759917699177991789917999180991819918299183991849918599186991879918899189991909919199192991939919499195991969919799198991999920099201992029920399204992059920699207992089920999210992119921299213992149921599216992179921899219992209922199222992239922499225992269922799228992299923099231992329923399234992359923699237992389923999240992419924299243992449924599246992479924899249992509925199252992539925499255992569925799258992599926099261992629926399264992659926699267992689926999270992719927299273992749927599276992779927899279992809928199282992839928499285992869928799288992899929099291992929929399294992959929699297992989929999300993019930299303993049930599306993079930899309993109931199312993139931499315993169931799318993199932099321993229932399324993259932699327993289932999330993319933299333993349933599336993379933899339993409934199342993439934499345993469934799348993499935099351993529935399354993559935699357993589935999360993619936299363993649936599366993679936899369993709937199372993739937499375993769937799378993799938099381993829938399384993859938699387993889938999390993919939299393993949939599396993979939899399994009940199402994039940499405994069940799408994099941099411994129941399414994159941699417994189941999420994219942299423994249942599426994279942899429994309943199432994339943499435994369943799438994399944099441994429944399444994459944699447994489944999450994519945299453994549945599456994579945899459994609946199462994639946499465994669946799468994699947099471994729947399474994759947699477994789947999480994819948299483994849948599486994879948899489994909949199492994939949499495994969949799498994999950099501995029950399504995059950699507995089950999510995119951299513995149951599516995179951899519995209952199522995239952499525995269952799528995299953099531995329953399534995359953699537995389953999540995419954299543995449954599546995479954899549995509955199552995539955499555995569955799558995599956099561995629956399564995659956699567995689956999570995719957299573995749957599576995779957899579995809958199582995839958499585995869958799588995899959099591995929959399594995959959699597995989959999600996019960299603996049960599606996079960899609996109961199612996139961499615996169961799618996199962099621996229962399624996259962699627996289962999630996319963299633996349963599636996379963899639996409964199642996439964499645996469964799648996499965099651996529965399654996559965699657996589965999660996619966299663996649966599666996679966899669996709967199672996739967499675996769967799678996799968099681996829968399684996859968699687996889968999690996919969299693996949969599696996979969899699997009970199702997039970499705997069970799708997099971099711997129971399714997159971699717997189971999720997219972299723997249972599726997279972899729997309973199732997339973499735997369973799738997399974099741997429974399744997459974699747997489974999750997519975299753997549975599756997579975899759997609976199762997639976499765997669976799768997699977099771997729977399774997759977699777997789977999780997819978299783997849978599786997879978899789997909979199792997939979499795997969979799798997999980099801998029980399804998059980699807998089980999810998119981299813998149981599816998179981899819998209982199822998239982499825998269982799828998299983099831998329983399834998359983699837998389983999840998419984299843998449984599846998479984899849998509985199852998539985499855998569985799858998599986099861998629986399864998659986699867998689986999870998719987299873998749987599876998779987899879998809988199882998839988499885998869988799888998899989099891998929989399894998959989699897998989989999900999019990299903999049990599906999079990899909999109991199912999139991499915999169991799918999199992099921999229992399924999259992699927999289992999930999319993299933999349993599936999379993899939999409994199942999439994499945999469994799948999499995099951999529995399954999559995699957999589995999960999619996299963999649996599966999679996899969999709997199972999739997499975999769997799978999799998099981999829998399984999859998699987999889998999990999919999299993999949999599996999979999899999100000100001100002100003100004100005100006100007100008100009100010100011100012100013100014100015100016100017100018100019100020100021100022100023100024100025100026100027100028100029100030100031100032100033100034100035100036100037100038100039100040100041100042100043100044100045100046100047100048100049100050100051100052100053100054100055100056100057100058100059100060100061100062100063100064100065100066100067100068100069100070100071100072100073100074100075100076100077100078100079100080100081100082100083100084100085100086100087100088100089100090100091100092100093100094100095100096100097100098100099100100100101100102100103100104100105100106100107100108100109100110100111100112100113100114100115100116100117100118100119100120100121100122100123100124100125100126100127100128100129100130100131100132100133100134100135100136100137100138100139100140100141100142100143100144100145100146100147100148100149100150100151100152100153100154100155100156100157100158100159100160100161100162100163100164100165100166100167100168100169100170100171100172100173100174100175100176100177100178100179100180100181100182100183100184100185100186100187100188100189100190100191100192100193100194100195100196100197100198100199100200100201100202100203100204100205100206100207100208100209100210100211100212100213100214100215100216100217100218100219100220100221100222100223100224100225100226100227100228100229100230100231100232100233100234100235100236100237100238100239100240100241100242100243100244100245100246100247100248100249100250100251100252100253100254100255100256100257100258100259100260100261100262100263100264100265100266100267100268100269100270100271100272100273100274100275100276100277100278100279100280100281100282100283100284100285100286100287100288100289100290100291100292100293100294100295100296100297100298100299100300100301100302100303100304100305100306100307100308100309100310100311100312100313100314100315100316100317100318100319100320100321100322100323100324100325100326100327100328100329100330100331100332100333100334100335100336100337100338100339100340100341100342100343100344100345100346100347100348100349100350100351100352100353100354100355100356100357100358100359100360100361100362100363100364100365100366100367100368100369100370100371100372100373100374100375100376100377100378100379100380100381100382100383100384100385100386100387100388100389100390100391100392100393100394100395100396100397100398100399100400100401100402100403100404100405100406100407100408100409100410100411100412100413100414100415100416100417100418100419100420100421100422100423100424100425100426100427100428100429100430100431100432100433100434100435100436100437100438100439100440100441100442100443100444100445100446100447100448100449100450100451100452100453100454100455100456100457100458100459100460100461100462100463100464100465100466100467100468100469100470100471100472100473100474100475100476100477100478100479100480100481100482100483100484100485100486100487100488100489100490100491100492100493100494100495100496100497100498100499100500100501100502100503100504100505100506100507100508100509100510100511100512100513100514100515100516100517100518100519100520100521100522100523100524100525100526100527100528100529100530100531100532100533100534100535100536100537100538100539100540100541100542100543100544100545100546100547100548100549100550100551100552100553100554100555100556100557100558100559100560100561100562100563100564100565100566100567100568100569100570100571100572100573100574100575100576100577100578100579100580100581100582100583100584100585100586100587100588100589100590100591100592100593100594100595100596100597100598100599100600100601100602100603100604100605100606100607100608100609100610100611100612100613100614100615100616100617100618100619100620100621100622100623100624100625100626100627100628100629100630100631100632100633100634100635100636100637100638100639100640100641100642100643100644100645100646100647100648100649100650100651100652100653100654100655100656100657100658100659100660100661100662100663100664100665100666100667100668100669100670100671100672100673100674100675100676100677100678100679100680100681100682100683100684100685100686100687100688100689100690100691100692100693100694100695100696100697100698100699100700100701100702100703100704100705100706100707100708100709100710100711100712100713100714100715100716100717100718100719100720100721100722100723100724100725100726100727100728100729100730100731100732100733100734100735100736100737100738100739100740100741100742100743100744100745100746100747100748100749100750100751100752100753100754100755100756100757100758100759100760100761100762100763100764100765100766100767100768100769100770100771100772100773100774100775100776100777100778100779100780100781100782100783100784100785100786100787100788100789100790100791100792100793100794100795100796100797100798100799100800100801100802100803100804100805100806100807100808100809100810100811100812100813100814100815100816100817100818100819100820100821100822100823100824100825100826100827100828100829100830100831100832100833100834100835100836100837100838100839100840100841100842100843100844100845100846100847100848100849100850100851100852100853100854100855100856100857100858100859100860100861100862100863100864100865100866100867100868100869100870100871100872100873100874100875100876100877100878100879100880100881100882100883100884100885100886100887100888100889100890100891100892100893100894100895100896100897100898100899100900100901100902100903100904100905100906100907100908100909100910100911100912100913100914100915100916100917100918100919100920100921100922100923100924100925100926100927100928100929100930100931100932100933100934100935100936100937100938100939100940100941100942100943100944100945100946100947100948100949100950100951100952100953100954100955100956100957100958100959100960100961100962100963100964100965100966100967100968100969100970100971100972100973100974100975100976100977100978100979100980100981100982100983100984100985100986100987100988100989100990100991100992100993100994100995100996100997100998100999101000101001101002101003101004101005101006101007101008101009101010101011101012101013101014
  1. diff -Nur linux-3.12.13/arch/arm/configs/bcmrpi_cutdown_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_cutdown_defconfig
  2. --- linux-3.12.13/arch/arm/configs/bcmrpi_cutdown_defconfig 1970-01-01 01:00:00.000000000 +0100
  3. +++ linux-raspberry-pi/arch/arm/configs/bcmrpi_cutdown_defconfig 2014-03-11 17:51:00.000000000 +0100
  4. @@ -0,0 +1,503 @@
  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. diff -Nur linux-3.12.13/arch/arm/configs/bcmrpi_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_defconfig
  509. --- linux-3.12.13/arch/arm/configs/bcmrpi_defconfig 1970-01-01 01:00:00.000000000 +0100
  510. +++ linux-raspberry-pi/arch/arm/configs/bcmrpi_defconfig 2014-03-11 17:51:00.000000000 +0100
  511. @@ -0,0 +1,1094 @@
  512. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  513. +# CONFIG_LOCALVERSION_AUTO is not set
  514. +CONFIG_SYSVIPC=y
  515. +CONFIG_POSIX_MQUEUE=y
  516. +CONFIG_FHANDLE=y
  517. +CONFIG_AUDIT=y
  518. +CONFIG_NO_HZ=y
  519. +CONFIG_HIGH_RES_TIMERS=y
  520. +CONFIG_BSD_PROCESS_ACCT=y
  521. +CONFIG_BSD_PROCESS_ACCT_V3=y
  522. +CONFIG_TASKSTATS=y
  523. +CONFIG_TASK_DELAY_ACCT=y
  524. +CONFIG_TASK_XACCT=y
  525. +CONFIG_TASK_IO_ACCOUNTING=y
  526. +CONFIG_IKCONFIG=y
  527. +CONFIG_IKCONFIG_PROC=y
  528. +CONFIG_CGROUP_FREEZER=y
  529. +CONFIG_CGROUP_DEVICE=y
  530. +CONFIG_CGROUP_CPUACCT=y
  531. +CONFIG_RESOURCE_COUNTERS=y
  532. +CONFIG_BLK_CGROUP=y
  533. +CONFIG_NAMESPACES=y
  534. +CONFIG_SCHED_AUTOGROUP=y
  535. +CONFIG_RELAY=y
  536. +CONFIG_BLK_DEV_INITRD=y
  537. +CONFIG_EMBEDDED=y
  538. +# CONFIG_COMPAT_BRK is not set
  539. +CONFIG_PROFILING=y
  540. +CONFIG_OPROFILE=m
  541. +CONFIG_KPROBES=y
  542. +CONFIG_JUMP_LABEL=y
  543. +CONFIG_MODULES=y
  544. +CONFIG_MODULE_UNLOAD=y
  545. +CONFIG_MODVERSIONS=y
  546. +CONFIG_MODULE_SRCVERSION_ALL=y
  547. +CONFIG_BLK_DEV_THROTTLING=y
  548. +CONFIG_PARTITION_ADVANCED=y
  549. +CONFIG_MAC_PARTITION=y
  550. +CONFIG_CFQ_GROUP_IOSCHED=y
  551. +CONFIG_ARCH_BCM2708=y
  552. +CONFIG_PREEMPT=y
  553. +CONFIG_AEABI=y
  554. +CONFIG_CLEANCACHE=y
  555. +CONFIG_FRONTSWAP=y
  556. +CONFIG_UACCESS_WITH_MEMCPY=y
  557. +CONFIG_SECCOMP=y
  558. +CONFIG_CC_STACKPROTECTOR=y
  559. +CONFIG_ZBOOT_ROM_TEXT=0x0
  560. +CONFIG_ZBOOT_ROM_BSS=0x0
  561. +CONFIG_CMDLINE="console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  562. +CONFIG_KEXEC=y
  563. +CONFIG_CPU_FREQ=y
  564. +CONFIG_CPU_FREQ_STAT=m
  565. +CONFIG_CPU_FREQ_STAT_DETAILS=y
  566. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  567. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  568. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  569. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  570. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  571. +CONFIG_CPU_IDLE=y
  572. +CONFIG_VFP=y
  573. +CONFIG_BINFMT_MISC=m
  574. +CONFIG_NET=y
  575. +CONFIG_PACKET=y
  576. +CONFIG_UNIX=y
  577. +CONFIG_XFRM_USER=y
  578. +CONFIG_NET_KEY=m
  579. +CONFIG_INET=y
  580. +CONFIG_IP_MULTICAST=y
  581. +CONFIG_IP_ADVANCED_ROUTER=y
  582. +CONFIG_IP_MULTIPLE_TABLES=y
  583. +CONFIG_IP_ROUTE_MULTIPATH=y
  584. +CONFIG_IP_ROUTE_VERBOSE=y
  585. +CONFIG_IP_PNP=y
  586. +CONFIG_IP_PNP_DHCP=y
  587. +CONFIG_IP_PNP_RARP=y
  588. +CONFIG_NET_IPIP=m
  589. +CONFIG_NET_IPGRE_DEMUX=m
  590. +CONFIG_NET_IPGRE=m
  591. +CONFIG_IP_MROUTE=y
  592. +CONFIG_IP_MROUTE_MULTIPLE_TABLES=y
  593. +CONFIG_IP_PIMSM_V1=y
  594. +CONFIG_IP_PIMSM_V2=y
  595. +CONFIG_SYN_COOKIES=y
  596. +CONFIG_INET_AH=m
  597. +CONFIG_INET_ESP=m
  598. +CONFIG_INET_IPCOMP=m
  599. +CONFIG_INET_XFRM_MODE_TRANSPORT=m
  600. +CONFIG_INET_XFRM_MODE_TUNNEL=m
  601. +CONFIG_INET_XFRM_MODE_BEET=m
  602. +CONFIG_INET_LRO=m
  603. +CONFIG_INET_DIAG=m
  604. +CONFIG_IPV6_PRIVACY=y
  605. +CONFIG_INET6_AH=m
  606. +CONFIG_INET6_ESP=m
  607. +CONFIG_INET6_IPCOMP=m
  608. +CONFIG_IPV6_MULTIPLE_TABLES=y
  609. +CONFIG_IPV6_MROUTE=y
  610. +CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
  611. +CONFIG_IPV6_PIMSM_V2=y
  612. +CONFIG_NETFILTER=y
  613. +CONFIG_NF_CONNTRACK=m
  614. +CONFIG_NF_CONNTRACK_ZONES=y
  615. +CONFIG_NF_CONNTRACK_EVENTS=y
  616. +CONFIG_NF_CONNTRACK_TIMESTAMP=y
  617. +CONFIG_NF_CT_PROTO_DCCP=m
  618. +CONFIG_NF_CT_PROTO_UDPLITE=m
  619. +CONFIG_NF_CONNTRACK_AMANDA=m
  620. +CONFIG_NF_CONNTRACK_FTP=m
  621. +CONFIG_NF_CONNTRACK_H323=m
  622. +CONFIG_NF_CONNTRACK_IRC=m
  623. +CONFIG_NF_CONNTRACK_NETBIOS_NS=m
  624. +CONFIG_NF_CONNTRACK_SNMP=m
  625. +CONFIG_NF_CONNTRACK_PPTP=m
  626. +CONFIG_NF_CONNTRACK_SANE=m
  627. +CONFIG_NF_CONNTRACK_SIP=m
  628. +CONFIG_NF_CONNTRACK_TFTP=m
  629. +CONFIG_NF_CT_NETLINK=m
  630. +CONFIG_NETFILTER_TPROXY=m
  631. +CONFIG_NETFILTER_XT_SET=m
  632. +CONFIG_NETFILTER_XT_TARGET_AUDIT=m
  633. +CONFIG_NETFILTER_XT_TARGET_CHECKSUM=m
  634. +CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
  635. +CONFIG_NETFILTER_XT_TARGET_CONNMARK=m
  636. +CONFIG_NETFILTER_XT_TARGET_DSCP=m
  637. +CONFIG_NETFILTER_XT_TARGET_HMARK=m
  638. +CONFIG_NETFILTER_XT_TARGET_IDLETIMER=m
  639. +CONFIG_NETFILTER_XT_TARGET_LED=m
  640. +CONFIG_NETFILTER_XT_TARGET_LOG=m
  641. +CONFIG_NETFILTER_XT_TARGET_MARK=m
  642. +CONFIG_NETFILTER_XT_TARGET_NFLOG=m
  643. +CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
  644. +CONFIG_NETFILTER_XT_TARGET_NOTRACK=m
  645. +CONFIG_NETFILTER_XT_TARGET_TEE=m
  646. +CONFIG_NETFILTER_XT_TARGET_TPROXY=m
  647. +CONFIG_NETFILTER_XT_TARGET_TRACE=m
  648. +CONFIG_NETFILTER_XT_TARGET_TCPMSS=m
  649. +CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP=m
  650. +CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=m
  651. +CONFIG_NETFILTER_XT_MATCH_BPF=m
  652. +CONFIG_NETFILTER_XT_MATCH_CLUSTER=m
  653. +CONFIG_NETFILTER_XT_MATCH_COMMENT=m
  654. +CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
  655. +CONFIG_NETFILTER_XT_MATCH_CONNLABEL=m
  656. +CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=m
  657. +CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
  658. +CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
  659. +CONFIG_NETFILTER_XT_MATCH_CPU=m
  660. +CONFIG_NETFILTER_XT_MATCH_DCCP=m
  661. +CONFIG_NETFILTER_XT_MATCH_DEVGROUP=m
  662. +CONFIG_NETFILTER_XT_MATCH_DSCP=m
  663. +CONFIG_NETFILTER_XT_MATCH_ESP=m
  664. +CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=m
  665. +CONFIG_NETFILTER_XT_MATCH_HELPER=m
  666. +CONFIG_NETFILTER_XT_MATCH_IPRANGE=m
  667. +CONFIG_NETFILTER_XT_MATCH_IPVS=m
  668. +CONFIG_NETFILTER_XT_MATCH_LENGTH=m
  669. +CONFIG_NETFILTER_XT_MATCH_LIMIT=m
  670. +CONFIG_NETFILTER_XT_MATCH_MAC=m
  671. +CONFIG_NETFILTER_XT_MATCH_MARK=m
  672. +CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
  673. +CONFIG_NETFILTER_XT_MATCH_NFACCT=m
  674. +CONFIG_NETFILTER_XT_MATCH_OSF=m
  675. +CONFIG_NETFILTER_XT_MATCH_OWNER=m
  676. +CONFIG_NETFILTER_XT_MATCH_POLICY=m
  677. +CONFIG_NETFILTER_XT_MATCH_PHYSDEV=m
  678. +CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
  679. +CONFIG_NETFILTER_XT_MATCH_QUOTA=m
  680. +CONFIG_NETFILTER_XT_MATCH_RATEEST=m
  681. +CONFIG_NETFILTER_XT_MATCH_REALM=m
  682. +CONFIG_NETFILTER_XT_MATCH_RECENT=m
  683. +CONFIG_NETFILTER_XT_MATCH_SOCKET=m
  684. +CONFIG_NETFILTER_XT_MATCH_STATE=m
  685. +CONFIG_NETFILTER_XT_MATCH_STATISTIC=m
  686. +CONFIG_NETFILTER_XT_MATCH_STRING=m
  687. +CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
  688. +CONFIG_NETFILTER_XT_MATCH_TIME=m
  689. +CONFIG_NETFILTER_XT_MATCH_U32=m
  690. +CONFIG_IP_SET=m
  691. +CONFIG_IP_SET_BITMAP_IP=m
  692. +CONFIG_IP_SET_BITMAP_IPMAC=m
  693. +CONFIG_IP_SET_BITMAP_PORT=m
  694. +CONFIG_IP_SET_HASH_IP=m
  695. +CONFIG_IP_SET_HASH_IPPORT=m
  696. +CONFIG_IP_SET_HASH_IPPORTIP=m
  697. +CONFIG_IP_SET_HASH_IPPORTNET=m
  698. +CONFIG_IP_SET_HASH_NET=m
  699. +CONFIG_IP_SET_HASH_NETPORT=m
  700. +CONFIG_IP_SET_HASH_NETIFACE=m
  701. +CONFIG_IP_SET_LIST_SET=m
  702. +CONFIG_IP_VS=m
  703. +CONFIG_IP_VS_PROTO_TCP=y
  704. +CONFIG_IP_VS_PROTO_UDP=y
  705. +CONFIG_IP_VS_PROTO_ESP=y
  706. +CONFIG_IP_VS_PROTO_AH=y
  707. +CONFIG_IP_VS_PROTO_SCTP=y
  708. +CONFIG_IP_VS_RR=m
  709. +CONFIG_IP_VS_WRR=m
  710. +CONFIG_IP_VS_LC=m
  711. +CONFIG_IP_VS_WLC=m
  712. +CONFIG_IP_VS_LBLC=m
  713. +CONFIG_IP_VS_LBLCR=m
  714. +CONFIG_IP_VS_DH=m
  715. +CONFIG_IP_VS_SH=m
  716. +CONFIG_IP_VS_SED=m
  717. +CONFIG_IP_VS_NQ=m
  718. +CONFIG_IP_VS_FTP=m
  719. +CONFIG_IP_VS_PE_SIP=m
  720. +CONFIG_NF_CONNTRACK_IPV4=m
  721. +CONFIG_IP_NF_IPTABLES=m
  722. +CONFIG_IP_NF_MATCH_AH=m
  723. +CONFIG_IP_NF_MATCH_ECN=m
  724. +CONFIG_IP_NF_MATCH_TTL=m
  725. +CONFIG_IP_NF_FILTER=m
  726. +CONFIG_IP_NF_TARGET_REJECT=m
  727. +CONFIG_IP_NF_TARGET_ULOG=m
  728. +CONFIG_NF_NAT_IPV4=m
  729. +CONFIG_IP_NF_TARGET_MASQUERADE=m
  730. +CONFIG_IP_NF_TARGET_NETMAP=m
  731. +CONFIG_IP_NF_TARGET_REDIRECT=m
  732. +CONFIG_IP_NF_MANGLE=m
  733. +CONFIG_IP_NF_TARGET_ECN=m
  734. +CONFIG_IP_NF_TARGET_TTL=m
  735. +CONFIG_IP_NF_RAW=m
  736. +CONFIG_IP_NF_ARPTABLES=m
  737. +CONFIG_IP_NF_ARPFILTER=m
  738. +CONFIG_IP_NF_ARP_MANGLE=m
  739. +CONFIG_NF_CONNTRACK_IPV6=m
  740. +CONFIG_IP6_NF_IPTABLES=m
  741. +CONFIG_IP6_NF_MATCH_AH=m
  742. +CONFIG_IP6_NF_MATCH_EUI64=m
  743. +CONFIG_IP6_NF_MATCH_FRAG=m
  744. +CONFIG_IP6_NF_MATCH_OPTS=m
  745. +CONFIG_IP6_NF_MATCH_HL=m
  746. +CONFIG_IP6_NF_MATCH_IPV6HEADER=m
  747. +CONFIG_IP6_NF_MATCH_MH=m
  748. +CONFIG_IP6_NF_MATCH_RT=m
  749. +CONFIG_IP6_NF_TARGET_HL=m
  750. +CONFIG_IP6_NF_FILTER=m
  751. +CONFIG_IP6_NF_TARGET_REJECT=m
  752. +CONFIG_IP6_NF_MANGLE=m
  753. +CONFIG_IP6_NF_RAW=m
  754. +CONFIG_NF_NAT_IPV6=m
  755. +CONFIG_IP6_NF_TARGET_MASQUERADE=m
  756. +CONFIG_IP6_NF_TARGET_NPT=m
  757. +CONFIG_BRIDGE_NF_EBTABLES=m
  758. +CONFIG_BRIDGE_EBT_BROUTE=m
  759. +CONFIG_BRIDGE_EBT_T_FILTER=m
  760. +CONFIG_BRIDGE_EBT_T_NAT=m
  761. +CONFIG_BRIDGE_EBT_802_3=m
  762. +CONFIG_BRIDGE_EBT_AMONG=m
  763. +CONFIG_BRIDGE_EBT_ARP=m
  764. +CONFIG_BRIDGE_EBT_IP=m
  765. +CONFIG_BRIDGE_EBT_IP6=m
  766. +CONFIG_BRIDGE_EBT_LIMIT=m
  767. +CONFIG_BRIDGE_EBT_MARK=m
  768. +CONFIG_BRIDGE_EBT_PKTTYPE=m
  769. +CONFIG_BRIDGE_EBT_STP=m
  770. +CONFIG_BRIDGE_EBT_VLAN=m
  771. +CONFIG_BRIDGE_EBT_ARPREPLY=m
  772. +CONFIG_BRIDGE_EBT_DNAT=m
  773. +CONFIG_BRIDGE_EBT_MARK_T=m
  774. +CONFIG_BRIDGE_EBT_REDIRECT=m
  775. +CONFIG_BRIDGE_EBT_SNAT=m
  776. +CONFIG_BRIDGE_EBT_LOG=m
  777. +CONFIG_BRIDGE_EBT_ULOG=m
  778. +CONFIG_BRIDGE_EBT_NFLOG=m
  779. +CONFIG_SCTP_COOKIE_HMAC_SHA1=y
  780. +CONFIG_L2TP=m
  781. +CONFIG_BRIDGE=m
  782. +CONFIG_VLAN_8021Q=m
  783. +CONFIG_VLAN_8021Q_GVRP=y
  784. +CONFIG_ATALK=m
  785. +CONFIG_NET_SCHED=y
  786. +CONFIG_NET_SCH_CBQ=m
  787. +CONFIG_NET_SCH_HTB=m
  788. +CONFIG_NET_SCH_HFSC=m
  789. +CONFIG_NET_SCH_PRIO=m
  790. +CONFIG_NET_SCH_MULTIQ=m
  791. +CONFIG_NET_SCH_RED=m
  792. +CONFIG_NET_SCH_SFB=m
  793. +CONFIG_NET_SCH_SFQ=m
  794. +CONFIG_NET_SCH_TEQL=m
  795. +CONFIG_NET_SCH_TBF=m
  796. +CONFIG_NET_SCH_GRED=m
  797. +CONFIG_NET_SCH_DSMARK=m
  798. +CONFIG_NET_SCH_NETEM=m
  799. +CONFIG_NET_SCH_DRR=m
  800. +CONFIG_NET_SCH_MQPRIO=m
  801. +CONFIG_NET_SCH_CHOKE=m
  802. +CONFIG_NET_SCH_QFQ=m
  803. +CONFIG_NET_SCH_CODEL=m
  804. +CONFIG_NET_SCH_FQ_CODEL=m
  805. +CONFIG_NET_SCH_INGRESS=m
  806. +CONFIG_NET_SCH_PLUG=m
  807. +CONFIG_NET_CLS_BASIC=m
  808. +CONFIG_NET_CLS_TCINDEX=m
  809. +CONFIG_NET_CLS_ROUTE4=m
  810. +CONFIG_NET_CLS_FW=m
  811. +CONFIG_NET_CLS_U32=m
  812. +CONFIG_CLS_U32_MARK=y
  813. +CONFIG_NET_CLS_RSVP=m
  814. +CONFIG_NET_CLS_RSVP6=m
  815. +CONFIG_NET_CLS_FLOW=m
  816. +CONFIG_NET_CLS_CGROUP=m
  817. +CONFIG_NET_EMATCH=y
  818. +CONFIG_NET_EMATCH_CMP=m
  819. +CONFIG_NET_EMATCH_NBYTE=m
  820. +CONFIG_NET_EMATCH_U32=m
  821. +CONFIG_NET_EMATCH_META=m
  822. +CONFIG_NET_EMATCH_TEXT=m
  823. +CONFIG_NET_EMATCH_IPSET=m
  824. +CONFIG_NET_CLS_ACT=y
  825. +CONFIG_NET_ACT_POLICE=m
  826. +CONFIG_NET_ACT_GACT=m
  827. +CONFIG_GACT_PROB=y
  828. +CONFIG_NET_ACT_MIRRED=m
  829. +CONFIG_NET_ACT_IPT=m
  830. +CONFIG_NET_ACT_NAT=m
  831. +CONFIG_NET_ACT_PEDIT=m
  832. +CONFIG_NET_ACT_SIMP=m
  833. +CONFIG_NET_ACT_SKBEDIT=m
  834. +CONFIG_NET_ACT_CSUM=m
  835. +CONFIG_BATMAN_ADV=m
  836. +CONFIG_OPENVSWITCH=m
  837. +CONFIG_NET_PKTGEN=m
  838. +CONFIG_HAMRADIO=y
  839. +CONFIG_AX25=m
  840. +CONFIG_NETROM=m
  841. +CONFIG_ROSE=m
  842. +CONFIG_MKISS=m
  843. +CONFIG_6PACK=m
  844. +CONFIG_BPQETHER=m
  845. +CONFIG_BAYCOM_SER_FDX=m
  846. +CONFIG_BAYCOM_SER_HDX=m
  847. +CONFIG_YAM=m
  848. +CONFIG_IRDA=m
  849. +CONFIG_IRLAN=m
  850. +CONFIG_IRNET=m
  851. +CONFIG_IRCOMM=m
  852. +CONFIG_IRDA_ULTRA=y
  853. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  854. +CONFIG_IRDA_FAST_RR=y
  855. +CONFIG_IRTTY_SIR=m
  856. +CONFIG_KINGSUN_DONGLE=m
  857. +CONFIG_KSDAZZLE_DONGLE=m
  858. +CONFIG_KS959_DONGLE=m
  859. +CONFIG_USB_IRDA=m
  860. +CONFIG_SIGMATEL_FIR=m
  861. +CONFIG_MCS_FIR=m
  862. +CONFIG_BT=m
  863. +CONFIG_BT_RFCOMM=m
  864. +CONFIG_BT_RFCOMM_TTY=y
  865. +CONFIG_BT_BNEP=m
  866. +CONFIG_BT_BNEP_MC_FILTER=y
  867. +CONFIG_BT_BNEP_PROTO_FILTER=y
  868. +CONFIG_BT_HIDP=m
  869. +CONFIG_BT_HCIBTUSB=m
  870. +CONFIG_BT_HCIBCM203X=m
  871. +CONFIG_BT_HCIBPA10X=m
  872. +CONFIG_BT_HCIBFUSB=m
  873. +CONFIG_BT_HCIVHCI=m
  874. +CONFIG_BT_MRVL=m
  875. +CONFIG_BT_MRVL_SDIO=m
  876. +CONFIG_BT_ATH3K=m
  877. +CONFIG_BT_WILINK=m
  878. +CONFIG_CFG80211=m
  879. +CONFIG_CFG80211_WEXT=y
  880. +CONFIG_MAC80211=m
  881. +CONFIG_MAC80211_RC_PID=y
  882. +CONFIG_MAC80211_MESH=y
  883. +CONFIG_WIMAX=m
  884. +CONFIG_RFKILL=m
  885. +CONFIG_RFKILL_INPUT=y
  886. +CONFIG_NET_9P=m
  887. +CONFIG_NFC=m
  888. +CONFIG_NFC_PN533=m
  889. +CONFIG_DEVTMPFS=y
  890. +CONFIG_DEVTMPFS_MOUNT=y
  891. +CONFIG_CMA=y
  892. +CONFIG_BLK_DEV_LOOP=y
  893. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  894. +CONFIG_BLK_DEV_DRBD=m
  895. +CONFIG_BLK_DEV_NBD=m
  896. +CONFIG_BLK_DEV_RAM=y
  897. +CONFIG_CDROM_PKTCDVD=m
  898. +CONFIG_SCSI=y
  899. +# CONFIG_SCSI_PROC_FS is not set
  900. +CONFIG_BLK_DEV_SD=y
  901. +CONFIG_CHR_DEV_ST=m
  902. +CONFIG_CHR_DEV_OSST=m
  903. +CONFIG_BLK_DEV_SR=m
  904. +CONFIG_SCSI_MULTI_LUN=y
  905. +CONFIG_SCSI_ISCSI_ATTRS=y
  906. +CONFIG_ISCSI_TCP=m
  907. +CONFIG_ISCSI_BOOT_SYSFS=m
  908. +CONFIG_MD=y
  909. +CONFIG_MD_LINEAR=m
  910. +CONFIG_MD_RAID0=m
  911. +CONFIG_BLK_DEV_DM=m
  912. +CONFIG_DM_CRYPT=m
  913. +CONFIG_DM_SNAPSHOT=m
  914. +CONFIG_DM_MIRROR=m
  915. +CONFIG_DM_RAID=m
  916. +CONFIG_DM_LOG_USERSPACE=m
  917. +CONFIG_DM_ZERO=m
  918. +CONFIG_DM_DELAY=m
  919. +CONFIG_NETDEVICES=y
  920. +CONFIG_BONDING=m
  921. +CONFIG_DUMMY=m
  922. +CONFIG_IFB=m
  923. +CONFIG_MACVLAN=m
  924. +CONFIG_NETCONSOLE=m
  925. +CONFIG_TUN=m
  926. +CONFIG_MDIO_BITBANG=m
  927. +CONFIG_PPP=m
  928. +CONFIG_PPP_BSDCOMP=m
  929. +CONFIG_PPP_DEFLATE=m
  930. +CONFIG_PPP_FILTER=y
  931. +CONFIG_PPP_MPPE=m
  932. +CONFIG_PPP_MULTILINK=y
  933. +CONFIG_PPPOE=m
  934. +CONFIG_PPPOL2TP=m
  935. +CONFIG_PPP_ASYNC=m
  936. +CONFIG_PPP_SYNC_TTY=m
  937. +CONFIG_SLIP=m
  938. +CONFIG_SLIP_COMPRESSED=y
  939. +CONFIG_SLIP_SMART=y
  940. +CONFIG_USB_CATC=m
  941. +CONFIG_USB_KAWETH=m
  942. +CONFIG_USB_PEGASUS=m
  943. +CONFIG_USB_RTL8150=m
  944. +CONFIG_USB_RTL8152=m
  945. +CONFIG_USB_USBNET=y
  946. +CONFIG_USB_NET_AX8817X=m
  947. +CONFIG_USB_NET_CDCETHER=m
  948. +CONFIG_USB_NET_CDC_EEM=m
  949. +CONFIG_USB_NET_CDC_NCM=m
  950. +CONFIG_USB_NET_CDC_MBIM=m
  951. +CONFIG_USB_NET_DM9601=m
  952. +CONFIG_USB_NET_SMSC75XX=m
  953. +CONFIG_USB_NET_SMSC95XX=y
  954. +CONFIG_USB_NET_GL620A=m
  955. +CONFIG_USB_NET_NET1080=m
  956. +CONFIG_USB_NET_PLUSB=m
  957. +CONFIG_USB_NET_MCS7830=m
  958. +CONFIG_USB_NET_CDC_SUBSET=m
  959. +CONFIG_USB_ALI_M5632=y
  960. +CONFIG_USB_AN2720=y
  961. +CONFIG_USB_EPSON2888=y
  962. +CONFIG_USB_KC2190=y
  963. +CONFIG_USB_NET_ZAURUS=m
  964. +CONFIG_USB_NET_CX82310_ETH=m
  965. +CONFIG_USB_NET_KALMIA=m
  966. +CONFIG_USB_NET_QMI_WWAN=m
  967. +CONFIG_USB_NET_INT51X1=m
  968. +CONFIG_USB_IPHETH=m
  969. +CONFIG_USB_SIERRA_NET=m
  970. +CONFIG_USB_VL600=m
  971. +CONFIG_LIBERTAS_THINFIRM=m
  972. +CONFIG_LIBERTAS_THINFIRM_USB=m
  973. +CONFIG_AT76C50X_USB=m
  974. +CONFIG_USB_ZD1201=m
  975. +CONFIG_USB_NET_RNDIS_WLAN=m
  976. +CONFIG_RTL8187=m
  977. +CONFIG_MAC80211_HWSIM=m
  978. +CONFIG_ATH_CARDS=m
  979. +CONFIG_ATH9K=m
  980. +CONFIG_ATH9K_HTC=m
  981. +CONFIG_CARL9170=m
  982. +CONFIG_ATH6KL=m
  983. +CONFIG_ATH6KL_USB=m
  984. +CONFIG_AR5523=m
  985. +CONFIG_B43=m
  986. +# CONFIG_B43_PHY_N is not set
  987. +CONFIG_B43LEGACY=m
  988. +CONFIG_HOSTAP=m
  989. +CONFIG_LIBERTAS=m
  990. +CONFIG_LIBERTAS_USB=m
  991. +CONFIG_LIBERTAS_SDIO=m
  992. +CONFIG_P54_COMMON=m
  993. +CONFIG_P54_USB=m
  994. +CONFIG_RT2X00=m
  995. +CONFIG_RT2500USB=m
  996. +CONFIG_RT73USB=m
  997. +CONFIG_RT2800USB=m
  998. +CONFIG_RT2800USB_RT53XX=y
  999. +CONFIG_RT2800USB_UNKNOWN=y
  1000. +CONFIG_RTL8192CU=m
  1001. +CONFIG_ZD1211RW=m
  1002. +CONFIG_MWIFIEX=m
  1003. +CONFIG_MWIFIEX_SDIO=m
  1004. +CONFIG_WIMAX_I2400M_USB=m
  1005. +CONFIG_INPUT_POLLDEV=m
  1006. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1007. +CONFIG_INPUT_JOYDEV=m
  1008. +CONFIG_INPUT_EVDEV=m
  1009. +# CONFIG_INPUT_KEYBOARD is not set
  1010. +# CONFIG_INPUT_MOUSE is not set
  1011. +CONFIG_INPUT_JOYSTICK=y
  1012. +CONFIG_JOYSTICK_IFORCE=m
  1013. +CONFIG_JOYSTICK_IFORCE_USB=y
  1014. +CONFIG_JOYSTICK_XPAD=y
  1015. +CONFIG_JOYSTICK_XPAD_FF=y
  1016. +CONFIG_INPUT_MISC=y
  1017. +CONFIG_INPUT_AD714X=m
  1018. +CONFIG_INPUT_ATI_REMOTE2=m
  1019. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1020. +CONFIG_INPUT_POWERMATE=m
  1021. +CONFIG_INPUT_YEALINK=m
  1022. +CONFIG_INPUT_CM109=m
  1023. +CONFIG_INPUT_UINPUT=m
  1024. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1025. +CONFIG_INPUT_ADXL34X=m
  1026. +CONFIG_INPUT_CMA3000=m
  1027. +CONFIG_SERIO=m
  1028. +CONFIG_SERIO_RAW=m
  1029. +CONFIG_GAMEPORT=m
  1030. +CONFIG_GAMEPORT_NS558=m
  1031. +CONFIG_GAMEPORT_L4=m
  1032. +CONFIG_VT_HW_CONSOLE_BINDING=y
  1033. +# CONFIG_LEGACY_PTYS is not set
  1034. +# CONFIG_DEVKMEM is not set
  1035. +CONFIG_SERIAL_AMBA_PL011=y
  1036. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1037. +CONFIG_TTY_PRINTK=y
  1038. +CONFIG_HW_RANDOM=y
  1039. +CONFIG_HW_RANDOM_BCM2708=m
  1040. +CONFIG_RAW_DRIVER=y
  1041. +CONFIG_BRCM_CHAR_DRIVERS=y
  1042. +CONFIG_BCM_VC_CMA=y
  1043. +CONFIG_I2C=y
  1044. +CONFIG_I2C_CHARDEV=m
  1045. +CONFIG_I2C_BCM2708=m
  1046. +CONFIG_SPI=y
  1047. +CONFIG_SPI_BCM2708=m
  1048. +CONFIG_SPI_SPIDEV=y
  1049. +CONFIG_GPIO_SYSFS=y
  1050. +CONFIG_W1=m
  1051. +CONFIG_W1_MASTER_DS2490=m
  1052. +CONFIG_W1_MASTER_DS2482=m
  1053. +CONFIG_W1_MASTER_DS1WM=m
  1054. +CONFIG_W1_MASTER_GPIO=m
  1055. +CONFIG_W1_SLAVE_THERM=m
  1056. +CONFIG_W1_SLAVE_SMEM=m
  1057. +CONFIG_W1_SLAVE_DS2408=m
  1058. +CONFIG_W1_SLAVE_DS2413=m
  1059. +CONFIG_W1_SLAVE_DS2423=m
  1060. +CONFIG_W1_SLAVE_DS2431=m
  1061. +CONFIG_W1_SLAVE_DS2433=m
  1062. +CONFIG_W1_SLAVE_DS2760=m
  1063. +CONFIG_W1_SLAVE_DS2780=m
  1064. +CONFIG_W1_SLAVE_DS2781=m
  1065. +CONFIG_W1_SLAVE_DS28E04=m
  1066. +CONFIG_W1_SLAVE_BQ27000=m
  1067. +CONFIG_BATTERY_DS2760=m
  1068. +# CONFIG_HWMON is not set
  1069. +CONFIG_THERMAL=y
  1070. +CONFIG_THERMAL_BCM2835=y
  1071. +CONFIG_WATCHDOG=y
  1072. +CONFIG_BCM2708_WDT=m
  1073. +CONFIG_MEDIA_SUPPORT=m
  1074. +CONFIG_MEDIA_CAMERA_SUPPORT=y
  1075. +CONFIG_MEDIA_ANALOG_TV_SUPPORT=y
  1076. +CONFIG_MEDIA_DIGITAL_TV_SUPPORT=y
  1077. +CONFIG_MEDIA_RADIO_SUPPORT=y
  1078. +CONFIG_MEDIA_RC_SUPPORT=y
  1079. +CONFIG_MEDIA_CONTROLLER=y
  1080. +CONFIG_LIRC=m
  1081. +CONFIG_RC_DEVICES=y
  1082. +CONFIG_RC_ATI_REMOTE=m
  1083. +CONFIG_IR_IMON=m
  1084. +CONFIG_IR_MCEUSB=m
  1085. +CONFIG_IR_REDRAT3=m
  1086. +CONFIG_IR_STREAMZAP=m
  1087. +CONFIG_IR_IGUANA=m
  1088. +CONFIG_IR_TTUSBIR=m
  1089. +CONFIG_RC_LOOPBACK=m
  1090. +CONFIG_IR_GPIO_CIR=m
  1091. +CONFIG_MEDIA_USB_SUPPORT=y
  1092. +CONFIG_USB_VIDEO_CLASS=m
  1093. +CONFIG_USB_M5602=m
  1094. +CONFIG_USB_STV06XX=m
  1095. +CONFIG_USB_GL860=m
  1096. +CONFIG_USB_GSPCA_BENQ=m
  1097. +CONFIG_USB_GSPCA_CONEX=m
  1098. +CONFIG_USB_GSPCA_CPIA1=m
  1099. +CONFIG_USB_GSPCA_ETOMS=m
  1100. +CONFIG_USB_GSPCA_FINEPIX=m
  1101. +CONFIG_USB_GSPCA_JEILINJ=m
  1102. +CONFIG_USB_GSPCA_JL2005BCD=m
  1103. +CONFIG_USB_GSPCA_KINECT=m
  1104. +CONFIG_USB_GSPCA_KONICA=m
  1105. +CONFIG_USB_GSPCA_MARS=m
  1106. +CONFIG_USB_GSPCA_MR97310A=m
  1107. +CONFIG_USB_GSPCA_NW80X=m
  1108. +CONFIG_USB_GSPCA_OV519=m
  1109. +CONFIG_USB_GSPCA_OV534=m
  1110. +CONFIG_USB_GSPCA_OV534_9=m
  1111. +CONFIG_USB_GSPCA_PAC207=m
  1112. +CONFIG_USB_GSPCA_PAC7302=m
  1113. +CONFIG_USB_GSPCA_PAC7311=m
  1114. +CONFIG_USB_GSPCA_SE401=m
  1115. +CONFIG_USB_GSPCA_SN9C2028=m
  1116. +CONFIG_USB_GSPCA_SN9C20X=m
  1117. +CONFIG_USB_GSPCA_SONIXB=m
  1118. +CONFIG_USB_GSPCA_SONIXJ=m
  1119. +CONFIG_USB_GSPCA_SPCA500=m
  1120. +CONFIG_USB_GSPCA_SPCA501=m
  1121. +CONFIG_USB_GSPCA_SPCA505=m
  1122. +CONFIG_USB_GSPCA_SPCA506=m
  1123. +CONFIG_USB_GSPCA_SPCA508=m
  1124. +CONFIG_USB_GSPCA_SPCA561=m
  1125. +CONFIG_USB_GSPCA_SPCA1528=m
  1126. +CONFIG_USB_GSPCA_SQ905=m
  1127. +CONFIG_USB_GSPCA_SQ905C=m
  1128. +CONFIG_USB_GSPCA_SQ930X=m
  1129. +CONFIG_USB_GSPCA_STK014=m
  1130. +CONFIG_USB_GSPCA_STV0680=m
  1131. +CONFIG_USB_GSPCA_SUNPLUS=m
  1132. +CONFIG_USB_GSPCA_T613=m
  1133. +CONFIG_USB_GSPCA_TOPRO=m
  1134. +CONFIG_USB_GSPCA_TV8532=m
  1135. +CONFIG_USB_GSPCA_VC032X=m
  1136. +CONFIG_USB_GSPCA_VICAM=m
  1137. +CONFIG_USB_GSPCA_XIRLINK_CIT=m
  1138. +CONFIG_USB_GSPCA_ZC3XX=m
  1139. +CONFIG_USB_PWC=m
  1140. +CONFIG_VIDEO_CPIA2=m
  1141. +CONFIG_USB_ZR364XX=m
  1142. +CONFIG_USB_STKWEBCAM=m
  1143. +CONFIG_USB_S2255=m
  1144. +CONFIG_USB_SN9C102=m
  1145. +CONFIG_VIDEO_PVRUSB2=m
  1146. +CONFIG_VIDEO_HDPVR=m
  1147. +CONFIG_VIDEO_TLG2300=m
  1148. +CONFIG_VIDEO_USBVISION=m
  1149. +CONFIG_VIDEO_STK1160=m
  1150. +CONFIG_VIDEO_STK1160_AC97=y
  1151. +CONFIG_VIDEO_AU0828=m
  1152. +CONFIG_VIDEO_CX231XX=m
  1153. +CONFIG_VIDEO_CX231XX_ALSA=m
  1154. +CONFIG_VIDEO_CX231XX_DVB=m
  1155. +CONFIG_VIDEO_TM6000=m
  1156. +CONFIG_VIDEO_TM6000_ALSA=m
  1157. +CONFIG_VIDEO_TM6000_DVB=m
  1158. +CONFIG_DVB_USB=m
  1159. +CONFIG_DVB_USB_A800=m
  1160. +CONFIG_DVB_USB_DIBUSB_MB=m
  1161. +CONFIG_DVB_USB_DIBUSB_MB_FAULTY=y
  1162. +CONFIG_DVB_USB_DIBUSB_MC=m
  1163. +CONFIG_DVB_USB_DIB0700=m
  1164. +CONFIG_DVB_USB_UMT_010=m
  1165. +CONFIG_DVB_USB_CXUSB=m
  1166. +CONFIG_DVB_USB_M920X=m
  1167. +CONFIG_DVB_USB_DIGITV=m
  1168. +CONFIG_DVB_USB_VP7045=m
  1169. +CONFIG_DVB_USB_VP702X=m
  1170. +CONFIG_DVB_USB_GP8PSK=m
  1171. +CONFIG_DVB_USB_NOVA_T_USB2=m
  1172. +CONFIG_DVB_USB_TTUSB2=m
  1173. +CONFIG_DVB_USB_DTT200U=m
  1174. +CONFIG_DVB_USB_OPERA1=m
  1175. +CONFIG_DVB_USB_AF9005=m
  1176. +CONFIG_DVB_USB_AF9005_REMOTE=m
  1177. +CONFIG_DVB_USB_PCTV452E=m
  1178. +CONFIG_DVB_USB_DW2102=m
  1179. +CONFIG_DVB_USB_CINERGY_T2=m
  1180. +CONFIG_DVB_USB_DTV5100=m
  1181. +CONFIG_DVB_USB_FRIIO=m
  1182. +CONFIG_DVB_USB_AZ6027=m
  1183. +CONFIG_DVB_USB_TECHNISAT_USB2=m
  1184. +CONFIG_DVB_USB_V2=m
  1185. +CONFIG_DVB_USB_AF9015=m
  1186. +CONFIG_DVB_USB_AF9035=m
  1187. +CONFIG_DVB_USB_ANYSEE=m
  1188. +CONFIG_DVB_USB_AU6610=m
  1189. +CONFIG_DVB_USB_AZ6007=m
  1190. +CONFIG_DVB_USB_CE6230=m
  1191. +CONFIG_DVB_USB_EC168=m
  1192. +CONFIG_DVB_USB_GL861=m
  1193. +CONFIG_DVB_USB_IT913X=m
  1194. +CONFIG_DVB_USB_LME2510=m
  1195. +CONFIG_DVB_USB_MXL111SF=m
  1196. +CONFIG_DVB_USB_RTL28XXU=m
  1197. +CONFIG_SMS_USB_DRV=m
  1198. +CONFIG_DVB_B2C2_FLEXCOP_USB=m
  1199. +CONFIG_VIDEO_EM28XX=m
  1200. +CONFIG_VIDEO_EM28XX_ALSA=m
  1201. +CONFIG_VIDEO_EM28XX_DVB=m
  1202. +CONFIG_V4L_PLATFORM_DRIVERS=y
  1203. +CONFIG_VIDEO_BCM2835=y
  1204. +CONFIG_VIDEO_BCM2835_MMAL=m
  1205. +CONFIG_RADIO_SI470X=y
  1206. +CONFIG_USB_SI470X=m
  1207. +CONFIG_I2C_SI470X=m
  1208. +CONFIG_USB_MR800=m
  1209. +CONFIG_USB_DSBR=m
  1210. +CONFIG_RADIO_SHARK=m
  1211. +CONFIG_RADIO_SHARK2=m
  1212. +CONFIG_RADIO_SI4713=m
  1213. +CONFIG_USB_KEENE=m
  1214. +CONFIG_USB_MA901=m
  1215. +CONFIG_RADIO_TEA5764=m
  1216. +CONFIG_RADIO_SAA7706H=m
  1217. +CONFIG_RADIO_TEF6862=m
  1218. +CONFIG_RADIO_WL1273=m
  1219. +CONFIG_RADIO_WL128X=m
  1220. +CONFIG_FB=y
  1221. +CONFIG_FB_BCM2708=y
  1222. +# CONFIG_BACKLIGHT_GENERIC is not set
  1223. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1224. +CONFIG_LOGO=y
  1225. +# CONFIG_LOGO_LINUX_MONO is not set
  1226. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1227. +CONFIG_SOUND=y
  1228. +CONFIG_SND=m
  1229. +CONFIG_SND_SEQUENCER=m
  1230. +CONFIG_SND_SEQ_DUMMY=m
  1231. +CONFIG_SND_MIXER_OSS=m
  1232. +CONFIG_SND_PCM_OSS=m
  1233. +CONFIG_SND_SEQUENCER_OSS=y
  1234. +CONFIG_SND_HRTIMER=m
  1235. +CONFIG_SND_DUMMY=m
  1236. +CONFIG_SND_ALOOP=m
  1237. +CONFIG_SND_VIRMIDI=m
  1238. +CONFIG_SND_MTPAV=m
  1239. +CONFIG_SND_SERIAL_U16550=m
  1240. +CONFIG_SND_MPU401=m
  1241. +CONFIG_SND_BCM2835=m
  1242. +CONFIG_SND_USB_AUDIO=m
  1243. +CONFIG_SND_USB_UA101=m
  1244. +CONFIG_SND_USB_CAIAQ=m
  1245. +CONFIG_SND_USB_CAIAQ_INPUT=y
  1246. +CONFIG_SND_USB_6FIRE=m
  1247. +CONFIG_SND_SOC=m
  1248. +CONFIG_SND_SOC_DMAENGINE_PCM=y
  1249. +CONFIG_SND_SOC_GENERIC_DMAENGINE_PCM=y
  1250. +CONFIG_SND_SOC_WM8804=m
  1251. +CONFIG_SND_BCM2708_SOC_I2S=m
  1252. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
  1253. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
  1254. +CONFIG_SND_BCM2708_SOC_RPI_DAC=m
  1255. +CONFIG_SND_SOC_I2C_AND_SPI=m
  1256. +CONFIG_SND_SOC_PCM5102A=m
  1257. +CONFIG_SND_SOC_PCM1794A=m
  1258. +CONFIG_SOUND_PRIME=m
  1259. +CONFIG_HIDRAW=y
  1260. +CONFIG_HID_A4TECH=m
  1261. +CONFIG_HID_ACRUX=m
  1262. +CONFIG_HID_APPLE=m
  1263. +CONFIG_HID_BELKIN=m
  1264. +CONFIG_HID_CHERRY=m
  1265. +CONFIG_HID_CHICONY=m
  1266. +CONFIG_HID_CYPRESS=m
  1267. +CONFIG_HID_DRAGONRISE=m
  1268. +CONFIG_HID_EMS_FF=m
  1269. +CONFIG_HID_ELECOM=m
  1270. +CONFIG_HID_EZKEY=m
  1271. +CONFIG_HID_HOLTEK=m
  1272. +CONFIG_HID_KEYTOUCH=m
  1273. +CONFIG_HID_KYE=m
  1274. +CONFIG_HID_UCLOGIC=m
  1275. +CONFIG_HID_WALTOP=m
  1276. +CONFIG_HID_GYRATION=m
  1277. +CONFIG_HID_TWINHAN=m
  1278. +CONFIG_HID_KENSINGTON=m
  1279. +CONFIG_HID_LCPOWER=m
  1280. +CONFIG_HID_LOGITECH=m
  1281. +CONFIG_HID_MAGICMOUSE=m
  1282. +CONFIG_HID_MICROSOFT=m
  1283. +CONFIG_HID_MONTEREY=m
  1284. +CONFIG_HID_MULTITOUCH=m
  1285. +CONFIG_HID_NTRIG=m
  1286. +CONFIG_HID_ORTEK=m
  1287. +CONFIG_HID_PANTHERLORD=m
  1288. +CONFIG_HID_PETALYNX=m
  1289. +CONFIG_HID_PICOLCD=m
  1290. +CONFIG_HID_ROCCAT=m
  1291. +CONFIG_HID_SAMSUNG=m
  1292. +CONFIG_HID_SONY=m
  1293. +CONFIG_HID_SPEEDLINK=m
  1294. +CONFIG_HID_SUNPLUS=m
  1295. +CONFIG_HID_GREENASIA=m
  1296. +CONFIG_HID_SMARTJOYPLUS=m
  1297. +CONFIG_HID_TOPSEED=m
  1298. +CONFIG_HID_THINGM=m
  1299. +CONFIG_HID_THRUSTMASTER=m
  1300. +CONFIG_HID_WACOM=m
  1301. +CONFIG_HID_WIIMOTE=m
  1302. +CONFIG_HID_ZEROPLUS=m
  1303. +CONFIG_HID_ZYDACRON=m
  1304. +CONFIG_HID_PID=y
  1305. +CONFIG_USB_HIDDEV=y
  1306. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1307. +CONFIG_USB_MON=m
  1308. +CONFIG_USB_DWCOTG=y
  1309. +CONFIG_USB_PRINTER=m
  1310. +CONFIG_USB_STORAGE=y
  1311. +CONFIG_USB_STORAGE_REALTEK=m
  1312. +CONFIG_USB_STORAGE_DATAFAB=m
  1313. +CONFIG_USB_STORAGE_FREECOM=m
  1314. +CONFIG_USB_STORAGE_ISD200=m
  1315. +CONFIG_USB_STORAGE_USBAT=m
  1316. +CONFIG_USB_STORAGE_SDDR09=m
  1317. +CONFIG_USB_STORAGE_SDDR55=m
  1318. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1319. +CONFIG_USB_STORAGE_ALAUDA=m
  1320. +CONFIG_USB_STORAGE_ONETOUCH=m
  1321. +CONFIG_USB_STORAGE_KARMA=m
  1322. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1323. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1324. +CONFIG_USB_MDC800=m
  1325. +CONFIG_USB_MICROTEK=m
  1326. +CONFIG_USB_SERIAL=m
  1327. +CONFIG_USB_SERIAL_GENERIC=y
  1328. +CONFIG_USB_SERIAL_AIRCABLE=m
  1329. +CONFIG_USB_SERIAL_ARK3116=m
  1330. +CONFIG_USB_SERIAL_BELKIN=m
  1331. +CONFIG_USB_SERIAL_CH341=m
  1332. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1333. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1334. +CONFIG_USB_SERIAL_CP210X=m
  1335. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1336. +CONFIG_USB_SERIAL_EMPEG=m
  1337. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1338. +CONFIG_USB_SERIAL_FUNSOFT=m
  1339. +CONFIG_USB_SERIAL_VISOR=m
  1340. +CONFIG_USB_SERIAL_IPAQ=m
  1341. +CONFIG_USB_SERIAL_IR=m
  1342. +CONFIG_USB_SERIAL_EDGEPORT=m
  1343. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1344. +CONFIG_USB_SERIAL_F81232=m
  1345. +CONFIG_USB_SERIAL_GARMIN=m
  1346. +CONFIG_USB_SERIAL_IPW=m
  1347. +CONFIG_USB_SERIAL_IUU=m
  1348. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1349. +CONFIG_USB_SERIAL_KEYSPAN=m
  1350. +CONFIG_USB_SERIAL_KLSI=m
  1351. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1352. +CONFIG_USB_SERIAL_MCT_U232=m
  1353. +CONFIG_USB_SERIAL_METRO=m
  1354. +CONFIG_USB_SERIAL_MOS7720=m
  1355. +CONFIG_USB_SERIAL_MOS7840=m
  1356. +CONFIG_USB_SERIAL_MOTOROLA=m
  1357. +CONFIG_USB_SERIAL_NAVMAN=m
  1358. +CONFIG_USB_SERIAL_PL2303=m
  1359. +CONFIG_USB_SERIAL_OTI6858=m
  1360. +CONFIG_USB_SERIAL_QCAUX=m
  1361. +CONFIG_USB_SERIAL_QUALCOMM=m
  1362. +CONFIG_USB_SERIAL_SPCP8X5=m
  1363. +CONFIG_USB_SERIAL_HP4X=m
  1364. +CONFIG_USB_SERIAL_SAFE=m
  1365. +CONFIG_USB_SERIAL_SIEMENS_MPI=m
  1366. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1367. +CONFIG_USB_SERIAL_SYMBOL=m
  1368. +CONFIG_USB_SERIAL_TI=m
  1369. +CONFIG_USB_SERIAL_CYBERJACK=m
  1370. +CONFIG_USB_SERIAL_XIRCOM=m
  1371. +CONFIG_USB_SERIAL_OPTION=m
  1372. +CONFIG_USB_SERIAL_OMNINET=m
  1373. +CONFIG_USB_SERIAL_OPTICON=m
  1374. +CONFIG_USB_SERIAL_VIVOPAY_SERIAL=m
  1375. +CONFIG_USB_SERIAL_XSENS_MT=m
  1376. +CONFIG_USB_SERIAL_ZIO=m
  1377. +CONFIG_USB_SERIAL_WISHBONE=m
  1378. +CONFIG_USB_SERIAL_ZTE=m
  1379. +CONFIG_USB_SERIAL_SSU100=m
  1380. +CONFIG_USB_SERIAL_QT2=m
  1381. +CONFIG_USB_SERIAL_DEBUG=m
  1382. +CONFIG_USB_EMI62=m
  1383. +CONFIG_USB_EMI26=m
  1384. +CONFIG_USB_ADUTUX=m
  1385. +CONFIG_USB_SEVSEG=m
  1386. +CONFIG_USB_RIO500=m
  1387. +CONFIG_USB_LEGOTOWER=m
  1388. +CONFIG_USB_LCD=m
  1389. +CONFIG_USB_LED=m
  1390. +CONFIG_USB_CYPRESS_CY7C63=m
  1391. +CONFIG_USB_CYTHERM=m
  1392. +CONFIG_USB_IDMOUSE=m
  1393. +CONFIG_USB_FTDI_ELAN=m
  1394. +CONFIG_USB_APPLEDISPLAY=m
  1395. +CONFIG_USB_LD=m
  1396. +CONFIG_USB_TRANCEVIBRATOR=m
  1397. +CONFIG_USB_IOWARRIOR=m
  1398. +CONFIG_USB_TEST=m
  1399. +CONFIG_USB_ISIGHTFW=m
  1400. +CONFIG_USB_YUREX=m
  1401. +CONFIG_MMC=y
  1402. +CONFIG_MMC_BLOCK_MINORS=32
  1403. +CONFIG_MMC_SDHCI=y
  1404. +CONFIG_MMC_SDHCI_PLTFM=y
  1405. +CONFIG_MMC_SDHCI_BCM2708=y
  1406. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1407. +CONFIG_MMC_SPI=m
  1408. +CONFIG_LEDS_GPIO=m
  1409. +CONFIG_LEDS_TRIGGER_TIMER=y
  1410. +CONFIG_LEDS_TRIGGER_ONESHOT=y
  1411. +CONFIG_LEDS_TRIGGER_HEARTBEAT=y
  1412. +CONFIG_LEDS_TRIGGER_BACKLIGHT=y
  1413. +CONFIG_LEDS_TRIGGER_CPU=y
  1414. +CONFIG_LEDS_TRIGGER_GPIO=y
  1415. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
  1416. +CONFIG_LEDS_TRIGGER_TRANSIENT=m
  1417. +CONFIG_LEDS_TRIGGER_CAMERA=m
  1418. +CONFIG_RTC_CLASS=y
  1419. +CONFIG_RTC_DRV_DS1307=m
  1420. +CONFIG_RTC_DRV_DS1374=m
  1421. +CONFIG_RTC_DRV_DS1672=m
  1422. +CONFIG_RTC_DRV_DS3232=m
  1423. +CONFIG_RTC_DRV_MAX6900=m
  1424. +CONFIG_RTC_DRV_RS5C372=m
  1425. +CONFIG_RTC_DRV_ISL1208=m
  1426. +CONFIG_RTC_DRV_ISL12022=m
  1427. +CONFIG_RTC_DRV_X1205=m
  1428. +CONFIG_RTC_DRV_PCF8523=m
  1429. +CONFIG_RTC_DRV_PCF8563=m
  1430. +CONFIG_RTC_DRV_PCF8583=m
  1431. +CONFIG_RTC_DRV_M41T80=m
  1432. +CONFIG_RTC_DRV_BQ32K=m
  1433. +CONFIG_RTC_DRV_S35390A=m
  1434. +CONFIG_RTC_DRV_FM3130=m
  1435. +CONFIG_RTC_DRV_RX8581=m
  1436. +CONFIG_RTC_DRV_RX8025=m
  1437. +CONFIG_RTC_DRV_EM3027=m
  1438. +CONFIG_RTC_DRV_RV3029C2=m
  1439. +CONFIG_RTC_DRV_M41T93=m
  1440. +CONFIG_RTC_DRV_M41T94=m
  1441. +CONFIG_RTC_DRV_DS1305=m
  1442. +CONFIG_RTC_DRV_DS1390=m
  1443. +CONFIG_RTC_DRV_MAX6902=m
  1444. +CONFIG_RTC_DRV_R9701=m
  1445. +CONFIG_RTC_DRV_RS5C348=m
  1446. +CONFIG_RTC_DRV_DS3234=m
  1447. +CONFIG_RTC_DRV_PCF2123=m
  1448. +CONFIG_RTC_DRV_RX4581=m
  1449. +CONFIG_DMADEVICES=y
  1450. +CONFIG_DMA_BCM2708=m
  1451. +CONFIG_DMA_ENGINE=y
  1452. +CONFIG_DMA_VIRTUAL_CHANNELS=m
  1453. +CONFIG_UIO=m
  1454. +CONFIG_UIO_PDRV=m
  1455. +CONFIG_UIO_PDRV_GENIRQ=m
  1456. +CONFIG_STAGING=y
  1457. +CONFIG_W35UND=m
  1458. +CONFIG_PRISM2_USB=m
  1459. +CONFIG_R8712U=m
  1460. +CONFIG_VT6656=m
  1461. +CONFIG_SPEAKUP=m
  1462. +CONFIG_SPEAKUP_SYNTH_SOFT=m
  1463. +CONFIG_STAGING_MEDIA=y
  1464. +CONFIG_DVB_AS102=m
  1465. +CONFIG_LIRC_STAGING=y
  1466. +CONFIG_LIRC_IGORPLUGUSB=m
  1467. +CONFIG_LIRC_IMON=m
  1468. +CONFIG_LIRC_RPI=m
  1469. +CONFIG_LIRC_SASEM=m
  1470. +CONFIG_LIRC_SERIAL=m
  1471. +# CONFIG_IOMMU_SUPPORT is not set
  1472. +CONFIG_EXT4_FS=y
  1473. +CONFIG_EXT4_FS_POSIX_ACL=y
  1474. +CONFIG_EXT4_FS_SECURITY=y
  1475. +CONFIG_REISERFS_FS=m
  1476. +CONFIG_REISERFS_FS_XATTR=y
  1477. +CONFIG_REISERFS_FS_POSIX_ACL=y
  1478. +CONFIG_REISERFS_FS_SECURITY=y
  1479. +CONFIG_JFS_FS=m
  1480. +CONFIG_JFS_POSIX_ACL=y
  1481. +CONFIG_JFS_SECURITY=y
  1482. +CONFIG_JFS_STATISTICS=y
  1483. +CONFIG_XFS_FS=m
  1484. +CONFIG_XFS_QUOTA=y
  1485. +CONFIG_XFS_POSIX_ACL=y
  1486. +CONFIG_XFS_RT=y
  1487. +CONFIG_GFS2_FS=m
  1488. +CONFIG_OCFS2_FS=m
  1489. +CONFIG_BTRFS_FS=m
  1490. +CONFIG_BTRFS_FS_POSIX_ACL=y
  1491. +CONFIG_NILFS2_FS=m
  1492. +CONFIG_FANOTIFY=y
  1493. +CONFIG_QFMT_V1=m
  1494. +CONFIG_QFMT_V2=m
  1495. +CONFIG_AUTOFS4_FS=y
  1496. +CONFIG_FUSE_FS=m
  1497. +CONFIG_CUSE=m
  1498. +CONFIG_FSCACHE=y
  1499. +CONFIG_FSCACHE_STATS=y
  1500. +CONFIG_FSCACHE_HISTOGRAM=y
  1501. +CONFIG_CACHEFILES=y
  1502. +CONFIG_ISO9660_FS=m
  1503. +CONFIG_JOLIET=y
  1504. +CONFIG_ZISOFS=y
  1505. +CONFIG_UDF_FS=m
  1506. +CONFIG_MSDOS_FS=y
  1507. +CONFIG_VFAT_FS=y
  1508. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1509. +CONFIG_NTFS_FS=m
  1510. +CONFIG_NTFS_RW=y
  1511. +CONFIG_TMPFS=y
  1512. +CONFIG_TMPFS_POSIX_ACL=y
  1513. +CONFIG_CONFIGFS_FS=y
  1514. +CONFIG_ECRYPT_FS=m
  1515. +CONFIG_HFS_FS=m
  1516. +CONFIG_HFSPLUS_FS=m
  1517. +CONFIG_SQUASHFS=m
  1518. +CONFIG_SQUASHFS_XATTR=y
  1519. +CONFIG_SQUASHFS_LZO=y
  1520. +CONFIG_SQUASHFS_XZ=y
  1521. +CONFIG_F2FS_FS=y
  1522. +CONFIG_NFS_FS=y
  1523. +CONFIG_NFS_V3_ACL=y
  1524. +CONFIG_NFS_V4=y
  1525. +CONFIG_ROOT_NFS=y
  1526. +CONFIG_NFS_FSCACHE=y
  1527. +CONFIG_NFSD=m
  1528. +CONFIG_NFSD_V3_ACL=y
  1529. +CONFIG_NFSD_V4=y
  1530. +CONFIG_CIFS=m
  1531. +CONFIG_CIFS_WEAK_PW_HASH=y
  1532. +CONFIG_CIFS_XATTR=y
  1533. +CONFIG_CIFS_POSIX=y
  1534. +CONFIG_9P_FS=m
  1535. +CONFIG_9P_FS_POSIX_ACL=y
  1536. +CONFIG_NLS_DEFAULT="utf8"
  1537. +CONFIG_NLS_CODEPAGE_437=y
  1538. +CONFIG_NLS_CODEPAGE_737=m
  1539. +CONFIG_NLS_CODEPAGE_775=m
  1540. +CONFIG_NLS_CODEPAGE_850=m
  1541. +CONFIG_NLS_CODEPAGE_852=m
  1542. +CONFIG_NLS_CODEPAGE_855=m
  1543. +CONFIG_NLS_CODEPAGE_857=m
  1544. +CONFIG_NLS_CODEPAGE_860=m
  1545. +CONFIG_NLS_CODEPAGE_861=m
  1546. +CONFIG_NLS_CODEPAGE_862=m
  1547. +CONFIG_NLS_CODEPAGE_863=m
  1548. +CONFIG_NLS_CODEPAGE_864=m
  1549. +CONFIG_NLS_CODEPAGE_865=m
  1550. +CONFIG_NLS_CODEPAGE_866=m
  1551. +CONFIG_NLS_CODEPAGE_869=m
  1552. +CONFIG_NLS_CODEPAGE_936=m
  1553. +CONFIG_NLS_CODEPAGE_950=m
  1554. +CONFIG_NLS_CODEPAGE_932=m
  1555. +CONFIG_NLS_CODEPAGE_949=m
  1556. +CONFIG_NLS_CODEPAGE_874=m
  1557. +CONFIG_NLS_ISO8859_8=m
  1558. +CONFIG_NLS_CODEPAGE_1250=m
  1559. +CONFIG_NLS_CODEPAGE_1251=m
  1560. +CONFIG_NLS_ASCII=y
  1561. +CONFIG_NLS_ISO8859_1=m
  1562. +CONFIG_NLS_ISO8859_2=m
  1563. +CONFIG_NLS_ISO8859_3=m
  1564. +CONFIG_NLS_ISO8859_4=m
  1565. +CONFIG_NLS_ISO8859_5=m
  1566. +CONFIG_NLS_ISO8859_6=m
  1567. +CONFIG_NLS_ISO8859_7=m
  1568. +CONFIG_NLS_ISO8859_9=m
  1569. +CONFIG_NLS_ISO8859_13=m
  1570. +CONFIG_NLS_ISO8859_14=m
  1571. +CONFIG_NLS_ISO8859_15=m
  1572. +CONFIG_NLS_KOI8_R=m
  1573. +CONFIG_NLS_KOI8_U=m
  1574. +CONFIG_DLM=m
  1575. +CONFIG_PRINTK_TIME=y
  1576. +CONFIG_BOOT_PRINTK_DELAY=y
  1577. +CONFIG_DEBUG_FS=y
  1578. +CONFIG_DEBUG_MEMORY_INIT=y
  1579. +CONFIG_DETECT_HUNG_TASK=y
  1580. +CONFIG_TIMER_STATS=y
  1581. +# CONFIG_DEBUG_PREEMPT is not set
  1582. +CONFIG_LATENCYTOP=y
  1583. +# CONFIG_KPROBE_EVENT is not set
  1584. +CONFIG_KGDB=y
  1585. +CONFIG_KGDB_KDB=y
  1586. +CONFIG_KDB_KEYBOARD=y
  1587. +CONFIG_STRICT_DEVMEM=y
  1588. +CONFIG_CRYPTO_USER=m
  1589. +CONFIG_CRYPTO_NULL=m
  1590. +CONFIG_CRYPTO_CRYPTD=m
  1591. +CONFIG_CRYPTO_SEQIV=m
  1592. +CONFIG_CRYPTO_CBC=y
  1593. +CONFIG_CRYPTO_XTS=m
  1594. +CONFIG_CRYPTO_XCBC=m
  1595. +CONFIG_CRYPTO_SHA1_ARM=m
  1596. +CONFIG_CRYPTO_SHA512=m
  1597. +CONFIG_CRYPTO_TGR192=m
  1598. +CONFIG_CRYPTO_WP512=m
  1599. +CONFIG_CRYPTO_AES_ARM=m
  1600. +CONFIG_CRYPTO_CAST5=m
  1601. +CONFIG_CRYPTO_DES=y
  1602. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1603. +# CONFIG_CRYPTO_HW is not set
  1604. +CONFIG_CRC_ITU_T=y
  1605. +CONFIG_LIBCRC32C=y
  1606. diff -Nur linux-3.12.13/arch/arm/configs/bcmrpi_emergency_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_emergency_defconfig
  1607. --- linux-3.12.13/arch/arm/configs/bcmrpi_emergency_defconfig 1970-01-01 01:00:00.000000000 +0100
  1608. +++ linux-raspberry-pi/arch/arm/configs/bcmrpi_emergency_defconfig 2014-03-11 17:31:42.000000000 +0100
  1609. @@ -0,0 +1,532 @@
  1610. +CONFIG_EXPERIMENTAL=y
  1611. +# CONFIG_LOCALVERSION_AUTO is not set
  1612. +CONFIG_SYSVIPC=y
  1613. +CONFIG_POSIX_MQUEUE=y
  1614. +CONFIG_BSD_PROCESS_ACCT=y
  1615. +CONFIG_BSD_PROCESS_ACCT_V3=y
  1616. +CONFIG_FHANDLE=y
  1617. +CONFIG_AUDIT=y
  1618. +CONFIG_IKCONFIG=y
  1619. +CONFIG_IKCONFIG_PROC=y
  1620. +CONFIG_BLK_DEV_INITRD=y
  1621. +CONFIG_INITRAMFS_SOURCE="../target_fs"
  1622. +CONFIG_CGROUP_FREEZER=y
  1623. +CONFIG_CGROUP_DEVICE=y
  1624. +CONFIG_CGROUP_CPUACCT=y
  1625. +CONFIG_RESOURCE_COUNTERS=y
  1626. +CONFIG_BLK_CGROUP=y
  1627. +CONFIG_NAMESPACES=y
  1628. +CONFIG_SCHED_AUTOGROUP=y
  1629. +CONFIG_EMBEDDED=y
  1630. +# CONFIG_COMPAT_BRK is not set
  1631. +CONFIG_SLAB=y
  1632. +CONFIG_PROFILING=y
  1633. +CONFIG_OPROFILE=m
  1634. +CONFIG_KPROBES=y
  1635. +CONFIG_MODULES=y
  1636. +CONFIG_MODULE_UNLOAD=y
  1637. +CONFIG_MODVERSIONS=y
  1638. +CONFIG_MODULE_SRCVERSION_ALL=y
  1639. +# CONFIG_BLK_DEV_BSG is not set
  1640. +CONFIG_BLK_DEV_THROTTLING=y
  1641. +CONFIG_CFQ_GROUP_IOSCHED=y
  1642. +CONFIG_ARCH_BCM2708=y
  1643. +CONFIG_NO_HZ=y
  1644. +CONFIG_HIGH_RES_TIMERS=y
  1645. +CONFIG_AEABI=y
  1646. +CONFIG_SECCOMP=y
  1647. +CONFIG_CC_STACKPROTECTOR=y
  1648. +CONFIG_ZBOOT_ROM_TEXT=0x0
  1649. +CONFIG_ZBOOT_ROM_BSS=0x0
  1650. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext3 rootwait"
  1651. +CONFIG_KEXEC=y
  1652. +CONFIG_CPU_IDLE=y
  1653. +CONFIG_VFP=y
  1654. +CONFIG_BINFMT_MISC=m
  1655. +CONFIG_NET=y
  1656. +CONFIG_PACKET=y
  1657. +CONFIG_UNIX=y
  1658. +CONFIG_XFRM_USER=y
  1659. +CONFIG_NET_KEY=m
  1660. +CONFIG_INET=y
  1661. +CONFIG_IP_MULTICAST=y
  1662. +CONFIG_IP_PNP=y
  1663. +CONFIG_IP_PNP_DHCP=y
  1664. +CONFIG_IP_PNP_RARP=y
  1665. +CONFIG_SYN_COOKIES=y
  1666. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  1667. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  1668. +# CONFIG_INET_XFRM_MODE_BEET is not set
  1669. +# CONFIG_INET_LRO is not set
  1670. +# CONFIG_INET_DIAG is not set
  1671. +# CONFIG_IPV6 is not set
  1672. +CONFIG_NET_PKTGEN=m
  1673. +CONFIG_IRDA=m
  1674. +CONFIG_IRLAN=m
  1675. +CONFIG_IRCOMM=m
  1676. +CONFIG_IRDA_ULTRA=y
  1677. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  1678. +CONFIG_IRDA_FAST_RR=y
  1679. +CONFIG_IRTTY_SIR=m
  1680. +CONFIG_KINGSUN_DONGLE=m
  1681. +CONFIG_KSDAZZLE_DONGLE=m
  1682. +CONFIG_KS959_DONGLE=m
  1683. +CONFIG_USB_IRDA=m
  1684. +CONFIG_SIGMATEL_FIR=m
  1685. +CONFIG_MCS_FIR=m
  1686. +CONFIG_BT=m
  1687. +CONFIG_BT_L2CAP=y
  1688. +CONFIG_BT_SCO=y
  1689. +CONFIG_BT_RFCOMM=m
  1690. +CONFIG_BT_RFCOMM_TTY=y
  1691. +CONFIG_BT_BNEP=m
  1692. +CONFIG_BT_BNEP_MC_FILTER=y
  1693. +CONFIG_BT_BNEP_PROTO_FILTER=y
  1694. +CONFIG_BT_HIDP=m
  1695. +CONFIG_BT_HCIBTUSB=m
  1696. +CONFIG_BT_HCIBCM203X=m
  1697. +CONFIG_BT_HCIBPA10X=m
  1698. +CONFIG_BT_HCIBFUSB=m
  1699. +CONFIG_BT_HCIVHCI=m
  1700. +CONFIG_BT_MRVL=m
  1701. +CONFIG_BT_MRVL_SDIO=m
  1702. +CONFIG_BT_ATH3K=m
  1703. +CONFIG_CFG80211=m
  1704. +CONFIG_MAC80211=m
  1705. +CONFIG_MAC80211_RC_PID=y
  1706. +CONFIG_MAC80211_MESH=y
  1707. +CONFIG_WIMAX=m
  1708. +CONFIG_NET_9P=m
  1709. +CONFIG_NFC=m
  1710. +CONFIG_NFC_PN533=m
  1711. +CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug"
  1712. +CONFIG_BLK_DEV_LOOP=y
  1713. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  1714. +CONFIG_BLK_DEV_NBD=m
  1715. +CONFIG_BLK_DEV_RAM=y
  1716. +CONFIG_CDROM_PKTCDVD=m
  1717. +CONFIG_MISC_DEVICES=y
  1718. +CONFIG_SCSI=y
  1719. +# CONFIG_SCSI_PROC_FS is not set
  1720. +CONFIG_BLK_DEV_SD=y
  1721. +CONFIG_BLK_DEV_SR=m
  1722. +CONFIG_SCSI_MULTI_LUN=y
  1723. +# CONFIG_SCSI_LOWLEVEL is not set
  1724. +CONFIG_MD=y
  1725. +CONFIG_NETDEVICES=y
  1726. +CONFIG_TUN=m
  1727. +CONFIG_PHYLIB=m
  1728. +CONFIG_MDIO_BITBANG=m
  1729. +CONFIG_NET_ETHERNET=y
  1730. +# CONFIG_NETDEV_1000 is not set
  1731. +# CONFIG_NETDEV_10000 is not set
  1732. +CONFIG_LIBERTAS_THINFIRM=m
  1733. +CONFIG_LIBERTAS_THINFIRM_USB=m
  1734. +CONFIG_AT76C50X_USB=m
  1735. +CONFIG_USB_ZD1201=m
  1736. +CONFIG_USB_NET_RNDIS_WLAN=m
  1737. +CONFIG_RTL8187=m
  1738. +CONFIG_MAC80211_HWSIM=m
  1739. +CONFIG_ATH_COMMON=m
  1740. +CONFIG_ATH9K=m
  1741. +CONFIG_ATH9K_HTC=m
  1742. +CONFIG_CARL9170=m
  1743. +CONFIG_B43=m
  1744. +CONFIG_B43LEGACY=m
  1745. +CONFIG_HOSTAP=m
  1746. +CONFIG_IWM=m
  1747. +CONFIG_LIBERTAS=m
  1748. +CONFIG_LIBERTAS_USB=m
  1749. +CONFIG_LIBERTAS_SDIO=m
  1750. +CONFIG_P54_COMMON=m
  1751. +CONFIG_P54_USB=m
  1752. +CONFIG_RT2X00=m
  1753. +CONFIG_RT2500USB=m
  1754. +CONFIG_RT73USB=m
  1755. +CONFIG_RT2800USB=m
  1756. +CONFIG_RT2800USB_RT53XX=y
  1757. +CONFIG_RTL8192CU=m
  1758. +CONFIG_WL1251=m
  1759. +CONFIG_WL12XX_MENU=m
  1760. +CONFIG_ZD1211RW=m
  1761. +CONFIG_MWIFIEX=m
  1762. +CONFIG_MWIFIEX_SDIO=m
  1763. +CONFIG_WIMAX_I2400M_USB=m
  1764. +CONFIG_USB_CATC=m
  1765. +CONFIG_USB_KAWETH=m
  1766. +CONFIG_USB_PEGASUS=m
  1767. +CONFIG_USB_RTL8150=m
  1768. +CONFIG_USB_USBNET=y
  1769. +CONFIG_USB_NET_AX8817X=m
  1770. +CONFIG_USB_NET_CDCETHER=m
  1771. +CONFIG_USB_NET_CDC_EEM=m
  1772. +CONFIG_USB_NET_DM9601=m
  1773. +CONFIG_USB_NET_SMSC75XX=m
  1774. +CONFIG_USB_NET_SMSC95XX=y
  1775. +CONFIG_USB_NET_GL620A=m
  1776. +CONFIG_USB_NET_NET1080=m
  1777. +CONFIG_USB_NET_PLUSB=m
  1778. +CONFIG_USB_NET_MCS7830=m
  1779. +CONFIG_USB_NET_CDC_SUBSET=m
  1780. +CONFIG_USB_ALI_M5632=y
  1781. +CONFIG_USB_AN2720=y
  1782. +CONFIG_USB_KC2190=y
  1783. +# CONFIG_USB_NET_ZAURUS is not set
  1784. +CONFIG_USB_NET_CX82310_ETH=m
  1785. +CONFIG_USB_NET_KALMIA=m
  1786. +CONFIG_USB_NET_INT51X1=m
  1787. +CONFIG_USB_IPHETH=m
  1788. +CONFIG_USB_SIERRA_NET=m
  1789. +CONFIG_USB_VL600=m
  1790. +CONFIG_PPP=m
  1791. +CONFIG_PPP_ASYNC=m
  1792. +CONFIG_PPP_SYNC_TTY=m
  1793. +CONFIG_PPP_DEFLATE=m
  1794. +CONFIG_PPP_BSDCOMP=m
  1795. +CONFIG_SLIP=m
  1796. +CONFIG_SLIP_COMPRESSED=y
  1797. +CONFIG_NETCONSOLE=m
  1798. +CONFIG_INPUT_POLLDEV=m
  1799. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1800. +CONFIG_INPUT_JOYDEV=m
  1801. +CONFIG_INPUT_EVDEV=m
  1802. +# CONFIG_INPUT_KEYBOARD is not set
  1803. +# CONFIG_INPUT_MOUSE is not set
  1804. +CONFIG_INPUT_MISC=y
  1805. +CONFIG_INPUT_AD714X=m
  1806. +CONFIG_INPUT_ATI_REMOTE=m
  1807. +CONFIG_INPUT_ATI_REMOTE2=m
  1808. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1809. +CONFIG_INPUT_POWERMATE=m
  1810. +CONFIG_INPUT_YEALINK=m
  1811. +CONFIG_INPUT_CM109=m
  1812. +CONFIG_INPUT_UINPUT=m
  1813. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1814. +CONFIG_INPUT_ADXL34X=m
  1815. +CONFIG_INPUT_CMA3000=m
  1816. +CONFIG_SERIO=m
  1817. +CONFIG_SERIO_RAW=m
  1818. +CONFIG_GAMEPORT=m
  1819. +CONFIG_GAMEPORT_NS558=m
  1820. +CONFIG_GAMEPORT_L4=m
  1821. +CONFIG_VT_HW_CONSOLE_BINDING=y
  1822. +# CONFIG_LEGACY_PTYS is not set
  1823. +# CONFIG_DEVKMEM is not set
  1824. +CONFIG_SERIAL_AMBA_PL011=y
  1825. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1826. +# CONFIG_HW_RANDOM is not set
  1827. +CONFIG_RAW_DRIVER=y
  1828. +CONFIG_GPIO_SYSFS=y
  1829. +# CONFIG_HWMON is not set
  1830. +CONFIG_WATCHDOG=y
  1831. +CONFIG_BCM2708_WDT=m
  1832. +# CONFIG_MFD_SUPPORT is not set
  1833. +CONFIG_FB=y
  1834. +CONFIG_FB_BCM2708=y
  1835. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1836. +CONFIG_LOGO=y
  1837. +# CONFIG_LOGO_LINUX_MONO is not set
  1838. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1839. +CONFIG_SOUND=y
  1840. +CONFIG_SND=m
  1841. +CONFIG_SND_SEQUENCER=m
  1842. +CONFIG_SND_SEQ_DUMMY=m
  1843. +CONFIG_SND_MIXER_OSS=m
  1844. +CONFIG_SND_PCM_OSS=m
  1845. +CONFIG_SND_SEQUENCER_OSS=y
  1846. +CONFIG_SND_HRTIMER=m
  1847. +CONFIG_SND_DUMMY=m
  1848. +CONFIG_SND_ALOOP=m
  1849. +CONFIG_SND_VIRMIDI=m
  1850. +CONFIG_SND_MTPAV=m
  1851. +CONFIG_SND_SERIAL_U16550=m
  1852. +CONFIG_SND_MPU401=m
  1853. +CONFIG_SND_BCM2835=m
  1854. +CONFIG_SND_USB_AUDIO=m
  1855. +CONFIG_SND_USB_UA101=m
  1856. +CONFIG_SND_USB_CAIAQ=m
  1857. +CONFIG_SND_USB_6FIRE=m
  1858. +CONFIG_SOUND_PRIME=m
  1859. +CONFIG_HID_PID=y
  1860. +CONFIG_USB_HIDDEV=y
  1861. +CONFIG_HID_A4TECH=m
  1862. +CONFIG_HID_ACRUX=m
  1863. +CONFIG_HID_APPLE=m
  1864. +CONFIG_HID_BELKIN=m
  1865. +CONFIG_HID_CHERRY=m
  1866. +CONFIG_HID_CHICONY=m
  1867. +CONFIG_HID_CYPRESS=m
  1868. +CONFIG_HID_DRAGONRISE=m
  1869. +CONFIG_HID_EMS_FF=m
  1870. +CONFIG_HID_ELECOM=m
  1871. +CONFIG_HID_EZKEY=m
  1872. +CONFIG_HID_HOLTEK=m
  1873. +CONFIG_HID_KEYTOUCH=m
  1874. +CONFIG_HID_KYE=m
  1875. +CONFIG_HID_UCLOGIC=m
  1876. +CONFIG_HID_WALTOP=m
  1877. +CONFIG_HID_GYRATION=m
  1878. +CONFIG_HID_TWINHAN=m
  1879. +CONFIG_HID_KENSINGTON=m
  1880. +CONFIG_HID_LCPOWER=m
  1881. +CONFIG_HID_LOGITECH=m
  1882. +CONFIG_HID_MAGICMOUSE=m
  1883. +CONFIG_HID_MICROSOFT=m
  1884. +CONFIG_HID_MONTEREY=m
  1885. +CONFIG_HID_MULTITOUCH=m
  1886. +CONFIG_HID_NTRIG=m
  1887. +CONFIG_HID_ORTEK=m
  1888. +CONFIG_HID_PANTHERLORD=m
  1889. +CONFIG_HID_PETALYNX=m
  1890. +CONFIG_HID_PICOLCD=m
  1891. +CONFIG_HID_QUANTA=m
  1892. +CONFIG_HID_ROCCAT=m
  1893. +CONFIG_HID_SAMSUNG=m
  1894. +CONFIG_HID_SONY=m
  1895. +CONFIG_HID_SPEEDLINK=m
  1896. +CONFIG_HID_SUNPLUS=m
  1897. +CONFIG_HID_GREENASIA=m
  1898. +CONFIG_HID_SMARTJOYPLUS=m
  1899. +CONFIG_HID_TOPSEED=m
  1900. +CONFIG_HID_THRUSTMASTER=m
  1901. +CONFIG_HID_WACOM=m
  1902. +CONFIG_HID_WIIMOTE=m
  1903. +CONFIG_HID_ZEROPLUS=m
  1904. +CONFIG_HID_ZYDACRON=m
  1905. +CONFIG_USB=y
  1906. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1907. +CONFIG_USB_MON=m
  1908. +CONFIG_USB_DWCOTG=y
  1909. +CONFIG_USB_STORAGE=y
  1910. +CONFIG_USB_STORAGE_REALTEK=m
  1911. +CONFIG_USB_STORAGE_DATAFAB=m
  1912. +CONFIG_USB_STORAGE_FREECOM=m
  1913. +CONFIG_USB_STORAGE_ISD200=m
  1914. +CONFIG_USB_STORAGE_USBAT=m
  1915. +CONFIG_USB_STORAGE_SDDR09=m
  1916. +CONFIG_USB_STORAGE_SDDR55=m
  1917. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1918. +CONFIG_USB_STORAGE_ALAUDA=m
  1919. +CONFIG_USB_STORAGE_ONETOUCH=m
  1920. +CONFIG_USB_STORAGE_KARMA=m
  1921. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1922. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1923. +CONFIG_USB_UAS=y
  1924. +CONFIG_USB_LIBUSUAL=y
  1925. +CONFIG_USB_MDC800=m
  1926. +CONFIG_USB_MICROTEK=m
  1927. +CONFIG_USB_SERIAL=m
  1928. +CONFIG_USB_SERIAL_GENERIC=y
  1929. +CONFIG_USB_SERIAL_AIRCABLE=m
  1930. +CONFIG_USB_SERIAL_ARK3116=m
  1931. +CONFIG_USB_SERIAL_BELKIN=m
  1932. +CONFIG_USB_SERIAL_CH341=m
  1933. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1934. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1935. +CONFIG_USB_SERIAL_CP210X=m
  1936. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1937. +CONFIG_USB_SERIAL_EMPEG=m
  1938. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1939. +CONFIG_USB_SERIAL_FUNSOFT=m
  1940. +CONFIG_USB_SERIAL_VISOR=m
  1941. +CONFIG_USB_SERIAL_IPAQ=m
  1942. +CONFIG_USB_SERIAL_IR=m
  1943. +CONFIG_USB_SERIAL_EDGEPORT=m
  1944. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1945. +CONFIG_USB_SERIAL_GARMIN=m
  1946. +CONFIG_USB_SERIAL_IPW=m
  1947. +CONFIG_USB_SERIAL_IUU=m
  1948. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1949. +CONFIG_USB_SERIAL_KEYSPAN=m
  1950. +CONFIG_USB_SERIAL_KLSI=m
  1951. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1952. +CONFIG_USB_SERIAL_MCT_U232=m
  1953. +CONFIG_USB_SERIAL_MOS7720=m
  1954. +CONFIG_USB_SERIAL_MOS7840=m
  1955. +CONFIG_USB_SERIAL_MOTOROLA=m
  1956. +CONFIG_USB_SERIAL_NAVMAN=m
  1957. +CONFIG_USB_SERIAL_PL2303=m
  1958. +CONFIG_USB_SERIAL_OTI6858=m
  1959. +CONFIG_USB_SERIAL_QCAUX=m
  1960. +CONFIG_USB_SERIAL_QUALCOMM=m
  1961. +CONFIG_USB_SERIAL_SPCP8X5=m
  1962. +CONFIG_USB_SERIAL_HP4X=m
  1963. +CONFIG_USB_SERIAL_SAFE=m
  1964. +CONFIG_USB_SERIAL_SIEMENS_MPI=m
  1965. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1966. +CONFIG_USB_SERIAL_SYMBOL=m
  1967. +CONFIG_USB_SERIAL_TI=m
  1968. +CONFIG_USB_SERIAL_CYBERJACK=m
  1969. +CONFIG_USB_SERIAL_XIRCOM=m
  1970. +CONFIG_USB_SERIAL_OPTION=m
  1971. +CONFIG_USB_SERIAL_OMNINET=m
  1972. +CONFIG_USB_SERIAL_OPTICON=m
  1973. +CONFIG_USB_SERIAL_VIVOPAY_SERIAL=m
  1974. +CONFIG_USB_SERIAL_ZIO=m
  1975. +CONFIG_USB_SERIAL_SSU100=m
  1976. +CONFIG_USB_SERIAL_DEBUG=m
  1977. +CONFIG_USB_EMI62=m
  1978. +CONFIG_USB_EMI26=m
  1979. +CONFIG_USB_ADUTUX=m
  1980. +CONFIG_USB_SEVSEG=m
  1981. +CONFIG_USB_RIO500=m
  1982. +CONFIG_USB_LEGOTOWER=m
  1983. +CONFIG_USB_LCD=m
  1984. +CONFIG_USB_LED=m
  1985. +CONFIG_USB_CYPRESS_CY7C63=m
  1986. +CONFIG_USB_CYTHERM=m
  1987. +CONFIG_USB_IDMOUSE=m
  1988. +CONFIG_USB_FTDI_ELAN=m
  1989. +CONFIG_USB_APPLEDISPLAY=m
  1990. +CONFIG_USB_LD=m
  1991. +CONFIG_USB_TRANCEVIBRATOR=m
  1992. +CONFIG_USB_IOWARRIOR=m
  1993. +CONFIG_USB_TEST=m
  1994. +CONFIG_USB_ISIGHTFW=m
  1995. +CONFIG_USB_YUREX=m
  1996. +CONFIG_MMC=y
  1997. +CONFIG_MMC_SDHCI=y
  1998. +CONFIG_MMC_SDHCI_PLTFM=y
  1999. +CONFIG_MMC_SDHCI_BCM2708=y
  2000. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  2001. +CONFIG_LEDS_GPIO=y
  2002. +CONFIG_LEDS_TRIGGER_TIMER=m
  2003. +CONFIG_LEDS_TRIGGER_HEARTBEAT=m
  2004. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=m
  2005. +CONFIG_UIO=m
  2006. +CONFIG_UIO_PDRV=m
  2007. +CONFIG_UIO_PDRV_GENIRQ=m
  2008. +# CONFIG_IOMMU_SUPPORT is not set
  2009. +CONFIG_EXT4_FS=y
  2010. +CONFIG_EXT4_FS_POSIX_ACL=y
  2011. +CONFIG_EXT4_FS_SECURITY=y
  2012. +CONFIG_REISERFS_FS=m
  2013. +CONFIG_REISERFS_FS_XATTR=y
  2014. +CONFIG_REISERFS_FS_POSIX_ACL=y
  2015. +CONFIG_REISERFS_FS_SECURITY=y
  2016. +CONFIG_JFS_FS=m
  2017. +CONFIG_JFS_POSIX_ACL=y
  2018. +CONFIG_JFS_SECURITY=y
  2019. +CONFIG_JFS_STATISTICS=y
  2020. +CONFIG_XFS_FS=m
  2021. +CONFIG_XFS_QUOTA=y
  2022. +CONFIG_XFS_POSIX_ACL=y
  2023. +CONFIG_XFS_RT=y
  2024. +CONFIG_GFS2_FS=m
  2025. +CONFIG_OCFS2_FS=m
  2026. +CONFIG_BTRFS_FS=m
  2027. +CONFIG_BTRFS_FS_POSIX_ACL=y
  2028. +CONFIG_NILFS2_FS=m
  2029. +CONFIG_FANOTIFY=y
  2030. +CONFIG_AUTOFS4_FS=y
  2031. +CONFIG_FUSE_FS=m
  2032. +CONFIG_CUSE=m
  2033. +CONFIG_FSCACHE=y
  2034. +CONFIG_FSCACHE_STATS=y
  2035. +CONFIG_FSCACHE_HISTOGRAM=y
  2036. +CONFIG_CACHEFILES=y
  2037. +CONFIG_ISO9660_FS=m
  2038. +CONFIG_JOLIET=y
  2039. +CONFIG_ZISOFS=y
  2040. +CONFIG_UDF_FS=m
  2041. +CONFIG_MSDOS_FS=y
  2042. +CONFIG_VFAT_FS=y
  2043. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  2044. +CONFIG_NTFS_FS=m
  2045. +CONFIG_TMPFS=y
  2046. +CONFIG_TMPFS_POSIX_ACL=y
  2047. +CONFIG_CONFIGFS_FS=y
  2048. +CONFIG_SQUASHFS=m
  2049. +CONFIG_SQUASHFS_XATTR=y
  2050. +CONFIG_SQUASHFS_LZO=y
  2051. +CONFIG_SQUASHFS_XZ=y
  2052. +CONFIG_NFS_FS=y
  2053. +CONFIG_NFS_V3=y
  2054. +CONFIG_NFS_V3_ACL=y
  2055. +CONFIG_NFS_V4=y
  2056. +CONFIG_ROOT_NFS=y
  2057. +CONFIG_NFS_FSCACHE=y
  2058. +CONFIG_CIFS=m
  2059. +CONFIG_CIFS_WEAK_PW_HASH=y
  2060. +CONFIG_CIFS_XATTR=y
  2061. +CONFIG_CIFS_POSIX=y
  2062. +CONFIG_9P_FS=m
  2063. +CONFIG_9P_FS_POSIX_ACL=y
  2064. +CONFIG_PARTITION_ADVANCED=y
  2065. +CONFIG_MAC_PARTITION=y
  2066. +CONFIG_EFI_PARTITION=y
  2067. +CONFIG_NLS_DEFAULT="utf8"
  2068. +CONFIG_NLS_CODEPAGE_437=y
  2069. +CONFIG_NLS_CODEPAGE_737=m
  2070. +CONFIG_NLS_CODEPAGE_775=m
  2071. +CONFIG_NLS_CODEPAGE_850=m
  2072. +CONFIG_NLS_CODEPAGE_852=m
  2073. +CONFIG_NLS_CODEPAGE_855=m
  2074. +CONFIG_NLS_CODEPAGE_857=m
  2075. +CONFIG_NLS_CODEPAGE_860=m
  2076. +CONFIG_NLS_CODEPAGE_861=m
  2077. +CONFIG_NLS_CODEPAGE_862=m
  2078. +CONFIG_NLS_CODEPAGE_863=m
  2079. +CONFIG_NLS_CODEPAGE_864=m
  2080. +CONFIG_NLS_CODEPAGE_865=m
  2081. +CONFIG_NLS_CODEPAGE_866=m
  2082. +CONFIG_NLS_CODEPAGE_869=m
  2083. +CONFIG_NLS_CODEPAGE_936=m
  2084. +CONFIG_NLS_CODEPAGE_950=m
  2085. +CONFIG_NLS_CODEPAGE_932=m
  2086. +CONFIG_NLS_CODEPAGE_949=m
  2087. +CONFIG_NLS_CODEPAGE_874=m
  2088. +CONFIG_NLS_ISO8859_8=m
  2089. +CONFIG_NLS_CODEPAGE_1250=m
  2090. +CONFIG_NLS_CODEPAGE_1251=m
  2091. +CONFIG_NLS_ASCII=y
  2092. +CONFIG_NLS_ISO8859_1=m
  2093. +CONFIG_NLS_ISO8859_2=m
  2094. +CONFIG_NLS_ISO8859_3=m
  2095. +CONFIG_NLS_ISO8859_4=m
  2096. +CONFIG_NLS_ISO8859_5=m
  2097. +CONFIG_NLS_ISO8859_6=m
  2098. +CONFIG_NLS_ISO8859_7=m
  2099. +CONFIG_NLS_ISO8859_9=m
  2100. +CONFIG_NLS_ISO8859_13=m
  2101. +CONFIG_NLS_ISO8859_14=m
  2102. +CONFIG_NLS_ISO8859_15=m
  2103. +CONFIG_NLS_KOI8_R=m
  2104. +CONFIG_NLS_KOI8_U=m
  2105. +CONFIG_NLS_UTF8=m
  2106. +CONFIG_PRINTK_TIME=y
  2107. +CONFIG_DETECT_HUNG_TASK=y
  2108. +CONFIG_TIMER_STATS=y
  2109. +CONFIG_DEBUG_STACK_USAGE=y
  2110. +CONFIG_DEBUG_INFO=y
  2111. +CONFIG_DEBUG_MEMORY_INIT=y
  2112. +CONFIG_BOOT_PRINTK_DELAY=y
  2113. +CONFIG_LATENCYTOP=y
  2114. +CONFIG_SYSCTL_SYSCALL_CHECK=y
  2115. +CONFIG_IRQSOFF_TRACER=y
  2116. +CONFIG_SCHED_TRACER=y
  2117. +CONFIG_STACK_TRACER=y
  2118. +CONFIG_BLK_DEV_IO_TRACE=y
  2119. +CONFIG_FUNCTION_PROFILER=y
  2120. +CONFIG_KGDB=y
  2121. +CONFIG_KGDB_KDB=y
  2122. +CONFIG_KDB_KEYBOARD=y
  2123. +CONFIG_STRICT_DEVMEM=y
  2124. +CONFIG_CRYPTO_AUTHENC=m
  2125. +CONFIG_CRYPTO_SEQIV=m
  2126. +CONFIG_CRYPTO_CBC=y
  2127. +CONFIG_CRYPTO_HMAC=y
  2128. +CONFIG_CRYPTO_XCBC=m
  2129. +CONFIG_CRYPTO_MD5=y
  2130. +CONFIG_CRYPTO_SHA1=y
  2131. +CONFIG_CRYPTO_SHA256=m
  2132. +CONFIG_CRYPTO_SHA512=m
  2133. +CONFIG_CRYPTO_TGR192=m
  2134. +CONFIG_CRYPTO_WP512=m
  2135. +CONFIG_CRYPTO_CAST5=m
  2136. +CONFIG_CRYPTO_DES=y
  2137. +CONFIG_CRYPTO_DEFLATE=m
  2138. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2139. +# CONFIG_CRYPTO_HW is not set
  2140. +CONFIG_CRC_ITU_T=y
  2141. +CONFIG_LIBCRC32C=y
  2142. diff -Nur linux-3.12.13/arch/arm/configs/bcmrpi_quick_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_quick_defconfig
  2143. --- linux-3.12.13/arch/arm/configs/bcmrpi_quick_defconfig 1970-01-01 01:00:00.000000000 +0100
  2144. +++ linux-raspberry-pi/arch/arm/configs/bcmrpi_quick_defconfig 2014-03-11 17:31:42.000000000 +0100
  2145. @@ -0,0 +1,197 @@
  2146. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  2147. +CONFIG_LOCALVERSION="-quick"
  2148. +# CONFIG_LOCALVERSION_AUTO is not set
  2149. +# CONFIG_SWAP is not set
  2150. +CONFIG_SYSVIPC=y
  2151. +CONFIG_POSIX_MQUEUE=y
  2152. +CONFIG_NO_HZ=y
  2153. +CONFIG_HIGH_RES_TIMERS=y
  2154. +CONFIG_IKCONFIG=y
  2155. +CONFIG_IKCONFIG_PROC=y
  2156. +CONFIG_KALLSYMS_ALL=y
  2157. +CONFIG_EMBEDDED=y
  2158. +CONFIG_PERF_EVENTS=y
  2159. +# CONFIG_COMPAT_BRK is not set
  2160. +CONFIG_SLAB=y
  2161. +CONFIG_MODULES=y
  2162. +CONFIG_MODULE_UNLOAD=y
  2163. +CONFIG_MODVERSIONS=y
  2164. +CONFIG_MODULE_SRCVERSION_ALL=y
  2165. +# CONFIG_BLK_DEV_BSG is not set
  2166. +CONFIG_ARCH_BCM2708=y
  2167. +CONFIG_PREEMPT=y
  2168. +CONFIG_AEABI=y
  2169. +CONFIG_UACCESS_WITH_MEMCPY=y
  2170. +CONFIG_ZBOOT_ROM_TEXT=0x0
  2171. +CONFIG_ZBOOT_ROM_BSS=0x0
  2172. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  2173. +CONFIG_CPU_FREQ=y
  2174. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  2175. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  2176. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  2177. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  2178. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  2179. +CONFIG_CPU_IDLE=y
  2180. +CONFIG_VFP=y
  2181. +CONFIG_BINFMT_MISC=y
  2182. +CONFIG_NET=y
  2183. +CONFIG_PACKET=y
  2184. +CONFIG_UNIX=y
  2185. +CONFIG_INET=y
  2186. +CONFIG_IP_MULTICAST=y
  2187. +CONFIG_IP_PNP=y
  2188. +CONFIG_IP_PNP_DHCP=y
  2189. +CONFIG_IP_PNP_RARP=y
  2190. +CONFIG_SYN_COOKIES=y
  2191. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  2192. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  2193. +# CONFIG_INET_XFRM_MODE_BEET is not set
  2194. +# CONFIG_INET_LRO is not set
  2195. +# CONFIG_INET_DIAG is not set
  2196. +# CONFIG_IPV6 is not set
  2197. +# CONFIG_WIRELESS is not set
  2198. +CONFIG_DEVTMPFS=y
  2199. +CONFIG_DEVTMPFS_MOUNT=y
  2200. +CONFIG_BLK_DEV_LOOP=y
  2201. +CONFIG_BLK_DEV_RAM=y
  2202. +CONFIG_SCSI=y
  2203. +# CONFIG_SCSI_PROC_FS is not set
  2204. +# CONFIG_SCSI_LOWLEVEL is not set
  2205. +CONFIG_NETDEVICES=y
  2206. +# CONFIG_NET_VENDOR_BROADCOM is not set
  2207. +# CONFIG_NET_VENDOR_CIRRUS is not set
  2208. +# CONFIG_NET_VENDOR_FARADAY is not set
  2209. +# CONFIG_NET_VENDOR_INTEL is not set
  2210. +# CONFIG_NET_VENDOR_MARVELL is not set
  2211. +# CONFIG_NET_VENDOR_MICREL is not set
  2212. +# CONFIG_NET_VENDOR_NATSEMI is not set
  2213. +# CONFIG_NET_VENDOR_SEEQ is not set
  2214. +# CONFIG_NET_VENDOR_STMICRO is not set
  2215. +# CONFIG_NET_VENDOR_WIZNET is not set
  2216. +CONFIG_USB_USBNET=y
  2217. +# CONFIG_USB_NET_AX8817X is not set
  2218. +# CONFIG_USB_NET_CDCETHER is not set
  2219. +# CONFIG_USB_NET_CDC_NCM is not set
  2220. +CONFIG_USB_NET_SMSC95XX=y
  2221. +# CONFIG_USB_NET_NET1080 is not set
  2222. +# CONFIG_USB_NET_CDC_SUBSET is not set
  2223. +# CONFIG_USB_NET_ZAURUS is not set
  2224. +# CONFIG_WLAN is not set
  2225. +# CONFIG_INPUT_MOUSEDEV is not set
  2226. +CONFIG_INPUT_EVDEV=y
  2227. +# CONFIG_INPUT_KEYBOARD is not set
  2228. +# CONFIG_INPUT_MOUSE is not set
  2229. +# CONFIG_SERIO is not set
  2230. +CONFIG_VT_HW_CONSOLE_BINDING=y
  2231. +# CONFIG_LEGACY_PTYS is not set
  2232. +# CONFIG_DEVKMEM is not set
  2233. +CONFIG_SERIAL_AMBA_PL011=y
  2234. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  2235. +CONFIG_TTY_PRINTK=y
  2236. +CONFIG_HW_RANDOM=y
  2237. +CONFIG_HW_RANDOM_BCM2708=y
  2238. +CONFIG_RAW_DRIVER=y
  2239. +CONFIG_THERMAL=y
  2240. +CONFIG_THERMAL_BCM2835=y
  2241. +CONFIG_WATCHDOG=y
  2242. +CONFIG_BCM2708_WDT=y
  2243. +CONFIG_REGULATOR=y
  2244. +CONFIG_REGULATOR_DEBUG=y
  2245. +CONFIG_REGULATOR_FIXED_VOLTAGE=y
  2246. +CONFIG_REGULATOR_VIRTUAL_CONSUMER=y
  2247. +CONFIG_REGULATOR_USERSPACE_CONSUMER=y
  2248. +CONFIG_FB=y
  2249. +CONFIG_FB_BCM2708=y
  2250. +CONFIG_FRAMEBUFFER_CONSOLE=y
  2251. +CONFIG_LOGO=y
  2252. +# CONFIG_LOGO_LINUX_MONO is not set
  2253. +# CONFIG_LOGO_LINUX_VGA16 is not set
  2254. +CONFIG_SOUND=y
  2255. +CONFIG_SND=y
  2256. +CONFIG_SND_BCM2835=y
  2257. +# CONFIG_SND_USB is not set
  2258. +CONFIG_USB=y
  2259. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  2260. +CONFIG_USB_DWCOTG=y
  2261. +CONFIG_MMC=y
  2262. +CONFIG_MMC_SDHCI=y
  2263. +CONFIG_MMC_SDHCI_PLTFM=y
  2264. +CONFIG_MMC_SDHCI_BCM2708=y
  2265. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  2266. +CONFIG_NEW_LEDS=y
  2267. +CONFIG_LEDS_CLASS=y
  2268. +CONFIG_LEDS_TRIGGERS=y
  2269. +# CONFIG_IOMMU_SUPPORT is not set
  2270. +CONFIG_EXT4_FS=y
  2271. +CONFIG_EXT4_FS_POSIX_ACL=y
  2272. +CONFIG_EXT4_FS_SECURITY=y
  2273. +CONFIG_AUTOFS4_FS=y
  2274. +CONFIG_FSCACHE=y
  2275. +CONFIG_CACHEFILES=y
  2276. +CONFIG_MSDOS_FS=y
  2277. +CONFIG_VFAT_FS=y
  2278. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  2279. +CONFIG_TMPFS=y
  2280. +CONFIG_TMPFS_POSIX_ACL=y
  2281. +CONFIG_CONFIGFS_FS=y
  2282. +# CONFIG_MISC_FILESYSTEMS is not set
  2283. +CONFIG_NFS_FS=y
  2284. +CONFIG_NFS_V3_ACL=y
  2285. +CONFIG_NFS_V4=y
  2286. +CONFIG_ROOT_NFS=y
  2287. +CONFIG_NFS_FSCACHE=y
  2288. +CONFIG_NLS_DEFAULT="utf8"
  2289. +CONFIG_NLS_CODEPAGE_437=y
  2290. +CONFIG_NLS_CODEPAGE_737=y
  2291. +CONFIG_NLS_CODEPAGE_775=y
  2292. +CONFIG_NLS_CODEPAGE_850=y
  2293. +CONFIG_NLS_CODEPAGE_852=y
  2294. +CONFIG_NLS_CODEPAGE_855=y
  2295. +CONFIG_NLS_CODEPAGE_857=y
  2296. +CONFIG_NLS_CODEPAGE_860=y
  2297. +CONFIG_NLS_CODEPAGE_861=y
  2298. +CONFIG_NLS_CODEPAGE_862=y
  2299. +CONFIG_NLS_CODEPAGE_863=y
  2300. +CONFIG_NLS_CODEPAGE_864=y
  2301. +CONFIG_NLS_CODEPAGE_865=y
  2302. +CONFIG_NLS_CODEPAGE_866=y
  2303. +CONFIG_NLS_CODEPAGE_869=y
  2304. +CONFIG_NLS_CODEPAGE_936=y
  2305. +CONFIG_NLS_CODEPAGE_950=y
  2306. +CONFIG_NLS_CODEPAGE_932=y
  2307. +CONFIG_NLS_CODEPAGE_949=y
  2308. +CONFIG_NLS_CODEPAGE_874=y
  2309. +CONFIG_NLS_ISO8859_8=y
  2310. +CONFIG_NLS_CODEPAGE_1250=y
  2311. +CONFIG_NLS_CODEPAGE_1251=y
  2312. +CONFIG_NLS_ASCII=y
  2313. +CONFIG_NLS_ISO8859_1=y
  2314. +CONFIG_NLS_ISO8859_2=y
  2315. +CONFIG_NLS_ISO8859_3=y
  2316. +CONFIG_NLS_ISO8859_4=y
  2317. +CONFIG_NLS_ISO8859_5=y
  2318. +CONFIG_NLS_ISO8859_6=y
  2319. +CONFIG_NLS_ISO8859_7=y
  2320. +CONFIG_NLS_ISO8859_9=y
  2321. +CONFIG_NLS_ISO8859_13=y
  2322. +CONFIG_NLS_ISO8859_14=y
  2323. +CONFIG_NLS_ISO8859_15=y
  2324. +CONFIG_NLS_UTF8=y
  2325. +CONFIG_PRINTK_TIME=y
  2326. +CONFIG_DEBUG_FS=y
  2327. +CONFIG_DETECT_HUNG_TASK=y
  2328. +# CONFIG_DEBUG_PREEMPT is not set
  2329. +# CONFIG_DEBUG_BUGVERBOSE is not set
  2330. +# CONFIG_FTRACE is not set
  2331. +CONFIG_KGDB=y
  2332. +CONFIG_KGDB_KDB=y
  2333. +# CONFIG_ARM_UNWIND is not set
  2334. +CONFIG_CRYPTO_CBC=y
  2335. +CONFIG_CRYPTO_HMAC=y
  2336. +CONFIG_CRYPTO_MD5=y
  2337. +CONFIG_CRYPTO_SHA1=y
  2338. +CONFIG_CRYPTO_DES=y
  2339. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2340. +# CONFIG_CRYPTO_HW is not set
  2341. +CONFIG_CRC_ITU_T=y
  2342. +CONFIG_LIBCRC32C=y
  2343. diff -Nur linux-3.12.13/arch/arm/include/asm/fiq.h linux-raspberry-pi/arch/arm/include/asm/fiq.h
  2344. --- linux-3.12.13/arch/arm/include/asm/fiq.h 2014-02-22 22:32:50.000000000 +0100
  2345. +++ linux-raspberry-pi/arch/arm/include/asm/fiq.h 2014-03-11 17:31:42.000000000 +0100
  2346. @@ -42,6 +42,7 @@
  2347. /* helpers defined in fiqasm.S: */
  2348. extern void __set_fiq_regs(unsigned long const *regs);
  2349. extern void __get_fiq_regs(unsigned long *regs);
  2350. +extern void __FIQ_Branch(unsigned long *regs);
  2351. static inline void set_fiq_regs(struct pt_regs const *regs)
  2352. {
  2353. diff -Nur linux-3.12.13/arch/arm/Kconfig linux-raspberry-pi/arch/arm/Kconfig
  2354. --- linux-3.12.13/arch/arm/Kconfig 2014-02-22 22:32:50.000000000 +0100
  2355. +++ linux-raspberry-pi/arch/arm/Kconfig 2014-03-11 17:51:00.000000000 +0100
  2356. @@ -368,6 +368,24 @@
  2357. This enables support for systems based on Atmel
  2358. AT91RM9200 and AT91SAM9* processors.
  2359. +config ARCH_BCM2708
  2360. + bool "Broadcom BCM2708 family"
  2361. + select CPU_V6
  2362. + select ARM_AMBA
  2363. + select HAVE_CLK
  2364. + select HAVE_SCHED_CLOCK
  2365. + select NEED_MACH_GPIO_H
  2366. + select NEED_MACH_MEMORY_H
  2367. + select CLKDEV_LOOKUP
  2368. + select ARCH_HAS_CPUFREQ
  2369. + select GENERIC_CLOCKEVENTS
  2370. + select ARM_ERRATA_411920
  2371. + select MACH_BCM2708
  2372. + select VC4
  2373. + select FIQ
  2374. + help
  2375. + This enables support for Broadcom BCM2708 boards.
  2376. +
  2377. config ARCH_CLPS711X
  2378. bool "Cirrus Logic CLPS711x/EP721x/EP731x-based"
  2379. select ARCH_REQUIRE_GPIOLIB
  2380. @@ -1043,6 +1061,7 @@
  2381. source "arch/arm/mach-vt8500/Kconfig"
  2382. source "arch/arm/mach-w90x900/Kconfig"
  2383. +source "arch/arm/mach-bcm2708/Kconfig"
  2384. source "arch/arm/mach-zynq/Kconfig"
  2385. diff -Nur linux-3.12.13/arch/arm/Kconfig.debug linux-raspberry-pi/arch/arm/Kconfig.debug
  2386. --- linux-3.12.13/arch/arm/Kconfig.debug 2014-02-22 22:32:50.000000000 +0100
  2387. +++ linux-raspberry-pi/arch/arm/Kconfig.debug 2014-03-11 17:51:00.000000000 +0100
  2388. @@ -847,6 +847,14 @@
  2389. options; the platform specific options are deprecated
  2390. and will be soon removed.
  2391. + config DEBUG_BCM2708_UART0
  2392. + bool "Broadcom BCM2708 UART0 (PL011)"
  2393. + depends on MACH_BCM2708
  2394. + help
  2395. + Say Y here if you want the debug print routines to direct
  2396. + their output to UART 0. The port must have been initialised
  2397. + by the boot-loader before use.
  2398. +
  2399. endchoice
  2400. config DEBUG_EXYNOS_UART
  2401. diff -Nur linux-3.12.13/arch/arm/kernel/fiqasm.S linux-raspberry-pi/arch/arm/kernel/fiqasm.S
  2402. --- linux-3.12.13/arch/arm/kernel/fiqasm.S 2014-02-22 22:32:50.000000000 +0100
  2403. +++ linux-raspberry-pi/arch/arm/kernel/fiqasm.S 2014-03-11 17:31:43.000000000 +0100
  2404. @@ -25,6 +25,9 @@
  2405. ENTRY(__set_fiq_regs)
  2406. mov r2, #PSR_I_BIT | PSR_F_BIT | FIQ_MODE
  2407. mrs r1, cpsr
  2408. +@@@@@@@@@@@@@@@ hack: enable the fiq here to keep usb driver happy
  2409. + and r1, #~PSR_F_BIT
  2410. +@@@@@@@@@@@@@@@ endhack: (need to find better place for this to happen)
  2411. msr cpsr_c, r2 @ select FIQ mode
  2412. mov r0, r0 @ avoid hazard prior to ARMv4
  2413. ldmia r0!, {r8 - r12}
  2414. @@ -47,3 +50,7 @@
  2415. mov r0, r0 @ avoid hazard prior to ARMv4
  2416. mov pc, lr
  2417. ENDPROC(__get_fiq_regs)
  2418. +
  2419. +ENTRY(__FIQ_Branch)
  2420. + mov pc, r8
  2421. +ENDPROC(__FIQ_Branch)
  2422. diff -Nur linux-3.12.13/arch/arm/kernel/fiq.c linux-raspberry-pi/arch/arm/kernel/fiq.c
  2423. --- linux-3.12.13/arch/arm/kernel/fiq.c 2014-02-22 22:32:50.000000000 +0100
  2424. +++ linux-raspberry-pi/arch/arm/kernel/fiq.c 2014-03-11 17:31:43.000000000 +0100
  2425. @@ -142,6 +142,7 @@
  2426. EXPORT_SYMBOL(set_fiq_handler);
  2427. EXPORT_SYMBOL(__set_fiq_regs); /* defined in fiqasm.S */
  2428. EXPORT_SYMBOL(__get_fiq_regs); /* defined in fiqasm.S */
  2429. +EXPORT_SYMBOL(__FIQ_Branch); /* defined in fiqasm.S */
  2430. EXPORT_SYMBOL(claim_fiq);
  2431. EXPORT_SYMBOL(release_fiq);
  2432. EXPORT_SYMBOL(enable_fiq);
  2433. diff -Nur linux-3.12.13/arch/arm/kernel/process.c linux-raspberry-pi/arch/arm/kernel/process.c
  2434. --- linux-3.12.13/arch/arm/kernel/process.c 2014-02-22 22:32:50.000000000 +0100
  2435. +++ linux-raspberry-pi/arch/arm/kernel/process.c 2014-03-11 17:51:02.000000000 +0100
  2436. @@ -176,6 +176,16 @@
  2437. default_idle();
  2438. }
  2439. +char bcm2708_reboot_mode = 'h';
  2440. +
  2441. +int __init reboot_setup(char *str)
  2442. +{
  2443. + bcm2708_reboot_mode = str[0];
  2444. + return 1;
  2445. +}
  2446. +
  2447. +__setup("reboot=", reboot_setup);
  2448. +
  2449. /*
  2450. * Called by kexec, immediately prior to machine_kexec().
  2451. *
  2452. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/armctrl.c linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.c
  2453. --- linux-3.12.13/arch/arm/mach-bcm2708/armctrl.c 1970-01-01 01:00:00.000000000 +0100
  2454. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.c 2014-03-11 17:31:43.000000000 +0100
  2455. @@ -0,0 +1,219 @@
  2456. +/*
  2457. + * linux/arch/arm/mach-bcm2708/armctrl.c
  2458. + *
  2459. + * Copyright (C) 2010 Broadcom
  2460. + *
  2461. + * This program is free software; you can redistribute it and/or modify
  2462. + * it under the terms of the GNU General Public License as published by
  2463. + * the Free Software Foundation; either version 2 of the License, or
  2464. + * (at your option) any later version.
  2465. + *
  2466. + * This program is distributed in the hope that it will be useful,
  2467. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2468. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2469. + * GNU General Public License for more details.
  2470. + *
  2471. + * You should have received a copy of the GNU General Public License
  2472. + * along with this program; if not, write to the Free Software
  2473. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2474. + */
  2475. +#include <linux/init.h>
  2476. +#include <linux/list.h>
  2477. +#include <linux/io.h>
  2478. +#include <linux/version.h>
  2479. +#include <linux/syscore_ops.h>
  2480. +#include <linux/interrupt.h>
  2481. +
  2482. +#include <asm/mach/irq.h>
  2483. +#include <mach/hardware.h>
  2484. +#include "armctrl.h"
  2485. +
  2486. +/* For support of kernels >= 3.0 assume only one VIC for now*/
  2487. +static unsigned int remap_irqs[(INTERRUPT_ARASANSDIO + 1) - INTERRUPT_JPEG] = {
  2488. + INTERRUPT_VC_JPEG,
  2489. + INTERRUPT_VC_USB,
  2490. + INTERRUPT_VC_3D,
  2491. + INTERRUPT_VC_DMA2,
  2492. + INTERRUPT_VC_DMA3,
  2493. + INTERRUPT_VC_I2C,
  2494. + INTERRUPT_VC_SPI,
  2495. + INTERRUPT_VC_I2SPCM,
  2496. + INTERRUPT_VC_SDIO,
  2497. + INTERRUPT_VC_UART,
  2498. + INTERRUPT_VC_ARASANSDIO
  2499. +};
  2500. +
  2501. +static void armctrl_mask_irq(struct irq_data *d)
  2502. +{
  2503. + static const unsigned int disables[4] = {
  2504. + ARM_IRQ_DIBL1,
  2505. + ARM_IRQ_DIBL2,
  2506. + ARM_IRQ_DIBL3,
  2507. + 0
  2508. + };
  2509. +
  2510. + if (d->irq >= FIQ_START) {
  2511. + writel(0, __io_address(ARM_IRQ_FAST));
  2512. + } else {
  2513. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2514. + writel(1 << (data & 0x1f), __io_address(disables[(data >> 5) & 0x3]));
  2515. + }
  2516. +}
  2517. +
  2518. +static void armctrl_unmask_irq(struct irq_data *d)
  2519. +{
  2520. + static const unsigned int enables[4] = {
  2521. + ARM_IRQ_ENBL1,
  2522. + ARM_IRQ_ENBL2,
  2523. + ARM_IRQ_ENBL3,
  2524. + 0
  2525. + };
  2526. +
  2527. + if (d->irq >= FIQ_START) {
  2528. + unsigned int data =
  2529. + (unsigned int)irq_get_chip_data(d->irq) - FIQ_START;
  2530. + writel(0x80 | data, __io_address(ARM_IRQ_FAST));
  2531. + } else {
  2532. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2533. + writel(1 << (data & 0x1f), __io_address(enables[(data >> 5) & 0x3]));
  2534. + }
  2535. +}
  2536. +
  2537. +#if defined(CONFIG_PM)
  2538. +
  2539. +/* for kernels 3.xx use the new syscore_ops apis but for older kernels use the sys dev class */
  2540. +
  2541. +/* Static defines
  2542. + * struct armctrl_device - VIC PM device (< 3.xx)
  2543. + * @sysdev: The system device which is registered. (< 3.xx)
  2544. + * @irq: The IRQ number for the base of the VIC.
  2545. + * @base: The register base for the VIC.
  2546. + * @resume_sources: A bitmask of interrupts for resume.
  2547. + * @resume_irqs: The IRQs enabled for resume.
  2548. + * @int_select: Save for VIC_INT_SELECT.
  2549. + * @int_enable: Save for VIC_INT_ENABLE.
  2550. + * @soft_int: Save for VIC_INT_SOFT.
  2551. + * @protect: Save for VIC_PROTECT.
  2552. + */
  2553. +struct armctrl_info {
  2554. + void __iomem *base;
  2555. + int irq;
  2556. + u32 resume_sources;
  2557. + u32 resume_irqs;
  2558. + u32 int_select;
  2559. + u32 int_enable;
  2560. + u32 soft_int;
  2561. + u32 protect;
  2562. +} armctrl;
  2563. +
  2564. +static int armctrl_suspend(void)
  2565. +{
  2566. + return 0;
  2567. +}
  2568. +
  2569. +static void armctrl_resume(void)
  2570. +{
  2571. + return;
  2572. +}
  2573. +
  2574. +/**
  2575. + * armctrl_pm_register - Register a VIC for later power management control
  2576. + * @base: The base address of the VIC.
  2577. + * @irq: The base IRQ for the VIC.
  2578. + * @resume_sources: bitmask of interrupts allowed for resume sources.
  2579. + *
  2580. + * For older kernels (< 3.xx) do -
  2581. + * Register the VIC with the system device tree so that it can be notified
  2582. + * of suspend and resume requests and ensure that the correct actions are
  2583. + * taken to re-instate the settings on resume.
  2584. + */
  2585. +static void __init armctrl_pm_register(void __iomem * base, unsigned int irq,
  2586. + u32 resume_sources)
  2587. +{
  2588. + armctrl.base = base;
  2589. + armctrl.resume_sources = resume_sources;
  2590. + armctrl.irq = irq;
  2591. +}
  2592. +
  2593. +static int armctrl_set_wake(struct irq_data *d, unsigned int on)
  2594. +{
  2595. + unsigned int off = d->irq & 31;
  2596. + u32 bit = 1 << off;
  2597. +
  2598. + if (!(bit & armctrl.resume_sources))
  2599. + return -EINVAL;
  2600. +
  2601. + if (on)
  2602. + armctrl.resume_irqs |= bit;
  2603. + else
  2604. + armctrl.resume_irqs &= ~bit;
  2605. +
  2606. + return 0;
  2607. +}
  2608. +
  2609. +#else
  2610. +static inline void armctrl_pm_register(void __iomem * base, unsigned int irq,
  2611. + u32 arg1)
  2612. +{
  2613. +}
  2614. +
  2615. +#define armctrl_suspend NULL
  2616. +#define armctrl_resume NULL
  2617. +#define armctrl_set_wake NULL
  2618. +#endif /* CONFIG_PM */
  2619. +
  2620. +static struct syscore_ops armctrl_syscore_ops = {
  2621. + .suspend = armctrl_suspend,
  2622. + .resume = armctrl_resume,
  2623. +};
  2624. +
  2625. +/**
  2626. + * armctrl_syscore_init - initicall to register VIC pm functions
  2627. + *
  2628. + * This is called via late_initcall() to register
  2629. + * the resources for the VICs due to the early
  2630. + * nature of the VIC's registration.
  2631. +*/
  2632. +static int __init armctrl_syscore_init(void)
  2633. +{
  2634. + register_syscore_ops(&armctrl_syscore_ops);
  2635. + return 0;
  2636. +}
  2637. +
  2638. +late_initcall(armctrl_syscore_init);
  2639. +
  2640. +static struct irq_chip armctrl_chip = {
  2641. + .name = "ARMCTRL",
  2642. + .irq_ack = armctrl_mask_irq,
  2643. + .irq_mask = armctrl_mask_irq,
  2644. + .irq_unmask = armctrl_unmask_irq,
  2645. + .irq_set_wake = armctrl_set_wake,
  2646. +};
  2647. +
  2648. +/**
  2649. + * armctrl_init - initialise a vectored interrupt controller
  2650. + * @base: iomem base address
  2651. + * @irq_start: starting interrupt number, must be muliple of 32
  2652. + * @armctrl_sources: bitmask of interrupt sources to allow
  2653. + * @resume_sources: bitmask of interrupt sources to allow for resume
  2654. + */
  2655. +int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  2656. + u32 armctrl_sources, u32 resume_sources)
  2657. +{
  2658. + unsigned int irq;
  2659. +
  2660. + for (irq = 0; irq < NR_IRQS; irq++) {
  2661. + unsigned int data = irq;
  2662. + if (irq >= INTERRUPT_JPEG && irq <= INTERRUPT_ARASANSDIO)
  2663. + data = remap_irqs[irq - INTERRUPT_JPEG];
  2664. +
  2665. + irq_set_chip(irq, &armctrl_chip);
  2666. + irq_set_chip_data(irq, (void *)data);
  2667. + irq_set_handler(irq, handle_level_irq);
  2668. + set_irq_flags(irq, IRQF_VALID | IRQF_PROBE | IRQF_DISABLED);
  2669. + }
  2670. +
  2671. + armctrl_pm_register(base, irq_start, resume_sources);
  2672. + init_FIQ(FIQ_START);
  2673. + return 0;
  2674. +}
  2675. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/armctrl.h linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.h
  2676. --- linux-3.12.13/arch/arm/mach-bcm2708/armctrl.h 1970-01-01 01:00:00.000000000 +0100
  2677. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.h 2014-03-11 17:31:43.000000000 +0100
  2678. @@ -0,0 +1,27 @@
  2679. +/*
  2680. + * linux/arch/arm/mach-bcm2708/armctrl.h
  2681. + *
  2682. + * Copyright (C) 2010 Broadcom
  2683. + *
  2684. + * This program is free software; you can redistribute it and/or modify
  2685. + * it under the terms of the GNU General Public License as published by
  2686. + * the Free Software Foundation; either version 2 of the License, or
  2687. + * (at your option) any later version.
  2688. + *
  2689. + * This program is distributed in the hope that it will be useful,
  2690. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2691. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2692. + * GNU General Public License for more details.
  2693. + *
  2694. + * You should have received a copy of the GNU General Public License
  2695. + * along with this program; if not, write to the Free Software
  2696. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2697. + */
  2698. +
  2699. +#ifndef __BCM2708_ARMCTRL_H
  2700. +#define __BCM2708_ARMCTRL_H
  2701. +
  2702. +extern int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  2703. + u32 armctrl_sources, u32 resume_sources);
  2704. +
  2705. +#endif
  2706. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/bcm2708.c linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.c
  2707. --- linux-3.12.13/arch/arm/mach-bcm2708/bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  2708. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.c 2014-03-11 17:51:02.000000000 +0100
  2709. @@ -0,0 +1,1011 @@
  2710. +/*
  2711. + * linux/arch/arm/mach-bcm2708/bcm2708.c
  2712. + *
  2713. + * Copyright (C) 2010 Broadcom
  2714. + *
  2715. + * This program is free software; you can redistribute it and/or modify
  2716. + * it under the terms of the GNU General Public License as published by
  2717. + * the Free Software Foundation; either version 2 of the License, or
  2718. + * (at your option) any later version.
  2719. + *
  2720. + * This program is distributed in the hope that it will be useful,
  2721. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2722. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2723. + * GNU General Public License for more details.
  2724. + *
  2725. + * You should have received a copy of the GNU General Public License
  2726. + * along with this program; if not, write to the Free Software
  2727. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2728. + */
  2729. +
  2730. +#include <linux/init.h>
  2731. +#include <linux/device.h>
  2732. +#include <linux/dma-mapping.h>
  2733. +#include <linux/serial_8250.h>
  2734. +#include <linux/platform_device.h>
  2735. +#include <linux/syscore_ops.h>
  2736. +#include <linux/interrupt.h>
  2737. +#include <linux/amba/bus.h>
  2738. +#include <linux/amba/clcd.h>
  2739. +#include <linux/clockchips.h>
  2740. +#include <linux/cnt32_to_63.h>
  2741. +#include <linux/io.h>
  2742. +#include <linux/module.h>
  2743. +#include <linux/spi/spi.h>
  2744. +#include <linux/w1-gpio.h>
  2745. +
  2746. +#include <linux/version.h>
  2747. +#include <linux/clkdev.h>
  2748. +#include <asm/system.h>
  2749. +#include <mach/hardware.h>
  2750. +#include <asm/irq.h>
  2751. +#include <linux/leds.h>
  2752. +#include <asm/mach-types.h>
  2753. +#include <asm/sched_clock.h>
  2754. +
  2755. +#include <asm/mach/arch.h>
  2756. +#include <asm/mach/flash.h>
  2757. +#include <asm/mach/irq.h>
  2758. +#include <asm/mach/time.h>
  2759. +#include <asm/mach/map.h>
  2760. +
  2761. +#include <mach/timex.h>
  2762. +#include <mach/dma.h>
  2763. +#include <mach/vcio.h>
  2764. +#include <mach/system.h>
  2765. +
  2766. +#include <linux/delay.h>
  2767. +
  2768. +#include "bcm2708.h"
  2769. +#include "armctrl.h"
  2770. +#include "clock.h"
  2771. +
  2772. +#ifdef CONFIG_BCM_VC_CMA
  2773. +#include <linux/broadcom/vc_cma.h>
  2774. +#endif
  2775. +
  2776. +
  2777. +/* Effectively we have an IOMMU (ARM<->VideoCore map) that is set up to
  2778. + * give us IO access only to 64Mbytes of physical memory (26 bits). We could
  2779. + * represent this window by setting our dmamasks to 26 bits but, in fact
  2780. + * we're not going to use addresses outside this range (they're not in real
  2781. + * memory) so we don't bother.
  2782. + *
  2783. + * In the future we might include code to use this IOMMU to remap other
  2784. + * physical addresses onto VideoCore memory then the use of 32-bits would be
  2785. + * more legitimate.
  2786. + */
  2787. +#define DMA_MASK_BITS_COMMON 32
  2788. +
  2789. +// use GPIO 4 for the one-wire GPIO pin, if enabled
  2790. +#define W1_GPIO 4
  2791. +
  2792. +/* command line parameters */
  2793. +static unsigned boardrev, serial;
  2794. +static unsigned uart_clock;
  2795. +static unsigned disk_led_gpio = 16;
  2796. +static unsigned disk_led_active_low = 1;
  2797. +static unsigned reboot_part = 0;
  2798. +
  2799. +static void __init bcm2708_init_led(void);
  2800. +
  2801. +void __init bcm2708_init_irq(void)
  2802. +{
  2803. + armctrl_init(__io_address(ARMCTRL_IC_BASE), 0, 0, 0);
  2804. +}
  2805. +
  2806. +static struct map_desc bcm2708_io_desc[] __initdata = {
  2807. + {
  2808. + .virtual = IO_ADDRESS(ARMCTRL_BASE),
  2809. + .pfn = __phys_to_pfn(ARMCTRL_BASE),
  2810. + .length = SZ_4K,
  2811. + .type = MT_DEVICE},
  2812. + {
  2813. + .virtual = IO_ADDRESS(UART0_BASE),
  2814. + .pfn = __phys_to_pfn(UART0_BASE),
  2815. + .length = SZ_4K,
  2816. + .type = MT_DEVICE},
  2817. + {
  2818. + .virtual = IO_ADDRESS(UART1_BASE),
  2819. + .pfn = __phys_to_pfn(UART1_BASE),
  2820. + .length = SZ_4K,
  2821. + .type = MT_DEVICE},
  2822. + {
  2823. + .virtual = IO_ADDRESS(DMA_BASE),
  2824. + .pfn = __phys_to_pfn(DMA_BASE),
  2825. + .length = SZ_4K,
  2826. + .type = MT_DEVICE},
  2827. + {
  2828. + .virtual = IO_ADDRESS(MCORE_BASE),
  2829. + .pfn = __phys_to_pfn(MCORE_BASE),
  2830. + .length = SZ_4K,
  2831. + .type = MT_DEVICE},
  2832. + {
  2833. + .virtual = IO_ADDRESS(ST_BASE),
  2834. + .pfn = __phys_to_pfn(ST_BASE),
  2835. + .length = SZ_4K,
  2836. + .type = MT_DEVICE},
  2837. + {
  2838. + .virtual = IO_ADDRESS(USB_BASE),
  2839. + .pfn = __phys_to_pfn(USB_BASE),
  2840. + .length = SZ_128K,
  2841. + .type = MT_DEVICE},
  2842. + {
  2843. + .virtual = IO_ADDRESS(PM_BASE),
  2844. + .pfn = __phys_to_pfn(PM_BASE),
  2845. + .length = SZ_4K,
  2846. + .type = MT_DEVICE},
  2847. + {
  2848. + .virtual = IO_ADDRESS(GPIO_BASE),
  2849. + .pfn = __phys_to_pfn(GPIO_BASE),
  2850. + .length = SZ_4K,
  2851. + .type = MT_DEVICE}
  2852. +};
  2853. +
  2854. +void __init bcm2708_map_io(void)
  2855. +{
  2856. + iotable_init(bcm2708_io_desc, ARRAY_SIZE(bcm2708_io_desc));
  2857. +}
  2858. +
  2859. +/* The STC is a free running counter that increments at the rate of 1MHz */
  2860. +#define STC_FREQ_HZ 1000000
  2861. +
  2862. +static inline uint32_t timer_read(void)
  2863. +{
  2864. + /* STC: a free running counter that increments at the rate of 1MHz */
  2865. + return readl(__io_address(ST_BASE + 0x04));
  2866. +}
  2867. +
  2868. +static unsigned long bcm2708_read_current_timer(void)
  2869. +{
  2870. + return timer_read();
  2871. +}
  2872. +
  2873. +static u32 notrace bcm2708_read_sched_clock(void)
  2874. +{
  2875. + return timer_read();
  2876. +}
  2877. +
  2878. +static cycle_t clksrc_read(struct clocksource *cs)
  2879. +{
  2880. + return timer_read();
  2881. +}
  2882. +
  2883. +static struct clocksource clocksource_stc = {
  2884. + .name = "stc",
  2885. + .rating = 300,
  2886. + .read = clksrc_read,
  2887. + .mask = CLOCKSOURCE_MASK(32),
  2888. + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  2889. +};
  2890. +
  2891. +unsigned long frc_clock_ticks32(void)
  2892. +{
  2893. + return timer_read();
  2894. +}
  2895. +
  2896. +static void __init bcm2708_clocksource_init(void)
  2897. +{
  2898. + if (clocksource_register_hz(&clocksource_stc, STC_FREQ_HZ)) {
  2899. + printk(KERN_ERR "timer: failed to initialize clock "
  2900. + "source %s\n", clocksource_stc.name);
  2901. + }
  2902. +}
  2903. +
  2904. +
  2905. +/*
  2906. + * These are fixed clocks.
  2907. + */
  2908. +static struct clk ref24_clk = {
  2909. + .rate = UART0_CLOCK, /* The UART is clocked at 3MHz via APB_CLK */
  2910. +};
  2911. +
  2912. +static struct clk osc_clk = {
  2913. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  2914. + .rate = 27000000,
  2915. +#else
  2916. + .rate = 500000000, /* ARM clock is set from the VideoCore booter */
  2917. +#endif
  2918. +};
  2919. +
  2920. +/* warning - the USB needs a clock > 34MHz */
  2921. +
  2922. +static struct clk sdhost_clk = {
  2923. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  2924. + .rate = 4000000, /* 4MHz */
  2925. +#else
  2926. + .rate = 250000000, /* 250MHz */
  2927. +#endif
  2928. +};
  2929. +
  2930. +static struct clk_lookup lookups[] = {
  2931. + { /* UART0 */
  2932. + .dev_id = "dev:f1",
  2933. + .clk = &ref24_clk,
  2934. + },
  2935. + { /* USB */
  2936. + .dev_id = "bcm2708_usb",
  2937. + .clk = &osc_clk,
  2938. + }, { /* SPI */
  2939. + .dev_id = "bcm2708_spi.0",
  2940. + .clk = &sdhost_clk,
  2941. + }, { /* BSC0 */
  2942. + .dev_id = "bcm2708_i2c.0",
  2943. + .clk = &sdhost_clk,
  2944. + }, { /* BSC1 */
  2945. + .dev_id = "bcm2708_i2c.1",
  2946. + .clk = &sdhost_clk,
  2947. + }
  2948. +};
  2949. +
  2950. +#define UART0_IRQ { IRQ_UART, 0 /*NO_IRQ*/ }
  2951. +#define UART0_DMA { 15, 14 }
  2952. +
  2953. +AMBA_DEVICE(uart0, "dev:f1", UART0, NULL);
  2954. +
  2955. +static struct amba_device *amba_devs[] __initdata = {
  2956. + &uart0_device,
  2957. +};
  2958. +
  2959. +static struct resource bcm2708_dmaman_resources[] = {
  2960. + {
  2961. + .start = DMA_BASE,
  2962. + .end = DMA_BASE + SZ_4K - 1,
  2963. + .flags = IORESOURCE_MEM,
  2964. + }
  2965. +};
  2966. +
  2967. +static struct platform_device bcm2708_dmaman_device = {
  2968. + .name = BCM_DMAMAN_DRIVER_NAME,
  2969. + .id = 0, /* first bcm2708_dma */
  2970. + .resource = bcm2708_dmaman_resources,
  2971. + .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
  2972. +};
  2973. +
  2974. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  2975. +static struct w1_gpio_platform_data w1_gpio_pdata = {
  2976. + .pin = W1_GPIO,
  2977. + .is_open_drain = 0,
  2978. +};
  2979. +
  2980. +static struct platform_device w1_device = {
  2981. + .name = "w1-gpio",
  2982. + .id = -1,
  2983. + .dev.platform_data = &w1_gpio_pdata,
  2984. +};
  2985. +#endif
  2986. +
  2987. +static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2988. +
  2989. +static struct platform_device bcm2708_fb_device = {
  2990. + .name = "bcm2708_fb",
  2991. + .id = -1, /* only one bcm2708_fb */
  2992. + .resource = NULL,
  2993. + .num_resources = 0,
  2994. + .dev = {
  2995. + .dma_mask = &fb_dmamask,
  2996. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  2997. + },
  2998. +};
  2999. +
  3000. +static struct plat_serial8250_port bcm2708_uart1_platform_data[] = {
  3001. + {
  3002. + .mapbase = UART1_BASE + 0x40,
  3003. + .irq = IRQ_AUX,
  3004. + .uartclk = 125000000,
  3005. + .regshift = 2,
  3006. + .iotype = UPIO_MEM,
  3007. + .flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_SKIP_TEST,
  3008. + .type = PORT_8250,
  3009. + },
  3010. + {},
  3011. +};
  3012. +
  3013. +static struct platform_device bcm2708_uart1_device = {
  3014. + .name = "serial8250",
  3015. + .id = PLAT8250_DEV_PLATFORM,
  3016. + .dev = {
  3017. + .platform_data = bcm2708_uart1_platform_data,
  3018. + },
  3019. +};
  3020. +
  3021. +static struct resource bcm2708_usb_resources[] = {
  3022. + [0] = {
  3023. + .start = USB_BASE,
  3024. + .end = USB_BASE + SZ_128K - 1,
  3025. + .flags = IORESOURCE_MEM,
  3026. + },
  3027. + [1] = {
  3028. + .start = MPHI_BASE,
  3029. + .end = MPHI_BASE + SZ_4K - 1,
  3030. + .flags = IORESOURCE_MEM,
  3031. + },
  3032. + [2] = {
  3033. + .start = IRQ_HOSTPORT,
  3034. + .end = IRQ_HOSTPORT,
  3035. + .flags = IORESOURCE_IRQ,
  3036. + },
  3037. +};
  3038. +
  3039. +bool fiq_fix_enable = true;
  3040. +
  3041. +static struct resource bcm2708_usb_resources_no_fiq_fix[] = {
  3042. + [0] = {
  3043. + .start = USB_BASE,
  3044. + .end = USB_BASE + SZ_128K - 1,
  3045. + .flags = IORESOURCE_MEM,
  3046. + },
  3047. + [1] = {
  3048. + .start = IRQ_USB,
  3049. + .end = IRQ_USB,
  3050. + .flags = IORESOURCE_IRQ,
  3051. + },
  3052. +};
  3053. +
  3054. +static u64 usb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3055. +
  3056. +static struct platform_device bcm2708_usb_device = {
  3057. + .name = "bcm2708_usb",
  3058. + .id = -1, /* only one bcm2708_usb */
  3059. + .resource = bcm2708_usb_resources,
  3060. + .num_resources = ARRAY_SIZE(bcm2708_usb_resources),
  3061. + .dev = {
  3062. + .dma_mask = &usb_dmamask,
  3063. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3064. + },
  3065. +};
  3066. +
  3067. +static struct resource bcm2708_vcio_resources[] = {
  3068. + [0] = { /* mailbox/semaphore/doorbell access */
  3069. + .start = MCORE_BASE,
  3070. + .end = MCORE_BASE + SZ_4K - 1,
  3071. + .flags = IORESOURCE_MEM,
  3072. + },
  3073. +};
  3074. +
  3075. +static u64 vcio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3076. +
  3077. +static struct platform_device bcm2708_vcio_device = {
  3078. + .name = BCM_VCIO_DRIVER_NAME,
  3079. + .id = -1, /* only one VideoCore I/O area */
  3080. + .resource = bcm2708_vcio_resources,
  3081. + .num_resources = ARRAY_SIZE(bcm2708_vcio_resources),
  3082. + .dev = {
  3083. + .dma_mask = &vcio_dmamask,
  3084. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3085. + },
  3086. +};
  3087. +
  3088. +#ifdef CONFIG_BCM2708_GPIO
  3089. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  3090. +
  3091. +static struct resource bcm2708_gpio_resources[] = {
  3092. + [0] = { /* general purpose I/O */
  3093. + .start = GPIO_BASE,
  3094. + .end = GPIO_BASE + SZ_4K - 1,
  3095. + .flags = IORESOURCE_MEM,
  3096. + },
  3097. +};
  3098. +
  3099. +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3100. +
  3101. +static struct platform_device bcm2708_gpio_device = {
  3102. + .name = BCM_GPIO_DRIVER_NAME,
  3103. + .id = -1, /* only one VideoCore I/O area */
  3104. + .resource = bcm2708_gpio_resources,
  3105. + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
  3106. + .dev = {
  3107. + .dma_mask = &gpio_dmamask,
  3108. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3109. + },
  3110. +};
  3111. +#endif
  3112. +
  3113. +static struct resource bcm2708_systemtimer_resources[] = {
  3114. + [0] = { /* system timer access */
  3115. + .start = ST_BASE,
  3116. + .end = ST_BASE + SZ_4K - 1,
  3117. + .flags = IORESOURCE_MEM,
  3118. + },
  3119. + {
  3120. + .start = IRQ_TIMER3,
  3121. + .end = IRQ_TIMER3,
  3122. + .flags = IORESOURCE_IRQ,
  3123. + }
  3124. +
  3125. +};
  3126. +
  3127. +static u64 systemtimer_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3128. +
  3129. +static struct platform_device bcm2708_systemtimer_device = {
  3130. + .name = "bcm2708_systemtimer",
  3131. + .id = -1, /* only one VideoCore I/O area */
  3132. + .resource = bcm2708_systemtimer_resources,
  3133. + .num_resources = ARRAY_SIZE(bcm2708_systemtimer_resources),
  3134. + .dev = {
  3135. + .dma_mask = &systemtimer_dmamask,
  3136. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3137. + },
  3138. +};
  3139. +
  3140. +#ifdef CONFIG_MMC_SDHCI_BCM2708 /* Arasan emmc SD */
  3141. +static struct resource bcm2708_emmc_resources[] = {
  3142. + [0] = {
  3143. + .start = EMMC_BASE,
  3144. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  3145. + /* the memory map actually makes SZ_4K available */
  3146. + .flags = IORESOURCE_MEM,
  3147. + },
  3148. + [1] = {
  3149. + .start = IRQ_ARASANSDIO,
  3150. + .end = IRQ_ARASANSDIO,
  3151. + .flags = IORESOURCE_IRQ,
  3152. + },
  3153. +};
  3154. +
  3155. +static u64 bcm2708_emmc_dmamask = 0xffffffffUL;
  3156. +
  3157. +struct platform_device bcm2708_emmc_device = {
  3158. + .name = "bcm2708_sdhci",
  3159. + .id = 0,
  3160. + .num_resources = ARRAY_SIZE(bcm2708_emmc_resources),
  3161. + .resource = bcm2708_emmc_resources,
  3162. + .dev = {
  3163. + .dma_mask = &bcm2708_emmc_dmamask,
  3164. + .coherent_dma_mask = 0xffffffffUL},
  3165. +};
  3166. +#endif /* CONFIG_MMC_SDHCI_BCM2708 */
  3167. +
  3168. +static struct resource bcm2708_powerman_resources[] = {
  3169. + [0] = {
  3170. + .start = PM_BASE,
  3171. + .end = PM_BASE + SZ_256 - 1,
  3172. + .flags = IORESOURCE_MEM,
  3173. + },
  3174. +};
  3175. +
  3176. +static u64 powerman_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3177. +
  3178. +struct platform_device bcm2708_powerman_device = {
  3179. + .name = "bcm2708_powerman",
  3180. + .id = 0,
  3181. + .num_resources = ARRAY_SIZE(bcm2708_powerman_resources),
  3182. + .resource = bcm2708_powerman_resources,
  3183. + .dev = {
  3184. + .dma_mask = &powerman_dmamask,
  3185. + .coherent_dma_mask = 0xffffffffUL},
  3186. +};
  3187. +
  3188. +
  3189. +static struct platform_device bcm2708_alsa_devices[] = {
  3190. + [0] = {
  3191. + .name = "bcm2835_AUD0",
  3192. + .id = 0, /* first audio device */
  3193. + .resource = 0,
  3194. + .num_resources = 0,
  3195. + },
  3196. + [1] = {
  3197. + .name = "bcm2835_AUD1",
  3198. + .id = 1, /* second audio device */
  3199. + .resource = 0,
  3200. + .num_resources = 0,
  3201. + },
  3202. + [2] = {
  3203. + .name = "bcm2835_AUD2",
  3204. + .id = 2, /* third audio device */
  3205. + .resource = 0,
  3206. + .num_resources = 0,
  3207. + },
  3208. + [3] = {
  3209. + .name = "bcm2835_AUD3",
  3210. + .id = 3, /* forth audio device */
  3211. + .resource = 0,
  3212. + .num_resources = 0,
  3213. + },
  3214. + [4] = {
  3215. + .name = "bcm2835_AUD4",
  3216. + .id = 4, /* fifth audio device */
  3217. + .resource = 0,
  3218. + .num_resources = 0,
  3219. + },
  3220. + [5] = {
  3221. + .name = "bcm2835_AUD5",
  3222. + .id = 5, /* sixth audio device */
  3223. + .resource = 0,
  3224. + .num_resources = 0,
  3225. + },
  3226. + [6] = {
  3227. + .name = "bcm2835_AUD6",
  3228. + .id = 6, /* seventh audio device */
  3229. + .resource = 0,
  3230. + .num_resources = 0,
  3231. + },
  3232. + [7] = {
  3233. + .name = "bcm2835_AUD7",
  3234. + .id = 7, /* eighth audio device */
  3235. + .resource = 0,
  3236. + .num_resources = 0,
  3237. + },
  3238. +};
  3239. +
  3240. +static struct resource bcm2708_spi_resources[] = {
  3241. + {
  3242. + .start = SPI0_BASE,
  3243. + .end = SPI0_BASE + SZ_256 - 1,
  3244. + .flags = IORESOURCE_MEM,
  3245. + }, {
  3246. + .start = IRQ_SPI,
  3247. + .end = IRQ_SPI,
  3248. + .flags = IORESOURCE_IRQ,
  3249. + }
  3250. +};
  3251. +
  3252. +
  3253. +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3254. +static struct platform_device bcm2708_spi_device = {
  3255. + .name = "bcm2708_spi",
  3256. + .id = 0,
  3257. + .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
  3258. + .resource = bcm2708_spi_resources,
  3259. + .dev = {
  3260. + .dma_mask = &bcm2708_spi_dmamask,
  3261. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
  3262. +};
  3263. +
  3264. +#ifdef CONFIG_BCM2708_SPIDEV
  3265. +static struct spi_board_info bcm2708_spi_devices[] = {
  3266. +#ifdef CONFIG_SPI_SPIDEV
  3267. + {
  3268. + .modalias = "spidev",
  3269. + .max_speed_hz = 500000,
  3270. + .bus_num = 0,
  3271. + .chip_select = 0,
  3272. + .mode = SPI_MODE_0,
  3273. + }, {
  3274. + .modalias = "spidev",
  3275. + .max_speed_hz = 500000,
  3276. + .bus_num = 0,
  3277. + .chip_select = 1,
  3278. + .mode = SPI_MODE_0,
  3279. + }
  3280. +#endif
  3281. +};
  3282. +#endif
  3283. +
  3284. +static struct resource bcm2708_bsc0_resources[] = {
  3285. + {
  3286. + .start = BSC0_BASE,
  3287. + .end = BSC0_BASE + SZ_256 - 1,
  3288. + .flags = IORESOURCE_MEM,
  3289. + }, {
  3290. + .start = INTERRUPT_I2C,
  3291. + .end = INTERRUPT_I2C,
  3292. + .flags = IORESOURCE_IRQ,
  3293. + }
  3294. +};
  3295. +
  3296. +static struct platform_device bcm2708_bsc0_device = {
  3297. + .name = "bcm2708_i2c",
  3298. + .id = 0,
  3299. + .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
  3300. + .resource = bcm2708_bsc0_resources,
  3301. +};
  3302. +
  3303. +
  3304. +static struct resource bcm2708_bsc1_resources[] = {
  3305. + {
  3306. + .start = BSC1_BASE,
  3307. + .end = BSC1_BASE + SZ_256 - 1,
  3308. + .flags = IORESOURCE_MEM,
  3309. + }, {
  3310. + .start = INTERRUPT_I2C,
  3311. + .end = INTERRUPT_I2C,
  3312. + .flags = IORESOURCE_IRQ,
  3313. + }
  3314. +};
  3315. +
  3316. +static struct platform_device bcm2708_bsc1_device = {
  3317. + .name = "bcm2708_i2c",
  3318. + .id = 1,
  3319. + .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
  3320. + .resource = bcm2708_bsc1_resources,
  3321. +};
  3322. +
  3323. +static struct platform_device bcm2835_hwmon_device = {
  3324. + .name = "bcm2835_hwmon",
  3325. +};
  3326. +
  3327. +static struct platform_device bcm2835_thermal_device = {
  3328. + .name = "bcm2835_thermal",
  3329. +};
  3330. +
  3331. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  3332. +static struct resource bcm2708_i2s_resources[] = {
  3333. + {
  3334. + .start = I2S_BASE,
  3335. + .end = I2S_BASE + 0x20,
  3336. + .flags = IORESOURCE_MEM,
  3337. + },
  3338. + {
  3339. + .start = PCM_CLOCK_BASE,
  3340. + .end = PCM_CLOCK_BASE + 0x02,
  3341. + .flags = IORESOURCE_MEM,
  3342. + }
  3343. +};
  3344. +
  3345. +static struct platform_device bcm2708_i2s_device = {
  3346. + .name = "bcm2708-i2s",
  3347. + .id = 0,
  3348. + .num_resources = ARRAY_SIZE(bcm2708_i2s_resources),
  3349. + .resource = bcm2708_i2s_resources,
  3350. +};
  3351. +#endif
  3352. +
  3353. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  3354. +static struct platform_device snd_hifiberry_dac_device = {
  3355. + .name = "snd-hifiberry-dac",
  3356. + .id = 0,
  3357. + .num_resources = 0,
  3358. +};
  3359. +
  3360. +static struct platform_device snd_pcm5102a_codec_device = {
  3361. + .name = "pcm5102a-codec",
  3362. + .id = -1,
  3363. + .num_resources = 0,
  3364. +};
  3365. +#endif
  3366. +
  3367. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  3368. +static struct platform_device snd_hifiberry_digi_device = {
  3369. + .name = "snd-hifiberry-digi",
  3370. + .id = 0,
  3371. + .num_resources = 0,
  3372. +};
  3373. +
  3374. +static struct i2c_board_info __initdata snd_wm8804_i2c_devices[] = {
  3375. + {
  3376. + I2C_BOARD_INFO("wm8804", 0x3b)
  3377. + },
  3378. +};
  3379. +
  3380. +#endif
  3381. +
  3382. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  3383. +static struct platform_device snd_rpi_dac_device = {
  3384. + .name = "snd-rpi-dac",
  3385. + .id = 0,
  3386. + .num_resources = 0,
  3387. +};
  3388. +
  3389. +static struct platform_device snd_pcm1794a_codec_device = {
  3390. + .name = "pcm1794a-codec",
  3391. + .id = -1,
  3392. + .num_resources = 0,
  3393. +};
  3394. +#endif
  3395. +
  3396. +int __init bcm_register_device(struct platform_device *pdev)
  3397. +{
  3398. + int ret;
  3399. +
  3400. + ret = platform_device_register(pdev);
  3401. + if (ret)
  3402. + pr_debug("Unable to register platform device '%s': %d\n",
  3403. + pdev->name, ret);
  3404. +
  3405. + return ret;
  3406. +}
  3407. +
  3408. +int calc_rsts(int partition)
  3409. +{
  3410. + return PM_PASSWORD |
  3411. + ((partition & (1 << 0)) << 0) |
  3412. + ((partition & (1 << 1)) << 1) |
  3413. + ((partition & (1 << 2)) << 2) |
  3414. + ((partition & (1 << 3)) << 3) |
  3415. + ((partition & (1 << 4)) << 4) |
  3416. + ((partition & (1 << 5)) << 5);
  3417. +}
  3418. +
  3419. +static void bcm2708_restart(enum reboot_mode mode, const char *cmd)
  3420. +{
  3421. + extern char bcm2708_reboot_mode;
  3422. + uint32_t pm_rstc, pm_wdog;
  3423. + uint32_t timeout = 10;
  3424. + uint32_t pm_rsts = 0;
  3425. +
  3426. + if(bcm2708_reboot_mode == 'q')
  3427. + {
  3428. + // NOOBS < 1.3 booting with reboot=q
  3429. + pm_rsts = readl(__io_address(PM_RSTS));
  3430. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRQ_SET;
  3431. + }
  3432. + else if(bcm2708_reboot_mode == 'p')
  3433. + {
  3434. + // NOOBS < 1.3 halting
  3435. + pm_rsts = readl(__io_address(PM_RSTS));
  3436. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRH_SET;
  3437. + }
  3438. + else
  3439. + {
  3440. + pm_rsts = calc_rsts(reboot_part);
  3441. + }
  3442. +
  3443. + writel(pm_rsts, __io_address(PM_RSTS));
  3444. +
  3445. + /* Setup watchdog for reset */
  3446. + pm_rstc = readl(__io_address(PM_RSTC));
  3447. +
  3448. + pm_wdog = PM_PASSWORD | (timeout & PM_WDOG_TIME_SET); // watchdog timer = timer clock / 16; need password (31:16) + value (11:0)
  3449. + pm_rstc = PM_PASSWORD | (pm_rstc & PM_RSTC_WRCFG_CLR) | PM_RSTC_WRCFG_FULL_RESET;
  3450. +
  3451. + writel(pm_wdog, __io_address(PM_WDOG));
  3452. + writel(pm_rstc, __io_address(PM_RSTC));
  3453. +}
  3454. +
  3455. +/* 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 */
  3456. +static void bcm2708_power_off(void)
  3457. +{
  3458. + extern char bcm2708_reboot_mode;
  3459. + if(bcm2708_reboot_mode == 'q')
  3460. + {
  3461. + // NOOBS < v1.3
  3462. + bcm2708_restart('p', "");
  3463. + }
  3464. + else
  3465. + {
  3466. + /* partition 63 is special code for HALT the bootloader knows not to boot*/
  3467. + reboot_part = 63;
  3468. + /* continue with normal reset mechanism */
  3469. + bcm2708_restart(0, "");
  3470. + }
  3471. +}
  3472. +
  3473. +void __init bcm2708_init(void)
  3474. +{
  3475. + int i;
  3476. +
  3477. +#if defined(CONFIG_BCM_VC_CMA)
  3478. + vc_cma_early_init();
  3479. +#endif
  3480. + printk("bcm2708.uart_clock = %d\n", uart_clock);
  3481. + pm_power_off = bcm2708_power_off;
  3482. +
  3483. + if (uart_clock)
  3484. + lookups[0].clk->rate = uart_clock;
  3485. +
  3486. + for (i = 0; i < ARRAY_SIZE(lookups); i++)
  3487. + clkdev_add(&lookups[i]);
  3488. +
  3489. + bcm_register_device(&bcm2708_dmaman_device);
  3490. + bcm_register_device(&bcm2708_vcio_device);
  3491. +#ifdef CONFIG_BCM2708_GPIO
  3492. + bcm_register_device(&bcm2708_gpio_device);
  3493. +#endif
  3494. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  3495. + platform_device_register(&w1_device);
  3496. +#endif
  3497. + bcm_register_device(&bcm2708_systemtimer_device);
  3498. + bcm_register_device(&bcm2708_fb_device);
  3499. + if (!fiq_fix_enable)
  3500. + {
  3501. + bcm2708_usb_device.resource = bcm2708_usb_resources_no_fiq_fix;
  3502. + bcm2708_usb_device.num_resources = ARRAY_SIZE(bcm2708_usb_resources_no_fiq_fix);
  3503. + }
  3504. + bcm_register_device(&bcm2708_usb_device);
  3505. + bcm_register_device(&bcm2708_uart1_device);
  3506. + bcm_register_device(&bcm2708_powerman_device);
  3507. +
  3508. +#ifdef CONFIG_MMC_SDHCI_BCM2708
  3509. + bcm_register_device(&bcm2708_emmc_device);
  3510. +#endif
  3511. + bcm2708_init_led();
  3512. + for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
  3513. + bcm_register_device(&bcm2708_alsa_devices[i]);
  3514. +
  3515. + bcm_register_device(&bcm2708_spi_device);
  3516. + bcm_register_device(&bcm2708_bsc0_device);
  3517. + bcm_register_device(&bcm2708_bsc1_device);
  3518. +
  3519. + bcm_register_device(&bcm2835_hwmon_device);
  3520. + bcm_register_device(&bcm2835_thermal_device);
  3521. +
  3522. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  3523. + bcm_register_device(&bcm2708_i2s_device);
  3524. +#endif
  3525. +
  3526. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  3527. + bcm_register_device(&snd_hifiberry_dac_device);
  3528. + bcm_register_device(&snd_pcm5102a_codec_device);
  3529. +#endif
  3530. +
  3531. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  3532. + bcm_register_device(&snd_hifiberry_digi_device);
  3533. + i2c_register_board_info(1, snd_wm8804_i2c_devices, ARRAY_SIZE(snd_wm8804_i2c_devices));
  3534. +#endif
  3535. +
  3536. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  3537. + bcm_register_device(&snd_rpi_dac_device);
  3538. + bcm_register_device(&snd_pcm1794a_codec_device);
  3539. +#endif
  3540. +
  3541. + for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  3542. + struct amba_device *d = amba_devs[i];
  3543. + amba_device_register(d, &iomem_resource);
  3544. + }
  3545. + system_rev = boardrev;
  3546. + system_serial_low = serial;
  3547. +
  3548. +#ifdef CONFIG_BCM2708_SPIDEV
  3549. + spi_register_board_info(bcm2708_spi_devices,
  3550. + ARRAY_SIZE(bcm2708_spi_devices));
  3551. +#endif
  3552. +}
  3553. +
  3554. +static void timer_set_mode(enum clock_event_mode mode,
  3555. + struct clock_event_device *clk)
  3556. +{
  3557. + switch (mode) {
  3558. + case CLOCK_EVT_MODE_ONESHOT: /* Leave the timer disabled, .set_next_event will enable it */
  3559. + case CLOCK_EVT_MODE_SHUTDOWN:
  3560. + break;
  3561. + case CLOCK_EVT_MODE_PERIODIC:
  3562. +
  3563. + case CLOCK_EVT_MODE_UNUSED:
  3564. + case CLOCK_EVT_MODE_RESUME:
  3565. +
  3566. + default:
  3567. + printk(KERN_ERR "timer_set_mode: unhandled mode:%d\n",
  3568. + (int)mode);
  3569. + break;
  3570. + }
  3571. +
  3572. +}
  3573. +
  3574. +static int timer_set_next_event(unsigned long cycles,
  3575. + struct clock_event_device *unused)
  3576. +{
  3577. + unsigned long stc;
  3578. +
  3579. + stc = readl(__io_address(ST_BASE + 0x04));
  3580. + writel(stc + cycles, __io_address(ST_BASE + 0x18)); /* stc3 */
  3581. + return 0;
  3582. +}
  3583. +
  3584. +static struct clock_event_device timer0_clockevent = {
  3585. + .name = "timer0",
  3586. + .shift = 32,
  3587. + .features = CLOCK_EVT_FEAT_ONESHOT,
  3588. + .set_mode = timer_set_mode,
  3589. + .set_next_event = timer_set_next_event,
  3590. +};
  3591. +
  3592. +/*
  3593. + * IRQ handler for the timer
  3594. + */
  3595. +static irqreturn_t bcm2708_timer_interrupt(int irq, void *dev_id)
  3596. +{
  3597. + struct clock_event_device *evt = &timer0_clockevent;
  3598. +
  3599. + writel(1 << 3, __io_address(ST_BASE + 0x00)); /* stcs clear timer int */
  3600. +
  3601. + evt->event_handler(evt);
  3602. +
  3603. + return IRQ_HANDLED;
  3604. +}
  3605. +
  3606. +static struct irqaction bcm2708_timer_irq = {
  3607. + .name = "BCM2708 Timer Tick",
  3608. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  3609. + .handler = bcm2708_timer_interrupt,
  3610. +};
  3611. +
  3612. +/*
  3613. + * Set up timer interrupt, and return the current time in seconds.
  3614. + */
  3615. +
  3616. +static struct delay_timer bcm2708_delay_timer = {
  3617. + .read_current_timer = bcm2708_read_current_timer,
  3618. + .freq = STC_FREQ_HZ,
  3619. +};
  3620. +
  3621. +static void __init bcm2708_timer_init(void)
  3622. +{
  3623. + /* init high res timer */
  3624. + bcm2708_clocksource_init();
  3625. +
  3626. + /*
  3627. + * Initialise to a known state (all timers off)
  3628. + */
  3629. + writel(0, __io_address(ARM_T_CONTROL));
  3630. + /*
  3631. + * Make irqs happen for the system timer
  3632. + */
  3633. + setup_irq(IRQ_TIMER3, &bcm2708_timer_irq);
  3634. +
  3635. + setup_sched_clock(bcm2708_read_sched_clock, 32, STC_FREQ_HZ);
  3636. +
  3637. + timer0_clockevent.mult =
  3638. + div_sc(STC_FREQ_HZ, NSEC_PER_SEC, timer0_clockevent.shift);
  3639. + timer0_clockevent.max_delta_ns =
  3640. + clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  3641. + timer0_clockevent.min_delta_ns =
  3642. + clockevent_delta2ns(0xf, &timer0_clockevent);
  3643. +
  3644. + timer0_clockevent.cpumask = cpumask_of(0);
  3645. + clockevents_register_device(&timer0_clockevent);
  3646. +
  3647. + register_current_timer_delay(&bcm2708_delay_timer);
  3648. +}
  3649. +
  3650. +#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
  3651. +#include <linux/leds.h>
  3652. +
  3653. +static struct gpio_led bcm2708_leds[] = {
  3654. + [0] = {
  3655. + .gpio = 16,
  3656. + .name = "led0",
  3657. + .default_trigger = "mmc0",
  3658. + .active_low = 1,
  3659. + },
  3660. +};
  3661. +
  3662. +static struct gpio_led_platform_data bcm2708_led_pdata = {
  3663. + .num_leds = ARRAY_SIZE(bcm2708_leds),
  3664. + .leds = bcm2708_leds,
  3665. +};
  3666. +
  3667. +static struct platform_device bcm2708_led_device = {
  3668. + .name = "leds-gpio",
  3669. + .id = -1,
  3670. + .dev = {
  3671. + .platform_data = &bcm2708_led_pdata,
  3672. + },
  3673. +};
  3674. +
  3675. +static void __init bcm2708_init_led(void)
  3676. +{
  3677. + bcm2708_leds[0].gpio = disk_led_gpio;
  3678. + bcm2708_leds[0].active_low = disk_led_active_low;
  3679. + platform_device_register(&bcm2708_led_device);
  3680. +}
  3681. +#else
  3682. +static inline void bcm2708_init_led(void)
  3683. +{
  3684. +}
  3685. +#endif
  3686. +
  3687. +void __init bcm2708_init_early(void)
  3688. +{
  3689. + /*
  3690. + * Some devices allocate their coherent buffers from atomic
  3691. + * context. Increase size of atomic coherent pool to make sure such
  3692. + * the allocations won't fail.
  3693. + */
  3694. + init_dma_coherent_pool_size(SZ_4M);
  3695. +}
  3696. +
  3697. +static void __init board_reserve(void)
  3698. +{
  3699. +#if defined(CONFIG_BCM_VC_CMA)
  3700. + vc_cma_reserve();
  3701. +#endif
  3702. +}
  3703. +
  3704. +MACHINE_START(BCM2708, "BCM2708")
  3705. + /* Maintainer: Broadcom Europe Ltd. */
  3706. + .map_io = bcm2708_map_io,
  3707. + .init_irq = bcm2708_init_irq,
  3708. + .init_time = bcm2708_timer_init,
  3709. + .init_machine = bcm2708_init,
  3710. + .init_early = bcm2708_init_early,
  3711. + .reserve = board_reserve,
  3712. + .restart = bcm2708_restart,
  3713. +MACHINE_END
  3714. +
  3715. +module_param(boardrev, uint, 0644);
  3716. +module_param(serial, uint, 0644);
  3717. +module_param(uart_clock, uint, 0644);
  3718. +module_param(disk_led_gpio, uint, 0644);
  3719. +module_param(disk_led_active_low, uint, 0644);
  3720. +module_param(reboot_part, uint, 0644);
  3721. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/bcm2708_gpio.c linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708_gpio.c
  3722. --- linux-3.12.13/arch/arm/mach-bcm2708/bcm2708_gpio.c 1970-01-01 01:00:00.000000000 +0100
  3723. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708_gpio.c 2014-03-11 17:51:02.000000000 +0100
  3724. @@ -0,0 +1,339 @@
  3725. +/*
  3726. + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
  3727. + *
  3728. + * Copyright (C) 2010 Broadcom
  3729. + *
  3730. + * This program is free software; you can redistribute it and/or modify
  3731. + * it under the terms of the GNU General Public License version 2 as
  3732. + * published by the Free Software Foundation.
  3733. + *
  3734. + */
  3735. +
  3736. +#include <linux/spinlock.h>
  3737. +#include <linux/module.h>
  3738. +#include <linux/list.h>
  3739. +#include <linux/io.h>
  3740. +#include <linux/irq.h>
  3741. +#include <linux/interrupt.h>
  3742. +#include <linux/slab.h>
  3743. +#include <mach/gpio.h>
  3744. +#include <linux/gpio.h>
  3745. +#include <linux/platform_device.h>
  3746. +#include <mach/platform.h>
  3747. +
  3748. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  3749. +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
  3750. +#define BCM_GPIO_USE_IRQ 1
  3751. +
  3752. +#define GPIOFSEL(x) (0x00+(x)*4)
  3753. +#define GPIOSET(x) (0x1c+(x)*4)
  3754. +#define GPIOCLR(x) (0x28+(x)*4)
  3755. +#define GPIOLEV(x) (0x34+(x)*4)
  3756. +#define GPIOEDS(x) (0x40+(x)*4)
  3757. +#define GPIOREN(x) (0x4c+(x)*4)
  3758. +#define GPIOFEN(x) (0x58+(x)*4)
  3759. +#define GPIOHEN(x) (0x64+(x)*4)
  3760. +#define GPIOLEN(x) (0x70+(x)*4)
  3761. +#define GPIOAREN(x) (0x7c+(x)*4)
  3762. +#define GPIOAFEN(x) (0x88+(x)*4)
  3763. +#define GPIOUD(x) (0x94+(x)*4)
  3764. +#define GPIOUDCLK(x) (0x98+(x)*4)
  3765. +
  3766. +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
  3767. + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
  3768. + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
  3769. + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
  3770. +};
  3771. +
  3772. + /* Each of the two spinlocks protects a different set of hardware
  3773. + * regiters and data structurs. This decouples the code of the IRQ from
  3774. + * the GPIO code. This also makes the case of a GPIO routine call from
  3775. + * the IRQ code simpler.
  3776. + */
  3777. +static DEFINE_SPINLOCK(lock); /* GPIO registers */
  3778. +
  3779. +struct bcm2708_gpio {
  3780. + struct list_head list;
  3781. + void __iomem *base;
  3782. + struct gpio_chip gc;
  3783. + unsigned long rising;
  3784. + unsigned long falling;
  3785. +};
  3786. +
  3787. +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
  3788. + int function)
  3789. +{
  3790. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3791. + unsigned long flags;
  3792. + unsigned gpiodir;
  3793. + unsigned gpio_bank = offset / 10;
  3794. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  3795. +
  3796. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
  3797. + if (offset >= ARCH_NR_GPIOS)
  3798. + return -EINVAL;
  3799. +
  3800. + spin_lock_irqsave(&lock, flags);
  3801. +
  3802. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  3803. + gpiodir &= ~(7 << gpio_field_offset);
  3804. + gpiodir |= function << gpio_field_offset;
  3805. + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
  3806. + spin_unlock_irqrestore(&lock, flags);
  3807. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  3808. +
  3809. + return 0;
  3810. +}
  3811. +
  3812. +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
  3813. +{
  3814. + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
  3815. +}
  3816. +
  3817. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
  3818. +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
  3819. + int value)
  3820. +{
  3821. + int ret;
  3822. + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
  3823. + if (ret >= 0)
  3824. + bcm2708_gpio_set(gc, offset, value);
  3825. + return ret;
  3826. +}
  3827. +
  3828. +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
  3829. +{
  3830. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3831. + unsigned gpio_bank = offset / 32;
  3832. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  3833. + unsigned lev;
  3834. +
  3835. + if (offset >= ARCH_NR_GPIOS)
  3836. + return 0;
  3837. + lev = readl(gpio->base + GPIOLEV(gpio_bank));
  3838. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
  3839. + return 0x1 & (lev >> gpio_field_offset);
  3840. +}
  3841. +
  3842. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  3843. +{
  3844. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3845. + unsigned gpio_bank = offset / 32;
  3846. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  3847. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
  3848. + if (offset >= ARCH_NR_GPIOS)
  3849. + return;
  3850. + if (value)
  3851. + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
  3852. + else
  3853. + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
  3854. +}
  3855. +
  3856. +/*************************************************************************************************************************
  3857. + * bcm2708 GPIO IRQ
  3858. + */
  3859. +
  3860. +#if BCM_GPIO_USE_IRQ
  3861. +
  3862. +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  3863. +{
  3864. + return gpio_to_irq(gpio);
  3865. +}
  3866. +
  3867. +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
  3868. +{
  3869. + unsigned irq = d->irq;
  3870. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  3871. +
  3872. + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  3873. + return -EINVAL;
  3874. +
  3875. + if (type & IRQ_TYPE_EDGE_RISING) {
  3876. + gpio->rising |= (1 << irq_to_gpio(irq));
  3877. + } else {
  3878. + gpio->rising &= ~(1 << irq_to_gpio(irq));
  3879. + }
  3880. +
  3881. + if (type & IRQ_TYPE_EDGE_FALLING) {
  3882. + gpio->falling |= (1 << irq_to_gpio(irq));
  3883. + } else {
  3884. + gpio->falling &= ~(1 << irq_to_gpio(irq));
  3885. + }
  3886. + return 0;
  3887. +}
  3888. +
  3889. +static void bcm2708_gpio_irq_mask(struct irq_data *d)
  3890. +{
  3891. + unsigned irq = d->irq;
  3892. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  3893. + unsigned gn = irq_to_gpio(irq);
  3894. + unsigned gb = gn / 32;
  3895. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  3896. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  3897. +
  3898. + gn = gn % 32;
  3899. +
  3900. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  3901. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  3902. +}
  3903. +
  3904. +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
  3905. +{
  3906. + unsigned irq = d->irq;
  3907. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  3908. + unsigned gn = irq_to_gpio(irq);
  3909. + unsigned gb = gn / 32;
  3910. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  3911. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  3912. +
  3913. + gn = gn % 32;
  3914. +
  3915. + writel(1 << gn, gpio->base + GPIOEDS(gb));
  3916. +
  3917. + if (gpio->rising & (1 << gn)) {
  3918. + writel(rising | (1 << gn), gpio->base + GPIOREN(gb));
  3919. + } else {
  3920. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  3921. + }
  3922. +
  3923. + if (gpio->falling & (1 << gn)) {
  3924. + writel(falling | (1 << gn), gpio->base + GPIOFEN(gb));
  3925. + } else {
  3926. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  3927. + }
  3928. +}
  3929. +
  3930. +static struct irq_chip bcm2708_irqchip = {
  3931. + .name = "GPIO",
  3932. + .irq_enable = bcm2708_gpio_irq_unmask,
  3933. + .irq_disable = bcm2708_gpio_irq_mask,
  3934. + .irq_unmask = bcm2708_gpio_irq_unmask,
  3935. + .irq_mask = bcm2708_gpio_irq_mask,
  3936. + .irq_set_type = bcm2708_gpio_irq_set_type,
  3937. +};
  3938. +
  3939. +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
  3940. +{
  3941. + unsigned long edsr;
  3942. + unsigned bank;
  3943. + int i;
  3944. + unsigned gpio;
  3945. + for (bank = 0; bank <= 1; bank++) {
  3946. + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
  3947. + for_each_set_bit(i, &edsr, 32) {
  3948. + gpio = i + bank * 32;
  3949. + generic_handle_irq(gpio_to_irq(gpio));
  3950. + }
  3951. + writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank));
  3952. + }
  3953. + return IRQ_HANDLED;
  3954. +}
  3955. +
  3956. +static struct irqaction bcm2708_gpio_irq = {
  3957. + .name = "BCM2708 GPIO catchall handler",
  3958. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  3959. + .handler = bcm2708_gpio_interrupt,
  3960. +};
  3961. +
  3962. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  3963. +{
  3964. + unsigned irq;
  3965. +
  3966. + ucb->gc.to_irq = bcm2708_gpio_to_irq;
  3967. +
  3968. + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
  3969. + irq_set_chip_data(irq, ucb);
  3970. + irq_set_chip(irq, &bcm2708_irqchip);
  3971. + set_irq_flags(irq, IRQF_VALID);
  3972. + }
  3973. + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
  3974. +}
  3975. +
  3976. +#else
  3977. +
  3978. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  3979. +{
  3980. +}
  3981. +
  3982. +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
  3983. +
  3984. +static int bcm2708_gpio_probe(struct platform_device *dev)
  3985. +{
  3986. + struct bcm2708_gpio *ucb;
  3987. + struct resource *res;
  3988. + int err = 0;
  3989. +
  3990. + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
  3991. +
  3992. + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
  3993. + if (NULL == ucb) {
  3994. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  3995. + "mailbox memory\n");
  3996. + err = -ENOMEM;
  3997. + goto err;
  3998. + }
  3999. +
  4000. + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  4001. +
  4002. + platform_set_drvdata(dev, ucb);
  4003. + ucb->base = __io_address(GPIO_BASE);
  4004. +
  4005. + ucb->gc.label = "bcm2708_gpio";
  4006. + ucb->gc.base = 0;
  4007. + ucb->gc.ngpio = ARCH_NR_GPIOS;
  4008. + ucb->gc.owner = THIS_MODULE;
  4009. +
  4010. + ucb->gc.direction_input = bcm2708_gpio_dir_in;
  4011. + ucb->gc.direction_output = bcm2708_gpio_dir_out;
  4012. + ucb->gc.get = bcm2708_gpio_get;
  4013. + ucb->gc.set = bcm2708_gpio_set;
  4014. + ucb->gc.can_sleep = 0;
  4015. +
  4016. + bcm2708_gpio_irq_init(ucb);
  4017. +
  4018. + err = gpiochip_add(&ucb->gc);
  4019. + if (err)
  4020. + goto err;
  4021. +
  4022. +err:
  4023. + return err;
  4024. +
  4025. +}
  4026. +
  4027. +static int bcm2708_gpio_remove(struct platform_device *dev)
  4028. +{
  4029. + int err = 0;
  4030. + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
  4031. +
  4032. + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
  4033. +
  4034. + err = gpiochip_remove(&ucb->gc);
  4035. +
  4036. + platform_set_drvdata(dev, NULL);
  4037. + kfree(ucb);
  4038. +
  4039. + return err;
  4040. +}
  4041. +
  4042. +static struct platform_driver bcm2708_gpio_driver = {
  4043. + .probe = bcm2708_gpio_probe,
  4044. + .remove = bcm2708_gpio_remove,
  4045. + .driver = {
  4046. + .name = "bcm2708_gpio"},
  4047. +};
  4048. +
  4049. +static int __init bcm2708_gpio_init(void)
  4050. +{
  4051. + return platform_driver_register(&bcm2708_gpio_driver);
  4052. +}
  4053. +
  4054. +static void __exit bcm2708_gpio_exit(void)
  4055. +{
  4056. + platform_driver_unregister(&bcm2708_gpio_driver);
  4057. +}
  4058. +
  4059. +module_init(bcm2708_gpio_init);
  4060. +module_exit(bcm2708_gpio_exit);
  4061. +
  4062. +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
  4063. +MODULE_LICENSE("GPL");
  4064. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/bcm2708.h linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.h
  4065. --- linux-3.12.13/arch/arm/mach-bcm2708/bcm2708.h 1970-01-01 01:00:00.000000000 +0100
  4066. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.h 2014-03-11 17:31:43.000000000 +0100
  4067. @@ -0,0 +1,51 @@
  4068. +/*
  4069. + * linux/arch/arm/mach-bcm2708/bcm2708.h
  4070. + *
  4071. + * BCM2708 machine support header
  4072. + *
  4073. + * Copyright (C) 2010 Broadcom
  4074. + *
  4075. + * This program is free software; you can redistribute it and/or modify
  4076. + * it under the terms of the GNU General Public License as published by
  4077. + * the Free Software Foundation; either version 2 of the License, or
  4078. + * (at your option) any later version.
  4079. + *
  4080. + * This program is distributed in the hope that it will be useful,
  4081. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4082. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4083. + * GNU General Public License for more details.
  4084. + *
  4085. + * You should have received a copy of the GNU General Public License
  4086. + * along with this program; if not, write to the Free Software
  4087. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4088. + */
  4089. +
  4090. +#ifndef __BCM2708_BCM2708_H
  4091. +#define __BCM2708_BCM2708_H
  4092. +
  4093. +#include <linux/amba/bus.h>
  4094. +
  4095. +extern void __init bcm2708_init(void);
  4096. +extern void __init bcm2708_init_irq(void);
  4097. +extern void __init bcm2708_map_io(void);
  4098. +extern struct sys_timer bcm2708_timer;
  4099. +extern unsigned int mmc_status(struct device *dev);
  4100. +
  4101. +#define AMBA_DEVICE(name, busid, base, plat) \
  4102. +static struct amba_device name##_device = { \
  4103. + .dev = { \
  4104. + .coherent_dma_mask = ~0, \
  4105. + .init_name = busid, \
  4106. + .platform_data = plat, \
  4107. + }, \
  4108. + .res = { \
  4109. + .start = base##_BASE, \
  4110. + .end = (base##_BASE) + SZ_4K - 1,\
  4111. + .flags = IORESOURCE_MEM, \
  4112. + }, \
  4113. + .dma_mask = ~0, \
  4114. + .irq = base##_IRQ, \
  4115. + /* .dma = base##_DMA,*/ \
  4116. +}
  4117. +
  4118. +#endif
  4119. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/clock.c linux-raspberry-pi/arch/arm/mach-bcm2708/clock.c
  4120. --- linux-3.12.13/arch/arm/mach-bcm2708/clock.c 1970-01-01 01:00:00.000000000 +0100
  4121. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/clock.c 2014-03-11 17:31:43.000000000 +0100
  4122. @@ -0,0 +1,61 @@
  4123. +/*
  4124. + * linux/arch/arm/mach-bcm2708/clock.c
  4125. + *
  4126. + * Copyright (C) 2010 Broadcom
  4127. + *
  4128. + * This program is free software; you can redistribute it and/or modify
  4129. + * it under the terms of the GNU General Public License as published by
  4130. + * the Free Software Foundation; either version 2 of the License, or
  4131. + * (at your option) any later version.
  4132. + *
  4133. + * This program is distributed in the hope that it will be useful,
  4134. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4135. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4136. + * GNU General Public License for more details.
  4137. + *
  4138. + * You should have received a copy of the GNU General Public License
  4139. + * along with this program; if not, write to the Free Software
  4140. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4141. + */
  4142. +#include <linux/module.h>
  4143. +#include <linux/kernel.h>
  4144. +#include <linux/device.h>
  4145. +#include <linux/list.h>
  4146. +#include <linux/errno.h>
  4147. +#include <linux/err.h>
  4148. +#include <linux/string.h>
  4149. +#include <linux/clk.h>
  4150. +#include <linux/mutex.h>
  4151. +
  4152. +#include <asm/clkdev.h>
  4153. +
  4154. +#include "clock.h"
  4155. +
  4156. +int clk_enable(struct clk *clk)
  4157. +{
  4158. + return 0;
  4159. +}
  4160. +EXPORT_SYMBOL(clk_enable);
  4161. +
  4162. +void clk_disable(struct clk *clk)
  4163. +{
  4164. +}
  4165. +EXPORT_SYMBOL(clk_disable);
  4166. +
  4167. +unsigned long clk_get_rate(struct clk *clk)
  4168. +{
  4169. + return clk->rate;
  4170. +}
  4171. +EXPORT_SYMBOL(clk_get_rate);
  4172. +
  4173. +long clk_round_rate(struct clk *clk, unsigned long rate)
  4174. +{
  4175. + return clk->rate;
  4176. +}
  4177. +EXPORT_SYMBOL(clk_round_rate);
  4178. +
  4179. +int clk_set_rate(struct clk *clk, unsigned long rate)
  4180. +{
  4181. + return -EIO;
  4182. +}
  4183. +EXPORT_SYMBOL(clk_set_rate);
  4184. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/clock.h linux-raspberry-pi/arch/arm/mach-bcm2708/clock.h
  4185. --- linux-3.12.13/arch/arm/mach-bcm2708/clock.h 1970-01-01 01:00:00.000000000 +0100
  4186. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/clock.h 2014-03-11 17:31:43.000000000 +0100
  4187. @@ -0,0 +1,24 @@
  4188. +/*
  4189. + * linux/arch/arm/mach-bcm2708/clock.h
  4190. + *
  4191. + * Copyright (C) 2010 Broadcom
  4192. + *
  4193. + * This program is free software; you can redistribute it and/or modify
  4194. + * it under the terms of the GNU General Public License as published by
  4195. + * the Free Software Foundation; either version 2 of the License, or
  4196. + * (at your option) any later version.
  4197. + *
  4198. + * This program is distributed in the hope that it will be useful,
  4199. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4200. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4201. + * GNU General Public License for more details.
  4202. + *
  4203. + * You should have received a copy of the GNU General Public License
  4204. + * along with this program; if not, write to the Free Software
  4205. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4206. + */
  4207. +struct module;
  4208. +
  4209. +struct clk {
  4210. + unsigned long rate;
  4211. +};
  4212. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/dma.c linux-raspberry-pi/arch/arm/mach-bcm2708/dma.c
  4213. --- linux-3.12.13/arch/arm/mach-bcm2708/dma.c 1970-01-01 01:00:00.000000000 +0100
  4214. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/dma.c 2014-03-11 17:31:43.000000000 +0100
  4215. @@ -0,0 +1,407 @@
  4216. +/*
  4217. + * linux/arch/arm/mach-bcm2708/dma.c
  4218. + *
  4219. + * Copyright (C) 2010 Broadcom
  4220. + *
  4221. + * This program is free software; you can redistribute it and/or modify
  4222. + * it under the terms of the GNU General Public License version 2 as
  4223. + * published by the Free Software Foundation.
  4224. + */
  4225. +
  4226. +#include <linux/slab.h>
  4227. +#include <linux/device.h>
  4228. +#include <linux/platform_device.h>
  4229. +#include <linux/module.h>
  4230. +#include <linux/scatterlist.h>
  4231. +
  4232. +#include <mach/dma.h>
  4233. +#include <mach/irqs.h>
  4234. +
  4235. +/*****************************************************************************\
  4236. + * *
  4237. + * Configuration *
  4238. + * *
  4239. +\*****************************************************************************/
  4240. +
  4241. +#define CACHE_LINE_MASK 31
  4242. +#define DRIVER_NAME BCM_DMAMAN_DRIVER_NAME
  4243. +#define DEFAULT_DMACHAN_BITMAP 0x10 /* channel 4 only */
  4244. +
  4245. +/* valid only for channels 0 - 14, 15 has its own base address */
  4246. +#define BCM2708_DMA_CHAN(n) ((n)<<8) /* base address */
  4247. +#define BCM2708_DMA_CHANIO(dma_base, n) \
  4248. + ((void __iomem *)((char *)(dma_base)+BCM2708_DMA_CHAN(n)))
  4249. +
  4250. +
  4251. +/*****************************************************************************\
  4252. + * *
  4253. + * DMA Auxilliary Functions *
  4254. + * *
  4255. +\*****************************************************************************/
  4256. +
  4257. +/* A DMA buffer on an arbitrary boundary may separate a cache line into a
  4258. + section inside the DMA buffer and another section outside it.
  4259. + Even if we flush DMA buffers from the cache there is always the chance that
  4260. + during a DMA someone will access the part of a cache line that is outside
  4261. + the DMA buffer - which will then bring in unwelcome data.
  4262. + Without being able to dictate our own buffer pools we must insist that
  4263. + DMA buffers consist of a whole number of cache lines.
  4264. +*/
  4265. +
  4266. +extern int
  4267. +bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len)
  4268. +{
  4269. + int i;
  4270. +
  4271. + for (i = 0; i < sg_len; i++) {
  4272. + if (sg_ptr[i].offset & CACHE_LINE_MASK ||
  4273. + sg_ptr[i].length & CACHE_LINE_MASK)
  4274. + return 0;
  4275. + }
  4276. +
  4277. + return 1;
  4278. +}
  4279. +EXPORT_SYMBOL_GPL(bcm_sg_suitable_for_dma);
  4280. +
  4281. +extern void
  4282. +bcm_dma_start(void __iomem *dma_chan_base, dma_addr_t control_block)
  4283. +{
  4284. + dsb(); /* ARM data synchronization (push) operation */
  4285. +
  4286. + writel(control_block, dma_chan_base + BCM2708_DMA_ADDR);
  4287. + writel(BCM2708_DMA_ACTIVE, dma_chan_base + BCM2708_DMA_CS);
  4288. +}
  4289. +
  4290. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base)
  4291. +{
  4292. + dsb();
  4293. +
  4294. + /* ugly busy wait only option for now */
  4295. + while (readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE)
  4296. + cpu_relax();
  4297. +}
  4298. +
  4299. +EXPORT_SYMBOL_GPL(bcm_dma_start);
  4300. +
  4301. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base)
  4302. +{
  4303. + dsb();
  4304. +
  4305. + return readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE;
  4306. +}
  4307. +EXPORT_SYMBOL_GPL(bcm_dma_is_busy);
  4308. +
  4309. +/* Complete an ongoing DMA (assuming its results are to be ignored)
  4310. + Does nothing if there is no DMA in progress.
  4311. + This routine waits for the current AXI transfer to complete before
  4312. + terminating the current DMA. If the current transfer is hung on a DREQ used
  4313. + by an uncooperative peripheral the AXI transfer may never complete. In this
  4314. + case the routine times out and return a non-zero error code.
  4315. + Use of this routine doesn't guarantee that the ongoing or aborted DMA
  4316. + does not produce an interrupt.
  4317. +*/
  4318. +extern int
  4319. +bcm_dma_abort(void __iomem *dma_chan_base)
  4320. +{
  4321. + unsigned long int cs;
  4322. + int rc = 0;
  4323. +
  4324. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  4325. +
  4326. + if (BCM2708_DMA_ACTIVE & cs) {
  4327. + long int timeout = 10000;
  4328. +
  4329. + /* write 0 to the active bit - pause the DMA */
  4330. + writel(0, dma_chan_base + BCM2708_DMA_CS);
  4331. +
  4332. + /* wait for any current AXI transfer to complete */
  4333. + while (0 != (cs & BCM2708_DMA_ISPAUSED) && --timeout >= 0)
  4334. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  4335. +
  4336. + if (0 != (cs & BCM2708_DMA_ISPAUSED)) {
  4337. + /* we'll un-pause when we set of our next DMA */
  4338. + rc = -ETIMEDOUT;
  4339. +
  4340. + } else if (BCM2708_DMA_ACTIVE & cs) {
  4341. + /* terminate the control block chain */
  4342. + writel(0, dma_chan_base + BCM2708_DMA_NEXTCB);
  4343. +
  4344. + /* abort the whole DMA */
  4345. + writel(BCM2708_DMA_ABORT | BCM2708_DMA_ACTIVE,
  4346. + dma_chan_base + BCM2708_DMA_CS);
  4347. + }
  4348. + }
  4349. +
  4350. + return rc;
  4351. +}
  4352. +EXPORT_SYMBOL_GPL(bcm_dma_abort);
  4353. +
  4354. +
  4355. +/***************************************************************************** \
  4356. + * *
  4357. + * DMA Manager Device Methods *
  4358. + * *
  4359. +\*****************************************************************************/
  4360. +
  4361. +struct vc_dmaman {
  4362. + void __iomem *dma_base;
  4363. + u32 chan_available; /* bitmap of available channels */
  4364. + u32 has_feature[BCM_DMA_FEATURE_COUNT]; /* bitmap of feature presence */
  4365. +};
  4366. +
  4367. +static void vc_dmaman_init(struct vc_dmaman *dmaman, void __iomem *dma_base,
  4368. + u32 chans_available)
  4369. +{
  4370. + dmaman->dma_base = dma_base;
  4371. + dmaman->chan_available = chans_available;
  4372. + dmaman->has_feature[BCM_DMA_FEATURE_FAST_ORD] = 0x0c; /* chans 2 & 3 */
  4373. + dmaman->has_feature[BCM_DMA_FEATURE_BULK_ORD] = 0x01; /* chan 0 */
  4374. +}
  4375. +
  4376. +static int vc_dmaman_chan_alloc(struct vc_dmaman *dmaman,
  4377. + unsigned preferred_feature_set)
  4378. +{
  4379. + u32 chans;
  4380. + int feature;
  4381. +
  4382. + chans = dmaman->chan_available;
  4383. + for (feature = 0; feature < BCM_DMA_FEATURE_COUNT; feature++)
  4384. + /* select the subset of available channels with the desired
  4385. + feature so long as some of the candidate channels have that
  4386. + feature */
  4387. + if ((preferred_feature_set & (1 << feature)) &&
  4388. + (chans & dmaman->has_feature[feature]))
  4389. + chans &= dmaman->has_feature[feature];
  4390. +
  4391. + if (chans) {
  4392. + int chan = 0;
  4393. + /* return the ordinal of the first channel in the bitmap */
  4394. + while (chans != 0 && (chans & 1) == 0) {
  4395. + chans >>= 1;
  4396. + chan++;
  4397. + }
  4398. + /* claim the channel */
  4399. + dmaman->chan_available &= ~(1 << chan);
  4400. + return chan;
  4401. + } else
  4402. + return -ENOMEM;
  4403. +}
  4404. +
  4405. +static int vc_dmaman_chan_free(struct vc_dmaman *dmaman, int chan)
  4406. +{
  4407. + if (chan < 0)
  4408. + return -EINVAL;
  4409. + else if ((1 << chan) & dmaman->chan_available)
  4410. + return -EIDRM;
  4411. + else {
  4412. + dmaman->chan_available |= (1 << chan);
  4413. + return 0;
  4414. + }
  4415. +}
  4416. +
  4417. +/*****************************************************************************\
  4418. + * *
  4419. + * DMA IRQs *
  4420. + * *
  4421. +\*****************************************************************************/
  4422. +
  4423. +static unsigned char bcm_dma_irqs[] = {
  4424. + IRQ_DMA0,
  4425. + IRQ_DMA1,
  4426. + IRQ_DMA2,
  4427. + IRQ_DMA3,
  4428. + IRQ_DMA4,
  4429. + IRQ_DMA5,
  4430. + IRQ_DMA6,
  4431. + IRQ_DMA7,
  4432. + IRQ_DMA8,
  4433. + IRQ_DMA9,
  4434. + IRQ_DMA10,
  4435. + IRQ_DMA11,
  4436. + IRQ_DMA12
  4437. +};
  4438. +
  4439. +
  4440. +/***************************************************************************** \
  4441. + * *
  4442. + * DMA Manager Monitor *
  4443. + * *
  4444. +\*****************************************************************************/
  4445. +
  4446. +static struct device *dmaman_dev; /* we assume there's only one! */
  4447. +
  4448. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  4449. + void __iomem **out_dma_base, int *out_dma_irq)
  4450. +{
  4451. + if (!dmaman_dev)
  4452. + return -ENODEV;
  4453. + else {
  4454. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  4455. + int rc;
  4456. +
  4457. + device_lock(dmaman_dev);
  4458. + rc = vc_dmaman_chan_alloc(dmaman, preferred_feature_set);
  4459. + if (rc >= 0) {
  4460. + *out_dma_base = BCM2708_DMA_CHANIO(dmaman->dma_base,
  4461. + rc);
  4462. + *out_dma_irq = bcm_dma_irqs[rc];
  4463. + }
  4464. + device_unlock(dmaman_dev);
  4465. +
  4466. + return rc;
  4467. + }
  4468. +}
  4469. +EXPORT_SYMBOL_GPL(bcm_dma_chan_alloc);
  4470. +
  4471. +extern int bcm_dma_chan_free(int channel)
  4472. +{
  4473. + if (dmaman_dev) {
  4474. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  4475. + int rc;
  4476. +
  4477. + device_lock(dmaman_dev);
  4478. + rc = vc_dmaman_chan_free(dmaman, channel);
  4479. + device_unlock(dmaman_dev);
  4480. +
  4481. + return rc;
  4482. + } else
  4483. + return -ENODEV;
  4484. +}
  4485. +EXPORT_SYMBOL_GPL(bcm_dma_chan_free);
  4486. +
  4487. +static int dev_dmaman_register(const char *dev_name, struct device *dev)
  4488. +{
  4489. + int rc = dmaman_dev ? -EINVAL : 0;
  4490. + dmaman_dev = dev;
  4491. + return rc;
  4492. +}
  4493. +
  4494. +static void dev_dmaman_deregister(const char *dev_name, struct device *dev)
  4495. +{
  4496. + dmaman_dev = NULL;
  4497. +}
  4498. +
  4499. +/*****************************************************************************\
  4500. + * *
  4501. + * DMA Device *
  4502. + * *
  4503. +\*****************************************************************************/
  4504. +
  4505. +static int dmachans = -1; /* module parameter */
  4506. +
  4507. +static int bcm_dmaman_probe(struct platform_device *pdev)
  4508. +{
  4509. + int ret = 0;
  4510. + struct vc_dmaman *dmaman;
  4511. + struct resource *dma_res = NULL;
  4512. + void __iomem *dma_base = NULL;
  4513. + int have_dma_region = 0;
  4514. +
  4515. + dmaman = kzalloc(sizeof(*dmaman), GFP_KERNEL);
  4516. + if (NULL == dmaman) {
  4517. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  4518. + "DMA management memory\n");
  4519. + ret = -ENOMEM;
  4520. + } else {
  4521. +
  4522. + dma_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4523. + if (dma_res == NULL) {
  4524. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  4525. + "resource\n");
  4526. + ret = -ENODEV;
  4527. + } else if (!request_mem_region(dma_res->start,
  4528. + resource_size(dma_res),
  4529. + DRIVER_NAME)) {
  4530. + dev_err(&pdev->dev, "cannot obtain DMA region\n");
  4531. + ret = -EBUSY;
  4532. + } else {
  4533. + have_dma_region = 1;
  4534. + dma_base = ioremap(dma_res->start,
  4535. + resource_size(dma_res));
  4536. + if (!dma_base) {
  4537. + dev_err(&pdev->dev, "cannot map DMA region\n");
  4538. + ret = -ENOMEM;
  4539. + } else {
  4540. + /* use module parameter if one was provided */
  4541. + if (dmachans > 0)
  4542. + vc_dmaman_init(dmaman, dma_base,
  4543. + dmachans);
  4544. + else
  4545. + vc_dmaman_init(dmaman, dma_base,
  4546. + DEFAULT_DMACHAN_BITMAP);
  4547. +
  4548. + platform_set_drvdata(pdev, dmaman);
  4549. + dev_dmaman_register(DRIVER_NAME, &pdev->dev);
  4550. +
  4551. + printk(KERN_INFO DRIVER_NAME ": DMA manager "
  4552. + "at %p\n", dma_base);
  4553. + }
  4554. + }
  4555. + }
  4556. + if (ret != 0) {
  4557. + if (dma_base)
  4558. + iounmap(dma_base);
  4559. + if (dma_res && have_dma_region)
  4560. + release_mem_region(dma_res->start,
  4561. + resource_size(dma_res));
  4562. + if (dmaman)
  4563. + kfree(dmaman);
  4564. + }
  4565. + return ret;
  4566. +}
  4567. +
  4568. +static int bcm_dmaman_remove(struct platform_device *pdev)
  4569. +{
  4570. + struct vc_dmaman *dmaman = platform_get_drvdata(pdev);
  4571. +
  4572. + platform_set_drvdata(pdev, NULL);
  4573. + dev_dmaman_deregister(DRIVER_NAME, &pdev->dev);
  4574. + kfree(dmaman);
  4575. +
  4576. + return 0;
  4577. +}
  4578. +
  4579. +static struct platform_driver bcm_dmaman_driver = {
  4580. + .probe = bcm_dmaman_probe,
  4581. + .remove = bcm_dmaman_remove,
  4582. +
  4583. + .driver = {
  4584. + .name = DRIVER_NAME,
  4585. + .owner = THIS_MODULE,
  4586. + },
  4587. +};
  4588. +
  4589. +/*****************************************************************************\
  4590. + * *
  4591. + * Driver init/exit *
  4592. + * *
  4593. +\*****************************************************************************/
  4594. +
  4595. +static int __init bcm_dmaman_drv_init(void)
  4596. +{
  4597. + int ret;
  4598. +
  4599. + ret = platform_driver_register(&bcm_dmaman_driver);
  4600. + if (ret != 0) {
  4601. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  4602. + "on platform\n");
  4603. + }
  4604. +
  4605. + return ret;
  4606. +}
  4607. +
  4608. +static void __exit bcm_dmaman_drv_exit(void)
  4609. +{
  4610. + platform_driver_unregister(&bcm_dmaman_driver);
  4611. +}
  4612. +
  4613. +module_init(bcm_dmaman_drv_init);
  4614. +module_exit(bcm_dmaman_drv_exit);
  4615. +
  4616. +module_param(dmachans, int, 0644);
  4617. +
  4618. +MODULE_AUTHOR("Gray Girling <grayg@broadcom.com>");
  4619. +MODULE_DESCRIPTION("DMA channel manager driver");
  4620. +MODULE_LICENSE("GPL");
  4621. +
  4622. +MODULE_PARM_DESC(dmachans, "Bitmap of DMA channels available to the ARM");
  4623. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/dmaer.c linux-raspberry-pi/arch/arm/mach-bcm2708/dmaer.c
  4624. --- linux-3.12.13/arch/arm/mach-bcm2708/dmaer.c 1970-01-01 01:00:00.000000000 +0100
  4625. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/dmaer.c 2014-03-11 17:51:02.000000000 +0100
  4626. @@ -0,0 +1,886 @@
  4627. +#include <linux/init.h>
  4628. +#include <linux/sched.h>
  4629. +#include <linux/module.h>
  4630. +#include <linux/types.h>
  4631. +#include <linux/kdev_t.h>
  4632. +#include <linux/fs.h>
  4633. +#include <linux/cdev.h>
  4634. +#include <linux/mm.h>
  4635. +#include <linux/slab.h>
  4636. +#include <linux/pagemap.h>
  4637. +#include <linux/device.h>
  4638. +#include <linux/jiffies.h>
  4639. +#include <linux/timex.h>
  4640. +#include <linux/dma-mapping.h>
  4641. +
  4642. +#include <asm/uaccess.h>
  4643. +#include <asm/atomic.h>
  4644. +#include <asm/cacheflush.h>
  4645. +#include <asm/io.h>
  4646. +
  4647. +#include <mach/dma.h>
  4648. +#include <mach/vc_support.h>
  4649. +
  4650. +#ifdef ECLIPSE_IGNORE
  4651. +
  4652. +#define __user
  4653. +#define __init
  4654. +#define __exit
  4655. +#define __iomem
  4656. +#define KERN_DEBUG
  4657. +#define KERN_ERR
  4658. +#define KERN_WARNING
  4659. +#define KERN_INFO
  4660. +#define _IOWR(a, b, c) b
  4661. +#define _IOW(a, b, c) b
  4662. +#define _IO(a, b) b
  4663. +
  4664. +#endif
  4665. +
  4666. +//#define inline
  4667. +
  4668. +#define PRINTK(args...) printk(args)
  4669. +//#define PRINTK_VERBOSE(args...) printk(args)
  4670. +//#define PRINTK(args...)
  4671. +#define PRINTK_VERBOSE(args...)
  4672. +
  4673. +/***** TYPES ****/
  4674. +#define PAGES_PER_LIST 500
  4675. +struct PageList
  4676. +{
  4677. + struct page *m_pPages[PAGES_PER_LIST];
  4678. + unsigned int m_used;
  4679. + struct PageList *m_pNext;
  4680. +};
  4681. +
  4682. +struct VmaPageList
  4683. +{
  4684. + //each vma has a linked list of pages associated with it
  4685. + struct PageList *m_pPageHead;
  4686. + struct PageList *m_pPageTail;
  4687. + unsigned int m_refCount;
  4688. +};
  4689. +
  4690. +struct DmaControlBlock
  4691. +{
  4692. + unsigned int m_transferInfo;
  4693. + void __user *m_pSourceAddr;
  4694. + void __user *m_pDestAddr;
  4695. + unsigned int m_xferLen;
  4696. + unsigned int m_tdStride;
  4697. + struct DmaControlBlock *m_pNext;
  4698. + unsigned int m_blank1, m_blank2;
  4699. +};
  4700. +
  4701. +/***** DEFINES ******/
  4702. +//magic number defining the module
  4703. +#define DMA_MAGIC 0xdd
  4704. +
  4705. +//do user virtual to physical translation of the CB chain
  4706. +#define DMA_PREPARE _IOWR(DMA_MAGIC, 0, struct DmaControlBlock *)
  4707. +
  4708. +//kick the pre-prepared CB chain
  4709. +#define DMA_KICK _IOW(DMA_MAGIC, 1, struct DmaControlBlock *)
  4710. +
  4711. +//prepare it, kick it, wait for it
  4712. +#define DMA_PREPARE_KICK_WAIT _IOWR(DMA_MAGIC, 2, struct DmaControlBlock *)
  4713. +
  4714. +//prepare it, kick it, don't wait for it
  4715. +#define DMA_PREPARE_KICK _IOWR(DMA_MAGIC, 3, struct DmaControlBlock *)
  4716. +
  4717. +//not currently implemented
  4718. +#define DMA_WAIT_ONE _IO(DMA_MAGIC, 4, struct DmaControlBlock *)
  4719. +
  4720. +//wait on all kicked CB chains
  4721. +#define DMA_WAIT_ALL _IO(DMA_MAGIC, 5)
  4722. +
  4723. +//in order to discover the largest AXI burst that should be programmed into the transfer params
  4724. +#define DMA_MAX_BURST _IO(DMA_MAGIC, 6)
  4725. +
  4726. +//set the address range through which the user address is assumed to already by a physical address
  4727. +#define DMA_SET_MIN_PHYS _IOW(DMA_MAGIC, 7, unsigned long)
  4728. +#define DMA_SET_MAX_PHYS _IOW(DMA_MAGIC, 8, unsigned long)
  4729. +#define DMA_SET_PHYS_OFFSET _IOW(DMA_MAGIC, 9, unsigned long)
  4730. +
  4731. +//used to define the size for the CMA-based allocation *in pages*, can only be done once once the file is opened
  4732. +#define DMA_CMA_SET_SIZE _IOW(DMA_MAGIC, 10, unsigned long)
  4733. +
  4734. +//used to get the version of the module, to test for a capability
  4735. +#define DMA_GET_VERSION _IO(DMA_MAGIC, 99)
  4736. +
  4737. +#define VERSION_NUMBER 1
  4738. +
  4739. +#define VIRT_TO_BUS_CACHE_SIZE 8
  4740. +
  4741. +/***** FILE OPS *****/
  4742. +static int Open(struct inode *pInode, struct file *pFile);
  4743. +static int Release(struct inode *pInode, struct file *pFile);
  4744. +static long Ioctl(struct file *pFile, unsigned int cmd, unsigned long arg);
  4745. +static ssize_t Read(struct file *pFile, char __user *pUser, size_t count, loff_t *offp);
  4746. +static int Mmap(struct file *pFile, struct vm_area_struct *pVma);
  4747. +
  4748. +/***** VMA OPS ****/
  4749. +static void VmaOpen4k(struct vm_area_struct *pVma);
  4750. +static void VmaClose4k(struct vm_area_struct *pVma);
  4751. +static int VmaFault4k(struct vm_area_struct *pVma, struct vm_fault *pVmf);
  4752. +
  4753. +/**** DMA PROTOTYPES */
  4754. +static struct DmaControlBlock __user *DmaPrepare(struct DmaControlBlock __user *pUserCB, int *pError);
  4755. +static int DmaKick(struct DmaControlBlock __user *pUserCB);
  4756. +static void DmaWaitAll(void);
  4757. +
  4758. +/**** GENERIC ****/
  4759. +static int __init dmaer_init(void);
  4760. +static void __exit dmaer_exit(void);
  4761. +
  4762. +/*** OPS ***/
  4763. +static struct vm_operations_struct g_vmOps4k = {
  4764. + .open = VmaOpen4k,
  4765. + .close = VmaClose4k,
  4766. + .fault = VmaFault4k,
  4767. +};
  4768. +
  4769. +static struct file_operations g_fOps = {
  4770. + .owner = THIS_MODULE,
  4771. + .llseek = 0,
  4772. + .read = Read,
  4773. + .write = 0,
  4774. + .unlocked_ioctl = Ioctl,
  4775. + .open = Open,
  4776. + .release = Release,
  4777. + .mmap = Mmap,
  4778. +};
  4779. +
  4780. +/***** GLOBALS ******/
  4781. +static dev_t g_majorMinor;
  4782. +
  4783. +//tracking usage of the two files
  4784. +static atomic_t g_oneLock4k = ATOMIC_INIT(1);
  4785. +
  4786. +//device operations
  4787. +static struct cdev g_cDev;
  4788. +static int g_trackedPages = 0;
  4789. +
  4790. +//dma control
  4791. +static unsigned int *g_pDmaChanBase;
  4792. +static int g_dmaIrq;
  4793. +static int g_dmaChan;
  4794. +
  4795. +//cma allocation
  4796. +static int g_cmaHandle;
  4797. +
  4798. +//user virtual to bus address translation acceleration
  4799. +static unsigned long g_virtAddr[VIRT_TO_BUS_CACHE_SIZE];
  4800. +static unsigned long g_busAddr[VIRT_TO_BUS_CACHE_SIZE];
  4801. +static unsigned long g_cbVirtAddr;
  4802. +static unsigned long g_cbBusAddr;
  4803. +static int g_cacheInsertAt;
  4804. +static int g_cacheHit, g_cacheMiss;
  4805. +
  4806. +//off by default
  4807. +static void __user *g_pMinPhys;
  4808. +static void __user *g_pMaxPhys;
  4809. +static unsigned long g_physOffset;
  4810. +
  4811. +/****** CACHE OPERATIONS ********/
  4812. +static inline void FlushAddrCache(void)
  4813. +{
  4814. + int count = 0;
  4815. + for (count = 0; count < VIRT_TO_BUS_CACHE_SIZE; count++)
  4816. + g_virtAddr[count] = 0xffffffff; //never going to match as we always chop the bottom bits anyway
  4817. +
  4818. + g_cbVirtAddr = 0xffffffff;
  4819. +
  4820. + g_cacheInsertAt = 0;
  4821. +}
  4822. +
  4823. +//translate from a user virtual address to a bus address by mapping the page
  4824. +//NB this won't lock a page in memory, so to avoid potential paging issues using kernel logical addresses
  4825. +static inline void __iomem *UserVirtualToBus(void __user *pUser)
  4826. +{
  4827. + int mapped;
  4828. + struct page *pPage;
  4829. + void *phys;
  4830. +
  4831. + //map it (requiring that the pointer points to something that does not hang off the page boundary)
  4832. + mapped = get_user_pages(current, current->mm,
  4833. + (unsigned long)pUser, 1,
  4834. + 1, 0,
  4835. + &pPage,
  4836. + 0);
  4837. +
  4838. + if (mapped <= 0) //error
  4839. + return 0;
  4840. +
  4841. + PRINTK_VERBOSE(KERN_DEBUG "user virtual %p arm phys %p bus %p\n",
  4842. + pUser, page_address(pPage), (void __iomem *)__virt_to_bus(page_address(pPage)));
  4843. +
  4844. + //get the arm physical address
  4845. + phys = page_address(pPage) + offset_in_page(pUser);
  4846. + page_cache_release(pPage);
  4847. +
  4848. + //and now the bus address
  4849. + return (void __iomem *)__virt_to_bus(phys);
  4850. +}
  4851. +
  4852. +static inline void __iomem *UserVirtualToBusViaCbCache(void __user *pUser)
  4853. +{
  4854. + unsigned long virtual_page = (unsigned long)pUser & ~4095;
  4855. + unsigned long page_offset = (unsigned long)pUser & 4095;
  4856. + unsigned long bus_addr;
  4857. +
  4858. + if (g_cbVirtAddr == virtual_page)
  4859. + {
  4860. + bus_addr = g_cbBusAddr + page_offset;
  4861. + g_cacheHit++;
  4862. + return (void __iomem *)bus_addr;
  4863. + }
  4864. + else
  4865. + {
  4866. + bus_addr = (unsigned long)UserVirtualToBus(pUser);
  4867. +
  4868. + if (!bus_addr)
  4869. + return 0;
  4870. +
  4871. + g_cbVirtAddr = virtual_page;
  4872. + g_cbBusAddr = bus_addr & ~4095;
  4873. + g_cacheMiss++;
  4874. +
  4875. + return (void __iomem *)bus_addr;
  4876. + }
  4877. +}
  4878. +
  4879. +//do the same as above, by query our virt->bus cache
  4880. +static inline void __iomem *UserVirtualToBusViaCache(void __user *pUser)
  4881. +{
  4882. + int count;
  4883. + //get the page and its offset
  4884. + unsigned long virtual_page = (unsigned long)pUser & ~4095;
  4885. + unsigned long page_offset = (unsigned long)pUser & 4095;
  4886. + unsigned long bus_addr;
  4887. +
  4888. + if (pUser >= g_pMinPhys && pUser < g_pMaxPhys)
  4889. + {
  4890. + PRINTK_VERBOSE(KERN_DEBUG "user->phys passthrough on %p\n", pUser);
  4891. + return (void __iomem *)((unsigned long)pUser + g_physOffset);
  4892. + }
  4893. +
  4894. + //check the cache for our entry
  4895. + for (count = 0; count < VIRT_TO_BUS_CACHE_SIZE; count++)
  4896. + if (g_virtAddr[count] == virtual_page)
  4897. + {
  4898. + bus_addr = g_busAddr[count] + page_offset;
  4899. + g_cacheHit++;
  4900. + return (void __iomem *)bus_addr;
  4901. + }
  4902. +
  4903. + //not found, look up manually and then insert its page address
  4904. + bus_addr = (unsigned long)UserVirtualToBus(pUser);
  4905. +
  4906. + if (!bus_addr)
  4907. + return 0;
  4908. +
  4909. + g_virtAddr[g_cacheInsertAt] = virtual_page;
  4910. + g_busAddr[g_cacheInsertAt] = bus_addr & ~4095;
  4911. +
  4912. + //round robin
  4913. + g_cacheInsertAt++;
  4914. + if (g_cacheInsertAt == VIRT_TO_BUS_CACHE_SIZE)
  4915. + g_cacheInsertAt = 0;
  4916. +
  4917. + g_cacheMiss++;
  4918. +
  4919. + return (void __iomem *)bus_addr;
  4920. +}
  4921. +
  4922. +/***** FILE OPERATIONS ****/
  4923. +static int Open(struct inode *pInode, struct file *pFile)
  4924. +{
  4925. + PRINTK(KERN_DEBUG "file opening: %d/%d\n", imajor(pInode), iminor(pInode));
  4926. +
  4927. + //check which device we are
  4928. + if (iminor(pInode) == 0) //4k
  4929. + {
  4930. + //only one at a time
  4931. + if (!atomic_dec_and_test(&g_oneLock4k))
  4932. + {
  4933. + atomic_inc(&g_oneLock4k);
  4934. + return -EBUSY;
  4935. + }
  4936. + }
  4937. + else
  4938. + return -EINVAL;
  4939. +
  4940. + //todo there will be trouble if two different processes open the files
  4941. +
  4942. + //reset after any file is opened
  4943. + g_pMinPhys = (void __user *)-1;
  4944. + g_pMaxPhys = (void __user *)0;
  4945. + g_physOffset = 0;
  4946. + g_cmaHandle = 0;
  4947. +
  4948. + return 0;
  4949. +}
  4950. +
  4951. +static int Release(struct inode *pInode, struct file *pFile)
  4952. +{
  4953. + PRINTK(KERN_DEBUG "file closing, %d pages tracked\n", g_trackedPages);
  4954. + if (g_trackedPages)
  4955. + PRINTK(KERN_ERR "we\'re leaking memory!\n");
  4956. +
  4957. + //wait for any dmas to finish
  4958. + DmaWaitAll();
  4959. +
  4960. + //free this memory on the application closing the file or it crashing (implicitly closing the file)
  4961. + if (g_cmaHandle)
  4962. + {
  4963. + PRINTK(KERN_DEBUG "unlocking vc memory\n");
  4964. + if (UnlockVcMemory(g_cmaHandle))
  4965. + PRINTK(KERN_ERR "uh-oh, unable to unlock vc memory!\n");
  4966. + PRINTK(KERN_DEBUG "releasing vc memory\n");
  4967. + if (ReleaseVcMemory(g_cmaHandle))
  4968. + PRINTK(KERN_ERR "uh-oh, unable to release vc memory!\n");
  4969. + }
  4970. +
  4971. + if (iminor(pInode) == 0)
  4972. + atomic_inc(&g_oneLock4k);
  4973. + else
  4974. + return -EINVAL;
  4975. +
  4976. + return 0;
  4977. +}
  4978. +
  4979. +static struct DmaControlBlock __user *DmaPrepare(struct DmaControlBlock __user *pUserCB, int *pError)
  4980. +{
  4981. + struct DmaControlBlock kernCB;
  4982. + struct DmaControlBlock __user *pUNext;
  4983. + void __iomem *pSourceBus, __iomem *pDestBus;
  4984. +
  4985. + //get the control block into kernel memory so we can work on it
  4986. + if (copy_from_user(&kernCB, pUserCB, sizeof(struct DmaControlBlock)) != 0)
  4987. + {
  4988. + PRINTK(KERN_ERR "copy_from_user failed for user cb %p\n", pUserCB);
  4989. + *pError = 1;
  4990. + return 0;
  4991. + }
  4992. +
  4993. + if (kernCB.m_pSourceAddr == 0 || kernCB.m_pDestAddr == 0)
  4994. + {
  4995. + PRINTK(KERN_ERR "faulty source (%p) dest (%p) addresses for user cb %p\n",
  4996. + kernCB.m_pSourceAddr, kernCB.m_pDestAddr, pUserCB);
  4997. + *pError = 1;
  4998. + return 0;
  4999. + }
  5000. +
  5001. + pSourceBus = UserVirtualToBusViaCache(kernCB.m_pSourceAddr);
  5002. + pDestBus = UserVirtualToBusViaCache(kernCB.m_pDestAddr);
  5003. +
  5004. + if (!pSourceBus || !pDestBus)
  5005. + {
  5006. + PRINTK(KERN_ERR "virtual to bus translation failure for source/dest %p/%p->%p/%p\n",
  5007. + kernCB.m_pSourceAddr, kernCB.m_pDestAddr,
  5008. + pSourceBus, pDestBus);
  5009. + *pError = 1;
  5010. + return 0;
  5011. + }
  5012. +
  5013. + //update the user structure with the new bus addresses
  5014. + kernCB.m_pSourceAddr = pSourceBus;
  5015. + kernCB.m_pDestAddr = pDestBus;
  5016. +
  5017. + PRINTK_VERBOSE(KERN_DEBUG "final source %p dest %p\n", kernCB.m_pSourceAddr, kernCB.m_pDestAddr);
  5018. +
  5019. + //sort out the bus address for the next block
  5020. + pUNext = kernCB.m_pNext;
  5021. +
  5022. + if (kernCB.m_pNext)
  5023. + {
  5024. + void __iomem *pNextBus;
  5025. + pNextBus = UserVirtualToBusViaCbCache(kernCB.m_pNext);
  5026. +
  5027. + if (!pNextBus)
  5028. + {
  5029. + PRINTK(KERN_ERR "virtual to bus translation failure for m_pNext\n");
  5030. + *pError = 1;
  5031. + return 0;
  5032. + }
  5033. +
  5034. + //update the pointer with the bus address
  5035. + kernCB.m_pNext = pNextBus;
  5036. + }
  5037. +
  5038. + //write it back to user space
  5039. + if (copy_to_user(pUserCB, &kernCB, sizeof(struct DmaControlBlock)) != 0)
  5040. + {
  5041. + PRINTK(KERN_ERR "copy_to_user failed for cb %p\n", pUserCB);
  5042. + *pError = 1;
  5043. + return 0;
  5044. + }
  5045. +
  5046. + __cpuc_flush_dcache_area(pUserCB, 32);
  5047. +
  5048. + *pError = 0;
  5049. + return pUNext;
  5050. +}
  5051. +
  5052. +static int DmaKick(struct DmaControlBlock __user *pUserCB)
  5053. +{
  5054. + void __iomem *pBusCB;
  5055. +
  5056. + pBusCB = UserVirtualToBusViaCbCache(pUserCB);
  5057. + if (!pBusCB)
  5058. + {
  5059. + PRINTK(KERN_ERR "virtual to bus translation failure for cb\n");
  5060. + return 1;
  5061. + }
  5062. +
  5063. + //flush_cache_all();
  5064. +
  5065. + bcm_dma_start(g_pDmaChanBase, (dma_addr_t)pBusCB);
  5066. +
  5067. + return 0;
  5068. +}
  5069. +
  5070. +static void DmaWaitAll(void)
  5071. +{
  5072. + int counter = 0;
  5073. + volatile int inner_count;
  5074. + volatile unsigned int cs;
  5075. + unsigned long time_before, time_after;
  5076. +
  5077. + time_before = jiffies;
  5078. + //bcm_dma_wait_idle(g_pDmaChanBase);
  5079. + dsb();
  5080. +
  5081. + cs = readl(g_pDmaChanBase);
  5082. +
  5083. + while ((cs & 1) == 1)
  5084. + {
  5085. + cs = readl(g_pDmaChanBase);
  5086. + counter++;
  5087. +
  5088. + for (inner_count = 0; inner_count < 32; inner_count++);
  5089. +
  5090. + asm volatile ("MCR p15,0,r0,c7,c0,4 \n");
  5091. + //cpu_do_idle();
  5092. + if (counter >= 1000000)
  5093. + {
  5094. + PRINTK(KERN_WARNING "DMA failed to finish in a timely fashion\n");
  5095. + break;
  5096. + }
  5097. + }
  5098. + time_after = jiffies;
  5099. + PRINTK_VERBOSE(KERN_DEBUG "done, counter %d, cs %08x", counter, cs);
  5100. + PRINTK_VERBOSE(KERN_DEBUG "took %ld jiffies, %d HZ\n", time_after - time_before, HZ);
  5101. +}
  5102. +
  5103. +static long Ioctl(struct file *pFile, unsigned int cmd, unsigned long arg)
  5104. +{
  5105. + int error = 0;
  5106. + PRINTK_VERBOSE(KERN_DEBUG "ioctl cmd %x arg %lx\n", cmd, arg);
  5107. +
  5108. + switch (cmd)
  5109. + {
  5110. + case DMA_PREPARE:
  5111. + case DMA_PREPARE_KICK:
  5112. + case DMA_PREPARE_KICK_WAIT:
  5113. + {
  5114. + struct DmaControlBlock __user *pUCB = (struct DmaControlBlock *)arg;
  5115. + int steps = 0;
  5116. + unsigned long start_time = jiffies;
  5117. + (void)start_time;
  5118. +
  5119. + //flush our address cache
  5120. + FlushAddrCache();
  5121. +
  5122. + PRINTK_VERBOSE(KERN_DEBUG "dma prepare\n");
  5123. +
  5124. + //do virtual to bus translation for each entry
  5125. + do
  5126. + {
  5127. + pUCB = DmaPrepare(pUCB, &error);
  5128. + } while (error == 0 && ++steps && pUCB);
  5129. + PRINTK_VERBOSE(KERN_DEBUG "prepare done in %d steps, %ld\n", steps, jiffies - start_time);
  5130. +
  5131. + //carry straight on if we want to kick too
  5132. + if (cmd == DMA_PREPARE || error)
  5133. + {
  5134. + PRINTK_VERBOSE(KERN_DEBUG "falling out\n");
  5135. + return error ? -EINVAL : 0;
  5136. + }
  5137. + }
  5138. + case DMA_KICK:
  5139. + PRINTK_VERBOSE(KERN_DEBUG "dma begin\n");
  5140. +
  5141. + if (cmd == DMA_KICK)
  5142. + FlushAddrCache();
  5143. +
  5144. + DmaKick((struct DmaControlBlock __user *)arg);
  5145. +
  5146. + if (cmd != DMA_PREPARE_KICK_WAIT)
  5147. + break;
  5148. +/* case DMA_WAIT_ONE:
  5149. + //PRINTK(KERN_DEBUG "dma wait one\n");
  5150. + break;*/
  5151. + case DMA_WAIT_ALL:
  5152. + //PRINTK(KERN_DEBUG "dma wait all\n");
  5153. + DmaWaitAll();
  5154. + break;
  5155. + case DMA_MAX_BURST:
  5156. + if (g_dmaChan == 0)
  5157. + return 10;
  5158. + else
  5159. + return 5;
  5160. + case DMA_SET_MIN_PHYS:
  5161. + g_pMinPhys = (void __user *)arg;
  5162. + PRINTK(KERN_DEBUG "min/max user/phys bypass set to %p %p\n", g_pMinPhys, g_pMaxPhys);
  5163. + break;
  5164. + case DMA_SET_MAX_PHYS:
  5165. + g_pMaxPhys = (void __user *)arg;
  5166. + PRINTK(KERN_DEBUG "min/max user/phys bypass set to %p %p\n", g_pMinPhys, g_pMaxPhys);
  5167. + break;
  5168. + case DMA_SET_PHYS_OFFSET:
  5169. + g_physOffset = arg;
  5170. + PRINTK(KERN_DEBUG "user/phys bypass offset set to %ld\n", g_physOffset);
  5171. + break;
  5172. + case DMA_CMA_SET_SIZE:
  5173. + {
  5174. + unsigned int pBusAddr;
  5175. +
  5176. + if (g_cmaHandle)
  5177. + {
  5178. + PRINTK(KERN_ERR "memory has already been allocated (handle %d)\n", g_cmaHandle);
  5179. + return -EINVAL;
  5180. + }
  5181. +
  5182. + PRINTK(KERN_INFO "allocating %ld bytes of VC memory\n", arg * 4096);
  5183. +
  5184. + //get the memory
  5185. + if (AllocateVcMemory(&g_cmaHandle, arg * 4096, 4096, MEM_FLAG_L1_NONALLOCATING | MEM_FLAG_NO_INIT | MEM_FLAG_HINT_PERMALOCK))
  5186. + {
  5187. + PRINTK(KERN_ERR "failed to allocate %ld bytes of VC memory\n", arg * 4096);
  5188. + g_cmaHandle = 0;
  5189. + return -EINVAL;
  5190. + }
  5191. +
  5192. + //get an address for it
  5193. + PRINTK(KERN_INFO "trying to map VC memory\n");
  5194. +
  5195. + if (LockVcMemory(&pBusAddr, g_cmaHandle))
  5196. + {
  5197. + PRINTK(KERN_ERR "failed to map CMA handle %d, releasing memory\n", g_cmaHandle);
  5198. + ReleaseVcMemory(g_cmaHandle);
  5199. + g_cmaHandle = 0;
  5200. + }
  5201. +
  5202. + PRINTK(KERN_INFO "bus address for CMA memory is %x\n", pBusAddr);
  5203. + return pBusAddr;
  5204. + }
  5205. + case DMA_GET_VERSION:
  5206. + PRINTK(KERN_DEBUG "returning version number, %d\n", VERSION_NUMBER);
  5207. + return VERSION_NUMBER;
  5208. + default:
  5209. + PRINTK(KERN_DEBUG "unknown ioctl: %d\n", cmd);
  5210. + return -EINVAL;
  5211. + }
  5212. +
  5213. + return 0;
  5214. +}
  5215. +
  5216. +static ssize_t Read(struct file *pFile, char __user *pUser, size_t count, loff_t *offp)
  5217. +{
  5218. + return -EIO;
  5219. +}
  5220. +
  5221. +static int Mmap(struct file *pFile, struct vm_area_struct *pVma)
  5222. +{
  5223. + struct PageList *pPages;
  5224. + struct VmaPageList *pVmaList;
  5225. +
  5226. + PRINTK_VERBOSE(KERN_DEBUG "MMAP vma %p, length %ld (%s %d)\n",
  5227. + pVma, pVma->vm_end - pVma->vm_start,
  5228. + current->comm, current->pid);
  5229. + PRINTK_VERBOSE(KERN_DEBUG "MMAP %p %d (tracked %d)\n", pVma, current->pid, g_trackedPages);
  5230. +
  5231. + //make a new page list
  5232. + pPages = (struct PageList *)kmalloc(sizeof(struct PageList), GFP_KERNEL);
  5233. + if (!pPages)
  5234. + {
  5235. + PRINTK(KERN_ERR "couldn\'t allocate a new page list (%s %d)\n",
  5236. + current->comm, current->pid);
  5237. + return -ENOMEM;
  5238. + }
  5239. +
  5240. + //clear the page list
  5241. + pPages->m_used = 0;
  5242. + pPages->m_pNext = 0;
  5243. +
  5244. + //insert our vma and new page list somewhere
  5245. + if (!pVma->vm_private_data)
  5246. + {
  5247. + struct VmaPageList *pList;
  5248. +
  5249. + PRINTK_VERBOSE(KERN_DEBUG "new vma list, making new one (%s %d)\n",
  5250. + current->comm, current->pid);
  5251. +
  5252. + //make a new vma list
  5253. + pList = (struct VmaPageList *)kmalloc(sizeof(struct VmaPageList), GFP_KERNEL);
  5254. + if (!pList)
  5255. + {
  5256. + PRINTK(KERN_ERR "couldn\'t allocate vma page list (%s %d)\n",
  5257. + current->comm, current->pid);
  5258. + kfree(pPages);
  5259. + return -ENOMEM;
  5260. + }
  5261. +
  5262. + //clear this list
  5263. + pVma->vm_private_data = (void *)pList;
  5264. + pList->m_refCount = 0;
  5265. + }
  5266. +
  5267. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  5268. +
  5269. + //add it to the vma list
  5270. + pVmaList->m_pPageHead = pPages;
  5271. + pVmaList->m_pPageTail = pPages;
  5272. +
  5273. + pVma->vm_ops = &g_vmOps4k;
  5274. + pVma->vm_flags |= VM_IO;
  5275. +
  5276. + VmaOpen4k(pVma);
  5277. +
  5278. + return 0;
  5279. +}
  5280. +
  5281. +/****** VMA OPERATIONS ******/
  5282. +
  5283. +static void VmaOpen4k(struct vm_area_struct *pVma)
  5284. +{
  5285. + struct VmaPageList *pVmaList;
  5286. +
  5287. + 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);
  5288. + PRINTK_VERBOSE(KERN_DEBUG "OPEN %p %d %ld pages (tracked pages %d)\n",
  5289. + pVma, current->pid, (pVma->vm_end - pVma->vm_start) >> 12,
  5290. + g_trackedPages);
  5291. +
  5292. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  5293. +
  5294. + if (pVmaList)
  5295. + {
  5296. + pVmaList->m_refCount++;
  5297. + PRINTK_VERBOSE(KERN_DEBUG "ref count is now %d\n", pVmaList->m_refCount);
  5298. + }
  5299. + else
  5300. + {
  5301. + PRINTK_VERBOSE(KERN_DEBUG "err, open but no vma page list\n");
  5302. + }
  5303. +}
  5304. +
  5305. +static void VmaClose4k(struct vm_area_struct *pVma)
  5306. +{
  5307. + struct VmaPageList *pVmaList;
  5308. + int freed = 0;
  5309. +
  5310. + PRINTK_VERBOSE(KERN_DEBUG "vma close %p private %p (%s %d)\n", pVma, pVma->vm_private_data, current->comm, current->pid);
  5311. +
  5312. + //wait for any dmas to finish
  5313. + DmaWaitAll();
  5314. +
  5315. + //find our vma in the list
  5316. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  5317. +
  5318. + //may be a fork
  5319. + if (pVmaList)
  5320. + {
  5321. + struct PageList *pPages;
  5322. +
  5323. + pVmaList->m_refCount--;
  5324. +
  5325. + if (pVmaList->m_refCount == 0)
  5326. + {
  5327. + PRINTK_VERBOSE(KERN_DEBUG "found vma, freeing pages (%s %d)\n",
  5328. + current->comm, current->pid);
  5329. +
  5330. + pPages = pVmaList->m_pPageHead;
  5331. +
  5332. + if (!pPages)
  5333. + {
  5334. + PRINTK(KERN_ERR "no page list (%s %d)!\n",
  5335. + current->comm, current->pid);
  5336. + return;
  5337. + }
  5338. +
  5339. + while (pPages)
  5340. + {
  5341. + struct PageList *next;
  5342. + int count;
  5343. +
  5344. + PRINTK_VERBOSE(KERN_DEBUG "page list (%s %d)\n",
  5345. + current->comm, current->pid);
  5346. +
  5347. + next = pPages->m_pNext;
  5348. + for (count = 0; count < pPages->m_used; count++)
  5349. + {
  5350. + PRINTK_VERBOSE(KERN_DEBUG "freeing page %p (%s %d)\n",
  5351. + pPages->m_pPages[count],
  5352. + current->comm, current->pid);
  5353. + __free_pages(pPages->m_pPages[count], 0);
  5354. + g_trackedPages--;
  5355. + freed++;
  5356. + }
  5357. +
  5358. + PRINTK_VERBOSE(KERN_DEBUG "freeing page list (%s %d)\n",
  5359. + current->comm, current->pid);
  5360. + kfree(pPages);
  5361. + pPages = next;
  5362. + }
  5363. +
  5364. + //remove our vma from the list
  5365. + kfree(pVmaList);
  5366. + pVma->vm_private_data = 0;
  5367. + }
  5368. + else
  5369. + {
  5370. + PRINTK_VERBOSE(KERN_DEBUG "ref count is %d, not closing\n", pVmaList->m_refCount);
  5371. + }
  5372. + }
  5373. + else
  5374. + {
  5375. + PRINTK_VERBOSE(KERN_ERR "uh-oh, vma %p not found (%s %d)!\n", pVma, current->comm, current->pid);
  5376. + PRINTK_VERBOSE(KERN_ERR "CLOSE ERR\n");
  5377. + }
  5378. +
  5379. + PRINTK_VERBOSE(KERN_DEBUG "CLOSE %p %d %d pages (tracked pages %d)",
  5380. + pVma, current->pid, freed, g_trackedPages);
  5381. +
  5382. + PRINTK_VERBOSE(KERN_DEBUG "%d pages open\n", g_trackedPages);
  5383. +}
  5384. +
  5385. +static int VmaFault4k(struct vm_area_struct *pVma, struct vm_fault *pVmf)
  5386. +{
  5387. + PRINTK_VERBOSE(KERN_DEBUG "vma fault for vma %p private %p at offset %ld (%s %d)\n", pVma, pVma->vm_private_data, pVmf->pgoff,
  5388. + current->comm, current->pid);
  5389. + PRINTK_VERBOSE(KERN_DEBUG "FAULT\n");
  5390. + pVmf->page = alloc_page(GFP_KERNEL);
  5391. +
  5392. + if (pVmf->page)
  5393. + {
  5394. + PRINTK_VERBOSE(KERN_DEBUG "alloc page virtual %p\n", page_address(pVmf->page));
  5395. + }
  5396. +
  5397. + if (!pVmf->page)
  5398. + {
  5399. + PRINTK(KERN_ERR "vma fault oom (%s %d)\n", current->comm, current->pid);
  5400. + return VM_FAULT_OOM;
  5401. + }
  5402. + else
  5403. + {
  5404. + struct VmaPageList *pVmaList;
  5405. +
  5406. + get_page(pVmf->page);
  5407. + g_trackedPages++;
  5408. +
  5409. + //find our vma in the list
  5410. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  5411. +
  5412. + if (pVmaList)
  5413. + {
  5414. + PRINTK_VERBOSE(KERN_DEBUG "vma found (%s %d)\n", current->comm, current->pid);
  5415. +
  5416. + if (pVmaList->m_pPageTail->m_used == PAGES_PER_LIST)
  5417. + {
  5418. + PRINTK_VERBOSE(KERN_DEBUG "making new page list (%s %d)\n", current->comm, current->pid);
  5419. + //making a new page list
  5420. + pVmaList->m_pPageTail->m_pNext = (struct PageList *)kmalloc(sizeof(struct PageList), GFP_KERNEL);
  5421. + if (!pVmaList->m_pPageTail->m_pNext)
  5422. + return -ENOMEM;
  5423. +
  5424. + //update the tail pointer
  5425. + pVmaList->m_pPageTail = pVmaList->m_pPageTail->m_pNext;
  5426. + pVmaList->m_pPageTail->m_used = 0;
  5427. + pVmaList->m_pPageTail->m_pNext = 0;
  5428. + }
  5429. +
  5430. + PRINTK_VERBOSE(KERN_DEBUG "adding page to list (%s %d)\n", current->comm, current->pid);
  5431. +
  5432. + pVmaList->m_pPageTail->m_pPages[pVmaList->m_pPageTail->m_used] = pVmf->page;
  5433. + pVmaList->m_pPageTail->m_used++;
  5434. + }
  5435. + else
  5436. + PRINTK(KERN_ERR "returned page for vma we don\'t know %p (%s %d)\n", pVma, current->comm, current->pid);
  5437. +
  5438. + return 0;
  5439. + }
  5440. +}
  5441. +
  5442. +/****** GENERIC FUNCTIONS ******/
  5443. +static int __init dmaer_init(void)
  5444. +{
  5445. + int result = alloc_chrdev_region(&g_majorMinor, 0, 1, "dmaer");
  5446. + if (result < 0)
  5447. + {
  5448. + PRINTK(KERN_ERR "unable to get major device number\n");
  5449. + return result;
  5450. + }
  5451. + else
  5452. + PRINTK(KERN_DEBUG "major device number %d\n", MAJOR(g_majorMinor));
  5453. +
  5454. + PRINTK(KERN_DEBUG "vma list size %d, page list size %d, page size %ld\n",
  5455. + sizeof(struct VmaPageList), sizeof(struct PageList), PAGE_SIZE);
  5456. +
  5457. + //get a dma channel to work with
  5458. + result = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST, (void **)&g_pDmaChanBase, &g_dmaIrq);
  5459. +
  5460. + //uncomment to force to channel 0
  5461. + //result = 0;
  5462. + //g_pDmaChanBase = 0xce808000;
  5463. +
  5464. + if (result < 0)
  5465. + {
  5466. + PRINTK(KERN_ERR "failed to allocate dma channel\n");
  5467. + cdev_del(&g_cDev);
  5468. + unregister_chrdev_region(g_majorMinor, 1);
  5469. + }
  5470. +
  5471. + //reset the channel
  5472. + PRINTK(KERN_DEBUG "allocated dma channel %d (%p), initial state %08x\n", result, g_pDmaChanBase, *g_pDmaChanBase);
  5473. + *g_pDmaChanBase = 1 << 31;
  5474. + PRINTK(KERN_DEBUG "post-reset %08x\n", *g_pDmaChanBase);
  5475. +
  5476. + g_dmaChan = result;
  5477. +
  5478. + //clear the cache stats
  5479. + g_cacheHit = 0;
  5480. + g_cacheMiss = 0;
  5481. +
  5482. + //register our device - after this we are go go go
  5483. + cdev_init(&g_cDev, &g_fOps);
  5484. + g_cDev.owner = THIS_MODULE;
  5485. + g_cDev.ops = &g_fOps;
  5486. +
  5487. + result = cdev_add(&g_cDev, g_majorMinor, 1);
  5488. + if (result < 0)
  5489. + {
  5490. + PRINTK(KERN_ERR "failed to add character device\n");
  5491. + unregister_chrdev_region(g_majorMinor, 1);
  5492. + bcm_dma_chan_free(g_dmaChan);
  5493. + return result;
  5494. + }
  5495. +
  5496. + return 0;
  5497. +}
  5498. +
  5499. +static void __exit dmaer_exit(void)
  5500. +{
  5501. + PRINTK(KERN_INFO "closing dmaer device, cache stats: %d hits %d misses\n", g_cacheHit, g_cacheMiss);
  5502. + //unregister the device
  5503. + cdev_del(&g_cDev);
  5504. + unregister_chrdev_region(g_majorMinor, 1);
  5505. + //free the dma channel
  5506. + bcm_dma_chan_free(g_dmaChan);
  5507. +}
  5508. +
  5509. +MODULE_LICENSE("Dual BSD/GPL");
  5510. +MODULE_AUTHOR("Simon Hall");
  5511. +module_init(dmaer_init);
  5512. +module_exit(dmaer_exit);
  5513. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/arm_control.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_control.h
  5514. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/arm_control.h 1970-01-01 01:00:00.000000000 +0100
  5515. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_control.h 2014-03-11 17:31:43.000000000 +0100
  5516. @@ -0,0 +1,419 @@
  5517. +/*
  5518. + * linux/arch/arm/mach-bcm2708/arm_control.h
  5519. + *
  5520. + * Copyright (C) 2010 Broadcom
  5521. + *
  5522. + * This program is free software; you can redistribute it and/or modify
  5523. + * it under the terms of the GNU General Public License as published by
  5524. + * the Free Software Foundation; either version 2 of the License, or
  5525. + * (at your option) any later version.
  5526. + *
  5527. + * This program is distributed in the hope that it will be useful,
  5528. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5529. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5530. + * GNU General Public License for more details.
  5531. + *
  5532. + * You should have received a copy of the GNU General Public License
  5533. + * along with this program; if not, write to the Free Software
  5534. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5535. + */
  5536. +
  5537. +#ifndef __BCM2708_ARM_CONTROL_H
  5538. +#define __BCM2708_ARM_CONTROL_H
  5539. +
  5540. +/*
  5541. + * Definitions and addresses for the ARM CONTROL logic
  5542. + * This file is manually generated.
  5543. + */
  5544. +
  5545. +#define ARM_BASE 0x7E00B000
  5546. +
  5547. +/* Basic configuration */
  5548. +#define ARM_CONTROL0 HW_REGISTER_RW(ARM_BASE+0x000)
  5549. +#define ARM_C0_SIZ128M 0x00000000
  5550. +#define ARM_C0_SIZ256M 0x00000001
  5551. +#define ARM_C0_SIZ512M 0x00000002
  5552. +#define ARM_C0_SIZ1G 0x00000003
  5553. +#define ARM_C0_BRESP0 0x00000000
  5554. +#define ARM_C0_BRESP1 0x00000004
  5555. +#define ARM_C0_BRESP2 0x00000008
  5556. +#define ARM_C0_BOOTHI 0x00000010
  5557. +#define ARM_C0_UNUSED05 0x00000020 /* free */
  5558. +#define ARM_C0_FULLPERI 0x00000040
  5559. +#define ARM_C0_UNUSED78 0x00000180 /* free */
  5560. +#define ARM_C0_JTAGMASK 0x00000E00
  5561. +#define ARM_C0_JTAGOFF 0x00000000
  5562. +#define ARM_C0_JTAGBASH 0x00000800 /* Debug on GPIO off */
  5563. +#define ARM_C0_JTAGGPIO 0x00000C00 /* Debug on GPIO on */
  5564. +#define ARM_C0_APROTMSK 0x0000F000
  5565. +#define ARM_C0_DBG0SYNC 0x00010000 /* VPU0 halt sync */
  5566. +#define ARM_C0_DBG1SYNC 0x00020000 /* VPU1 halt sync */
  5567. +#define ARM_C0_SWDBGREQ 0x00040000 /* HW debug request */
  5568. +#define ARM_C0_PASSHALT 0x00080000 /* ARM halt passed to debugger */
  5569. +#define ARM_C0_PRIO_PER 0x00F00000 /* per priority mask */
  5570. +#define ARM_C0_PRIO_L2 0x0F000000
  5571. +#define ARM_C0_PRIO_UC 0xF0000000
  5572. +
  5573. +#define ARM_C0_APROTPASS 0x0000A000 /* Translate 1:1 */
  5574. +#define ARM_C0_APROTUSER 0x00000000 /* Only user mode */
  5575. +#define ARM_C0_APROTSYST 0x0000F000 /* Only system mode */
  5576. +
  5577. +
  5578. +#define ARM_CONTROL1 HW_REGISTER_RW(ARM_BASE+0x440)
  5579. +#define ARM_C1_TIMER 0x00000001 /* re-route timer IRQ to VC */
  5580. +#define ARM_C1_MAIL 0x00000002 /* re-route Mail IRQ to VC */
  5581. +#define ARM_C1_BELL0 0x00000004 /* re-route Doorbell 0 to VC */
  5582. +#define ARM_C1_BELL1 0x00000008 /* re-route Doorbell 1 to VC */
  5583. +#define ARM_C1_PERSON 0x00000100 /* peripherals on */
  5584. +#define ARM_C1_REQSTOP 0x00000200 /* ASYNC bridge request stop */
  5585. +
  5586. +#define ARM_STATUS HW_REGISTER_RW(ARM_BASE+0x444)
  5587. +#define ARM_S_ACKSTOP 0x80000000 /* Bridge stopped */
  5588. +#define ARM_S_READPEND 0x000003FF /* pending reads counter */
  5589. +#define ARM_S_WRITPEND 0x000FFC00 /* pending writes counter */
  5590. +
  5591. +#define ARM_ERRHALT HW_REGISTER_RW(ARM_BASE+0x448)
  5592. +#define ARM_EH_PERIBURST 0x00000001 /* Burst write seen on peri bus */
  5593. +#define ARM_EH_ILLADDRS1 0x00000002 /* Address bits 25-27 error */
  5594. +#define ARM_EH_ILLADDRS2 0x00000004 /* Address bits 31-28 error */
  5595. +#define ARM_EH_VPU0HALT 0x00000008 /* VPU0 halted & in debug mode */
  5596. +#define ARM_EH_VPU1HALT 0x00000010 /* VPU1 halted & in debug mode */
  5597. +#define ARM_EH_ARMHALT 0x00000020 /* ARM in halted debug mode */
  5598. +
  5599. +#define ARM_ID_SECURE HW_REGISTER_RW(ARM_BASE+0x00C)
  5600. +#define ARM_ID HW_REGISTER_RW(ARM_BASE+0x44C)
  5601. +#define ARM_IDVAL 0x364D5241
  5602. +
  5603. +/* Translation memory */
  5604. +#define ARM_TRANSLATE HW_REGISTER_RW(ARM_BASE+0x100)
  5605. +/* 32 locations: 0x100.. 0x17F */
  5606. +/* 32 spare means we CAN go to 64 pages.... */
  5607. +
  5608. +
  5609. +/* Interrupts */
  5610. +#define ARM_IRQ_PEND0 HW_REGISTER_RW(ARM_BASE+0x200) /* Top IRQ bits */
  5611. +#define ARM_I0_TIMER 0x00000001 /* timer IRQ */
  5612. +#define ARM_I0_MAIL 0x00000002 /* Mail IRQ */
  5613. +#define ARM_I0_BELL0 0x00000004 /* Doorbell 0 */
  5614. +#define ARM_I0_BELL1 0x00000008 /* Doorbell 1 */
  5615. +#define ARM_I0_BANK1 0x00000100 /* Bank1 IRQ */
  5616. +#define ARM_I0_BANK2 0x00000200 /* Bank2 IRQ */
  5617. +
  5618. +#define ARM_IRQ_PEND1 HW_REGISTER_RW(ARM_BASE+0x204) /* All bank1 IRQ bits */
  5619. +/* todo: all I1_interrupt sources */
  5620. +#define ARM_IRQ_PEND2 HW_REGISTER_RW(ARM_BASE+0x208) /* All bank2 IRQ bits */
  5621. +/* todo: all I2_interrupt sources */
  5622. +
  5623. +#define ARM_IRQ_FAST HW_REGISTER_RW(ARM_BASE+0x20C) /* FIQ control */
  5624. +#define ARM_IF_INDEX 0x0000007F /* FIQ select */
  5625. +#define ARM_IF_ENABLE 0x00000080 /* FIQ enable */
  5626. +#define ARM_IF_VCMASK 0x0000003F /* FIQ = (index from VC source) */
  5627. +#define ARM_IF_TIMER 0x00000040 /* FIQ = ARM timer */
  5628. +#define ARM_IF_MAIL 0x00000041 /* FIQ = ARM Mail */
  5629. +#define ARM_IF_BELL0 0x00000042 /* FIQ = ARM Doorbell 0 */
  5630. +#define ARM_IF_BELL1 0x00000043 /* FIQ = ARM Doorbell 1 */
  5631. +#define ARM_IF_VP0HALT 0x00000044 /* FIQ = VPU0 Halt seen */
  5632. +#define ARM_IF_VP1HALT 0x00000045 /* FIQ = VPU1 Halt seen */
  5633. +#define ARM_IF_ILLEGAL 0x00000046 /* FIQ = Illegal access seen */
  5634. +
  5635. +#define ARM_IRQ_ENBL1 HW_REGISTER_RW(ARM_BASE+0x210) /* Bank1 enable bits */
  5636. +#define ARM_IRQ_ENBL2 HW_REGISTER_RW(ARM_BASE+0x214) /* Bank2 enable bits */
  5637. +#define ARM_IRQ_ENBL3 HW_REGISTER_RW(ARM_BASE+0x218) /* ARM irqs enable bits */
  5638. +#define ARM_IRQ_DIBL1 HW_REGISTER_RW(ARM_BASE+0x21C) /* Bank1 disable bits */
  5639. +#define ARM_IRQ_DIBL2 HW_REGISTER_RW(ARM_BASE+0x220) /* Bank2 disable bits */
  5640. +#define ARM_IRQ_DIBL3 HW_REGISTER_RW(ARM_BASE+0x224) /* ARM irqs disable bits */
  5641. +#define ARM_IE_TIMER 0x00000001 /* Timer IRQ */
  5642. +#define ARM_IE_MAIL 0x00000002 /* Mail IRQ */
  5643. +#define ARM_IE_BELL0 0x00000004 /* Doorbell 0 */
  5644. +#define ARM_IE_BELL1 0x00000008 /* Doorbell 1 */
  5645. +#define ARM_IE_VP0HALT 0x00000010 /* VPU0 Halt */
  5646. +#define ARM_IE_VP1HALT 0x00000020 /* VPU1 Halt */
  5647. +#define ARM_IE_ILLEGAL 0x00000040 /* Illegal access seen */
  5648. +
  5649. +/* Timer */
  5650. +/* For reg. fields see sp804 spec. */
  5651. +#define ARM_T_LOAD HW_REGISTER_RW(ARM_BASE+0x400)
  5652. +#define ARM_T_VALUE HW_REGISTER_RW(ARM_BASE+0x404)
  5653. +#define ARM_T_CONTROL HW_REGISTER_RW(ARM_BASE+0x408)
  5654. +#define ARM_T_IRQCNTL HW_REGISTER_RW(ARM_BASE+0x40C)
  5655. +#define ARM_T_RAWIRQ HW_REGISTER_RW(ARM_BASE+0x410)
  5656. +#define ARM_T_MSKIRQ HW_REGISTER_RW(ARM_BASE+0x414)
  5657. +#define ARM_T_RELOAD HW_REGISTER_RW(ARM_BASE+0x418)
  5658. +#define ARM_T_PREDIV HW_REGISTER_RW(ARM_BASE+0x41c)
  5659. +#define ARM_T_FREECNT HW_REGISTER_RW(ARM_BASE+0x420)
  5660. +
  5661. +#define TIMER_CTRL_ONESHOT (1 << 0)
  5662. +#define TIMER_CTRL_32BIT (1 << 1)
  5663. +#define TIMER_CTRL_DIV1 (0 << 2)
  5664. +#define TIMER_CTRL_DIV16 (1 << 2)
  5665. +#define TIMER_CTRL_DIV256 (2 << 2)
  5666. +#define TIMER_CTRL_IE (1 << 5)
  5667. +#define TIMER_CTRL_PERIODIC (1 << 6)
  5668. +#define TIMER_CTRL_ENABLE (1 << 7)
  5669. +#define TIMER_CTRL_DBGHALT (1 << 8)
  5670. +#define TIMER_CTRL_ENAFREE (1 << 9)
  5671. +#define TIMER_CTRL_FREEDIV_SHIFT 16)
  5672. +#define TIMER_CTRL_FREEDIV_MASK 0xff
  5673. +
  5674. +/* Semaphores, Doorbells, Mailboxes */
  5675. +#define ARM_SBM_OWN0 (ARM_BASE+0x800)
  5676. +#define ARM_SBM_OWN1 (ARM_BASE+0x900)
  5677. +#define ARM_SBM_OWN2 (ARM_BASE+0xA00)
  5678. +#define ARM_SBM_OWN3 (ARM_BASE+0xB00)
  5679. +
  5680. +/* MAILBOXES
  5681. + * Register flags are common across all
  5682. + * owner registers. See end of this section
  5683. + *
  5684. + * Semaphores, Doorbells, Mailboxes Owner 0
  5685. + *
  5686. + */
  5687. +
  5688. +#define ARM_0_SEMS HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5689. +#define ARM_0_SEM0 HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5690. +#define ARM_0_SEM1 HW_REGISTER_RW(ARM_SBM_OWN0+0x04)
  5691. +#define ARM_0_SEM2 HW_REGISTER_RW(ARM_SBM_OWN0+0x08)
  5692. +#define ARM_0_SEM3 HW_REGISTER_RW(ARM_SBM_OWN0+0x0C)
  5693. +#define ARM_0_SEM4 HW_REGISTER_RW(ARM_SBM_OWN0+0x10)
  5694. +#define ARM_0_SEM5 HW_REGISTER_RW(ARM_SBM_OWN0+0x14)
  5695. +#define ARM_0_SEM6 HW_REGISTER_RW(ARM_SBM_OWN0+0x18)
  5696. +#define ARM_0_SEM7 HW_REGISTER_RW(ARM_SBM_OWN0+0x1C)
  5697. +#define ARM_0_BELL0 HW_REGISTER_RW(ARM_SBM_OWN0+0x40)
  5698. +#define ARM_0_BELL1 HW_REGISTER_RW(ARM_SBM_OWN0+0x44)
  5699. +#define ARM_0_BELL2 HW_REGISTER_RW(ARM_SBM_OWN0+0x48)
  5700. +#define ARM_0_BELL3 HW_REGISTER_RW(ARM_SBM_OWN0+0x4C)
  5701. +/* MAILBOX 0 access in Owner 0 area */
  5702. +/* Some addresses should ONLY be used by owner 0 */
  5703. +#define ARM_0_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) */
  5704. +#define ARM_0_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) Normal read */
  5705. +#define ARM_0_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN0+0x90) /* none-pop read */
  5706. +#define ARM_0_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN0+0x94) /* Sender read (only LS 2 bits) */
  5707. +#define ARM_0_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN0+0x98) /* Status read */
  5708. +#define ARM_0_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0x9C) /* Config read/write */
  5709. +/* MAILBOX 1 access in Owner 0 area */
  5710. +/* Owner 0 should only WRITE to this mailbox */
  5711. +#define ARM_0_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) /* .. 0xAC (4 locations) */
  5712. +/*#define ARM_0_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) */ /* DO NOT USE THIS !!!!! */
  5713. +/*#define ARM_0_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN0+0xB0) */ /* DO NOT USE THIS !!!!! */
  5714. +/*#define ARM_0_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN0+0xB4) */ /* DO NOT USE THIS !!!!! */
  5715. +#define ARM_0_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN0+0xB8) /* Status read */
  5716. +/*#define ARM_0_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0xBC) */ /* DO NOT USE THIS !!!!! */
  5717. +/* General SEM, BELL, MAIL config/status */
  5718. +#define ARM_0_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE0) /* semaphore clear/debug register */
  5719. +#define ARM_0_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE4) /* Doorbells clear/debug register */
  5720. +#define ARM_0_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xF8) /* ALL interrupts */
  5721. +#define ARM_0_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xFC) /* IRQS pending for owner 0 */
  5722. +
  5723. +/* Semaphores, Doorbells, Mailboxes Owner 1 */
  5724. +#define ARM_1_SEMS HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5725. +#define ARM_1_SEM0 HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5726. +#define ARM_1_SEM1 HW_REGISTER_RW(ARM_SBM_OWN1+0x04)
  5727. +#define ARM_1_SEM2 HW_REGISTER_RW(ARM_SBM_OWN1+0x08)
  5728. +#define ARM_1_SEM3 HW_REGISTER_RW(ARM_SBM_OWN1+0x0C)
  5729. +#define ARM_1_SEM4 HW_REGISTER_RW(ARM_SBM_OWN1+0x10)
  5730. +#define ARM_1_SEM5 HW_REGISTER_RW(ARM_SBM_OWN1+0x14)
  5731. +#define ARM_1_SEM6 HW_REGISTER_RW(ARM_SBM_OWN1+0x18)
  5732. +#define ARM_1_SEM7 HW_REGISTER_RW(ARM_SBM_OWN1+0x1C)
  5733. +#define ARM_1_BELL0 HW_REGISTER_RW(ARM_SBM_OWN1+0x40)
  5734. +#define ARM_1_BELL1 HW_REGISTER_RW(ARM_SBM_OWN1+0x44)
  5735. +#define ARM_1_BELL2 HW_REGISTER_RW(ARM_SBM_OWN1+0x48)
  5736. +#define ARM_1_BELL3 HW_REGISTER_RW(ARM_SBM_OWN1+0x4C)
  5737. +/* MAILBOX 0 access in Owner 0 area */
  5738. +/* Owner 1 should only WRITE to this mailbox */
  5739. +#define ARM_1_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0x80) /* .. 0x8C (4 locations) */
  5740. +/*#define ARM_1_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN1+0x80) */ /* DO NOT USE THIS !!!!! */
  5741. +/*#define ARM_1_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN1+0x90) */ /* DO NOT USE THIS !!!!! */
  5742. +/*#define ARM_1_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN1+0x94) */ /* DO NOT USE THIS !!!!! */
  5743. +#define ARM_1_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN1+0x98) /* Status read */
  5744. +/*#define ARM_1_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0x9C) */ /* DO NOT USE THIS !!!!! */
  5745. +/* MAILBOX 1 access in Owner 0 area */
  5746. +#define ARM_1_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) */
  5747. +#define ARM_1_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) Normal read */
  5748. +#define ARM_1_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN1+0xB0) /* none-pop read */
  5749. +#define ARM_1_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN1+0xB4) /* Sender read (only LS 2 bits) */
  5750. +#define ARM_1_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN1+0xB8) /* Status read */
  5751. +#define ARM_1_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0xBC)
  5752. +/* General SEM, BELL, MAIL config/status */
  5753. +#define ARM_1_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE0) /* semaphore clear/debug register */
  5754. +#define ARM_1_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE4) /* Doorbells clear/debug register */
  5755. +#define ARM_1_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xFC) /* IRQS pending for owner 1 */
  5756. +#define ARM_1_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xF8) /* ALL interrupts */
  5757. +
  5758. +/* Semaphores, Doorbells, Mailboxes Owner 2 */
  5759. +#define ARM_2_SEMS HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5760. +#define ARM_2_SEM0 HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5761. +#define ARM_2_SEM1 HW_REGISTER_RW(ARM_SBM_OWN2+0x04)
  5762. +#define ARM_2_SEM2 HW_REGISTER_RW(ARM_SBM_OWN2+0x08)
  5763. +#define ARM_2_SEM3 HW_REGISTER_RW(ARM_SBM_OWN2+0x0C)
  5764. +#define ARM_2_SEM4 HW_REGISTER_RW(ARM_SBM_OWN2+0x10)
  5765. +#define ARM_2_SEM5 HW_REGISTER_RW(ARM_SBM_OWN2+0x14)
  5766. +#define ARM_2_SEM6 HW_REGISTER_RW(ARM_SBM_OWN2+0x18)
  5767. +#define ARM_2_SEM7 HW_REGISTER_RW(ARM_SBM_OWN2+0x1C)
  5768. +#define ARM_2_BELL0 HW_REGISTER_RW(ARM_SBM_OWN2+0x40)
  5769. +#define ARM_2_BELL1 HW_REGISTER_RW(ARM_SBM_OWN2+0x44)
  5770. +#define ARM_2_BELL2 HW_REGISTER_RW(ARM_SBM_OWN2+0x48)
  5771. +#define ARM_2_BELL3 HW_REGISTER_RW(ARM_SBM_OWN2+0x4C)
  5772. +/* MAILBOX 0 access in Owner 2 area */
  5773. +/* Owner 2 should only WRITE to this mailbox */
  5774. +#define ARM_2_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0x80) /* .. 0x8C (4 locations) */
  5775. +/*#define ARM_2_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN2+0x80) */ /* DO NOT USE THIS !!!!! */
  5776. +/*#define ARM_2_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN2+0x90) */ /* DO NOT USE THIS !!!!! */
  5777. +/*#define ARM_2_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN2+0x94) */ /* DO NOT USE THIS !!!!! */
  5778. +#define ARM_2_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN2+0x98) /* Status read */
  5779. +/*#define ARM_2_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0x9C) */ /* DO NOT USE THIS !!!!! */
  5780. +/* MAILBOX 1 access in Owner 2 area */
  5781. +/* Owner 2 should only WRITE to this mailbox */
  5782. +#define ARM_2_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) /* .. 0xAC (4 locations) */
  5783. +/*#define ARM_2_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) */ /* DO NOT USE THIS !!!!! */
  5784. +/*#define ARM_2_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN2+0xB0) */ /* DO NOT USE THIS !!!!! */
  5785. +/*#define ARM_2_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN2+0xB4) */ /* DO NOT USE THIS !!!!! */
  5786. +#define ARM_2_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN2+0xB8) /* Status read */
  5787. +/*#define ARM_2_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0xBC) */ /* DO NOT USE THIS !!!!! */
  5788. +/* General SEM, BELL, MAIL config/status */
  5789. +#define ARM_2_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE0) /* semaphore clear/debug register */
  5790. +#define ARM_2_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE4) /* Doorbells clear/debug register */
  5791. +#define ARM_2_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xFC) /* IRQS pending for owner 2 */
  5792. +#define ARM_2_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xF8) /* ALL interrupts */
  5793. +
  5794. +/* Semaphores, Doorbells, Mailboxes Owner 3 */
  5795. +#define ARM_3_SEMS HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5796. +#define ARM_3_SEM0 HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5797. +#define ARM_3_SEM1 HW_REGISTER_RW(ARM_SBM_OWN3+0x04)
  5798. +#define ARM_3_SEM2 HW_REGISTER_RW(ARM_SBM_OWN3+0x08)
  5799. +#define ARM_3_SEM3 HW_REGISTER_RW(ARM_SBM_OWN3+0x0C)
  5800. +#define ARM_3_SEM4 HW_REGISTER_RW(ARM_SBM_OWN3+0x10)
  5801. +#define ARM_3_SEM5 HW_REGISTER_RW(ARM_SBM_OWN3+0x14)
  5802. +#define ARM_3_SEM6 HW_REGISTER_RW(ARM_SBM_OWN3+0x18)
  5803. +#define ARM_3_SEM7 HW_REGISTER_RW(ARM_SBM_OWN3+0x1C)
  5804. +#define ARM_3_BELL0 HW_REGISTER_RW(ARM_SBM_OWN3+0x40)
  5805. +#define ARM_3_BELL1 HW_REGISTER_RW(ARM_SBM_OWN3+0x44)
  5806. +#define ARM_3_BELL2 HW_REGISTER_RW(ARM_SBM_OWN3+0x48)
  5807. +#define ARM_3_BELL3 HW_REGISTER_RW(ARM_SBM_OWN3+0x4C)
  5808. +/* MAILBOX 0 access in Owner 3 area */
  5809. +/* Owner 3 should only WRITE to this mailbox */
  5810. +#define ARM_3_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0x80) /* .. 0x8C (4 locations) */
  5811. +/*#define ARM_3_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN3+0x80) */ /* DO NOT USE THIS !!!!! */
  5812. +/*#define ARM_3_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN3+0x90) */ /* DO NOT USE THIS !!!!! */
  5813. +/*#define ARM_3_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN3+0x94) */ /* DO NOT USE THIS !!!!! */
  5814. +#define ARM_3_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN3+0x98) /* Status read */
  5815. +/*#define ARM_3_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0x9C) */ /* DO NOT USE THIS !!!!! */
  5816. +/* MAILBOX 1 access in Owner 3 area */
  5817. +/* Owner 3 should only WRITE to this mailbox */
  5818. +#define ARM_3_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) /* .. 0xAC (4 locations) */
  5819. +/*#define ARM_3_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) */ /* DO NOT USE THIS !!!!! */
  5820. +/*#define ARM_3_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN3+0xB0) */ /* DO NOT USE THIS !!!!! */
  5821. +/*#define ARM_3_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN3+0xB4) */ /* DO NOT USE THIS !!!!! */
  5822. +#define ARM_3_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN3+0xB8) /* Status read */
  5823. +/*#define ARM_3_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0xBC) */ /* DO NOT USE THIS !!!!! */
  5824. +/* General SEM, BELL, MAIL config/status */
  5825. +#define ARM_3_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE0) /* semaphore clear/debug register */
  5826. +#define ARM_3_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE4) /* Doorbells clear/debug register */
  5827. +#define ARM_3_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xFC) /* IRQS pending for owner 3 */
  5828. +#define ARM_3_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xF8) /* ALL interrupts */
  5829. +
  5830. +
  5831. +
  5832. +/* Mailbox flags. Valid for all owners */
  5833. +
  5834. +/* Mailbox status register (...0x98) */
  5835. +#define ARM_MS_FULL 0x80000000
  5836. +#define ARM_MS_EMPTY 0x40000000
  5837. +#define ARM_MS_LEVEL 0x400000FF /* Max. value depdnds on mailbox depth parameter */
  5838. +
  5839. +/* MAILBOX config/status register (...0x9C) */
  5840. +/* ANY write to this register clears the error bits! */
  5841. +#define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mailbox irq enable: has data */
  5842. +#define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mailbox irq enable: has space */
  5843. +#define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mailbox irq enable: Opp. is empty */
  5844. +#define ARM_MC_MAIL_CLEAR 0x00000008 /* mailbox clear write 1, then 0 */
  5845. +#define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mailbox irq pending: has space */
  5846. +#define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mailbox irq pending: Opp. is empty */
  5847. +#define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mailbox irq pending */
  5848. +/* Bit 7 is unused */
  5849. +#define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
  5850. +#define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
  5851. +#define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
  5852. +
  5853. +/* Semaphore clear/debug register (...0xE0) */
  5854. +#define ARM_SD_OWN0 0x00000003 /* Owner of sem 0 */
  5855. +#define ARM_SD_OWN1 0x0000000C /* Owner of sem 1 */
  5856. +#define ARM_SD_OWN2 0x00000030 /* Owner of sem 2 */
  5857. +#define ARM_SD_OWN3 0x000000C0 /* Owner of sem 3 */
  5858. +#define ARM_SD_OWN4 0x00000300 /* Owner of sem 4 */
  5859. +#define ARM_SD_OWN5 0x00000C00 /* Owner of sem 5 */
  5860. +#define ARM_SD_OWN6 0x00003000 /* Owner of sem 6 */
  5861. +#define ARM_SD_OWN7 0x0000C000 /* Owner of sem 7 */
  5862. +#define ARM_SD_SEM0 0x00010000 /* Status of sem 0 */
  5863. +#define ARM_SD_SEM1 0x00020000 /* Status of sem 1 */
  5864. +#define ARM_SD_SEM2 0x00040000 /* Status of sem 2 */
  5865. +#define ARM_SD_SEM3 0x00080000 /* Status of sem 3 */
  5866. +#define ARM_SD_SEM4 0x00100000 /* Status of sem 4 */
  5867. +#define ARM_SD_SEM5 0x00200000 /* Status of sem 5 */
  5868. +#define ARM_SD_SEM6 0x00400000 /* Status of sem 6 */
  5869. +#define ARM_SD_SEM7 0x00800000 /* Status of sem 7 */
  5870. +
  5871. +/* Doorbells clear/debug register (...0xE4) */
  5872. +#define ARM_BD_OWN0 0x00000003 /* Owner of doorbell 0 */
  5873. +#define ARM_BD_OWN1 0x0000000C /* Owner of doorbell 1 */
  5874. +#define ARM_BD_OWN2 0x00000030 /* Owner of doorbell 2 */
  5875. +#define ARM_BD_OWN3 0x000000C0 /* Owner of doorbell 3 */
  5876. +#define ARM_BD_BELL0 0x00000100 /* Status of doorbell 0 */
  5877. +#define ARM_BD_BELL1 0x00000200 /* Status of doorbell 1 */
  5878. +#define ARM_BD_BELL2 0x00000400 /* Status of doorbell 2 */
  5879. +#define ARM_BD_BELL3 0x00000800 /* Status of doorbell 3 */
  5880. +
  5881. +/* MY IRQS register (...0xF8) */
  5882. +#define ARM_MYIRQ_BELL 0x00000001 /* This owner has a doorbell IRQ */
  5883. +#define ARM_MYIRQ_MAIL 0x00000002 /* This owner has a mailbox IRQ */
  5884. +
  5885. +/* ALL IRQS register (...0xF8) */
  5886. +#define ARM_AIS_BELL0 0x00000001 /* Doorbell 0 IRQ pending */
  5887. +#define ARM_AIS_BELL1 0x00000002 /* Doorbell 1 IRQ pending */
  5888. +#define ARM_AIS_BELL2 0x00000004 /* Doorbell 2 IRQ pending */
  5889. +#define ARM_AIS_BELL3 0x00000008 /* Doorbell 3 IRQ pending */
  5890. +#define ARM_AIS0_HAVEDATA 0x00000010 /* MAIL 0 has data IRQ pending */
  5891. +#define ARM_AIS0_HAVESPAC 0x00000020 /* MAIL 0 has space IRQ pending */
  5892. +#define ARM_AIS0_OPPEMPTY 0x00000040 /* MAIL 0 opposite is empty IRQ */
  5893. +#define ARM_AIS1_HAVEDATA 0x00000080 /* MAIL 1 has data IRQ pending */
  5894. +#define ARM_AIS1_HAVESPAC 0x00000100 /* MAIL 1 has space IRQ pending */
  5895. +#define ARM_AIS1_OPPEMPTY 0x00000200 /* MAIL 1 opposite is empty IRQ */
  5896. +/* Note that bell-0, bell-1 and MAIL0 IRQ go only to the ARM */
  5897. +/* Whilst that bell-2, bell-3 and MAIL1 IRQ go only to the VC */
  5898. +/* */
  5899. +/* ARM JTAG BASH */
  5900. +/* */
  5901. +#define AJB_BASE 0x7e2000c0
  5902. +
  5903. +#define AJBCONF HW_REGISTER_RW(AJB_BASE+0x00)
  5904. +#define AJB_BITS0 0x000000
  5905. +#define AJB_BITS4 0x000004
  5906. +#define AJB_BITS8 0x000008
  5907. +#define AJB_BITS12 0x00000C
  5908. +#define AJB_BITS16 0x000010
  5909. +#define AJB_BITS20 0x000014
  5910. +#define AJB_BITS24 0x000018
  5911. +#define AJB_BITS28 0x00001C
  5912. +#define AJB_BITS32 0x000020
  5913. +#define AJB_BITS34 0x000022
  5914. +#define AJB_OUT_MS 0x000040
  5915. +#define AJB_OUT_LS 0x000000
  5916. +#define AJB_INV_CLK 0x000080
  5917. +#define AJB_D0_RISE 0x000100
  5918. +#define AJB_D0_FALL 0x000000
  5919. +#define AJB_D1_RISE 0x000200
  5920. +#define AJB_D1_FALL 0x000000
  5921. +#define AJB_IN_RISE 0x000400
  5922. +#define AJB_IN_FALL 0x000000
  5923. +#define AJB_ENABLE 0x000800
  5924. +#define AJB_HOLD0 0x000000
  5925. +#define AJB_HOLD1 0x001000
  5926. +#define AJB_HOLD2 0x002000
  5927. +#define AJB_HOLD3 0x003000
  5928. +#define AJB_RESETN 0x004000
  5929. +#define AJB_CLKSHFT 16
  5930. +#define AJB_BUSY 0x80000000
  5931. +#define AJBTMS HW_REGISTER_RW(AJB_BASE+0x04)
  5932. +#define AJBTDI HW_REGISTER_RW(AJB_BASE+0x08)
  5933. +#define AJBTDO HW_REGISTER_RW(AJB_BASE+0x0c)
  5934. +
  5935. +#endif
  5936. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/arm_power.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5937. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/arm_power.h 1970-01-01 01:00:00.000000000 +0100
  5938. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_power.h 2014-03-11 17:31:43.000000000 +0100
  5939. @@ -0,0 +1,60 @@
  5940. +/*
  5941. + * linux/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5942. + *
  5943. + * Copyright (C) 2010 Broadcom
  5944. + *
  5945. + * This program is free software; you can redistribute it and/or modify
  5946. + * it under the terms of the GNU General Public License as published by
  5947. + * the Free Software Foundation; either version 2 of the License, or
  5948. + * (at your option) any later version.
  5949. + *
  5950. + * This program is distributed in the hope that it will be useful,
  5951. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5952. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5953. + * GNU General Public License for more details.
  5954. + *
  5955. + * You should have received a copy of the GNU General Public License
  5956. + * along with this program; if not, write to the Free Software
  5957. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5958. + */
  5959. +
  5960. +#ifndef _ARM_POWER_H
  5961. +#define _ARM_POWER_H
  5962. +
  5963. +/* Use meaningful names on each side */
  5964. +#ifdef __VIDEOCORE__
  5965. +#define PREFIX(x) ARM_##x
  5966. +#else
  5967. +#define PREFIX(x) BCM_##x
  5968. +#endif
  5969. +
  5970. +enum {
  5971. + PREFIX(POWER_SDCARD_BIT),
  5972. + PREFIX(POWER_UART_BIT),
  5973. + PREFIX(POWER_MINIUART_BIT),
  5974. + PREFIX(POWER_USB_BIT),
  5975. + PREFIX(POWER_I2C0_BIT),
  5976. + PREFIX(POWER_I2C1_BIT),
  5977. + PREFIX(POWER_I2C2_BIT),
  5978. + PREFIX(POWER_SPI_BIT),
  5979. + PREFIX(POWER_CCP2TX_BIT),
  5980. +
  5981. + PREFIX(POWER_MAX)
  5982. +};
  5983. +
  5984. +enum {
  5985. + PREFIX(POWER_SDCARD) = (1 << PREFIX(POWER_SDCARD_BIT)),
  5986. + PREFIX(POWER_UART) = (1 << PREFIX(POWER_UART_BIT)),
  5987. + PREFIX(POWER_MINIUART) = (1 << PREFIX(POWER_MINIUART_BIT)),
  5988. + PREFIX(POWER_USB) = (1 << PREFIX(POWER_USB_BIT)),
  5989. + PREFIX(POWER_I2C0) = (1 << PREFIX(POWER_I2C0_BIT)),
  5990. + PREFIX(POWER_I2C1_MASK) = (1 << PREFIX(POWER_I2C1_BIT)),
  5991. + PREFIX(POWER_I2C2_MASK) = (1 << PREFIX(POWER_I2C2_BIT)),
  5992. + PREFIX(POWER_SPI_MASK) = (1 << PREFIX(POWER_SPI_BIT)),
  5993. + PREFIX(POWER_CCP2TX_MASK) = (1 << PREFIX(POWER_CCP2TX_BIT)),
  5994. +
  5995. + PREFIX(POWER_MASK) = (1 << PREFIX(POWER_MAX)) - 1,
  5996. + PREFIX(POWER_NONE) = 0
  5997. +};
  5998. +
  5999. +#endif
  6000. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/clkdev.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/clkdev.h
  6001. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/clkdev.h 1970-01-01 01:00:00.000000000 +0100
  6002. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/clkdev.h 2014-03-11 17:31:43.000000000 +0100
  6003. @@ -0,0 +1,7 @@
  6004. +#ifndef __ASM_MACH_CLKDEV_H
  6005. +#define __ASM_MACH_CLKDEV_H
  6006. +
  6007. +#define __clk_get(clk) ({ 1; })
  6008. +#define __clk_put(clk) do { } while (0)
  6009. +
  6010. +#endif
  6011. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/debug-macro.S linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/debug-macro.S
  6012. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/debug-macro.S 1970-01-01 01:00:00.000000000 +0100
  6013. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/debug-macro.S 2014-03-11 17:51:02.000000000 +0100
  6014. @@ -0,0 +1,22 @@
  6015. +/* arch/arm/mach-bcm2708/include/mach/debug-macro.S
  6016. + *
  6017. + * Debugging macro include header
  6018. + *
  6019. + * Copyright (C) 2010 Broadcom
  6020. + * Copyright (C) 1994-1999 Russell King
  6021. + * Moved from linux/arch/arm/kernel/debug.S by Ben Dooks
  6022. + *
  6023. + * This program is free software; you can redistribute it and/or modify
  6024. + * it under the terms of the GNU General Public License version 2 as
  6025. + * published by the Free Software Foundation.
  6026. + *
  6027. +*/
  6028. +
  6029. +#include <mach/platform.h>
  6030. +
  6031. + .macro addruart, rp, rv, tmp
  6032. + ldr \rp, =UART0_BASE
  6033. + ldr \rv, =IO_ADDRESS(UART0_BASE)
  6034. + .endm
  6035. +
  6036. +#include <debug/pl01x.S>
  6037. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/dma.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/dma.h
  6038. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/dma.h 1970-01-01 01:00:00.000000000 +0100
  6039. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/dma.h 2014-03-11 17:31:43.000000000 +0100
  6040. @@ -0,0 +1,90 @@
  6041. +/*
  6042. + * linux/arch/arm/mach-bcm2708/include/mach/dma.h
  6043. + *
  6044. + * Copyright (C) 2010 Broadcom
  6045. + *
  6046. + * This program is free software; you can redistribute it and/or modify
  6047. + * it under the terms of the GNU General Public License version 2 as
  6048. + * published by the Free Software Foundation.
  6049. + */
  6050. +
  6051. +
  6052. +#ifndef _MACH_BCM2708_DMA_H
  6053. +#define _MACH_BCM2708_DMA_H
  6054. +
  6055. +#define BCM_DMAMAN_DRIVER_NAME "bcm2708_dma"
  6056. +
  6057. +/* DMA CS Control and Status bits */
  6058. +#define BCM2708_DMA_ACTIVE (1 << 0)
  6059. +#define BCM2708_DMA_INT (1 << 2)
  6060. +#define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
  6061. +#define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
  6062. +#define BCM2708_DMA_ERR (1 << 8)
  6063. +#define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
  6064. +#define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
  6065. +
  6066. +/* DMA control block "info" field bits */
  6067. +#define BCM2708_DMA_INT_EN (1 << 0)
  6068. +#define BCM2708_DMA_TDMODE (1 << 1)
  6069. +#define BCM2708_DMA_WAIT_RESP (1 << 3)
  6070. +#define BCM2708_DMA_D_INC (1 << 4)
  6071. +#define BCM2708_DMA_D_WIDTH (1 << 5)
  6072. +#define BCM2708_DMA_D_DREQ (1 << 6)
  6073. +#define BCM2708_DMA_S_INC (1 << 8)
  6074. +#define BCM2708_DMA_S_WIDTH (1 << 9)
  6075. +#define BCM2708_DMA_S_DREQ (1 << 10)
  6076. +
  6077. +#define BCM2708_DMA_BURST(x) (((x)&0xf) << 12)
  6078. +#define BCM2708_DMA_PER_MAP(x) ((x) << 16)
  6079. +#define BCM2708_DMA_WAITS(x) (((x)&0x1f) << 21)
  6080. +
  6081. +#define BCM2708_DMA_DREQ_EMMC 11
  6082. +#define BCM2708_DMA_DREQ_SDHOST 13
  6083. +
  6084. +#define BCM2708_DMA_CS 0x00 /* Control and Status */
  6085. +#define BCM2708_DMA_ADDR 0x04
  6086. +/* the current control block appears in the following registers - read only */
  6087. +#define BCM2708_DMA_INFO 0x08
  6088. +#define BCM2708_DMA_SOURCE_AD 0x0c
  6089. +#define BCM2708_DMA_DEST_AD 0x10
  6090. +#define BCM2708_DMA_NEXTCB 0x1C
  6091. +#define BCM2708_DMA_DEBUG 0x20
  6092. +
  6093. +#define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4)+BCM2708_DMA_CS)
  6094. +#define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4)+BCM2708_DMA_ADDR)
  6095. +
  6096. +#define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
  6097. +
  6098. +struct bcm2708_dma_cb {
  6099. + unsigned long info;
  6100. + unsigned long src;
  6101. + unsigned long dst;
  6102. + unsigned long length;
  6103. + unsigned long stride;
  6104. + unsigned long next;
  6105. + unsigned long pad[2];
  6106. +};
  6107. +struct scatterlist;
  6108. +
  6109. +extern int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len);
  6110. +extern void bcm_dma_start(void __iomem *dma_chan_base,
  6111. + dma_addr_t control_block);
  6112. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base);
  6113. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base);
  6114. +extern int /*rc*/ bcm_dma_abort(void __iomem *dma_chan_base);
  6115. +
  6116. +/* When listing features we can ask for when allocating DMA channels give
  6117. + those with higher priority smaller ordinal numbers */
  6118. +#define BCM_DMA_FEATURE_FAST_ORD 0
  6119. +#define BCM_DMA_FEATURE_BULK_ORD 1
  6120. +#define BCM_DMA_FEATURE_FAST (1<<BCM_DMA_FEATURE_FAST_ORD)
  6121. +#define BCM_DMA_FEATURE_BULK (1<<BCM_DMA_FEATURE_BULK_ORD)
  6122. +#define BCM_DMA_FEATURE_COUNT 2
  6123. +
  6124. +/* return channel no or -ve error */
  6125. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  6126. + void __iomem **out_dma_base, int *out_dma_irq);
  6127. +extern int bcm_dma_chan_free(int channel);
  6128. +
  6129. +
  6130. +#endif /* _MACH_BCM2708_DMA_H */
  6131. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/entry-macro.S linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/entry-macro.S
  6132. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/entry-macro.S 1970-01-01 01:00:00.000000000 +0100
  6133. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/entry-macro.S 2014-03-11 17:31:43.000000000 +0100
  6134. @@ -0,0 +1,69 @@
  6135. +/*
  6136. + * arch/arm/mach-bcm2708/include/mach/entry-macro.S
  6137. + *
  6138. + * Low-level IRQ helper macros for BCM2708 platforms
  6139. + *
  6140. + * Copyright (C) 2010 Broadcom
  6141. + *
  6142. + * This program is free software; you can redistribute it and/or modify
  6143. + * it under the terms of the GNU General Public License as published by
  6144. + * the Free Software Foundation; either version 2 of the License, or
  6145. + * (at your option) any later version.
  6146. + *
  6147. + * This program is distributed in the hope that it will be useful,
  6148. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6149. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6150. + * GNU General Public License for more details.
  6151. + *
  6152. + * You should have received a copy of the GNU General Public License
  6153. + * along with this program; if not, write to the Free Software
  6154. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6155. + */
  6156. +#include <mach/hardware.h>
  6157. +
  6158. + .macro disable_fiq
  6159. + .endm
  6160. +
  6161. + .macro get_irqnr_preamble, base, tmp
  6162. + ldr \base, =IO_ADDRESS(ARMCTRL_IC_BASE)
  6163. + .endm
  6164. +
  6165. + .macro arch_ret_to_user, tmp1, tmp2
  6166. + .endm
  6167. +
  6168. + .macro get_irqnr_and_base, irqnr, irqstat, base, tmp
  6169. + /* get masked status */
  6170. + ldr \irqstat, [\base, #(ARM_IRQ_PEND0 - ARMCTRL_IC_BASE)]
  6171. + mov \irqnr, #(ARM_IRQ0_BASE + 31)
  6172. + and \tmp, \irqstat, #0x300 @ save bits 8 and 9
  6173. + /* clear bits 8 and 9, and test */
  6174. + bics \irqstat, \irqstat, #0x300
  6175. + bne 1010f
  6176. +
  6177. + tst \tmp, #0x100
  6178. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND1 - ARMCTRL_IC_BASE)]
  6179. + movne \irqnr, #(ARM_IRQ1_BASE + 31)
  6180. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  6181. + bicne \irqstat, #((1<<7) | (1<<9) | (1<<10))
  6182. + bicne \irqstat, #((1<<18) | (1<<19))
  6183. + bne 1010f
  6184. +
  6185. + tst \tmp, #0x200
  6186. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND2 - ARMCTRL_IC_BASE)]
  6187. + movne \irqnr, #(ARM_IRQ2_BASE + 31)
  6188. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  6189. + bicne \irqstat, #((1<<21) | (1<<22) | (1<<23) | (1<<24) | (1<<25))
  6190. + bicne \irqstat, #((1<<30))
  6191. + beq 1020f
  6192. +
  6193. +1010:
  6194. + @ For non-zero x, LSB(x) = 31 - CLZ(x^(x-1))
  6195. + @ N.B. CLZ is an ARM5 instruction.
  6196. + sub \tmp, \irqstat, #1
  6197. + eor \irqstat, \irqstat, \tmp
  6198. + clz \tmp, \irqstat
  6199. + sub \irqnr, \tmp
  6200. +
  6201. +1020: @ EQ will be set if no irqs pending
  6202. +
  6203. + .endm
  6204. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/frc.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/frc.h
  6205. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/frc.h 1970-01-01 01:00:00.000000000 +0100
  6206. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/frc.h 2014-03-11 17:31:43.000000000 +0100
  6207. @@ -0,0 +1,38 @@
  6208. +/*
  6209. + * arch/arm/mach-bcm2708/include/mach/timex.h
  6210. + *
  6211. + * BCM2708 free running counter (timer)
  6212. + *
  6213. + * Copyright (C) 2010 Broadcom
  6214. + *
  6215. + * This program is free software; you can redistribute it and/or modify
  6216. + * it under the terms of the GNU General Public License as published by
  6217. + * the Free Software Foundation; either version 2 of the License, or
  6218. + * (at your option) any later version.
  6219. + *
  6220. + * This program is distributed in the hope that it will be useful,
  6221. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6222. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6223. + * GNU General Public License for more details.
  6224. + *
  6225. + * You should have received a copy of the GNU General Public License
  6226. + * along with this program; if not, write to the Free Software
  6227. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6228. + */
  6229. +
  6230. +#ifndef _MACH_FRC_H
  6231. +#define _MACH_FRC_H
  6232. +
  6233. +#define FRC_TICK_RATE (1000000)
  6234. +
  6235. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  6236. + (slightly faster than frc_clock_ticks63()
  6237. + */
  6238. +extern unsigned long frc_clock_ticks32(void);
  6239. +
  6240. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  6241. + * Note - top bit should be ignored (see cnt32_to_63)
  6242. + */
  6243. +extern unsigned long long frc_clock_ticks63(void);
  6244. +
  6245. +#endif
  6246. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/gpio.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/gpio.h
  6247. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/gpio.h 1970-01-01 01:00:00.000000000 +0100
  6248. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/gpio.h 2014-03-11 17:51:02.000000000 +0100
  6249. @@ -0,0 +1,17 @@
  6250. +/*
  6251. + * arch/arm/mach-bcm2708/include/mach/gpio.h
  6252. + *
  6253. + * This file is licensed under the terms of the GNU General Public
  6254. + * License version 2. This program is licensed "as is" without any
  6255. + * warranty of any kind, whether express or implied.
  6256. + */
  6257. +
  6258. +#ifndef __ASM_ARCH_GPIO_H
  6259. +#define __ASM_ARCH_GPIO_H
  6260. +
  6261. +#define ARCH_NR_GPIOS 54 // number of gpio lines
  6262. +
  6263. +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
  6264. +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
  6265. +
  6266. +#endif
  6267. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/hardware.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/hardware.h
  6268. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/hardware.h 1970-01-01 01:00:00.000000000 +0100
  6269. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/hardware.h 2014-03-11 17:31:43.000000000 +0100
  6270. @@ -0,0 +1,28 @@
  6271. +/*
  6272. + * arch/arm/mach-bcm2708/include/mach/hardware.h
  6273. + *
  6274. + * This file contains the hardware definitions of the BCM2708 devices.
  6275. + *
  6276. + * Copyright (C) 2010 Broadcom
  6277. + *
  6278. + * This program is free software; you can redistribute it and/or modify
  6279. + * it under the terms of the GNU General Public License as published by
  6280. + * the Free Software Foundation; either version 2 of the License, or
  6281. + * (at your option) any later version.
  6282. + *
  6283. + * This program is distributed in the hope that it will be useful,
  6284. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6285. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6286. + * GNU General Public License for more details.
  6287. + *
  6288. + * You should have received a copy of the GNU General Public License
  6289. + * along with this program; if not, write to the Free Software
  6290. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6291. + */
  6292. +#ifndef __ASM_ARCH_HARDWARE_H
  6293. +#define __ASM_ARCH_HARDWARE_H
  6294. +
  6295. +#include <asm/sizes.h>
  6296. +#include <mach/platform.h>
  6297. +
  6298. +#endif
  6299. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/io.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/io.h
  6300. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/io.h 1970-01-01 01:00:00.000000000 +0100
  6301. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/io.h 2014-03-11 17:31:43.000000000 +0100
  6302. @@ -0,0 +1,27 @@
  6303. +/*
  6304. + * arch/arm/mach-bcm2708/include/mach/io.h
  6305. + *
  6306. + * Copyright (C) 2003 ARM Limited
  6307. + *
  6308. + * This program is free software; you can redistribute it and/or modify
  6309. + * it under the terms of the GNU General Public License as published by
  6310. + * the Free Software Foundation; either version 2 of the License, or
  6311. + * (at your option) any later version.
  6312. + *
  6313. + * This program is distributed in the hope that it will be useful,
  6314. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6315. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6316. + * GNU General Public License for more details.
  6317. + *
  6318. + * You should have received a copy of the GNU General Public License
  6319. + * along with this program; if not, write to the Free Software
  6320. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6321. + */
  6322. +#ifndef __ASM_ARM_ARCH_IO_H
  6323. +#define __ASM_ARM_ARCH_IO_H
  6324. +
  6325. +#define IO_SPACE_LIMIT 0xffffffff
  6326. +
  6327. +#define __io(a) __typesafe_io(a)
  6328. +
  6329. +#endif
  6330. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/irqs.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/irqs.h
  6331. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/irqs.h 1970-01-01 01:00:00.000000000 +0100
  6332. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/irqs.h 2014-03-11 17:31:43.000000000 +0100
  6333. @@ -0,0 +1,199 @@
  6334. +/*
  6335. + * arch/arm/mach-bcm2708/include/mach/irqs.h
  6336. + *
  6337. + * Copyright (C) 2010 Broadcom
  6338. + * Copyright (C) 2003 ARM Limited
  6339. + * Copyright (C) 2000 Deep Blue Solutions Ltd.
  6340. + *
  6341. + * This program is free software; you can redistribute it and/or modify
  6342. + * it under the terms of the GNU General Public License as published by
  6343. + * the Free Software Foundation; either version 2 of the License, or
  6344. + * (at your option) any later version.
  6345. + *
  6346. + * This program is distributed in the hope that it will be useful,
  6347. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6348. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6349. + * GNU General Public License for more details.
  6350. + *
  6351. + * You should have received a copy of the GNU General Public License
  6352. + * along with this program; if not, write to the Free Software
  6353. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6354. + */
  6355. +
  6356. +#ifndef _BCM2708_IRQS_H_
  6357. +#define _BCM2708_IRQS_H_
  6358. +
  6359. +#include <mach/platform.h>
  6360. +
  6361. +/*
  6362. + * IRQ interrupts definitions are the same as the INT definitions
  6363. + * held within platform.h
  6364. + */
  6365. +#define IRQ_ARMCTRL_START 0
  6366. +#define IRQ_TIMER0 (IRQ_ARMCTRL_START + INTERRUPT_TIMER0)
  6367. +#define IRQ_TIMER1 (IRQ_ARMCTRL_START + INTERRUPT_TIMER1)
  6368. +#define IRQ_TIMER2 (IRQ_ARMCTRL_START + INTERRUPT_TIMER2)
  6369. +#define IRQ_TIMER3 (IRQ_ARMCTRL_START + INTERRUPT_TIMER3)
  6370. +#define IRQ_CODEC0 (IRQ_ARMCTRL_START + INTERRUPT_CODEC0)
  6371. +#define IRQ_CODEC1 (IRQ_ARMCTRL_START + INTERRUPT_CODEC1)
  6372. +#define IRQ_CODEC2 (IRQ_ARMCTRL_START + INTERRUPT_CODEC2)
  6373. +#define IRQ_JPEG (IRQ_ARMCTRL_START + INTERRUPT_JPEG)
  6374. +#define IRQ_ISP (IRQ_ARMCTRL_START + INTERRUPT_ISP)
  6375. +#define IRQ_USB (IRQ_ARMCTRL_START + INTERRUPT_USB)
  6376. +#define IRQ_3D (IRQ_ARMCTRL_START + INTERRUPT_3D)
  6377. +#define IRQ_TRANSPOSER (IRQ_ARMCTRL_START + INTERRUPT_TRANSPOSER)
  6378. +#define IRQ_MULTICORESYNC0 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC0)
  6379. +#define IRQ_MULTICORESYNC1 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC1)
  6380. +#define IRQ_MULTICORESYNC2 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC2)
  6381. +#define IRQ_MULTICORESYNC3 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC3)
  6382. +#define IRQ_DMA0 (IRQ_ARMCTRL_START + INTERRUPT_DMA0)
  6383. +#define IRQ_DMA1 (IRQ_ARMCTRL_START + INTERRUPT_DMA1)
  6384. +#define IRQ_DMA2 (IRQ_ARMCTRL_START + INTERRUPT_DMA2)
  6385. +#define IRQ_DMA3 (IRQ_ARMCTRL_START + INTERRUPT_DMA3)
  6386. +#define IRQ_DMA4 (IRQ_ARMCTRL_START + INTERRUPT_DMA4)
  6387. +#define IRQ_DMA5 (IRQ_ARMCTRL_START + INTERRUPT_DMA5)
  6388. +#define IRQ_DMA6 (IRQ_ARMCTRL_START + INTERRUPT_DMA6)
  6389. +#define IRQ_DMA7 (IRQ_ARMCTRL_START + INTERRUPT_DMA7)
  6390. +#define IRQ_DMA8 (IRQ_ARMCTRL_START + INTERRUPT_DMA8)
  6391. +#define IRQ_DMA9 (IRQ_ARMCTRL_START + INTERRUPT_DMA9)
  6392. +#define IRQ_DMA10 (IRQ_ARMCTRL_START + INTERRUPT_DMA10)
  6393. +#define IRQ_DMA11 (IRQ_ARMCTRL_START + INTERRUPT_DMA11)
  6394. +#define IRQ_DMA12 (IRQ_ARMCTRL_START + INTERRUPT_DMA12)
  6395. +#define IRQ_AUX (IRQ_ARMCTRL_START + INTERRUPT_AUX)
  6396. +#define IRQ_ARM (IRQ_ARMCTRL_START + INTERRUPT_ARM)
  6397. +#define IRQ_VPUDMA (IRQ_ARMCTRL_START + INTERRUPT_VPUDMA)
  6398. +#define IRQ_HOSTPORT (IRQ_ARMCTRL_START + INTERRUPT_HOSTPORT)
  6399. +#define IRQ_VIDEOSCALER (IRQ_ARMCTRL_START + INTERRUPT_VIDEOSCALER)
  6400. +#define IRQ_CCP2TX (IRQ_ARMCTRL_START + INTERRUPT_CCP2TX)
  6401. +#define IRQ_SDC (IRQ_ARMCTRL_START + INTERRUPT_SDC)
  6402. +#define IRQ_DSI0 (IRQ_ARMCTRL_START + INTERRUPT_DSI0)
  6403. +#define IRQ_AVE (IRQ_ARMCTRL_START + INTERRUPT_AVE)
  6404. +#define IRQ_CAM0 (IRQ_ARMCTRL_START + INTERRUPT_CAM0)
  6405. +#define IRQ_CAM1 (IRQ_ARMCTRL_START + INTERRUPT_CAM1)
  6406. +#define IRQ_HDMI0 (IRQ_ARMCTRL_START + INTERRUPT_HDMI0)
  6407. +#define IRQ_HDMI1 (IRQ_ARMCTRL_START + INTERRUPT_HDMI1)
  6408. +#define IRQ_PIXELVALVE1 (IRQ_ARMCTRL_START + INTERRUPT_PIXELVALVE1)
  6409. +#define IRQ_I2CSPISLV (IRQ_ARMCTRL_START + INTERRUPT_I2CSPISLV)
  6410. +#define IRQ_DSI1 (IRQ_ARMCTRL_START + INTERRUPT_DSI1)
  6411. +#define IRQ_PWA0 (IRQ_ARMCTRL_START + INTERRUPT_PWA0)
  6412. +#define IRQ_PWA1 (IRQ_ARMCTRL_START + INTERRUPT_PWA1)
  6413. +#define IRQ_CPR (IRQ_ARMCTRL_START + INTERRUPT_CPR)
  6414. +#define IRQ_SMI (IRQ_ARMCTRL_START + INTERRUPT_SMI)
  6415. +#define IRQ_GPIO0 (IRQ_ARMCTRL_START + INTERRUPT_GPIO0)
  6416. +#define IRQ_GPIO1 (IRQ_ARMCTRL_START + INTERRUPT_GPIO1)
  6417. +#define IRQ_GPIO2 (IRQ_ARMCTRL_START + INTERRUPT_GPIO2)
  6418. +#define IRQ_GPIO3 (IRQ_ARMCTRL_START + INTERRUPT_GPIO3)
  6419. +#define IRQ_I2C (IRQ_ARMCTRL_START + INTERRUPT_I2C)
  6420. +#define IRQ_SPI (IRQ_ARMCTRL_START + INTERRUPT_SPI)
  6421. +#define IRQ_I2SPCM (IRQ_ARMCTRL_START + INTERRUPT_I2SPCM)
  6422. +#define IRQ_SDIO (IRQ_ARMCTRL_START + INTERRUPT_SDIO)
  6423. +#define IRQ_UART (IRQ_ARMCTRL_START + INTERRUPT_UART)
  6424. +#define IRQ_SLIMBUS (IRQ_ARMCTRL_START + INTERRUPT_SLIMBUS)
  6425. +#define IRQ_VEC (IRQ_ARMCTRL_START + INTERRUPT_VEC)
  6426. +#define IRQ_CPG (IRQ_ARMCTRL_START + INTERRUPT_CPG)
  6427. +#define IRQ_RNG (IRQ_ARMCTRL_START + INTERRUPT_RNG)
  6428. +#define IRQ_ARASANSDIO (IRQ_ARMCTRL_START + INTERRUPT_ARASANSDIO)
  6429. +#define IRQ_AVSPMON (IRQ_ARMCTRL_START + INTERRUPT_AVSPMON)
  6430. +
  6431. +#define IRQ_ARM_TIMER (IRQ_ARMCTRL_START + INTERRUPT_ARM_TIMER)
  6432. +#define IRQ_ARM_MAILBOX (IRQ_ARMCTRL_START + INTERRUPT_ARM_MAILBOX)
  6433. +#define IRQ_ARM_DOORBELL_0 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_0)
  6434. +#define IRQ_ARM_DOORBELL_1 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_1)
  6435. +#define IRQ_VPU0_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU0_HALTED)
  6436. +#define IRQ_VPU1_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU1_HALTED)
  6437. +#define IRQ_ILLEGAL_TYPE0 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE0)
  6438. +#define IRQ_ILLEGAL_TYPE1 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE1)
  6439. +#define IRQ_PENDING1 (IRQ_ARMCTRL_START + INTERRUPT_PENDING1)
  6440. +#define IRQ_PENDING2 (IRQ_ARMCTRL_START + INTERRUPT_PENDING2)
  6441. +
  6442. +#define FIQ_START HARD_IRQS
  6443. +
  6444. +/*
  6445. + * FIQ interrupts definitions are the same as the INT definitions.
  6446. + */
  6447. +#define FIQ_TIMER0 (FIQ_START+INTERRUPT_TIMER0)
  6448. +#define FIQ_TIMER1 (FIQ_START+INTERRUPT_TIMER1)
  6449. +#define FIQ_TIMER2 (FIQ_START+INTERRUPT_TIMER2)
  6450. +#define FIQ_TIMER3 (FIQ_START+INTERRUPT_TIMER3)
  6451. +#define FIQ_CODEC0 (FIQ_START+INTERRUPT_CODEC0)
  6452. +#define FIQ_CODEC1 (FIQ_START+INTERRUPT_CODEC1)
  6453. +#define FIQ_CODEC2 (FIQ_START+INTERRUPT_CODEC2)
  6454. +#define FIQ_JPEG (FIQ_START+INTERRUPT_JPEG)
  6455. +#define FIQ_ISP (FIQ_START+INTERRUPT_ISP)
  6456. +#define FIQ_USB (FIQ_START+INTERRUPT_USB)
  6457. +#define FIQ_3D (FIQ_START+INTERRUPT_3D)
  6458. +#define FIQ_TRANSPOSER (FIQ_START+INTERRUPT_TRANSPOSER)
  6459. +#define FIQ_MULTICORESYNC0 (FIQ_START+INTERRUPT_MULTICORESYNC0)
  6460. +#define FIQ_MULTICORESYNC1 (FIQ_START+INTERRUPT_MULTICORESYNC1)
  6461. +#define FIQ_MULTICORESYNC2 (FIQ_START+INTERRUPT_MULTICORESYNC2)
  6462. +#define FIQ_MULTICORESYNC3 (FIQ_START+INTERRUPT_MULTICORESYNC3)
  6463. +#define FIQ_DMA0 (FIQ_START+INTERRUPT_DMA0)
  6464. +#define FIQ_DMA1 (FIQ_START+INTERRUPT_DMA1)
  6465. +#define FIQ_DMA2 (FIQ_START+INTERRUPT_DMA2)
  6466. +#define FIQ_DMA3 (FIQ_START+INTERRUPT_DMA3)
  6467. +#define FIQ_DMA4 (FIQ_START+INTERRUPT_DMA4)
  6468. +#define FIQ_DMA5 (FIQ_START+INTERRUPT_DMA5)
  6469. +#define FIQ_DMA6 (FIQ_START+INTERRUPT_DMA6)
  6470. +#define FIQ_DMA7 (FIQ_START+INTERRUPT_DMA7)
  6471. +#define FIQ_DMA8 (FIQ_START+INTERRUPT_DMA8)
  6472. +#define FIQ_DMA9 (FIQ_START+INTERRUPT_DMA9)
  6473. +#define FIQ_DMA10 (FIQ_START+INTERRUPT_DMA10)
  6474. +#define FIQ_DMA11 (FIQ_START+INTERRUPT_DMA11)
  6475. +#define FIQ_DMA12 (FIQ_START+INTERRUPT_DMA12)
  6476. +#define FIQ_AUX (FIQ_START+INTERRUPT_AUX)
  6477. +#define FIQ_ARM (FIQ_START+INTERRUPT_ARM)
  6478. +#define FIQ_VPUDMA (FIQ_START+INTERRUPT_VPUDMA)
  6479. +#define FIQ_HOSTPORT (FIQ_START+INTERRUPT_HOSTPORT)
  6480. +#define FIQ_VIDEOSCALER (FIQ_START+INTERRUPT_VIDEOSCALER)
  6481. +#define FIQ_CCP2TX (FIQ_START+INTERRUPT_CCP2TX)
  6482. +#define FIQ_SDC (FIQ_START+INTERRUPT_SDC)
  6483. +#define FIQ_DSI0 (FIQ_START+INTERRUPT_DSI0)
  6484. +#define FIQ_AVE (FIQ_START+INTERRUPT_AVE)
  6485. +#define FIQ_CAM0 (FIQ_START+INTERRUPT_CAM0)
  6486. +#define FIQ_CAM1 (FIQ_START+INTERRUPT_CAM1)
  6487. +#define FIQ_HDMI0 (FIQ_START+INTERRUPT_HDMI0)
  6488. +#define FIQ_HDMI1 (FIQ_START+INTERRUPT_HDMI1)
  6489. +#define FIQ_PIXELVALVE1 (FIQ_START+INTERRUPT_PIXELVALVE1)
  6490. +#define FIQ_I2CSPISLV (FIQ_START+INTERRUPT_I2CSPISLV)
  6491. +#define FIQ_DSI1 (FIQ_START+INTERRUPT_DSI1)
  6492. +#define FIQ_PWA0 (FIQ_START+INTERRUPT_PWA0)
  6493. +#define FIQ_PWA1 (FIQ_START+INTERRUPT_PWA1)
  6494. +#define FIQ_CPR (FIQ_START+INTERRUPT_CPR)
  6495. +#define FIQ_SMI (FIQ_START+INTERRUPT_SMI)
  6496. +#define FIQ_GPIO0 (FIQ_START+INTERRUPT_GPIO0)
  6497. +#define FIQ_GPIO1 (FIQ_START+INTERRUPT_GPIO1)
  6498. +#define FIQ_GPIO2 (FIQ_START+INTERRUPT_GPIO2)
  6499. +#define FIQ_GPIO3 (FIQ_START+INTERRUPT_GPIO3)
  6500. +#define FIQ_I2C (FIQ_START+INTERRUPT_I2C)
  6501. +#define FIQ_SPI (FIQ_START+INTERRUPT_SPI)
  6502. +#define FIQ_I2SPCM (FIQ_START+INTERRUPT_I2SPCM)
  6503. +#define FIQ_SDIO (FIQ_START+INTERRUPT_SDIO)
  6504. +#define FIQ_UART (FIQ_START+INTERRUPT_UART)
  6505. +#define FIQ_SLIMBUS (FIQ_START+INTERRUPT_SLIMBUS)
  6506. +#define FIQ_VEC (FIQ_START+INTERRUPT_VEC)
  6507. +#define FIQ_CPG (FIQ_START+INTERRUPT_CPG)
  6508. +#define FIQ_RNG (FIQ_START+INTERRUPT_RNG)
  6509. +#define FIQ_ARASANSDIO (FIQ_START+INTERRUPT_ARASANSDIO)
  6510. +#define FIQ_AVSPMON (FIQ_START+INTERRUPT_AVSPMON)
  6511. +
  6512. +#define FIQ_ARM_TIMER (FIQ_START+INTERRUPT_ARM_TIMER)
  6513. +#define FIQ_ARM_MAILBOX (FIQ_START+INTERRUPT_ARM_MAILBOX)
  6514. +#define FIQ_ARM_DOORBELL_0 (FIQ_START+INTERRUPT_ARM_DOORBELL_0)
  6515. +#define FIQ_ARM_DOORBELL_1 (FIQ_START+INTERRUPT_ARM_DOORBELL_1)
  6516. +#define FIQ_VPU0_HALTED (FIQ_START+INTERRUPT_VPU0_HALTED)
  6517. +#define FIQ_VPU1_HALTED (FIQ_START+INTERRUPT_VPU1_HALTED)
  6518. +#define FIQ_ILLEGAL_TYPE0 (FIQ_START+INTERRUPT_ILLEGAL_TYPE0)
  6519. +#define FIQ_ILLEGAL_TYPE1 (FIQ_START+INTERRUPT_ILLEGAL_TYPE1)
  6520. +#define FIQ_PENDING1 (FIQ_START+INTERRUPT_PENDING1)
  6521. +#define FIQ_PENDING2 (FIQ_START+INTERRUPT_PENDING2)
  6522. +
  6523. +#define GPIO_IRQ_START (HARD_IRQS + FIQ_IRQS)
  6524. +
  6525. +#define HARD_IRQS (64 + 21)
  6526. +#define FIQ_IRQS (64 + 21)
  6527. +#define GPIO_IRQS (32*5)
  6528. +
  6529. +#define NR_IRQS HARD_IRQS+FIQ_IRQS+GPIO_IRQS
  6530. +
  6531. +
  6532. +#endif /* _BCM2708_IRQS_H_ */
  6533. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/memory.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/memory.h
  6534. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/memory.h 1970-01-01 01:00:00.000000000 +0100
  6535. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/memory.h 2014-03-11 17:31:43.000000000 +0100
  6536. @@ -0,0 +1,57 @@
  6537. +/*
  6538. + * arch/arm/mach-bcm2708/include/mach/memory.h
  6539. + *
  6540. + * Copyright (C) 2010 Broadcom
  6541. + *
  6542. + * This program is free software; you can redistribute it and/or modify
  6543. + * it under the terms of the GNU General Public License as published by
  6544. + * the Free Software Foundation; either version 2 of the License, or
  6545. + * (at your option) any later version.
  6546. + *
  6547. + * This program is distributed in the hope that it will be useful,
  6548. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6549. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6550. + * GNU General Public License for more details.
  6551. + *
  6552. + * You should have received a copy of the GNU General Public License
  6553. + * along with this program; if not, write to the Free Software
  6554. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6555. + */
  6556. +#ifndef __ASM_ARCH_MEMORY_H
  6557. +#define __ASM_ARCH_MEMORY_H
  6558. +
  6559. +/* Memory overview:
  6560. +
  6561. + [ARMcore] <--virtual addr-->
  6562. + [ARMmmu] <--physical addr-->
  6563. + [GERTmap] <--bus add-->
  6564. + [VCperiph]
  6565. +
  6566. +*/
  6567. +
  6568. +/*
  6569. + * Physical DRAM offset.
  6570. + */
  6571. +#define PLAT_PHYS_OFFSET UL(0x00000000)
  6572. +#define VC_ARMMEM_OFFSET UL(0x00000000) /* offset in VC of ARM memory */
  6573. +
  6574. +#ifdef CONFIG_BCM2708_NOL2CACHE
  6575. + #define _REAL_BUS_OFFSET UL(0xC0000000) /* don't use L1 or L2 caches */
  6576. +#else
  6577. + #define _REAL_BUS_OFFSET UL(0x40000000) /* use L2 cache */
  6578. +#endif
  6579. +
  6580. +/* We're using the memory at 64M in the VideoCore for Linux - this adjustment
  6581. + * will provide the offset into this area as well as setting the bits that
  6582. + * stop the L1 and L2 cache from being used
  6583. + *
  6584. + * WARNING: this only works because the ARM is given memory at a fixed location
  6585. + * (ARMMEM_OFFSET)
  6586. + */
  6587. +#define BUS_OFFSET (VC_ARMMEM_OFFSET + _REAL_BUS_OFFSET)
  6588. +#define __virt_to_bus(x) ((x) + (BUS_OFFSET - PAGE_OFFSET))
  6589. +#define __bus_to_virt(x) ((x) - (BUS_OFFSET - PAGE_OFFSET))
  6590. +#define __pfn_to_bus(x) (__pfn_to_phys(x) + (BUS_OFFSET - PLAT_PHYS_OFFSET))
  6591. +#define __bus_to_pfn(x) __phys_to_pfn((x) - (BUS_OFFSET - PLAT_PHYS_OFFSET))
  6592. +
  6593. +#endif
  6594. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/platform.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/platform.h
  6595. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/platform.h 1970-01-01 01:00:00.000000000 +0100
  6596. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/platform.h 2014-03-11 17:31:43.000000000 +0100
  6597. @@ -0,0 +1,228 @@
  6598. +/*
  6599. + * arch/arm/mach-bcm2708/include/mach/platform.h
  6600. + *
  6601. + * Copyright (C) 2010 Broadcom
  6602. + *
  6603. + * This program is free software; you can redistribute it and/or modify
  6604. + * it under the terms of the GNU General Public License as published by
  6605. + * the Free Software Foundation; either version 2 of the License, or
  6606. + * (at your option) any later version.
  6607. + *
  6608. + * This program is distributed in the hope that it will be useful,
  6609. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6610. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6611. + * GNU General Public License for more details.
  6612. + *
  6613. + * You should have received a copy of the GNU General Public License
  6614. + * along with this program; if not, write to the Free Software
  6615. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6616. + */
  6617. +
  6618. +#ifndef _BCM2708_PLATFORM_H
  6619. +#define _BCM2708_PLATFORM_H
  6620. +
  6621. +
  6622. +/* macros to get at IO space when running virtually */
  6623. +#define IO_ADDRESS(x) (((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
  6624. +
  6625. +#define __io_address(n) IOMEM(IO_ADDRESS(n))
  6626. +
  6627. +
  6628. +/*
  6629. + * SDRAM
  6630. + */
  6631. +#define BCM2708_SDRAM_BASE 0x00000000
  6632. +
  6633. +/*
  6634. + * Logic expansion modules
  6635. + *
  6636. + */
  6637. +
  6638. +
  6639. +/* ------------------------------------------------------------------------
  6640. + * BCM2708 ARMCTRL Registers
  6641. + * ------------------------------------------------------------------------
  6642. + */
  6643. +
  6644. +#define HW_REGISTER_RW(addr) (addr)
  6645. +#define HW_REGISTER_RO(addr) (addr)
  6646. +
  6647. +#include "arm_control.h"
  6648. +#undef ARM_BASE
  6649. +
  6650. +/*
  6651. + * Definitions and addresses for the ARM CONTROL logic
  6652. + * This file is manually generated.
  6653. + */
  6654. +
  6655. +#define BCM2708_PERI_BASE 0x20000000
  6656. +#define IC0_BASE (BCM2708_PERI_BASE + 0x2000)
  6657. +#define ST_BASE (BCM2708_PERI_BASE + 0x3000) /* System Timer */
  6658. +#define MPHI_BASE (BCM2708_PERI_BASE + 0x6000) /* Message -based Parallel Host Interface */
  6659. +#define DMA_BASE (BCM2708_PERI_BASE + 0x7000) /* DMA controller */
  6660. +#define ARM_BASE (BCM2708_PERI_BASE + 0xB000) /* BCM2708 ARM control block */
  6661. +#define PM_BASE (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
  6662. +#define PCM_CLOCK_BASE (BCM2708_PERI_BASE + 0x101098) /* PCM Clock */
  6663. +#define RNG_BASE (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
  6664. +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */
  6665. +#define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */
  6666. +#define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */
  6667. +#define I2S_BASE (BCM2708_PERI_BASE + 0x203000) /* I2S */
  6668. +#define SPI0_BASE (BCM2708_PERI_BASE + 0x204000) /* SPI0 */
  6669. +#define BSC0_BASE (BCM2708_PERI_BASE + 0x205000) /* BSC0 I2C/TWI */
  6670. +#define UART1_BASE (BCM2708_PERI_BASE + 0x215000) /* Uart 1 */
  6671. +#define EMMC_BASE (BCM2708_PERI_BASE + 0x300000) /* eMMC interface */
  6672. +#define SMI_BASE (BCM2708_PERI_BASE + 0x600000) /* SMI */
  6673. +#define BSC1_BASE (BCM2708_PERI_BASE + 0x804000) /* BSC1 I2C/TWI */
  6674. +#define USB_BASE (BCM2708_PERI_BASE + 0x980000) /* DTC_OTG USB controller */
  6675. +#define MCORE_BASE (BCM2708_PERI_BASE + 0x0000) /* Fake frame buffer device (actually the multicore sync block*/
  6676. +
  6677. +#define ARMCTRL_BASE (ARM_BASE + 0x000)
  6678. +#define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  6679. +#define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  6680. +#define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  6681. +
  6682. +
  6683. +/*
  6684. + * Interrupt assignments
  6685. + */
  6686. +
  6687. +#define ARM_IRQ1_BASE 0
  6688. +#define INTERRUPT_TIMER0 (ARM_IRQ1_BASE + 0)
  6689. +#define INTERRUPT_TIMER1 (ARM_IRQ1_BASE + 1)
  6690. +#define INTERRUPT_TIMER2 (ARM_IRQ1_BASE + 2)
  6691. +#define INTERRUPT_TIMER3 (ARM_IRQ1_BASE + 3)
  6692. +#define INTERRUPT_CODEC0 (ARM_IRQ1_BASE + 4)
  6693. +#define INTERRUPT_CODEC1 (ARM_IRQ1_BASE + 5)
  6694. +#define INTERRUPT_CODEC2 (ARM_IRQ1_BASE + 6)
  6695. +#define INTERRUPT_VC_JPEG (ARM_IRQ1_BASE + 7)
  6696. +#define INTERRUPT_ISP (ARM_IRQ1_BASE + 8)
  6697. +#define INTERRUPT_VC_USB (ARM_IRQ1_BASE + 9)
  6698. +#define INTERRUPT_VC_3D (ARM_IRQ1_BASE + 10)
  6699. +#define INTERRUPT_TRANSPOSER (ARM_IRQ1_BASE + 11)
  6700. +#define INTERRUPT_MULTICORESYNC0 (ARM_IRQ1_BASE + 12)
  6701. +#define INTERRUPT_MULTICORESYNC1 (ARM_IRQ1_BASE + 13)
  6702. +#define INTERRUPT_MULTICORESYNC2 (ARM_IRQ1_BASE + 14)
  6703. +#define INTERRUPT_MULTICORESYNC3 (ARM_IRQ1_BASE + 15)
  6704. +#define INTERRUPT_DMA0 (ARM_IRQ1_BASE + 16)
  6705. +#define INTERRUPT_DMA1 (ARM_IRQ1_BASE + 17)
  6706. +#define INTERRUPT_VC_DMA2 (ARM_IRQ1_BASE + 18)
  6707. +#define INTERRUPT_VC_DMA3 (ARM_IRQ1_BASE + 19)
  6708. +#define INTERRUPT_DMA4 (ARM_IRQ1_BASE + 20)
  6709. +#define INTERRUPT_DMA5 (ARM_IRQ1_BASE + 21)
  6710. +#define INTERRUPT_DMA6 (ARM_IRQ1_BASE + 22)
  6711. +#define INTERRUPT_DMA7 (ARM_IRQ1_BASE + 23)
  6712. +#define INTERRUPT_DMA8 (ARM_IRQ1_BASE + 24)
  6713. +#define INTERRUPT_DMA9 (ARM_IRQ1_BASE + 25)
  6714. +#define INTERRUPT_DMA10 (ARM_IRQ1_BASE + 26)
  6715. +#define INTERRUPT_DMA11 (ARM_IRQ1_BASE + 27)
  6716. +#define INTERRUPT_DMA12 (ARM_IRQ1_BASE + 28)
  6717. +#define INTERRUPT_AUX (ARM_IRQ1_BASE + 29)
  6718. +#define INTERRUPT_ARM (ARM_IRQ1_BASE + 30)
  6719. +#define INTERRUPT_VPUDMA (ARM_IRQ1_BASE + 31)
  6720. +
  6721. +#define ARM_IRQ2_BASE 32
  6722. +#define INTERRUPT_HOSTPORT (ARM_IRQ2_BASE + 0)
  6723. +#define INTERRUPT_VIDEOSCALER (ARM_IRQ2_BASE + 1)
  6724. +#define INTERRUPT_CCP2TX (ARM_IRQ2_BASE + 2)
  6725. +#define INTERRUPT_SDC (ARM_IRQ2_BASE + 3)
  6726. +#define INTERRUPT_DSI0 (ARM_IRQ2_BASE + 4)
  6727. +#define INTERRUPT_AVE (ARM_IRQ2_BASE + 5)
  6728. +#define INTERRUPT_CAM0 (ARM_IRQ2_BASE + 6)
  6729. +#define INTERRUPT_CAM1 (ARM_IRQ2_BASE + 7)
  6730. +#define INTERRUPT_HDMI0 (ARM_IRQ2_BASE + 8)
  6731. +#define INTERRUPT_HDMI1 (ARM_IRQ2_BASE + 9)
  6732. +#define INTERRUPT_PIXELVALVE1 (ARM_IRQ2_BASE + 10)
  6733. +#define INTERRUPT_I2CSPISLV (ARM_IRQ2_BASE + 11)
  6734. +#define INTERRUPT_DSI1 (ARM_IRQ2_BASE + 12)
  6735. +#define INTERRUPT_PWA0 (ARM_IRQ2_BASE + 13)
  6736. +#define INTERRUPT_PWA1 (ARM_IRQ2_BASE + 14)
  6737. +#define INTERRUPT_CPR (ARM_IRQ2_BASE + 15)
  6738. +#define INTERRUPT_SMI (ARM_IRQ2_BASE + 16)
  6739. +#define INTERRUPT_GPIO0 (ARM_IRQ2_BASE + 17)
  6740. +#define INTERRUPT_GPIO1 (ARM_IRQ2_BASE + 18)
  6741. +#define INTERRUPT_GPIO2 (ARM_IRQ2_BASE + 19)
  6742. +#define INTERRUPT_GPIO3 (ARM_IRQ2_BASE + 20)
  6743. +#define INTERRUPT_VC_I2C (ARM_IRQ2_BASE + 21)
  6744. +#define INTERRUPT_VC_SPI (ARM_IRQ2_BASE + 22)
  6745. +#define INTERRUPT_VC_I2SPCM (ARM_IRQ2_BASE + 23)
  6746. +#define INTERRUPT_VC_SDIO (ARM_IRQ2_BASE + 24)
  6747. +#define INTERRUPT_VC_UART (ARM_IRQ2_BASE + 25)
  6748. +#define INTERRUPT_SLIMBUS (ARM_IRQ2_BASE + 26)
  6749. +#define INTERRUPT_VEC (ARM_IRQ2_BASE + 27)
  6750. +#define INTERRUPT_CPG (ARM_IRQ2_BASE + 28)
  6751. +#define INTERRUPT_RNG (ARM_IRQ2_BASE + 29)
  6752. +#define INTERRUPT_VC_ARASANSDIO (ARM_IRQ2_BASE + 30)
  6753. +#define INTERRUPT_AVSPMON (ARM_IRQ2_BASE + 31)
  6754. +
  6755. +#define ARM_IRQ0_BASE 64
  6756. +#define INTERRUPT_ARM_TIMER (ARM_IRQ0_BASE + 0)
  6757. +#define INTERRUPT_ARM_MAILBOX (ARM_IRQ0_BASE + 1)
  6758. +#define INTERRUPT_ARM_DOORBELL_0 (ARM_IRQ0_BASE + 2)
  6759. +#define INTERRUPT_ARM_DOORBELL_1 (ARM_IRQ0_BASE + 3)
  6760. +#define INTERRUPT_VPU0_HALTED (ARM_IRQ0_BASE + 4)
  6761. +#define INTERRUPT_VPU1_HALTED (ARM_IRQ0_BASE + 5)
  6762. +#define INTERRUPT_ILLEGAL_TYPE0 (ARM_IRQ0_BASE + 6)
  6763. +#define INTERRUPT_ILLEGAL_TYPE1 (ARM_IRQ0_BASE + 7)
  6764. +#define INTERRUPT_PENDING1 (ARM_IRQ0_BASE + 8)
  6765. +#define INTERRUPT_PENDING2 (ARM_IRQ0_BASE + 9)
  6766. +#define INTERRUPT_JPEG (ARM_IRQ0_BASE + 10)
  6767. +#define INTERRUPT_USB (ARM_IRQ0_BASE + 11)
  6768. +#define INTERRUPT_3D (ARM_IRQ0_BASE + 12)
  6769. +#define INTERRUPT_DMA2 (ARM_IRQ0_BASE + 13)
  6770. +#define INTERRUPT_DMA3 (ARM_IRQ0_BASE + 14)
  6771. +#define INTERRUPT_I2C (ARM_IRQ0_BASE + 15)
  6772. +#define INTERRUPT_SPI (ARM_IRQ0_BASE + 16)
  6773. +#define INTERRUPT_I2SPCM (ARM_IRQ0_BASE + 17)
  6774. +#define INTERRUPT_SDIO (ARM_IRQ0_BASE + 18)
  6775. +#define INTERRUPT_UART (ARM_IRQ0_BASE + 19)
  6776. +#define INTERRUPT_ARASANSDIO (ARM_IRQ0_BASE + 20)
  6777. +
  6778. +#define MAXIRQNUM (32 + 32 + 20)
  6779. +#define MAXFIQNUM (32 + 32 + 20)
  6780. +
  6781. +#define MAX_TIMER 2
  6782. +#define MAX_PERIOD 699050
  6783. +#define TICKS_PER_uSEC 1
  6784. +
  6785. +/*
  6786. + * These are useconds NOT ticks.
  6787. + *
  6788. + */
  6789. +#define mSEC_1 1000
  6790. +#define mSEC_5 (mSEC_1 * 5)
  6791. +#define mSEC_10 (mSEC_1 * 10)
  6792. +#define mSEC_25 (mSEC_1 * 25)
  6793. +#define SEC_1 (mSEC_1 * 1000)
  6794. +
  6795. +/*
  6796. + * Watchdog
  6797. + */
  6798. +#define PM_RSTC (PM_BASE+0x1c)
  6799. +#define PM_RSTS (PM_BASE+0x20)
  6800. +#define PM_WDOG (PM_BASE+0x24)
  6801. +
  6802. +#define PM_WDOG_RESET 0000000000
  6803. +#define PM_PASSWORD 0x5a000000
  6804. +#define PM_WDOG_TIME_SET 0x000fffff
  6805. +#define PM_RSTC_WRCFG_CLR 0xffffffcf
  6806. +#define PM_RSTC_WRCFG_SET 0x00000030
  6807. +#define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  6808. +#define PM_RSTC_RESET 0x00000102
  6809. +
  6810. +#define PM_RSTS_HADPOR_SET 0x00001000
  6811. +#define PM_RSTS_HADSRH_SET 0x00000400
  6812. +#define PM_RSTS_HADSRF_SET 0x00000200
  6813. +#define PM_RSTS_HADSRQ_SET 0x00000100
  6814. +#define PM_RSTS_HADWRH_SET 0x00000040
  6815. +#define PM_RSTS_HADWRF_SET 0x00000020
  6816. +#define PM_RSTS_HADWRQ_SET 0x00000010
  6817. +#define PM_RSTS_HADDRH_SET 0x00000004
  6818. +#define PM_RSTS_HADDRF_SET 0x00000002
  6819. +#define PM_RSTS_HADDRQ_SET 0x00000001
  6820. +
  6821. +#define UART0_CLOCK 3000000
  6822. +
  6823. +#endif
  6824. +
  6825. +/* END */
  6826. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/power.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/power.h
  6827. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/power.h 1970-01-01 01:00:00.000000000 +0100
  6828. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/power.h 2014-03-11 17:31:43.000000000 +0100
  6829. @@ -0,0 +1,26 @@
  6830. +/*
  6831. + * linux/arch/arm/mach-bcm2708/power.h
  6832. + *
  6833. + * Copyright (C) 2010 Broadcom
  6834. + *
  6835. + * This program is free software; you can redistribute it and/or modify
  6836. + * it under the terms of the GNU General Public License version 2 as
  6837. + * published by the Free Software Foundation.
  6838. + *
  6839. + * This device provides a shared mechanism for controlling the power to
  6840. + * VideoCore subsystems.
  6841. + */
  6842. +
  6843. +#ifndef _MACH_BCM2708_POWER_H
  6844. +#define _MACH_BCM2708_POWER_H
  6845. +
  6846. +#include <linux/types.h>
  6847. +#include <mach/arm_power.h>
  6848. +
  6849. +typedef unsigned int BCM_POWER_HANDLE_T;
  6850. +
  6851. +extern int bcm_power_open(BCM_POWER_HANDLE_T *handle);
  6852. +extern int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request);
  6853. +extern int bcm_power_close(BCM_POWER_HANDLE_T handle);
  6854. +
  6855. +#endif
  6856. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/system.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/system.h
  6857. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/system.h 1970-01-01 01:00:00.000000000 +0100
  6858. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/system.h 2014-03-11 17:31:43.000000000 +0100
  6859. @@ -0,0 +1,38 @@
  6860. +/*
  6861. + * arch/arm/mach-bcm2708/include/mach/system.h
  6862. + *
  6863. + * Copyright (C) 2010 Broadcom
  6864. + * Copyright (C) 2003 ARM Limited
  6865. + * Copyright (C) 2000 Deep Blue Solutions Ltd
  6866. + *
  6867. + * This program is free software; you can redistribute it and/or modify
  6868. + * it under the terms of the GNU General Public License as published by
  6869. + * the Free Software Foundation; either version 2 of the License, or
  6870. + * (at your option) any later version.
  6871. + *
  6872. + * This program is distributed in the hope that it will be useful,
  6873. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6874. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6875. + * GNU General Public License for more details.
  6876. + *
  6877. + * You should have received a copy of the GNU General Public License
  6878. + * along with this program; if not, write to the Free Software
  6879. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6880. + */
  6881. +#ifndef __ASM_ARCH_SYSTEM_H
  6882. +#define __ASM_ARCH_SYSTEM_H
  6883. +
  6884. +#include <linux/io.h>
  6885. +#include <mach/hardware.h>
  6886. +#include <mach/platform.h>
  6887. +
  6888. +static inline void arch_idle(void)
  6889. +{
  6890. + /*
  6891. + * This should do all the clock switching
  6892. + * and wait for interrupt tricks
  6893. + */
  6894. + cpu_do_idle();
  6895. +}
  6896. +
  6897. +#endif
  6898. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/timex.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/timex.h
  6899. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/timex.h 1970-01-01 01:00:00.000000000 +0100
  6900. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/timex.h 2014-03-11 17:31:43.000000000 +0100
  6901. @@ -0,0 +1,23 @@
  6902. +/*
  6903. + * arch/arm/mach-bcm2708/include/mach/timex.h
  6904. + *
  6905. + * BCM2708 sysem clock frequency
  6906. + *
  6907. + * Copyright (C) 2010 Broadcom
  6908. + *
  6909. + * This program is free software; you can redistribute it and/or modify
  6910. + * it under the terms of the GNU General Public License as published by
  6911. + * the Free Software Foundation; either version 2 of the License, or
  6912. + * (at your option) any later version.
  6913. + *
  6914. + * This program is distributed in the hope that it will be useful,
  6915. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6916. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6917. + * GNU General Public License for more details.
  6918. + *
  6919. + * You should have received a copy of the GNU General Public License
  6920. + * along with this program; if not, write to the Free Software
  6921. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6922. + */
  6923. +
  6924. +#define CLOCK_TICK_RATE (1000000)
  6925. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/uncompress.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/uncompress.h
  6926. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/uncompress.h 1970-01-01 01:00:00.000000000 +0100
  6927. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/uncompress.h 2014-03-11 17:51:02.000000000 +0100
  6928. @@ -0,0 +1,84 @@
  6929. +/*
  6930. + * arch/arm/mach-bcn2708/include/mach/uncompress.h
  6931. + *
  6932. + * Copyright (C) 2010 Broadcom
  6933. + * Copyright (C) 2003 ARM Limited
  6934. + *
  6935. + * This program is free software; you can redistribute it and/or modify
  6936. + * it under the terms of the GNU General Public License as published by
  6937. + * the Free Software Foundation; either version 2 of the License, or
  6938. + * (at your option) any later version.
  6939. + *
  6940. + * This program is distributed in the hope that it will be useful,
  6941. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6942. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6943. + * GNU General Public License for more details.
  6944. + *
  6945. + * You should have received a copy of the GNU General Public License
  6946. + * along with this program; if not, write to the Free Software
  6947. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6948. + */
  6949. +
  6950. +#include <linux/io.h>
  6951. +#include <linux/amba/serial.h>
  6952. +#include <mach/hardware.h>
  6953. +
  6954. +#define UART_BAUD 115200
  6955. +
  6956. +#define BCM2708_UART_DR __io(UART0_BASE + UART01x_DR)
  6957. +#define BCM2708_UART_FR __io(UART0_BASE + UART01x_FR)
  6958. +#define BCM2708_UART_IBRD __io(UART0_BASE + UART011_IBRD)
  6959. +#define BCM2708_UART_FBRD __io(UART0_BASE + UART011_FBRD)
  6960. +#define BCM2708_UART_LCRH __io(UART0_BASE + UART011_LCRH)
  6961. +#define BCM2708_UART_CR __io(UART0_BASE + UART011_CR)
  6962. +
  6963. +/*
  6964. + * This does not append a newline
  6965. + */
  6966. +static inline void putc(int c)
  6967. +{
  6968. + while (__raw_readl(BCM2708_UART_FR) & UART01x_FR_TXFF)
  6969. + barrier();
  6970. +
  6971. + __raw_writel(c, BCM2708_UART_DR);
  6972. +}
  6973. +
  6974. +static inline void flush(void)
  6975. +{
  6976. + int fr;
  6977. +
  6978. + do {
  6979. + fr = __raw_readl(BCM2708_UART_FR);
  6980. + barrier();
  6981. + } while ((fr & (UART011_FR_TXFE | UART01x_FR_BUSY)) != UART011_FR_TXFE);
  6982. +}
  6983. +
  6984. +static inline void arch_decomp_setup(void)
  6985. +{
  6986. + int temp, div, rem, frac;
  6987. +
  6988. + temp = 16 * UART_BAUD;
  6989. + div = UART0_CLOCK / temp;
  6990. + rem = UART0_CLOCK % temp;
  6991. + temp = (8 * rem) / UART_BAUD;
  6992. + frac = (temp >> 1) + (temp & 1);
  6993. +
  6994. + /* Make sure the UART is disabled before we start */
  6995. + __raw_writel(0, BCM2708_UART_CR);
  6996. +
  6997. + /* Set the baud rate */
  6998. + __raw_writel(div, BCM2708_UART_IBRD);
  6999. + __raw_writel(frac, BCM2708_UART_FBRD);
  7000. +
  7001. + /* Set the UART to 8n1, FIFO enabled */
  7002. + __raw_writel(UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN, BCM2708_UART_LCRH);
  7003. +
  7004. + /* Enable the UART */
  7005. + __raw_writel(UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_RXE,
  7006. + BCM2708_UART_CR);
  7007. +}
  7008. +
  7009. +/*
  7010. + * nothing to do
  7011. + */
  7012. +#define arch_decomp_wdog()
  7013. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vcio.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vcio.h
  7014. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vcio.h 1970-01-01 01:00:00.000000000 +0100
  7015. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vcio.h 2014-03-11 17:51:02.000000000 +0100
  7016. @@ -0,0 +1,141 @@
  7017. +/*
  7018. + * arch/arm/mach-bcm2708/include/mach/vcio.h
  7019. + *
  7020. + * Copyright (C) 2010 Broadcom
  7021. + *
  7022. + * This program is free software; you can redistribute it and/or modify
  7023. + * it under the terms of the GNU General Public License as published by
  7024. + * the Free Software Foundation; either version 2 of the License, or
  7025. + * (at your option) any later version.
  7026. + *
  7027. + * This program is distributed in the hope that it will be useful,
  7028. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7029. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7030. + * GNU General Public License for more details.
  7031. + *
  7032. + * You should have received a copy of the GNU General Public License
  7033. + * along with this program; if not, write to the Free Software
  7034. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7035. + */
  7036. +#ifndef _MACH_BCM2708_VCIO_H
  7037. +#define _MACH_BCM2708_VCIO_H
  7038. +
  7039. +/* Routines to handle I/O via the VideoCore "ARM control" registers
  7040. + * (semaphores, doorbells, mailboxes)
  7041. + */
  7042. +
  7043. +#define BCM_VCIO_DRIVER_NAME "bcm2708_vcio"
  7044. +
  7045. +/* Constants shared with the ARM identifying separate mailbox channels */
  7046. +#define MBOX_CHAN_POWER 0 /* for use by the power management interface */
  7047. +#define MBOX_CHAN_FB 1 /* for use by the frame buffer */
  7048. +#define MBOX_CHAN_VCHIQ 3 /* for use by the VCHIQ interface */
  7049. +#define MBOX_CHAN_PROPERTY 8 /* for use by the property channel */
  7050. +#define MBOX_CHAN_COUNT 9
  7051. +
  7052. +/* Mailbox property tags */
  7053. +enum {
  7054. + VCMSG_PROPERTY_END = 0x00000000,
  7055. + VCMSG_GET_FIRMWARE_REVISION = 0x00000001,
  7056. + VCMSG_GET_BOARD_MODEL = 0x00010001,
  7057. + VCMSG_GET_BOARD_REVISION = 0x00020002,
  7058. + VCMSG_GET_BOARD_MAC_ADDRESS = 0x00020003,
  7059. + VCMSG_GET_BOARD_SERIAL = 0x00020004,
  7060. + VCMSG_GET_ARM_MEMORY = 0x00020005,
  7061. + VCMSG_GET_VC_MEMORY = 0x00020006,
  7062. + VCMSG_GET_CLOCKS = 0x00020007,
  7063. + VCMSG_GET_COMMAND_LINE = 0x00050001,
  7064. + VCMSG_GET_DMA_CHANNELS = 0x00060001,
  7065. + VCMSG_GET_POWER_STATE = 0x00020001,
  7066. + VCMSG_GET_TIMING = 0x00020002,
  7067. + VCMSG_SET_POWER_STATE = 0x00028001,
  7068. + VCMSG_GET_CLOCK_STATE = 0x00030001,
  7069. + VCMSG_SET_CLOCK_STATE = 0x00038001,
  7070. + VCMSG_GET_CLOCK_RATE = 0x00030002,
  7071. + VCMSG_SET_CLOCK_RATE = 0x00038002,
  7072. + VCMSG_GET_VOLTAGE = 0x00030003,
  7073. + VCMSG_SET_VOLTAGE = 0x00038003,
  7074. + VCMSG_GET_MAX_CLOCK = 0x00030004,
  7075. + VCMSG_GET_MAX_VOLTAGE = 0x00030005,
  7076. + VCMSG_GET_TEMPERATURE = 0x00030006,
  7077. + VCMSG_GET_MIN_CLOCK = 0x00030007,
  7078. + VCMSG_GET_MIN_VOLTAGE = 0x00030008,
  7079. + VCMSG_GET_TURBO = 0x00030009,
  7080. + VCMSG_SET_TURBO = 0x00038009,
  7081. + VCMSG_SET_ALLOCATE_BUFFER = 0x00040001,
  7082. + VCMSG_SET_RELEASE_BUFFER = 0x00048001,
  7083. + VCMSG_SET_BLANK_SCREEN = 0x00040002,
  7084. + VCMSG_TST_BLANK_SCREEN = 0x00044002,
  7085. + VCMSG_GET_PHYSICAL_WIDTH_HEIGHT = 0x00040003,
  7086. + VCMSG_TST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
  7087. + VCMSG_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
  7088. + VCMSG_GET_VIRTUAL_WIDTH_HEIGHT = 0x00040004,
  7089. + VCMSG_TST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
  7090. + VCMSG_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
  7091. + VCMSG_GET_DEPTH = 0x00040005,
  7092. + VCMSG_TST_DEPTH = 0x00044005,
  7093. + VCMSG_SET_DEPTH = 0x00048005,
  7094. + VCMSG_GET_PIXEL_ORDER = 0x00040006,
  7095. + VCMSG_TST_PIXEL_ORDER = 0x00044006,
  7096. + VCMSG_SET_PIXEL_ORDER = 0x00048006,
  7097. + VCMSG_GET_ALPHA_MODE = 0x00040007,
  7098. + VCMSG_TST_ALPHA_MODE = 0x00044007,
  7099. + VCMSG_SET_ALPHA_MODE = 0x00048007,
  7100. + VCMSG_GET_PITCH = 0x00040008,
  7101. + VCMSG_TST_PITCH = 0x00044008,
  7102. + VCMSG_SET_PITCH = 0x00048008,
  7103. + VCMSG_GET_VIRTUAL_OFFSET = 0x00040009,
  7104. + VCMSG_TST_VIRTUAL_OFFSET = 0x00044009,
  7105. + VCMSG_SET_VIRTUAL_OFFSET = 0x00048009,
  7106. + VCMSG_GET_OVERSCAN = 0x0004000a,
  7107. + VCMSG_TST_OVERSCAN = 0x0004400a,
  7108. + VCMSG_SET_OVERSCAN = 0x0004800a,
  7109. + VCMSG_GET_PALETTE = 0x0004000b,
  7110. + VCMSG_TST_PALETTE = 0x0004400b,
  7111. + VCMSG_SET_PALETTE = 0x0004800b,
  7112. + VCMSG_GET_LAYER = 0x0004000c,
  7113. + VCMSG_TST_LAYER = 0x0004400c,
  7114. + VCMSG_SET_LAYER = 0x0004800c,
  7115. + VCMSG_GET_TRANSFORM = 0x0004000d,
  7116. + VCMSG_TST_TRANSFORM = 0x0004400d,
  7117. + VCMSG_SET_TRANSFORM = 0x0004800d,
  7118. +};
  7119. +
  7120. +extern int /*rc*/ bcm_mailbox_read(unsigned chan, uint32_t *data28);
  7121. +extern int /*rc*/ bcm_mailbox_write(unsigned chan, uint32_t data28);
  7122. +extern int /*rc*/ bcm_mailbox_property(void *data, int size);
  7123. +
  7124. +#include <linux/ioctl.h>
  7125. +
  7126. +/*
  7127. + * The major device number. We can't rely on dynamic
  7128. + * registration any more, because ioctls need to know
  7129. + * it.
  7130. + */
  7131. +#define MAJOR_NUM 100
  7132. +
  7133. +/*
  7134. + * Set the message of the device driver
  7135. + */
  7136. +#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *)
  7137. +/*
  7138. + * _IOWR means that we're creating an ioctl command
  7139. + * number for passing information from a user process
  7140. + * to the kernel module and from the kernel module to user process
  7141. + *
  7142. + * The first arguments, MAJOR_NUM, is the major device
  7143. + * number we're using.
  7144. + *
  7145. + * The second argument is the number of the command
  7146. + * (there could be several with different meanings).
  7147. + *
  7148. + * The third argument is the type we want to get from
  7149. + * the process to the kernel.
  7150. + */
  7151. +
  7152. +/*
  7153. + * The name of the device file
  7154. + */
  7155. +#define DEVICE_FILE_NAME "char_dev"
  7156. +
  7157. +#endif
  7158. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vc_mem.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vc_mem.h
  7159. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vc_mem.h 1970-01-01 01:00:00.000000000 +0100
  7160. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vc_mem.h 2014-03-11 17:51:02.000000000 +0100
  7161. @@ -0,0 +1,35 @@
  7162. +/*****************************************************************************
  7163. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  7164. +*
  7165. +* Unless you and Broadcom execute a separate written software license
  7166. +* agreement governing use of this software, this software is licensed to you
  7167. +* under the terms of the GNU General Public License version 2, available at
  7168. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7169. +*
  7170. +* Notwithstanding the above, under no circumstances may you combine this
  7171. +* software in any way with any other Broadcom software provided under a
  7172. +* license other than the GPL, without Broadcom's express prior written
  7173. +* consent.
  7174. +*****************************************************************************/
  7175. +
  7176. +#if !defined( VC_MEM_H )
  7177. +#define VC_MEM_H
  7178. +
  7179. +#include <linux/ioctl.h>
  7180. +
  7181. +#define VC_MEM_IOC_MAGIC 'v'
  7182. +
  7183. +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
  7184. +#define VC_MEM_IOC_MEM_SIZE _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
  7185. +#define VC_MEM_IOC_MEM_BASE _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
  7186. +#define VC_MEM_IOC_MEM_LOAD _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
  7187. +
  7188. +#if defined( __KERNEL__ )
  7189. +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
  7190. +
  7191. +extern unsigned long mm_vc_mem_phys_addr;
  7192. +extern unsigned int mm_vc_mem_size;
  7193. +extern int vc_mem_get_current_size( void );
  7194. +#endif
  7195. +
  7196. +#endif /* VC_MEM_H */
  7197. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vc_support.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vc_support.h
  7198. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vc_support.h 1970-01-01 01:00:00.000000000 +0100
  7199. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vc_support.h 2014-03-11 17:31:43.000000000 +0100
  7200. @@ -0,0 +1,69 @@
  7201. +#ifndef _VC_SUPPORT_H_
  7202. +#define _VC_SUPPORT_H_
  7203. +
  7204. +/*
  7205. + * vc_support.h
  7206. + *
  7207. + * Created on: 25 Nov 2012
  7208. + * Author: Simon
  7209. + */
  7210. +
  7211. +enum {
  7212. +/*
  7213. + If a MEM_HANDLE_T is discardable, the memory manager may resize it to size
  7214. + 0 at any time when it is not locked or retained.
  7215. + */
  7216. + MEM_FLAG_DISCARDABLE = 1 << 0,
  7217. +
  7218. + /*
  7219. + If a MEM_HANDLE_T is allocating (or normal), its block of memory will be
  7220. + accessed in an allocating fashion through the cache.
  7221. + */
  7222. + MEM_FLAG_NORMAL = 0 << 2,
  7223. + MEM_FLAG_ALLOCATING = MEM_FLAG_NORMAL,
  7224. +
  7225. + /*
  7226. + If a MEM_HANDLE_T is direct, its block of memory will be accessed
  7227. + directly, bypassing the cache.
  7228. + */
  7229. + MEM_FLAG_DIRECT = 1 << 2,
  7230. +
  7231. + /*
  7232. + If a MEM_HANDLE_T is coherent, its block of memory will be accessed in a
  7233. + non-allocating fashion through the cache.
  7234. + */
  7235. + MEM_FLAG_COHERENT = 2 << 2,
  7236. +
  7237. + /*
  7238. + If a MEM_HANDLE_T is L1-nonallocating, its block of memory will be accessed by
  7239. + the VPU in a fashion which is allocating in L2, but only coherent in L1.
  7240. + */
  7241. + MEM_FLAG_L1_NONALLOCATING = (MEM_FLAG_DIRECT | MEM_FLAG_COHERENT),
  7242. +
  7243. + /*
  7244. + If a MEM_HANDLE_T is zero'd, its contents are set to 0 rather than
  7245. + MEM_HANDLE_INVALID on allocation and resize up.
  7246. + */
  7247. + MEM_FLAG_ZERO = 1 << 4,
  7248. +
  7249. + /*
  7250. + If a MEM_HANDLE_T is uninitialised, it will not be reset to a defined value
  7251. + (either zero, or all 1's) on allocation.
  7252. + */
  7253. + MEM_FLAG_NO_INIT = 1 << 5,
  7254. +
  7255. + /*
  7256. + Hints.
  7257. + */
  7258. + MEM_FLAG_HINT_PERMALOCK = 1 << 6, /* Likely to be locked for long periods of time. */
  7259. +};
  7260. +
  7261. +unsigned int AllocateVcMemory(unsigned int *pHandle, unsigned int size, unsigned int alignment, unsigned int flags);
  7262. +unsigned int ReleaseVcMemory(unsigned int handle);
  7263. +unsigned int LockVcMemory(unsigned int *pBusAddress, unsigned int handle);
  7264. +unsigned int UnlockVcMemory(unsigned int handle);
  7265. +
  7266. +unsigned int ExecuteVcCode(unsigned int code,
  7267. + unsigned int r0, unsigned int r1, unsigned int r2, unsigned int r3, unsigned int r4, unsigned int r5);
  7268. +
  7269. +#endif
  7270. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vmalloc.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vmalloc.h
  7271. --- linux-3.12.13/arch/arm/mach-bcm2708/include/mach/vmalloc.h 1970-01-01 01:00:00.000000000 +0100
  7272. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vmalloc.h 2014-03-11 17:31:43.000000000 +0100
  7273. @@ -0,0 +1,20 @@
  7274. +/*
  7275. + * arch/arm/mach-bcm2708/include/mach/vmalloc.h
  7276. + *
  7277. + * Copyright (C) 2010 Broadcom
  7278. + *
  7279. + * This program is free software; you can redistribute it and/or modify
  7280. + * it under the terms of the GNU General Public License as published by
  7281. + * the Free Software Foundation; either version 2 of the License, or
  7282. + * (at your option) any later version.
  7283. + *
  7284. + * This program is distributed in the hope that it will be useful,
  7285. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7286. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7287. + * GNU General Public License for more details.
  7288. + *
  7289. + * You should have received a copy of the GNU General Public License
  7290. + * along with this program; if not, write to the Free Software
  7291. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7292. + */
  7293. +#define VMALLOC_END (0xe8000000)
  7294. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/Kconfig linux-raspberry-pi/arch/arm/mach-bcm2708/Kconfig
  7295. --- linux-3.12.13/arch/arm/mach-bcm2708/Kconfig 1970-01-01 01:00:00.000000000 +0100
  7296. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/Kconfig 2014-03-11 17:51:02.000000000 +0100
  7297. @@ -0,0 +1,49 @@
  7298. +menu "Broadcom BCM2708 Implementations"
  7299. + depends on ARCH_BCM2708
  7300. +
  7301. +config MACH_BCM2708
  7302. + bool "Broadcom BCM2708 Development Platform"
  7303. + select NEED_MACH_MEMORY_H
  7304. + select NEED_MACH_IO_H
  7305. + select CPU_V6
  7306. + help
  7307. + Include support for the Broadcom(R) BCM2708 platform.
  7308. +
  7309. +config BCM2708_GPIO
  7310. + bool "BCM2708 gpio support"
  7311. + depends on MACH_BCM2708
  7312. + select ARCH_REQUIRE_GPIOLIB
  7313. + default y
  7314. + help
  7315. + Include support for the Broadcom(R) BCM2708 gpio.
  7316. +
  7317. +config BCM2708_VCMEM
  7318. + bool "Videocore Memory"
  7319. + depends on MACH_BCM2708
  7320. + default y
  7321. + help
  7322. + Helper for videocore memory access and total size allocation.
  7323. +
  7324. +config BCM2708_NOL2CACHE
  7325. + bool "Videocore L2 cache disable"
  7326. + depends on MACH_BCM2708
  7327. + default n
  7328. + help
  7329. + Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
  7330. +
  7331. +config BCM2708_SPIDEV
  7332. + bool "Bind spidev to SPI0 master"
  7333. + depends on MACH_BCM2708
  7334. + depends on SPI
  7335. + default y
  7336. + help
  7337. + Binds spidev driver to the SPI0 master
  7338. +
  7339. +config BCM2708_DMAER
  7340. + tristate "BCM2708 DMA helper"
  7341. + depends on MACH_BCM2708
  7342. + default n
  7343. + help
  7344. + Enable DMA helper for accelerating X composition
  7345. +
  7346. +endmenu
  7347. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/Makefile linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile
  7348. --- linux-3.12.13/arch/arm/mach-bcm2708/Makefile 1970-01-01 01:00:00.000000000 +0100
  7349. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile 2014-03-11 17:51:02.000000000 +0100
  7350. @@ -0,0 +1,10 @@
  7351. +#
  7352. +# Makefile for the linux kernel.
  7353. +#
  7354. +
  7355. +obj-$(CONFIG_MACH_BCM2708) += clock.o bcm2708.o armctrl.o vcio.o power.o dma.o
  7356. +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
  7357. +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
  7358. +
  7359. +obj-$(CONFIG_BCM2708_DMAER) += dmaer_master.o
  7360. +dmaer_master-objs := dmaer.o vc_support.o
  7361. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/Makefile.boot linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile.boot
  7362. --- linux-3.12.13/arch/arm/mach-bcm2708/Makefile.boot 1970-01-01 01:00:00.000000000 +0100
  7363. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile.boot 2014-03-11 17:31:43.000000000 +0100
  7364. @@ -0,0 +1,3 @@
  7365. + zreladdr-y := 0x00008000
  7366. +params_phys-y := 0x00000100
  7367. +initrd_phys-y := 0x00800000
  7368. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/power.c linux-raspberry-pi/arch/arm/mach-bcm2708/power.c
  7369. --- linux-3.12.13/arch/arm/mach-bcm2708/power.c 1970-01-01 01:00:00.000000000 +0100
  7370. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/power.c 2014-03-11 17:31:43.000000000 +0100
  7371. @@ -0,0 +1,194 @@
  7372. +/*
  7373. + * linux/arch/arm/mach-bcm2708/power.c
  7374. + *
  7375. + * Copyright (C) 2010 Broadcom
  7376. + *
  7377. + * This program is free software; you can redistribute it and/or modify
  7378. + * it under the terms of the GNU General Public License version 2 as
  7379. + * published by the Free Software Foundation.
  7380. + *
  7381. + * This device provides a shared mechanism for controlling the power to
  7382. + * VideoCore subsystems.
  7383. + */
  7384. +
  7385. +#include <linux/module.h>
  7386. +#include <linux/semaphore.h>
  7387. +#include <linux/bug.h>
  7388. +#include <mach/power.h>
  7389. +#include <mach/vcio.h>
  7390. +#include <mach/arm_power.h>
  7391. +
  7392. +#define DRIVER_NAME "bcm2708_power"
  7393. +
  7394. +#define BCM_POWER_MAXCLIENTS 4
  7395. +#define BCM_POWER_NOCLIENT (1<<31)
  7396. +
  7397. +/* Some drivers expect there devices to be permanently powered */
  7398. +#define BCM_POWER_ALWAYS_ON (BCM_POWER_USB)
  7399. +
  7400. +#if 1
  7401. +#define DPRINTK printk
  7402. +#else
  7403. +#define DPRINTK if (0) printk
  7404. +#endif
  7405. +
  7406. +struct state_struct {
  7407. + uint32_t global_request;
  7408. + uint32_t client_request[BCM_POWER_MAXCLIENTS];
  7409. + struct semaphore client_mutex;
  7410. + struct semaphore mutex;
  7411. +} g_state;
  7412. +
  7413. +int bcm_power_open(BCM_POWER_HANDLE_T *handle)
  7414. +{
  7415. + BCM_POWER_HANDLE_T i;
  7416. + int ret = -EBUSY;
  7417. +
  7418. + down(&g_state.client_mutex);
  7419. +
  7420. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  7421. + if (g_state.client_request[i] == BCM_POWER_NOCLIENT) {
  7422. + g_state.client_request[i] = BCM_POWER_NONE;
  7423. + *handle = i;
  7424. + ret = 0;
  7425. + break;
  7426. + }
  7427. + }
  7428. +
  7429. + up(&g_state.client_mutex);
  7430. +
  7431. + DPRINTK("bcm_power_open() -> %d\n", *handle);
  7432. +
  7433. + return ret;
  7434. +}
  7435. +EXPORT_SYMBOL_GPL(bcm_power_open);
  7436. +
  7437. +int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request)
  7438. +{
  7439. + int rc = 0;
  7440. +
  7441. + DPRINTK("bcm_power_request(%d, %x)\n", handle, request);
  7442. +
  7443. + if ((handle < BCM_POWER_MAXCLIENTS) &&
  7444. + (g_state.client_request[handle] != BCM_POWER_NOCLIENT)) {
  7445. + if (down_interruptible(&g_state.mutex) != 0) {
  7446. + DPRINTK("bcm_power_request -> interrupted\n");
  7447. + return -EINTR;
  7448. + }
  7449. +
  7450. + if (request != g_state.client_request[handle]) {
  7451. + uint32_t others_request = 0;
  7452. + uint32_t global_request;
  7453. + BCM_POWER_HANDLE_T i;
  7454. +
  7455. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  7456. + if (i != handle)
  7457. + others_request |=
  7458. + g_state.client_request[i];
  7459. + }
  7460. + others_request &= ~BCM_POWER_NOCLIENT;
  7461. +
  7462. + global_request = request | others_request;
  7463. + if (global_request != g_state.global_request) {
  7464. + uint32_t actual;
  7465. +
  7466. + /* Send a request to VideoCore */
  7467. + bcm_mailbox_write(MBOX_CHAN_POWER,
  7468. + global_request << 4);
  7469. +
  7470. + /* Wait for a response during power-up */
  7471. + if (global_request & ~g_state.global_request) {
  7472. + rc = bcm_mailbox_read(MBOX_CHAN_POWER,
  7473. + &actual);
  7474. + DPRINTK
  7475. + ("bcm_mailbox_read -> %08x, %d\n",
  7476. + actual, rc);
  7477. + actual >>= 4;
  7478. + } else {
  7479. + rc = 0;
  7480. + actual = global_request;
  7481. + }
  7482. +
  7483. + if (rc == 0) {
  7484. + if (actual != global_request) {
  7485. + printk(KERN_ERR
  7486. + "%s: prev global %x, new global %x, actual %x, request %x, others_request %x\n",
  7487. + __func__,
  7488. + g_state.global_request,
  7489. + global_request, actual, request, others_request);
  7490. + /* A failure */
  7491. + BUG_ON((others_request & actual)
  7492. + != others_request);
  7493. + request &= actual;
  7494. + rc = -EIO;
  7495. + }
  7496. +
  7497. + g_state.global_request = actual;
  7498. + g_state.client_request[handle] =
  7499. + request;
  7500. + }
  7501. + }
  7502. + }
  7503. + up(&g_state.mutex);
  7504. + } else {
  7505. + rc = -EINVAL;
  7506. + }
  7507. + DPRINTK("bcm_power_request -> %d\n", rc);
  7508. + return rc;
  7509. +}
  7510. +EXPORT_SYMBOL_GPL(bcm_power_request);
  7511. +
  7512. +int bcm_power_close(BCM_POWER_HANDLE_T handle)
  7513. +{
  7514. + int rc;
  7515. +
  7516. + DPRINTK("bcm_power_close(%d)\n", handle);
  7517. +
  7518. + rc = bcm_power_request(handle, BCM_POWER_NONE);
  7519. + if (rc == 0)
  7520. + g_state.client_request[handle] = BCM_POWER_NOCLIENT;
  7521. +
  7522. + return rc;
  7523. +}
  7524. +EXPORT_SYMBOL_GPL(bcm_power_close);
  7525. +
  7526. +static int __init bcm_power_init(void)
  7527. +{
  7528. +#if defined(BCM_POWER_ALWAYS_ON)
  7529. + BCM_POWER_HANDLE_T always_on_handle;
  7530. +#endif
  7531. + int rc = 0;
  7532. + int i;
  7533. +
  7534. + printk(KERN_INFO "bcm_power: Broadcom power driver\n");
  7535. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  7536. +
  7537. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++)
  7538. + g_state.client_request[i] = BCM_POWER_NOCLIENT;
  7539. +
  7540. + sema_init(&g_state.client_mutex, 1);
  7541. + sema_init(&g_state.mutex, 1);
  7542. +
  7543. + g_state.global_request = 0;
  7544. +
  7545. +#if defined(BCM_POWER_ALWAYS_ON)
  7546. + if (BCM_POWER_ALWAYS_ON) {
  7547. + bcm_power_open(&always_on_handle);
  7548. + bcm_power_request(always_on_handle, BCM_POWER_ALWAYS_ON);
  7549. + }
  7550. +#endif
  7551. +
  7552. + return rc;
  7553. +}
  7554. +
  7555. +static void __exit bcm_power_exit(void)
  7556. +{
  7557. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  7558. +}
  7559. +
  7560. +arch_initcall(bcm_power_init); /* Initialize early */
  7561. +module_exit(bcm_power_exit);
  7562. +
  7563. +MODULE_AUTHOR("Phil Elwell");
  7564. +MODULE_DESCRIPTION("Interface to BCM2708 power management");
  7565. +MODULE_LICENSE("GPL");
  7566. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/vcio.c linux-raspberry-pi/arch/arm/mach-bcm2708/vcio.c
  7567. --- linux-3.12.13/arch/arm/mach-bcm2708/vcio.c 1970-01-01 01:00:00.000000000 +0100
  7568. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/vcio.c 2014-03-11 17:51:02.000000000 +0100
  7569. @@ -0,0 +1,474 @@
  7570. +/*
  7571. + * linux/arch/arm/mach-bcm2708/vcio.c
  7572. + *
  7573. + * Copyright (C) 2010 Broadcom
  7574. + *
  7575. + * This program is free software; you can redistribute it and/or modify
  7576. + * it under the terms of the GNU General Public License version 2 as
  7577. + * published by the Free Software Foundation.
  7578. + *
  7579. + * This device provides a shared mechanism for writing to the mailboxes,
  7580. + * semaphores, doorbells etc. that are shared between the ARM and the
  7581. + * VideoCore processor
  7582. + */
  7583. +
  7584. +#if defined(CONFIG_SERIAL_BCM_MBOX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  7585. +#define SUPPORT_SYSRQ
  7586. +#endif
  7587. +
  7588. +#include <linux/module.h>
  7589. +#include <linux/console.h>
  7590. +#include <linux/serial_core.h>
  7591. +#include <linux/serial.h>
  7592. +#include <linux/errno.h>
  7593. +#include <linux/device.h>
  7594. +#include <linux/init.h>
  7595. +#include <linux/mm.h>
  7596. +#include <linux/dma-mapping.h>
  7597. +#include <linux/platform_device.h>
  7598. +#include <linux/sysrq.h>
  7599. +#include <linux/delay.h>
  7600. +#include <linux/slab.h>
  7601. +#include <linux/interrupt.h>
  7602. +#include <linux/irq.h>
  7603. +
  7604. +#include <linux/io.h>
  7605. +
  7606. +#include <mach/vcio.h>
  7607. +#include <mach/platform.h>
  7608. +
  7609. +#include <asm/uaccess.h>
  7610. +
  7611. +
  7612. +#define DRIVER_NAME BCM_VCIO_DRIVER_NAME
  7613. +
  7614. +/* ----------------------------------------------------------------------
  7615. + * Mailbox
  7616. + * -------------------------------------------------------------------- */
  7617. +
  7618. +/* offsets from a mail box base address */
  7619. +#define MAIL_WRT 0x00 /* write - and next 4 words */
  7620. +#define MAIL_RD 0x00 /* read - and next 4 words */
  7621. +#define MAIL_POL 0x10 /* read without popping the fifo */
  7622. +#define MAIL_SND 0x14 /* sender ID (bottom two bits) */
  7623. +#define MAIL_STA 0x18 /* status */
  7624. +#define MAIL_CNF 0x1C /* configuration */
  7625. +
  7626. +#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  7627. +#define MBOX_MSG_LSB(chan, data28) (((data28) << 4) | ((chan) & 0xf))
  7628. +#define MBOX_CHAN(msg) ((msg) & 0xf)
  7629. +#define MBOX_DATA28(msg) ((msg) & ~0xf)
  7630. +#define MBOX_DATA28_LSB(msg) (((uint32_t)msg) >> 4)
  7631. +
  7632. +#define MBOX_MAGIC 0xd0d0c0de
  7633. +
  7634. +struct vc_mailbox {
  7635. + struct device *dev; /* parent device */
  7636. + void __iomem *status;
  7637. + void __iomem *config;
  7638. + void __iomem *read;
  7639. + void __iomem *write;
  7640. + uint32_t msg[MBOX_CHAN_COUNT];
  7641. + struct semaphore sema[MBOX_CHAN_COUNT];
  7642. + uint32_t magic;
  7643. +};
  7644. +
  7645. +static void mbox_init(struct vc_mailbox *mbox_out, struct device *dev,
  7646. + uint32_t addr_mbox)
  7647. +{
  7648. + int i;
  7649. +
  7650. + mbox_out->dev = dev;
  7651. + mbox_out->status = __io_address(addr_mbox + MAIL_STA);
  7652. + mbox_out->config = __io_address(addr_mbox + MAIL_CNF);
  7653. + mbox_out->read = __io_address(addr_mbox + MAIL_RD);
  7654. + /* Write to the other mailbox */
  7655. + mbox_out->write =
  7656. + __io_address((addr_mbox ^ ARM_0_MAIL0_WRT ^ ARM_0_MAIL1_WRT) +
  7657. + MAIL_WRT);
  7658. +
  7659. + for (i = 0; i < MBOX_CHAN_COUNT; i++) {
  7660. + mbox_out->msg[i] = 0;
  7661. + sema_init(&mbox_out->sema[i], 0);
  7662. + }
  7663. +
  7664. + /* Enable the interrupt on data reception */
  7665. + writel(ARM_MC_IHAVEDATAIRQEN, mbox_out->config);
  7666. +
  7667. + mbox_out->magic = MBOX_MAGIC;
  7668. +}
  7669. +
  7670. +static int mbox_write(struct vc_mailbox *mbox, unsigned chan, uint32_t data28)
  7671. +{
  7672. + int rc;
  7673. +
  7674. + if (mbox->magic != MBOX_MAGIC)
  7675. + rc = -EINVAL;
  7676. + else {
  7677. + /* wait for the mailbox FIFO to have some space in it */
  7678. + while (0 != (readl(mbox->status) & ARM_MS_FULL))
  7679. + cpu_relax();
  7680. +
  7681. + writel(MBOX_MSG(chan, data28), mbox->write);
  7682. + rc = 0;
  7683. + }
  7684. + return rc;
  7685. +}
  7686. +
  7687. +static int mbox_read(struct vc_mailbox *mbox, unsigned chan, uint32_t *data28)
  7688. +{
  7689. + int rc;
  7690. +
  7691. + if (mbox->magic != MBOX_MAGIC)
  7692. + rc = -EINVAL;
  7693. + else {
  7694. + down(&mbox->sema[chan]);
  7695. + *data28 = MBOX_DATA28(mbox->msg[chan]);
  7696. + mbox->msg[chan] = 0;
  7697. + rc = 0;
  7698. + }
  7699. + return rc;
  7700. +}
  7701. +
  7702. +static irqreturn_t mbox_irq(int irq, void *dev_id)
  7703. +{
  7704. + /* wait for the mailbox FIFO to have some data in it */
  7705. + struct vc_mailbox *mbox = (struct vc_mailbox *) dev_id;
  7706. + int status = readl(mbox->status);
  7707. + int ret = IRQ_NONE;
  7708. +
  7709. + while (!(status & ARM_MS_EMPTY)) {
  7710. + uint32_t msg = readl(mbox->read);
  7711. + int chan = MBOX_CHAN(msg);
  7712. + if (chan < MBOX_CHAN_COUNT) {
  7713. + if (mbox->msg[chan]) {
  7714. + /* Overflow */
  7715. + printk(KERN_ERR DRIVER_NAME
  7716. + ": mbox chan %d overflow - drop %08x\n",
  7717. + chan, msg);
  7718. + } else {
  7719. + mbox->msg[chan] = (msg | 0xf);
  7720. + up(&mbox->sema[chan]);
  7721. + }
  7722. + } else {
  7723. + printk(KERN_ERR DRIVER_NAME
  7724. + ": invalid channel selector (msg %08x)\n", msg);
  7725. + }
  7726. + ret = IRQ_HANDLED;
  7727. + status = readl(mbox->status);
  7728. + }
  7729. + return ret;
  7730. +}
  7731. +
  7732. +static struct irqaction mbox_irqaction = {
  7733. + .name = "ARM Mailbox IRQ",
  7734. + .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  7735. + .handler = mbox_irq,
  7736. +};
  7737. +
  7738. +/* ----------------------------------------------------------------------
  7739. + * Mailbox Methods
  7740. + * -------------------------------------------------------------------- */
  7741. +
  7742. +static struct device *mbox_dev; /* we assume there's only one! */
  7743. +
  7744. +static int dev_mbox_write(struct device *dev, unsigned chan, uint32_t data28)
  7745. +{
  7746. + int rc;
  7747. +
  7748. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  7749. + device_lock(dev);
  7750. + rc = mbox_write(mailbox, chan, data28);
  7751. + device_unlock(dev);
  7752. +
  7753. + return rc;
  7754. +}
  7755. +
  7756. +static int dev_mbox_read(struct device *dev, unsigned chan, uint32_t *data28)
  7757. +{
  7758. + int rc;
  7759. +
  7760. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  7761. + device_lock(dev);
  7762. + rc = mbox_read(mailbox, chan, data28);
  7763. + device_unlock(dev);
  7764. +
  7765. + return rc;
  7766. +}
  7767. +
  7768. +extern int bcm_mailbox_write(unsigned chan, uint32_t data28)
  7769. +{
  7770. + if (mbox_dev)
  7771. + return dev_mbox_write(mbox_dev, chan, data28);
  7772. + else
  7773. + return -ENODEV;
  7774. +}
  7775. +EXPORT_SYMBOL_GPL(bcm_mailbox_write);
  7776. +
  7777. +extern int bcm_mailbox_read(unsigned chan, uint32_t *data28)
  7778. +{
  7779. + if (mbox_dev)
  7780. + return dev_mbox_read(mbox_dev, chan, data28);
  7781. + else
  7782. + return -ENODEV;
  7783. +}
  7784. +EXPORT_SYMBOL_GPL(bcm_mailbox_read);
  7785. +
  7786. +static void dev_mbox_register(const char *dev_name, struct device *dev)
  7787. +{
  7788. + mbox_dev = dev;
  7789. +}
  7790. +
  7791. +static int mbox_copy_from_user(void *dst, const void *src, int size)
  7792. +{
  7793. + if ( (uint32_t)src < TASK_SIZE)
  7794. + {
  7795. + return copy_from_user(dst, src, size);
  7796. + }
  7797. + else
  7798. + {
  7799. + memcpy( dst, src, size );
  7800. + return 0;
  7801. + }
  7802. +}
  7803. +
  7804. +static int mbox_copy_to_user(void *dst, const void *src, int size)
  7805. +{
  7806. + if ( (uint32_t)dst < TASK_SIZE)
  7807. + {
  7808. + return copy_to_user(dst, src, size);
  7809. + }
  7810. + else
  7811. + {
  7812. + memcpy( dst, src, size );
  7813. + return 0;
  7814. + }
  7815. +}
  7816. +
  7817. +static DEFINE_MUTEX(mailbox_lock);
  7818. +extern int bcm_mailbox_property(void *data, int size)
  7819. +{
  7820. + uint32_t success;
  7821. + dma_addr_t mem_bus; /* the memory address accessed from videocore */
  7822. + void *mem_kern; /* the memory address accessed from driver */
  7823. + int s = 0;
  7824. +
  7825. + mutex_lock(&mailbox_lock);
  7826. + /* allocate some memory for the messages communicating with GPU */
  7827. + mem_kern = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &mem_bus, GFP_ATOMIC);
  7828. + if (mem_kern) {
  7829. + /* create the message */
  7830. + mbox_copy_from_user(mem_kern, data, size);
  7831. +
  7832. + /* send the message */
  7833. + wmb();
  7834. + s = bcm_mailbox_write(MBOX_CHAN_PROPERTY, (uint32_t)mem_bus);
  7835. + if (s == 0) {
  7836. + s = bcm_mailbox_read(MBOX_CHAN_PROPERTY, &success);
  7837. + }
  7838. + if (s == 0) {
  7839. + /* copy the response */
  7840. + rmb();
  7841. + mbox_copy_to_user(data, mem_kern, size);
  7842. + }
  7843. + dma_free_coherent(NULL, PAGE_ALIGN(size), mem_kern, mem_bus);
  7844. + } else {
  7845. + s = -ENOMEM;
  7846. + }
  7847. + if (s != 0)
  7848. + printk(KERN_ERR DRIVER_NAME ": %s failed (%d)\n", __func__, s);
  7849. +
  7850. + mutex_unlock(&mailbox_lock);
  7851. + return s;
  7852. +}
  7853. +EXPORT_SYMBOL_GPL(bcm_mailbox_property);
  7854. +
  7855. +/* ----------------------------------------------------------------------
  7856. + * Platform Device for Mailbox
  7857. + * -------------------------------------------------------------------- */
  7858. +
  7859. +/*
  7860. + * Is the device open right now? Used to prevent
  7861. + * concurent access into the same device
  7862. + */
  7863. +static int Device_Open = 0;
  7864. +
  7865. +/*
  7866. + * This is called whenever a process attempts to open the device file
  7867. + */
  7868. +static int device_open(struct inode *inode, struct file *file)
  7869. +{
  7870. + /*
  7871. + * We don't want to talk to two processes at the same time
  7872. + */
  7873. + if (Device_Open)
  7874. + return -EBUSY;
  7875. +
  7876. + Device_Open++;
  7877. + /*
  7878. + * Initialize the message
  7879. + */
  7880. + try_module_get(THIS_MODULE);
  7881. + return 0;
  7882. +}
  7883. +
  7884. +static int device_release(struct inode *inode, struct file *file)
  7885. +{
  7886. + /*
  7887. + * We're now ready for our next caller
  7888. + */
  7889. + Device_Open--;
  7890. +
  7891. + module_put(THIS_MODULE);
  7892. + return 0;
  7893. +}
  7894. +
  7895. +/*
  7896. + * This function is called whenever a process tries to do an ioctl on our
  7897. + * device file. We get two extra parameters (additional to the inode and file
  7898. + * structures, which all device functions get): the number of the ioctl called
  7899. + * and the parameter given to the ioctl function.
  7900. + *
  7901. + * If the ioctl is write or read/write (meaning output is returned to the
  7902. + * calling process), the ioctl call returns the output of this function.
  7903. + *
  7904. + */
  7905. +static long device_ioctl(struct file *file, /* see include/linux/fs.h */
  7906. + unsigned int ioctl_num, /* number and param for ioctl */
  7907. + unsigned long ioctl_param)
  7908. +{
  7909. + unsigned size;
  7910. + /*
  7911. + * Switch according to the ioctl called
  7912. + */
  7913. + switch (ioctl_num) {
  7914. + case IOCTL_MBOX_PROPERTY:
  7915. + /*
  7916. + * Receive a pointer to a message (in user space) and set that
  7917. + * to be the device's message. Get the parameter given to
  7918. + * ioctl by the process.
  7919. + */
  7920. + mbox_copy_from_user(&size, (void *)ioctl_param, sizeof size);
  7921. + return bcm_mailbox_property((void *)ioctl_param, size);
  7922. + break;
  7923. + default:
  7924. + printk(KERN_ERR DRIVER_NAME "unknown ioctl: %d\n", ioctl_num);
  7925. + return -EINVAL;
  7926. + }
  7927. +
  7928. + return 0;
  7929. +}
  7930. +
  7931. +/* Module Declarations */
  7932. +
  7933. +/*
  7934. + * This structure will hold the functions to be called
  7935. + * when a process does something to the device we
  7936. + * created. Since a pointer to this structure is kept in
  7937. + * the devices table, it can't be local to
  7938. + * init_module. NULL is for unimplemented functios.
  7939. + */
  7940. +struct file_operations fops = {
  7941. + .unlocked_ioctl = device_ioctl,
  7942. + .open = device_open,
  7943. + .release = device_release, /* a.k.a. close */
  7944. +};
  7945. +
  7946. +static int bcm_vcio_probe(struct platform_device *pdev)
  7947. +{
  7948. + int ret = 0;
  7949. + struct vc_mailbox *mailbox;
  7950. +
  7951. + mailbox = kzalloc(sizeof(*mailbox), GFP_KERNEL);
  7952. + if (NULL == mailbox) {
  7953. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  7954. + "mailbox memory\n");
  7955. + ret = -ENOMEM;
  7956. + } else {
  7957. + struct resource *res;
  7958. +
  7959. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  7960. + if (res == NULL) {
  7961. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  7962. + "resource\n");
  7963. + ret = -ENODEV;
  7964. + kfree(mailbox);
  7965. + } else {
  7966. + /* should be based on the registers from res really */
  7967. + mbox_init(mailbox, &pdev->dev, ARM_0_MAIL0_RD);
  7968. +
  7969. + platform_set_drvdata(pdev, mailbox);
  7970. + dev_mbox_register(DRIVER_NAME, &pdev->dev);
  7971. +
  7972. + mbox_irqaction.dev_id = mailbox;
  7973. + setup_irq(IRQ_ARM_MAILBOX, &mbox_irqaction);
  7974. + printk(KERN_INFO DRIVER_NAME ": mailbox at %p\n",
  7975. + __io_address(ARM_0_MAIL0_RD));
  7976. + }
  7977. + }
  7978. +
  7979. + if (ret == 0) {
  7980. + /*
  7981. + * Register the character device
  7982. + */
  7983. + ret = register_chrdev(MAJOR_NUM, DEVICE_FILE_NAME, &fops);
  7984. +
  7985. + /*
  7986. + * Negative values signify an error
  7987. + */
  7988. + if (ret < 0) {
  7989. + printk(KERN_ERR DRIVER_NAME
  7990. + "Failed registering the character device %d\n", ret);
  7991. + return ret;
  7992. + }
  7993. + }
  7994. + return ret;
  7995. +}
  7996. +
  7997. +static int bcm_vcio_remove(struct platform_device *pdev)
  7998. +{
  7999. + struct vc_mailbox *mailbox = platform_get_drvdata(pdev);
  8000. +
  8001. + platform_set_drvdata(pdev, NULL);
  8002. + kfree(mailbox);
  8003. +
  8004. + return 0;
  8005. +}
  8006. +
  8007. +static struct platform_driver bcm_mbox_driver = {
  8008. + .probe = bcm_vcio_probe,
  8009. + .remove = bcm_vcio_remove,
  8010. +
  8011. + .driver = {
  8012. + .name = DRIVER_NAME,
  8013. + .owner = THIS_MODULE,
  8014. + },
  8015. +};
  8016. +
  8017. +static int __init bcm_mbox_init(void)
  8018. +{
  8019. + int ret;
  8020. +
  8021. + printk(KERN_INFO "mailbox: Broadcom VideoCore Mailbox driver\n");
  8022. +
  8023. + ret = platform_driver_register(&bcm_mbox_driver);
  8024. + if (ret != 0) {
  8025. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  8026. + "on platform\n");
  8027. + }
  8028. +
  8029. + return ret;
  8030. +}
  8031. +
  8032. +static void __exit bcm_mbox_exit(void)
  8033. +{
  8034. + platform_driver_unregister(&bcm_mbox_driver);
  8035. +}
  8036. +
  8037. +arch_initcall(bcm_mbox_init); /* Initialize early */
  8038. +module_exit(bcm_mbox_exit);
  8039. +
  8040. +MODULE_AUTHOR("Gray Girling");
  8041. +MODULE_DESCRIPTION("ARM I/O to VideoCore processor");
  8042. +MODULE_LICENSE("GPL");
  8043. +MODULE_ALIAS("platform:bcm-mbox");
  8044. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/vc_mem.c linux-raspberry-pi/arch/arm/mach-bcm2708/vc_mem.c
  8045. --- linux-3.12.13/arch/arm/mach-bcm2708/vc_mem.c 1970-01-01 01:00:00.000000000 +0100
  8046. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/vc_mem.c 2014-03-11 17:51:02.000000000 +0100
  8047. @@ -0,0 +1,462 @@
  8048. +/*****************************************************************************
  8049. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  8050. +*
  8051. +* Unless you and Broadcom execute a separate written software license
  8052. +* agreement governing use of this software, this software is licensed to you
  8053. +* under the terms of the GNU General Public License version 2, available at
  8054. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8055. +*
  8056. +* Notwithstanding the above, under no circumstances may you combine this
  8057. +* software in any way with any other Broadcom software provided under a
  8058. +* license other than the GPL, without Broadcom's express prior written
  8059. +* consent.
  8060. +*****************************************************************************/
  8061. +
  8062. +#include <linux/kernel.h>
  8063. +#include <linux/module.h>
  8064. +#include <linux/fs.h>
  8065. +#include <linux/device.h>
  8066. +#include <linux/cdev.h>
  8067. +#include <linux/mm.h>
  8068. +#include <linux/slab.h>
  8069. +#include <linux/proc_fs.h>
  8070. +#include <asm/uaccess.h>
  8071. +#include <linux/dma-mapping.h>
  8072. +
  8073. +#ifdef CONFIG_ARCH_KONA
  8074. +#include <chal/chal_ipc.h>
  8075. +#elif CONFIG_ARCH_BCM2708
  8076. +#else
  8077. +#include <csp/chal_ipc.h>
  8078. +#endif
  8079. +
  8080. +#include "mach/vc_mem.h"
  8081. +#include <mach/vcio.h>
  8082. +
  8083. +#define DRIVER_NAME "vc-mem"
  8084. +
  8085. +// Uncomment to enable debug logging
  8086. +// #define ENABLE_DBG
  8087. +
  8088. +#if defined(ENABLE_DBG)
  8089. +#define LOG_DBG( fmt, ... ) printk( KERN_INFO fmt "\n", ##__VA_ARGS__ )
  8090. +#else
  8091. +#define LOG_DBG( fmt, ... )
  8092. +#endif
  8093. +#define LOG_ERR( fmt, ... ) printk( KERN_ERR fmt "\n", ##__VA_ARGS__ )
  8094. +
  8095. +// Device (/dev) related variables
  8096. +static dev_t vc_mem_devnum = 0;
  8097. +static struct class *vc_mem_class = NULL;
  8098. +static struct cdev vc_mem_cdev;
  8099. +static int vc_mem_inited = 0;
  8100. +
  8101. +// Proc entry
  8102. +static struct proc_dir_entry *vc_mem_proc_entry;
  8103. +
  8104. +/*
  8105. + * Videocore memory addresses and size
  8106. + *
  8107. + * Drivers that wish to know the videocore memory addresses and sizes should
  8108. + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
  8109. + * headers. This allows the other drivers to not be tied down to a a certain
  8110. + * address/size at compile time.
  8111. + *
  8112. + * In the future, the goal is to have the videocore memory virtual address and
  8113. + * size be calculated at boot time rather than at compile time. The decision of
  8114. + * where the videocore memory resides and its size would be in the hands of the
  8115. + * bootloader (and/or kernel). When that happens, the values of these variables
  8116. + * would be calculated and assigned in the init function.
  8117. + */
  8118. +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
  8119. +unsigned long mm_vc_mem_phys_addr = 0x00000000;
  8120. +unsigned int mm_vc_mem_size = 0;
  8121. +unsigned int mm_vc_mem_base = 0;
  8122. +
  8123. +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
  8124. +EXPORT_SYMBOL(mm_vc_mem_size);
  8125. +EXPORT_SYMBOL(mm_vc_mem_base);
  8126. +
  8127. +static uint phys_addr = 0;
  8128. +static uint mem_size = 0;
  8129. +static uint mem_base = 0;
  8130. +
  8131. +
  8132. +/****************************************************************************
  8133. +*
  8134. +* vc_mem_open
  8135. +*
  8136. +***************************************************************************/
  8137. +
  8138. +static int
  8139. +vc_mem_open(struct inode *inode, struct file *file)
  8140. +{
  8141. + (void) inode;
  8142. + (void) file;
  8143. +
  8144. + LOG_DBG("%s: called file = 0x%p", __func__, file);
  8145. +
  8146. + return 0;
  8147. +}
  8148. +
  8149. +/****************************************************************************
  8150. +*
  8151. +* vc_mem_release
  8152. +*
  8153. +***************************************************************************/
  8154. +
  8155. +static int
  8156. +vc_mem_release(struct inode *inode, struct file *file)
  8157. +{
  8158. + (void) inode;
  8159. + (void) file;
  8160. +
  8161. + LOG_DBG("%s: called file = 0x%p", __func__, file);
  8162. +
  8163. + return 0;
  8164. +}
  8165. +
  8166. +/****************************************************************************
  8167. +*
  8168. +* vc_mem_get_size
  8169. +*
  8170. +***************************************************************************/
  8171. +
  8172. +static void
  8173. +vc_mem_get_size(void)
  8174. +{
  8175. +}
  8176. +
  8177. +/****************************************************************************
  8178. +*
  8179. +* vc_mem_get_base
  8180. +*
  8181. +***************************************************************************/
  8182. +
  8183. +static void
  8184. +vc_mem_get_base(void)
  8185. +{
  8186. +}
  8187. +
  8188. +/****************************************************************************
  8189. +*
  8190. +* vc_mem_get_current_size
  8191. +*
  8192. +***************************************************************************/
  8193. +
  8194. +int
  8195. +vc_mem_get_current_size(void)
  8196. +{
  8197. + return mm_vc_mem_size;
  8198. +}
  8199. +
  8200. +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
  8201. +
  8202. +/****************************************************************************
  8203. +*
  8204. +* vc_mem_ioctl
  8205. +*
  8206. +***************************************************************************/
  8207. +
  8208. +static long
  8209. +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  8210. +{
  8211. + int rc = 0;
  8212. +
  8213. + (void) cmd;
  8214. + (void) arg;
  8215. +
  8216. + LOG_DBG("%s: called file = 0x%p", __func__, file);
  8217. +
  8218. + switch (cmd) {
  8219. + case VC_MEM_IOC_MEM_PHYS_ADDR:
  8220. + {
  8221. + LOG_DBG("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p",
  8222. + __func__, (void *) mm_vc_mem_phys_addr);
  8223. +
  8224. + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
  8225. + sizeof (mm_vc_mem_phys_addr)) != 0) {
  8226. + rc = -EFAULT;
  8227. + }
  8228. + break;
  8229. + }
  8230. + case VC_MEM_IOC_MEM_SIZE:
  8231. + {
  8232. + // Get the videocore memory size first
  8233. + vc_mem_get_size();
  8234. +
  8235. + LOG_DBG("%s: VC_MEM_IOC_MEM_SIZE=%u", __func__,
  8236. + mm_vc_mem_size);
  8237. +
  8238. + if (copy_to_user((void *) arg, &mm_vc_mem_size,
  8239. + sizeof (mm_vc_mem_size)) != 0) {
  8240. + rc = -EFAULT;
  8241. + }
  8242. + break;
  8243. + }
  8244. + case VC_MEM_IOC_MEM_BASE:
  8245. + {
  8246. + // Get the videocore memory base
  8247. + vc_mem_get_base();
  8248. +
  8249. + LOG_DBG("%s: VC_MEM_IOC_MEM_BASE=%u", __func__,
  8250. + mm_vc_mem_base);
  8251. +
  8252. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  8253. + sizeof (mm_vc_mem_base)) != 0) {
  8254. + rc = -EFAULT;
  8255. + }
  8256. + break;
  8257. + }
  8258. + case VC_MEM_IOC_MEM_LOAD:
  8259. + {
  8260. + // Get the videocore memory base
  8261. + vc_mem_get_base();
  8262. +
  8263. + LOG_DBG("%s: VC_MEM_IOC_MEM_LOAD=%u", __func__,
  8264. + mm_vc_mem_base);
  8265. +
  8266. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  8267. + sizeof (mm_vc_mem_base)) != 0) {
  8268. + rc = -EFAULT;
  8269. + }
  8270. + break;
  8271. + }
  8272. + default:
  8273. + {
  8274. + return -ENOTTY;
  8275. + }
  8276. + }
  8277. + LOG_DBG("%s: file = 0x%p returning %d", __func__, file, rc);
  8278. +
  8279. + return rc;
  8280. +}
  8281. +
  8282. +/****************************************************************************
  8283. +*
  8284. +* vc_mem_mmap
  8285. +*
  8286. +***************************************************************************/
  8287. +
  8288. +static int
  8289. +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
  8290. +{
  8291. + int rc = 0;
  8292. + unsigned long length = vma->vm_end - vma->vm_start;
  8293. + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  8294. +
  8295. + LOG_DBG("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx",
  8296. + __func__, (long) vma->vm_start, (long) vma->vm_end,
  8297. + (long) vma->vm_pgoff);
  8298. +
  8299. + if (offset + length > mm_vc_mem_size) {
  8300. + LOG_ERR("%s: length %ld is too big", __func__, length);
  8301. + return -EINVAL;
  8302. + }
  8303. + // Do not cache the memory map
  8304. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  8305. +
  8306. + rc = remap_pfn_range(vma, vma->vm_start,
  8307. + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
  8308. + vma->vm_pgoff, length, vma->vm_page_prot);
  8309. + if (rc != 0) {
  8310. + LOG_ERR("%s: remap_pfn_range failed (rc=%d)", __func__, rc);
  8311. + }
  8312. +
  8313. + return rc;
  8314. +}
  8315. +
  8316. +/****************************************************************************
  8317. +*
  8318. +* File Operations for the driver.
  8319. +*
  8320. +***************************************************************************/
  8321. +
  8322. +static const struct file_operations vc_mem_fops = {
  8323. + .owner = THIS_MODULE,
  8324. + .open = vc_mem_open,
  8325. + .release = vc_mem_release,
  8326. + .unlocked_ioctl = vc_mem_ioctl,
  8327. + .mmap = vc_mem_mmap,
  8328. +};
  8329. +
  8330. +/****************************************************************************
  8331. +*
  8332. +* vc_mem_proc_read
  8333. +*
  8334. +***************************************************************************/
  8335. +
  8336. +static int
  8337. +vc_mem_proc_read(char *buf, char **start, off_t offset, int count, int *eof,
  8338. + void *data)
  8339. +{
  8340. + char *p = buf;
  8341. +
  8342. + (void) start;
  8343. + (void) count;
  8344. + (void) data;
  8345. +
  8346. + if (offset > 0) {
  8347. + *eof = 1;
  8348. + return 0;
  8349. + }
  8350. + // Get the videocore memory size first
  8351. + vc_mem_get_size();
  8352. +
  8353. + p += sprintf(p, "Videocore memory:\n");
  8354. + if (mm_vc_mem_phys_addr != 0)
  8355. + p += sprintf(p, " Physical address: 0x%p\n",
  8356. + (void *) mm_vc_mem_phys_addr);
  8357. + else
  8358. + p += sprintf(p, " Physical address: 0x00000000\n");
  8359. + p += sprintf(p, " Length (bytes): %u\n", mm_vc_mem_size);
  8360. +
  8361. + *eof = 1;
  8362. + return p - buf;
  8363. +}
  8364. +
  8365. +/****************************************************************************
  8366. +*
  8367. +* vc_mem_proc_write
  8368. +*
  8369. +***************************************************************************/
  8370. +
  8371. +static int
  8372. +vc_mem_proc_write(struct file *file, const char __user * buffer,
  8373. + unsigned long count, void *data)
  8374. +{
  8375. + int rc = -EFAULT;
  8376. + char input_str[10];
  8377. +
  8378. + memset(input_str, 0, sizeof (input_str));
  8379. +
  8380. + if (count > sizeof (input_str)) {
  8381. + LOG_ERR("%s: input string length too long", __func__);
  8382. + goto out;
  8383. + }
  8384. +
  8385. + if (copy_from_user(input_str, buffer, count - 1)) {
  8386. + LOG_ERR("%s: failed to get input string", __func__);
  8387. + goto out;
  8388. + }
  8389. +
  8390. + if (strncmp(input_str, "connect", strlen("connect")) == 0) {
  8391. + // Get the videocore memory size from the videocore
  8392. + vc_mem_get_size();
  8393. + }
  8394. +
  8395. + out:
  8396. + return rc;
  8397. +}
  8398. +
  8399. +/****************************************************************************
  8400. +*
  8401. +* vc_mem_init
  8402. +*
  8403. +***************************************************************************/
  8404. +
  8405. +static int __init
  8406. +vc_mem_init(void)
  8407. +{
  8408. + int rc = -EFAULT;
  8409. + struct device *dev;
  8410. +
  8411. + LOG_DBG("%s: called", __func__);
  8412. +
  8413. + mm_vc_mem_phys_addr = phys_addr;
  8414. + mm_vc_mem_size = mem_size;
  8415. + mm_vc_mem_base = mem_base;
  8416. +
  8417. + vc_mem_get_size();
  8418. +
  8419. + printk("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
  8420. + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
  8421. +
  8422. + if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
  8423. + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
  8424. + goto out_err;
  8425. + }
  8426. +
  8427. + cdev_init(&vc_mem_cdev, &vc_mem_fops);
  8428. + if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
  8429. + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
  8430. + goto out_unregister;
  8431. + }
  8432. +
  8433. + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
  8434. + if (IS_ERR(vc_mem_class)) {
  8435. + rc = PTR_ERR(vc_mem_class);
  8436. + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
  8437. + goto out_cdev_del;
  8438. + }
  8439. +
  8440. + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
  8441. + DRIVER_NAME);
  8442. + if (IS_ERR(dev)) {
  8443. + rc = PTR_ERR(dev);
  8444. + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
  8445. + goto out_class_destroy;
  8446. + }
  8447. +
  8448. +#if 0
  8449. + vc_mem_proc_entry = create_proc_entry(DRIVER_NAME, 0444, NULL);
  8450. + if (vc_mem_proc_entry == NULL) {
  8451. + rc = -EFAULT;
  8452. + LOG_ERR("%s: create_proc_entry failed", __func__);
  8453. + goto out_device_destroy;
  8454. + }
  8455. + vc_mem_proc_entry->read_proc = vc_mem_proc_read;
  8456. + vc_mem_proc_entry->write_proc = vc_mem_proc_write;
  8457. +#endif
  8458. +
  8459. + vc_mem_inited = 1;
  8460. + return 0;
  8461. +
  8462. + out_device_destroy:
  8463. + device_destroy(vc_mem_class, vc_mem_devnum);
  8464. +
  8465. + out_class_destroy:
  8466. + class_destroy(vc_mem_class);
  8467. + vc_mem_class = NULL;
  8468. +
  8469. + out_cdev_del:
  8470. + cdev_del(&vc_mem_cdev);
  8471. +
  8472. + out_unregister:
  8473. + unregister_chrdev_region(vc_mem_devnum, 1);
  8474. +
  8475. + out_err:
  8476. + return -1;
  8477. +}
  8478. +
  8479. +/****************************************************************************
  8480. +*
  8481. +* vc_mem_exit
  8482. +*
  8483. +***************************************************************************/
  8484. +
  8485. +static void __exit
  8486. +vc_mem_exit(void)
  8487. +{
  8488. + LOG_DBG("%s: called", __func__);
  8489. +
  8490. + if (vc_mem_inited) {
  8491. +#if 0
  8492. + remove_proc_entry(vc_mem_proc_entry->name, NULL);
  8493. +#endif
  8494. + device_destroy(vc_mem_class, vc_mem_devnum);
  8495. + class_destroy(vc_mem_class);
  8496. + cdev_del(&vc_mem_cdev);
  8497. + unregister_chrdev_region(vc_mem_devnum, 1);
  8498. + }
  8499. +}
  8500. +
  8501. +module_init(vc_mem_init);
  8502. +module_exit(vc_mem_exit);
  8503. +MODULE_LICENSE("GPL");
  8504. +MODULE_AUTHOR("Broadcom Corporation");
  8505. +
  8506. +module_param(phys_addr, uint, 0644);
  8507. +module_param(mem_size, uint, 0644);
  8508. +module_param(mem_base, uint, 0644);
  8509. +
  8510. diff -Nur linux-3.12.13/arch/arm/mach-bcm2708/vc_support.c linux-raspberry-pi/arch/arm/mach-bcm2708/vc_support.c
  8511. --- linux-3.12.13/arch/arm/mach-bcm2708/vc_support.c 1970-01-01 01:00:00.000000000 +0100
  8512. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/vc_support.c 2014-03-11 17:51:02.000000000 +0100
  8513. @@ -0,0 +1,318 @@
  8514. +/*
  8515. + * vc_support.c
  8516. + *
  8517. + * Created on: 25 Nov 2012
  8518. + * Author: Simon
  8519. + */
  8520. +
  8521. +#include <linux/module.h>
  8522. +#include <mach/vcio.h>
  8523. +
  8524. +#ifdef ECLIPSE_IGNORE
  8525. +
  8526. +#define __user
  8527. +#define __init
  8528. +#define __exit
  8529. +#define __iomem
  8530. +#define KERN_DEBUG
  8531. +#define KERN_ERR
  8532. +#define KERN_WARNING
  8533. +#define KERN_INFO
  8534. +#define _IOWR(a, b, c) b
  8535. +#define _IOW(a, b, c) b
  8536. +#define _IO(a, b) b
  8537. +
  8538. +#endif
  8539. +
  8540. +/****** VC MAILBOX FUNCTIONALITY ******/
  8541. +unsigned int AllocateVcMemory(unsigned int *pHandle, unsigned int size, unsigned int alignment, unsigned int flags)
  8542. +{
  8543. + struct vc_msg
  8544. + {
  8545. + unsigned int m_msgSize;
  8546. + unsigned int m_response;
  8547. +
  8548. + struct vc_tag
  8549. + {
  8550. + unsigned int m_tagId;
  8551. + unsigned int m_sendBufferSize;
  8552. + union {
  8553. + unsigned int m_sendDataSize;
  8554. + unsigned int m_recvDataSize;
  8555. + };
  8556. +
  8557. + struct args
  8558. + {
  8559. + union {
  8560. + unsigned int m_size;
  8561. + unsigned int m_handle;
  8562. + };
  8563. + unsigned int m_alignment;
  8564. + unsigned int m_flags;
  8565. + } m_args;
  8566. + } m_tag;
  8567. +
  8568. + unsigned int m_endTag;
  8569. + } msg;
  8570. + int s;
  8571. +
  8572. + msg.m_msgSize = sizeof(msg);
  8573. + msg.m_response = 0;
  8574. + msg.m_endTag = 0;
  8575. +
  8576. + //fill in the tag for the allocation command
  8577. + msg.m_tag.m_tagId = 0x3000c;
  8578. + msg.m_tag.m_sendBufferSize = 12;
  8579. + msg.m_tag.m_sendDataSize = 12;
  8580. +
  8581. + //fill in our args
  8582. + msg.m_tag.m_args.m_size = size;
  8583. + msg.m_tag.m_args.m_alignment = alignment;
  8584. + msg.m_tag.m_args.m_flags = flags;
  8585. +
  8586. + //run the command
  8587. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8588. +
  8589. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004)
  8590. + {
  8591. + *pHandle = msg.m_tag.m_args.m_handle;
  8592. + return 0;
  8593. + }
  8594. + else
  8595. + {
  8596. + printk(KERN_ERR "failed to allocate vc memory: s=%d response=%08x recv data size=%08x\n",
  8597. + s, msg.m_response, msg.m_tag.m_recvDataSize);
  8598. + return 1;
  8599. + }
  8600. +}
  8601. +
  8602. +unsigned int ReleaseVcMemory(unsigned int handle)
  8603. +{
  8604. + struct vc_msg
  8605. + {
  8606. + unsigned int m_msgSize;
  8607. + unsigned int m_response;
  8608. +
  8609. + struct vc_tag
  8610. + {
  8611. + unsigned int m_tagId;
  8612. + unsigned int m_sendBufferSize;
  8613. + union {
  8614. + unsigned int m_sendDataSize;
  8615. + unsigned int m_recvDataSize;
  8616. + };
  8617. +
  8618. + struct args
  8619. + {
  8620. + union {
  8621. + unsigned int m_handle;
  8622. + unsigned int m_error;
  8623. + };
  8624. + } m_args;
  8625. + } m_tag;
  8626. +
  8627. + unsigned int m_endTag;
  8628. + } msg;
  8629. + int s;
  8630. +
  8631. + msg.m_msgSize = sizeof(msg);
  8632. + msg.m_response = 0;
  8633. + msg.m_endTag = 0;
  8634. +
  8635. + //fill in the tag for the release command
  8636. + msg.m_tag.m_tagId = 0x3000f;
  8637. + msg.m_tag.m_sendBufferSize = 4;
  8638. + msg.m_tag.m_sendDataSize = 4;
  8639. +
  8640. + //pass across the handle
  8641. + msg.m_tag.m_args.m_handle = handle;
  8642. +
  8643. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8644. +
  8645. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004 && msg.m_tag.m_args.m_error == 0)
  8646. + return 0;
  8647. + else
  8648. + {
  8649. + printk(KERN_ERR "failed to release vc memory: s=%d response=%08x recv data size=%08x error=%08x\n",
  8650. + s, msg.m_response, msg.m_tag.m_recvDataSize, msg.m_tag.m_args.m_error);
  8651. + return 1;
  8652. + }
  8653. +}
  8654. +
  8655. +unsigned int LockVcMemory(unsigned int *pBusAddress, unsigned int handle)
  8656. +{
  8657. + struct vc_msg
  8658. + {
  8659. + unsigned int m_msgSize;
  8660. + unsigned int m_response;
  8661. +
  8662. + struct vc_tag
  8663. + {
  8664. + unsigned int m_tagId;
  8665. + unsigned int m_sendBufferSize;
  8666. + union {
  8667. + unsigned int m_sendDataSize;
  8668. + unsigned int m_recvDataSize;
  8669. + };
  8670. +
  8671. + struct args
  8672. + {
  8673. + union {
  8674. + unsigned int m_handle;
  8675. + unsigned int m_busAddress;
  8676. + };
  8677. + } m_args;
  8678. + } m_tag;
  8679. +
  8680. + unsigned int m_endTag;
  8681. + } msg;
  8682. + int s;
  8683. +
  8684. + msg.m_msgSize = sizeof(msg);
  8685. + msg.m_response = 0;
  8686. + msg.m_endTag = 0;
  8687. +
  8688. + //fill in the tag for the lock command
  8689. + msg.m_tag.m_tagId = 0x3000d;
  8690. + msg.m_tag.m_sendBufferSize = 4;
  8691. + msg.m_tag.m_sendDataSize = 4;
  8692. +
  8693. + //pass across the handle
  8694. + msg.m_tag.m_args.m_handle = handle;
  8695. +
  8696. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8697. +
  8698. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004)
  8699. + {
  8700. + //pick out the bus address
  8701. + *pBusAddress = msg.m_tag.m_args.m_busAddress;
  8702. + return 0;
  8703. + }
  8704. + else
  8705. + {
  8706. + printk(KERN_ERR "failed to lock vc memory: s=%d response=%08x recv data size=%08x\n",
  8707. + s, msg.m_response, msg.m_tag.m_recvDataSize);
  8708. + return 1;
  8709. + }
  8710. +}
  8711. +
  8712. +unsigned int UnlockVcMemory(unsigned int handle)
  8713. +{
  8714. + struct vc_msg
  8715. + {
  8716. + unsigned int m_msgSize;
  8717. + unsigned int m_response;
  8718. +
  8719. + struct vc_tag
  8720. + {
  8721. + unsigned int m_tagId;
  8722. + unsigned int m_sendBufferSize;
  8723. + union {
  8724. + unsigned int m_sendDataSize;
  8725. + unsigned int m_recvDataSize;
  8726. + };
  8727. +
  8728. + struct args
  8729. + {
  8730. + union {
  8731. + unsigned int m_handle;
  8732. + unsigned int m_error;
  8733. + };
  8734. + } m_args;
  8735. + } m_tag;
  8736. +
  8737. + unsigned int m_endTag;
  8738. + } msg;
  8739. + int s;
  8740. +
  8741. + msg.m_msgSize = sizeof(msg);
  8742. + msg.m_response = 0;
  8743. + msg.m_endTag = 0;
  8744. +
  8745. + //fill in the tag for the unlock command
  8746. + msg.m_tag.m_tagId = 0x3000e;
  8747. + msg.m_tag.m_sendBufferSize = 4;
  8748. + msg.m_tag.m_sendDataSize = 4;
  8749. +
  8750. + //pass across the handle
  8751. + msg.m_tag.m_args.m_handle = handle;
  8752. +
  8753. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8754. +
  8755. + //check the error code too
  8756. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004 && msg.m_tag.m_args.m_error == 0)
  8757. + return 0;
  8758. + else
  8759. + {
  8760. + printk(KERN_ERR "failed to unlock vc memory: s=%d response=%08x recv data size=%08x error%08x\n",
  8761. + s, msg.m_response, msg.m_tag.m_recvDataSize, msg.m_tag.m_args.m_error);
  8762. + return 1;
  8763. + }
  8764. +}
  8765. +
  8766. +unsigned int ExecuteVcCode(unsigned int code,
  8767. + unsigned int r0, unsigned int r1, unsigned int r2, unsigned int r3, unsigned int r4, unsigned int r5)
  8768. +{
  8769. + struct vc_msg
  8770. + {
  8771. + unsigned int m_msgSize;
  8772. + unsigned int m_response;
  8773. +
  8774. + struct vc_tag
  8775. + {
  8776. + unsigned int m_tagId;
  8777. + unsigned int m_sendBufferSize;
  8778. + union {
  8779. + unsigned int m_sendDataSize;
  8780. + unsigned int m_recvDataSize;
  8781. + };
  8782. +
  8783. + struct args
  8784. + {
  8785. + union {
  8786. + unsigned int m_pCode;
  8787. + unsigned int m_return;
  8788. + };
  8789. + unsigned int m_r0;
  8790. + unsigned int m_r1;
  8791. + unsigned int m_r2;
  8792. + unsigned int m_r3;
  8793. + unsigned int m_r4;
  8794. + unsigned int m_r5;
  8795. + } m_args;
  8796. + } m_tag;
  8797. +
  8798. + unsigned int m_endTag;
  8799. + } msg;
  8800. + int s;
  8801. +
  8802. + msg.m_msgSize = sizeof(msg);
  8803. + msg.m_response = 0;
  8804. + msg.m_endTag = 0;
  8805. +
  8806. + //fill in the tag for the unlock command
  8807. + msg.m_tag.m_tagId = 0x30010;
  8808. + msg.m_tag.m_sendBufferSize = 28;
  8809. + msg.m_tag.m_sendDataSize = 28;
  8810. +
  8811. + //pass across the handle
  8812. + msg.m_tag.m_args.m_pCode = code;
  8813. + msg.m_tag.m_args.m_r0 = r0;
  8814. + msg.m_tag.m_args.m_r1 = r1;
  8815. + msg.m_tag.m_args.m_r2 = r2;
  8816. + msg.m_tag.m_args.m_r3 = r3;
  8817. + msg.m_tag.m_args.m_r4 = r4;
  8818. + msg.m_tag.m_args.m_r5 = r5;
  8819. +
  8820. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8821. +
  8822. + //check the error code too
  8823. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004)
  8824. + return msg.m_tag.m_args.m_return;
  8825. + else
  8826. + {
  8827. + printk(KERN_ERR "failed to execute: s=%d response=%08x recv data size=%08x\n",
  8828. + s, msg.m_response, msg.m_tag.m_recvDataSize);
  8829. + return 1;
  8830. + }
  8831. +}
  8832. diff -Nur linux-3.12.13/arch/arm/Makefile linux-raspberry-pi/arch/arm/Makefile
  8833. --- linux-3.12.13/arch/arm/Makefile 2014-02-22 22:32:50.000000000 +0100
  8834. +++ linux-raspberry-pi/arch/arm/Makefile 2014-03-11 17:51:00.000000000 +0100
  8835. @@ -146,6 +146,7 @@
  8836. # by CONFIG_* macro name.
  8837. machine-$(CONFIG_ARCH_AT91) += at91
  8838. machine-$(CONFIG_ARCH_BCM) += bcm
  8839. +machine-$(CONFIG_ARCH_BCM2708) += bcm2708
  8840. machine-$(CONFIG_ARCH_BCM2835) += bcm2835
  8841. machine-$(CONFIG_ARCH_CLPS711X) += clps711x
  8842. machine-$(CONFIG_ARCH_CNS3XXX) += cns3xxx
  8843. diff -Nur linux-3.12.13/arch/arm/mm/Kconfig linux-raspberry-pi/arch/arm/mm/Kconfig
  8844. --- linux-3.12.13/arch/arm/mm/Kconfig 2014-02-22 22:32:50.000000000 +0100
  8845. +++ linux-raspberry-pi/arch/arm/mm/Kconfig 2014-03-11 17:51:03.000000000 +0100
  8846. @@ -358,7 +358,7 @@
  8847. # ARMv6
  8848. config CPU_V6
  8849. - bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX
  8850. + bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX || MACH_BCM2708
  8851. select CPU_32v6
  8852. select CPU_ABRT_EV6
  8853. select CPU_CACHE_V6
  8854. diff -Nur linux-3.12.13/arch/arm/mm/proc-v6.S linux-raspberry-pi/arch/arm/mm/proc-v6.S
  8855. --- linux-3.12.13/arch/arm/mm/proc-v6.S 2014-02-22 22:32:50.000000000 +0100
  8856. +++ linux-raspberry-pi/arch/arm/mm/proc-v6.S 2014-03-11 17:51:03.000000000 +0100
  8857. @@ -73,10 +73,19 @@
  8858. *
  8859. * IRQs are already disabled.
  8860. */
  8861. +
  8862. +/* See jira SW-5991 for details of this workaround */
  8863. ENTRY(cpu_v6_do_idle)
  8864. - mov r1, #0
  8865. - mcr p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  8866. - mcr p15, 0, r1, c7, c0, 4 @ wait for interrupt
  8867. + .align 5
  8868. + mov r1, #2
  8869. +1: subs r1, #1
  8870. + nop
  8871. + mcreq p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  8872. + mcreq p15, 0, r1, c7, c0, 4 @ wait for interrupt
  8873. + nop
  8874. + nop
  8875. + nop
  8876. + bne 1b
  8877. mov pc, lr
  8878. ENTRY(cpu_v6_dcache_clean_area)
  8879. diff -Nur linux-3.12.13/arch/arm/tools/mach-types linux-raspberry-pi/arch/arm/tools/mach-types
  8880. --- linux-3.12.13/arch/arm/tools/mach-types 2014-02-22 22:32:50.000000000 +0100
  8881. +++ linux-raspberry-pi/arch/arm/tools/mach-types 2014-03-11 17:31:54.000000000 +0100
  8882. @@ -522,6 +522,7 @@
  8883. prima2_evb MACH_PRIMA2_EVB PRIMA2_EVB 3103
  8884. paz00 MACH_PAZ00 PAZ00 3128
  8885. acmenetusfoxg20 MACH_ACMENETUSFOXG20 ACMENETUSFOXG20 3129
  8886. +bcm2708 MACH_BCM2708 BCM2708 3138
  8887. ag5evm MACH_AG5EVM AG5EVM 3189
  8888. ics_if_voip MACH_ICS_IF_VOIP ICS_IF_VOIP 3206
  8889. wlf_cragg_6410 MACH_WLF_CRAGG_6410 WLF_CRAGG_6410 3207
  8890. diff -Nur linux-3.12.13/Documentation/video4linux/bcm2835-v4l2.txt linux-raspberry-pi/Documentation/video4linux/bcm2835-v4l2.txt
  8891. --- linux-3.12.13/Documentation/video4linux/bcm2835-v4l2.txt 1970-01-01 01:00:00.000000000 +0100
  8892. +++ linux-raspberry-pi/Documentation/video4linux/bcm2835-v4l2.txt 2014-03-11 17:31:41.000000000 +0100
  8893. @@ -0,0 +1,60 @@
  8894. +
  8895. +BCM2835 (aka Raspberry Pi) V4L2 driver
  8896. +======================================
  8897. +
  8898. +1. Copyright
  8899. +============
  8900. +
  8901. +Copyright © 2013 Raspberry Pi (Trading) Ltd.
  8902. +
  8903. +2. License
  8904. +==========
  8905. +
  8906. +This program is free software; you can redistribute it and/or modify
  8907. +it under the terms of the GNU General Public License as published by
  8908. +the Free Software Foundation; either version 2 of the License, or
  8909. +(at your option) any later version.
  8910. +
  8911. +This program is distributed in the hope that it will be useful,
  8912. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  8913. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8914. +GNU General Public License for more details.
  8915. +
  8916. +You should have received a copy of the GNU General Public License
  8917. +along with this program; if not, write to the Free Software
  8918. +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  8919. +
  8920. +3. Quick Start
  8921. +==============
  8922. +
  8923. +You need a version 1.0 or later of v4l2-ctl, available from:
  8924. + git://git.linuxtv.org/v4l-utils.git
  8925. +
  8926. +$ sudo modprobe bcm2835-v4l2
  8927. +
  8928. +Turn on the overlay:
  8929. +
  8930. +$ v4l2-ctl --overlay=1
  8931. +
  8932. +Turn off the overlay:
  8933. +
  8934. +$ v4l2-ctl --overlay=0
  8935. +
  8936. +Set the capture format for video:
  8937. +
  8938. +$ v4l2-ctl --set-fmt-video=width=1920,height=1088,pixelformat=4
  8939. +
  8940. +(Note: 1088 not 1080).
  8941. +
  8942. +Capture:
  8943. +
  8944. +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
  8945. +
  8946. +Stills capture:
  8947. +
  8948. +$ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
  8949. +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
  8950. +
  8951. +List of available formats:
  8952. +
  8953. +$ v4l2-ctl --list-formats
  8954. diff -Nur linux-3.12.13/drivers/char/broadcom/Kconfig linux-raspberry-pi/drivers/char/broadcom/Kconfig
  8955. --- linux-3.12.13/drivers/char/broadcom/Kconfig 1970-01-01 01:00:00.000000000 +0100
  8956. +++ linux-raspberry-pi/drivers/char/broadcom/Kconfig 2014-03-11 17:51:12.000000000 +0100
  8957. @@ -0,0 +1,16 @@
  8958. +#
  8959. +# Broadcom char driver config
  8960. +#
  8961. +
  8962. +menuconfig BRCM_CHAR_DRIVERS
  8963. + bool "Broadcom Char Drivers"
  8964. + help
  8965. + Broadcom's char drivers
  8966. +
  8967. +config BCM_VC_CMA
  8968. + bool "Videocore CMA"
  8969. + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
  8970. + default n
  8971. + help
  8972. + Helper for videocore CMA access.
  8973. +
  8974. diff -Nur linux-3.12.13/drivers/char/broadcom/Makefile linux-raspberry-pi/drivers/char/broadcom/Makefile
  8975. --- linux-3.12.13/drivers/char/broadcom/Makefile 1970-01-01 01:00:00.000000000 +0100
  8976. +++ linux-raspberry-pi/drivers/char/broadcom/Makefile 2014-03-11 17:51:12.000000000 +0100
  8977. @@ -0,0 +1 @@
  8978. +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
  8979. diff -Nur linux-3.12.13/drivers/char/broadcom/vc_cma/Makefile linux-raspberry-pi/drivers/char/broadcom/vc_cma/Makefile
  8980. --- linux-3.12.13/drivers/char/broadcom/vc_cma/Makefile 1970-01-01 01:00:00.000000000 +0100
  8981. +++ linux-raspberry-pi/drivers/char/broadcom/vc_cma/Makefile 2014-03-11 17:51:12.000000000 +0100
  8982. @@ -0,0 +1,14 @@
  8983. +EXTRA_CFLAGS += -Wall -Wstrict-prototypes -Wno-trigraphs
  8984. +EXTRA_CFLAGS += -Werror
  8985. +EXTRA_CFLAGS += -I"include/linux/broadcom"
  8986. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services"
  8987. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchi"
  8988. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchiq_arm"
  8989. +
  8990. +EXTRA_CFLAGS += -D__KERNEL__
  8991. +EXTRA_CFLAGS += -D__linux__
  8992. +EXTRA_CFLAGS += -Werror
  8993. +
  8994. +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
  8995. +
  8996. +vc-cma-objs := vc_cma.o
  8997. diff -Nur linux-3.12.13/drivers/char/broadcom/vc_cma/vc_cma.c linux-raspberry-pi/drivers/char/broadcom/vc_cma/vc_cma.c
  8998. --- linux-3.12.13/drivers/char/broadcom/vc_cma/vc_cma.c 1970-01-01 01:00:00.000000000 +0100
  8999. +++ linux-raspberry-pi/drivers/char/broadcom/vc_cma/vc_cma.c 2014-03-11 17:51:12.000000000 +0100
  9000. @@ -0,0 +1,1143 @@
  9001. +/**
  9002. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  9003. + *
  9004. + * Redistribution and use in source and binary forms, with or without
  9005. + * modification, are permitted provided that the following conditions
  9006. + * are met:
  9007. + * 1. Redistributions of source code must retain the above copyright
  9008. + * notice, this list of conditions, and the following disclaimer,
  9009. + * without modification.
  9010. + * 2. Redistributions in binary form must reproduce the above copyright
  9011. + * notice, this list of conditions and the following disclaimer in the
  9012. + * documentation and/or other materials provided with the distribution.
  9013. + * 3. The names of the above-listed copyright holders may not be used
  9014. + * to endorse or promote products derived from this software without
  9015. + * specific prior written permission.
  9016. + *
  9017. + * ALTERNATIVELY, this software may be distributed under the terms of the
  9018. + * GNU General Public License ("GPL") version 2, as published by the Free
  9019. + * Software Foundation.
  9020. + *
  9021. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9022. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  9023. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  9024. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  9025. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9026. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9027. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9028. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  9029. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  9030. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  9031. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9032. + */
  9033. +
  9034. +#include <linux/kernel.h>
  9035. +#include <linux/module.h>
  9036. +#include <linux/kthread.h>
  9037. +#include <linux/fs.h>
  9038. +#include <linux/device.h>
  9039. +#include <linux/cdev.h>
  9040. +#include <linux/mm.h>
  9041. +#include <linux/proc_fs.h>
  9042. +#include <linux/seq_file.h>
  9043. +#include <linux/dma-mapping.h>
  9044. +#include <linux/dma-contiguous.h>
  9045. +#include <linux/platform_device.h>
  9046. +#include <linux/uaccess.h>
  9047. +#include <asm/cacheflush.h>
  9048. +
  9049. +#include "vc_cma.h"
  9050. +
  9051. +#include "vchiq_util.h"
  9052. +#include "vchiq_connected.h"
  9053. +//#include "debug_sym.h"
  9054. +//#include "vc_mem.h"
  9055. +
  9056. +#define DRIVER_NAME "vc-cma"
  9057. +
  9058. +#define LOG_DBG(fmt, ...) \
  9059. + if (vc_cma_debug) \
  9060. + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
  9061. +#define LOG_ERR(fmt, ...) \
  9062. + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
  9063. +
  9064. +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
  9065. +#define VC_CMA_VERSION 2
  9066. +
  9067. +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
  9068. +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
  9069. +#define VC_CMA_MAX_PARAMS_PER_MSG \
  9070. + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
  9071. +#define VC_CMA_RESERVE_COUNT_MAX 16
  9072. +
  9073. +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
  9074. +
  9075. +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
  9076. +
  9077. +#define loud_error(...) \
  9078. + LOG_ERR("===== " __VA_ARGS__)
  9079. +
  9080. +enum {
  9081. + VC_CMA_MSG_QUIT,
  9082. + VC_CMA_MSG_OPEN,
  9083. + VC_CMA_MSG_TICK,
  9084. + VC_CMA_MSG_ALLOC, /* chunk count */
  9085. + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
  9086. + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
  9087. + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
  9088. + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
  9089. + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
  9090. + VC_CMA_MSG_UPDATE_RESERVE,
  9091. + VC_CMA_MSG_MAX
  9092. +};
  9093. +
  9094. +struct cma_msg {
  9095. + unsigned short type;
  9096. + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
  9097. +};
  9098. +
  9099. +struct vc_cma_reserve_user {
  9100. + unsigned int pid;
  9101. + unsigned int reserve;
  9102. +};
  9103. +
  9104. +/* Device (/dev) related variables */
  9105. +static dev_t vc_cma_devnum;
  9106. +static struct class *vc_cma_class;
  9107. +static struct cdev vc_cma_cdev;
  9108. +static int vc_cma_inited;
  9109. +static int vc_cma_debug;
  9110. +
  9111. +/* Proc entry */
  9112. +static struct proc_dir_entry *vc_cma_proc_entry;
  9113. +
  9114. +phys_addr_t vc_cma_base;
  9115. +struct page *vc_cma_base_page;
  9116. +unsigned int vc_cma_size;
  9117. +EXPORT_SYMBOL(vc_cma_size);
  9118. +unsigned int vc_cma_initial;
  9119. +unsigned int vc_cma_chunks;
  9120. +unsigned int vc_cma_chunks_used;
  9121. +unsigned int vc_cma_chunks_reserved;
  9122. +
  9123. +static int in_loud_error;
  9124. +
  9125. +unsigned int vc_cma_reserve_total;
  9126. +unsigned int vc_cma_reserve_count;
  9127. +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
  9128. +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
  9129. +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
  9130. +
  9131. +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
  9132. +static struct platform_device vc_cma_device = {
  9133. + .name = "vc-cma",
  9134. + .id = 0,
  9135. + .dev = {
  9136. + .dma_mask = &vc_cma_dma_mask,
  9137. + .coherent_dma_mask = DMA_BIT_MASK(32),
  9138. + },
  9139. +};
  9140. +
  9141. +static VCHIQ_INSTANCE_T cma_instance;
  9142. +static VCHIQ_SERVICE_HANDLE_T cma_service;
  9143. +static VCHIU_QUEUE_T cma_msg_queue;
  9144. +static struct task_struct *cma_worker;
  9145. +
  9146. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
  9147. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
  9148. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  9149. + VCHIQ_HEADER_T * header,
  9150. + VCHIQ_SERVICE_HANDLE_T service,
  9151. + void *bulk_userdata);
  9152. +static void send_vc_msg(unsigned short type,
  9153. + unsigned short param1, unsigned short param2);
  9154. +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
  9155. +
  9156. +static int early_vc_cma_mem(char *p)
  9157. +{
  9158. + unsigned int new_size;
  9159. + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
  9160. + vc_cma_size = memparse(p, &p);
  9161. + vc_cma_initial = vc_cma_size;
  9162. + if (*p == '/')
  9163. + vc_cma_size = memparse(p + 1, &p);
  9164. + if (*p == '@')
  9165. + vc_cma_base = memparse(p + 1, &p);
  9166. +
  9167. + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
  9168. + & ~(VC_CMA_CHUNK_SIZE - 1);
  9169. + if (new_size > vc_cma_size)
  9170. + vc_cma_size = 0;
  9171. + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
  9172. + & ~(VC_CMA_CHUNK_SIZE - 1);
  9173. + if (vc_cma_initial > vc_cma_size)
  9174. + vc_cma_initial = vc_cma_size;
  9175. + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
  9176. + & ~(VC_CMA_CHUNK_SIZE - 1);
  9177. +
  9178. + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
  9179. + vc_cma_size, (unsigned int)vc_cma_base);
  9180. +
  9181. + return 0;
  9182. +}
  9183. +
  9184. +early_param("vc-cma-mem", early_vc_cma_mem);
  9185. +
  9186. +void vc_cma_early_init(void)
  9187. +{
  9188. + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
  9189. + if (vc_cma_size) {
  9190. + int rc = platform_device_register(&vc_cma_device);
  9191. + LOG_DBG("platform_device_register -> %d", rc);
  9192. + }
  9193. +}
  9194. +
  9195. +void vc_cma_reserve(void)
  9196. +{
  9197. + /* if vc_cma_size is set, then declare vc CMA area of the same
  9198. + * size from the end of memory
  9199. + */
  9200. + if (vc_cma_size) {
  9201. + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
  9202. + vc_cma_base, 0) == 0) {
  9203. + } else {
  9204. + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
  9205. + vc_cma_size, (unsigned int)vc_cma_base);
  9206. + vc_cma_size = 0;
  9207. + }
  9208. + }
  9209. + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
  9210. +}
  9211. +
  9212. +/****************************************************************************
  9213. +*
  9214. +* vc_cma_open
  9215. +*
  9216. +***************************************************************************/
  9217. +
  9218. +static int vc_cma_open(struct inode *inode, struct file *file)
  9219. +{
  9220. + (void)inode;
  9221. + (void)file;
  9222. +
  9223. + return 0;
  9224. +}
  9225. +
  9226. +/****************************************************************************
  9227. +*
  9228. +* vc_cma_release
  9229. +*
  9230. +***************************************************************************/
  9231. +
  9232. +static int vc_cma_release(struct inode *inode, struct file *file)
  9233. +{
  9234. + (void)inode;
  9235. + (void)file;
  9236. +
  9237. + vc_cma_set_reserve(0, current->tgid);
  9238. +
  9239. + return 0;
  9240. +}
  9241. +
  9242. +/****************************************************************************
  9243. +*
  9244. +* vc_cma_ioctl
  9245. +*
  9246. +***************************************************************************/
  9247. +
  9248. +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  9249. +{
  9250. + int rc = 0;
  9251. +
  9252. + (void)cmd;
  9253. + (void)arg;
  9254. +
  9255. + switch (cmd) {
  9256. + case VC_CMA_IOC_RESERVE:
  9257. + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
  9258. + if (rc >= 0)
  9259. + rc = 0;
  9260. + break;
  9261. + default:
  9262. + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
  9263. + return -ENOTTY;
  9264. + }
  9265. +
  9266. + return rc;
  9267. +}
  9268. +
  9269. +/****************************************************************************
  9270. +*
  9271. +* File Operations for the driver.
  9272. +*
  9273. +***************************************************************************/
  9274. +
  9275. +static const struct file_operations vc_cma_fops = {
  9276. + .owner = THIS_MODULE,
  9277. + .open = vc_cma_open,
  9278. + .release = vc_cma_release,
  9279. + .unlocked_ioctl = vc_cma_ioctl,
  9280. +};
  9281. +
  9282. +/****************************************************************************
  9283. +*
  9284. +* vc_cma_proc_open
  9285. +*
  9286. +***************************************************************************/
  9287. +
  9288. +static int vc_cma_show_info(struct seq_file *m, void *v)
  9289. +{
  9290. + int i;
  9291. +
  9292. + seq_printf(m, "Videocore CMA:\n");
  9293. + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
  9294. + seq_printf(m, " Length : %08x\n", vc_cma_size);
  9295. + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
  9296. + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
  9297. + seq_printf(m, " Chunks : %4d (%d bytes)\n",
  9298. + (int)vc_cma_chunks,
  9299. + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
  9300. + seq_printf(m, " Used : %4d (%d bytes)\n",
  9301. + (int)vc_cma_chunks_used,
  9302. + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
  9303. + seq_printf(m, " Reserved : %4d (%d bytes)\n",
  9304. + (unsigned int)vc_cma_chunks_reserved,
  9305. + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
  9306. +
  9307. + for (i = 0; i < vc_cma_reserve_count; i++) {
  9308. + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
  9309. + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
  9310. + user->reserve);
  9311. + }
  9312. +
  9313. + seq_printf(m, "\n");
  9314. +
  9315. + return 0;
  9316. +}
  9317. +
  9318. +static int vc_cma_proc_open(struct inode *inode, struct file *file)
  9319. +{
  9320. + return single_open(file, vc_cma_show_info, NULL);
  9321. +}
  9322. +
  9323. +/****************************************************************************
  9324. +*
  9325. +* vc_cma_proc_write
  9326. +*
  9327. +***************************************************************************/
  9328. +
  9329. +static int vc_cma_proc_write(struct file *file,
  9330. + const char __user *buffer,
  9331. + size_t size, loff_t *ppos)
  9332. +{
  9333. + int rc = -EFAULT;
  9334. + char input_str[20];
  9335. +
  9336. + memset(input_str, 0, sizeof(input_str));
  9337. +
  9338. + if (size > sizeof(input_str)) {
  9339. + LOG_ERR("%s: input string length too long", __func__);
  9340. + goto out;
  9341. + }
  9342. +
  9343. + if (copy_from_user(input_str, buffer, size - 1)) {
  9344. + LOG_ERR("%s: failed to get input string", __func__);
  9345. + goto out;
  9346. + }
  9347. +#define ALLOC_STR "alloc"
  9348. +#define FREE_STR "free"
  9349. +#define DEBUG_STR "debug"
  9350. +#define RESERVE_STR "reserve"
  9351. + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
  9352. + int size;
  9353. + char *p = input_str + strlen(ALLOC_STR);
  9354. +
  9355. + while (*p == ' ')
  9356. + p++;
  9357. + size = memparse(p, NULL);
  9358. + LOG_ERR("/proc/vc-cma: alloc %d", size);
  9359. + if (size)
  9360. + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
  9361. + size / VC_CMA_CHUNK_SIZE, 0);
  9362. + else
  9363. + LOG_ERR("invalid size '%s'", p);
  9364. + rc = size;
  9365. + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
  9366. + int size;
  9367. + char *p = input_str + strlen(FREE_STR);
  9368. +
  9369. + while (*p == ' ')
  9370. + p++;
  9371. + size = memparse(p, NULL);
  9372. + LOG_ERR("/proc/vc-cma: free %d", size);
  9373. + if (size)
  9374. + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
  9375. + size / VC_CMA_CHUNK_SIZE, 0);
  9376. + else
  9377. + LOG_ERR("invalid size '%s'", p);
  9378. + rc = size;
  9379. + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
  9380. + char *p = input_str + strlen(DEBUG_STR);
  9381. + while (*p == ' ')
  9382. + p++;
  9383. + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
  9384. + vc_cma_debug = 1;
  9385. + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
  9386. + vc_cma_debug = 0;
  9387. + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
  9388. + rc = size;
  9389. + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
  9390. + int size;
  9391. + int reserved;
  9392. + char *p = input_str + strlen(RESERVE_STR);
  9393. + while (*p == ' ')
  9394. + p++;
  9395. + size = memparse(p, NULL);
  9396. +
  9397. + reserved = vc_cma_set_reserve(size, current->tgid);
  9398. + rc = (reserved >= 0) ? size : reserved;
  9399. + }
  9400. +
  9401. +out:
  9402. + return rc;
  9403. +}
  9404. +
  9405. +/****************************************************************************
  9406. +*
  9407. +* File Operations for /proc interface.
  9408. +*
  9409. +***************************************************************************/
  9410. +
  9411. +static const struct file_operations vc_cma_proc_fops = {
  9412. + .open = vc_cma_proc_open,
  9413. + .read = seq_read,
  9414. + .write = vc_cma_proc_write,
  9415. + .llseek = seq_lseek,
  9416. + .release = single_release
  9417. +};
  9418. +
  9419. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
  9420. +{
  9421. + struct vc_cma_reserve_user *user = NULL;
  9422. + int delta = 0;
  9423. + int i;
  9424. +
  9425. + if (down_interruptible(&vc_cma_reserve_mutex))
  9426. + return -ERESTARTSYS;
  9427. +
  9428. + for (i = 0; i < vc_cma_reserve_count; i++) {
  9429. + if (pid == vc_cma_reserve_users[i].pid) {
  9430. + user = &vc_cma_reserve_users[i];
  9431. + delta = reserve - user->reserve;
  9432. + if (reserve)
  9433. + user->reserve = reserve;
  9434. + else {
  9435. + /* Remove this entry by copying downwards */
  9436. + while ((i + 1) < vc_cma_reserve_count) {
  9437. + user[0].pid = user[1].pid;
  9438. + user[0].reserve = user[1].reserve;
  9439. + user++;
  9440. + i++;
  9441. + }
  9442. + vc_cma_reserve_count--;
  9443. + user = NULL;
  9444. + }
  9445. + break;
  9446. + }
  9447. + }
  9448. +
  9449. + if (reserve && !user) {
  9450. + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
  9451. + LOG_ERR("vc-cma: Too many reservations - "
  9452. + "increase CMA_RESERVE_COUNT_MAX");
  9453. + up(&vc_cma_reserve_mutex);
  9454. + return -EBUSY;
  9455. + }
  9456. + user = &vc_cma_reserve_users[vc_cma_reserve_count];
  9457. + user->pid = pid;
  9458. + user->reserve = reserve;
  9459. + delta = reserve;
  9460. + vc_cma_reserve_count++;
  9461. + }
  9462. +
  9463. + vc_cma_reserve_total += delta;
  9464. +
  9465. + send_vc_msg(VC_CMA_MSG_RESERVE,
  9466. + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
  9467. +
  9468. + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
  9469. +
  9470. + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
  9471. + reserve, pid, vc_cma_reserve_total);
  9472. +
  9473. + up(&vc_cma_reserve_mutex);
  9474. +
  9475. + return vc_cma_reserve_total;
  9476. +}
  9477. +
  9478. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  9479. + VCHIQ_HEADER_T * header,
  9480. + VCHIQ_SERVICE_HANDLE_T service,
  9481. + void *bulk_userdata)
  9482. +{
  9483. + switch (reason) {
  9484. + case VCHIQ_MESSAGE_AVAILABLE:
  9485. + if (!send_worker_msg(header))
  9486. + return VCHIQ_RETRY;
  9487. + break;
  9488. + case VCHIQ_SERVICE_CLOSED:
  9489. + LOG_DBG("CMA service closed");
  9490. + break;
  9491. + default:
  9492. + LOG_ERR("Unexpected CMA callback reason %d", reason);
  9493. + break;
  9494. + }
  9495. + return VCHIQ_SUCCESS;
  9496. +}
  9497. +
  9498. +static void send_vc_msg(unsigned short type,
  9499. + unsigned short param1, unsigned short param2)
  9500. +{
  9501. + unsigned short msg[] = { type, param1, param2 };
  9502. + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
  9503. + VCHIQ_STATUS_T ret;
  9504. + vchiq_use_service(cma_service);
  9505. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9506. + vchiq_release_service(cma_service);
  9507. + if (ret != VCHIQ_SUCCESS)
  9508. + LOG_ERR("vchiq_queue_message returned %x", ret);
  9509. +}
  9510. +
  9511. +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
  9512. +{
  9513. + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
  9514. + return false;
  9515. + vchiu_queue_push(&cma_msg_queue, msg);
  9516. + up(&vc_cma_worker_queue_push_mutex);
  9517. + return true;
  9518. +}
  9519. +
  9520. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
  9521. +{
  9522. + int i;
  9523. + for (i = 0; i < num_chunks; i++) {
  9524. + struct page *chunk;
  9525. + unsigned int chunk_num;
  9526. + uint8_t *chunk_addr;
  9527. + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
  9528. +
  9529. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  9530. + PAGES_PER_CHUNK,
  9531. + VC_CMA_CHUNK_ORDER);
  9532. + if (!chunk)
  9533. + break;
  9534. +
  9535. + chunk_addr = page_address(chunk);
  9536. + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
  9537. + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
  9538. + chunk_size);
  9539. +
  9540. + chunk_num =
  9541. + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
  9542. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  9543. + VC_CMA_CHUNK_SIZE) != 0);
  9544. + if (chunk_num >= vc_cma_chunks) {
  9545. + LOG_ERR("%s: ===============================",
  9546. + __func__);
  9547. + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
  9548. + "bad SPARSEMEM configuration?",
  9549. + __func__, (unsigned int)page_to_phys(chunk),
  9550. + vc_cma_base, vc_cma_base + vc_cma_size - 1);
  9551. + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
  9552. + (void*)0/*vc_cma_device.dev.cma_area*/);
  9553. + LOG_ERR("%s: ===============================",
  9554. + __func__);
  9555. + break;
  9556. + }
  9557. + reply->params[i] = chunk_num;
  9558. + vc_cma_chunks_used++;
  9559. + }
  9560. +
  9561. + if (i < num_chunks) {
  9562. + LOG_ERR("%s: dma_alloc_from_contiguous failed "
  9563. + "for %x bytes (alloc %d of %d, %d free)",
  9564. + __func__, VC_CMA_CHUNK_SIZE, i,
  9565. + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
  9566. + num_chunks = i;
  9567. + }
  9568. +
  9569. + LOG_DBG("CMA allocated %d chunks -> %d used",
  9570. + num_chunks, vc_cma_chunks_used);
  9571. + reply->type = VC_CMA_MSG_ALLOCATED;
  9572. +
  9573. + {
  9574. + VCHIQ_ELEMENT_T elem = {
  9575. + reply,
  9576. + offsetof(struct cma_msg, params[0]) +
  9577. + num_chunks * sizeof(reply->params[0])
  9578. + };
  9579. + VCHIQ_STATUS_T ret;
  9580. + vchiq_use_service(cma_service);
  9581. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9582. + vchiq_release_service(cma_service);
  9583. + if (ret != VCHIQ_SUCCESS)
  9584. + LOG_ERR("vchiq_queue_message return " "%x", ret);
  9585. + }
  9586. +
  9587. + return num_chunks;
  9588. +}
  9589. +
  9590. +static int cma_worker_proc(void *param)
  9591. +{
  9592. + static struct cma_msg reply;
  9593. + (void)param;
  9594. +
  9595. + while (1) {
  9596. + VCHIQ_HEADER_T *msg;
  9597. + static struct cma_msg msg_copy;
  9598. + struct cma_msg *cma_msg = &msg_copy;
  9599. + int type, msg_size;
  9600. +
  9601. + msg = vchiu_queue_pop(&cma_msg_queue);
  9602. + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
  9603. + msg_size = msg->size;
  9604. + memcpy(&msg_copy, msg->data, msg_size);
  9605. + type = cma_msg->type;
  9606. + vchiq_release_message(cma_service, msg);
  9607. + } else {
  9608. + msg_size = 0;
  9609. + type = (int)msg;
  9610. + if (type == VC_CMA_MSG_QUIT)
  9611. + break;
  9612. + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
  9613. + msg = NULL;
  9614. + cma_msg = NULL;
  9615. + } else {
  9616. + BUG();
  9617. + continue;
  9618. + }
  9619. + }
  9620. +
  9621. + switch (type) {
  9622. + case VC_CMA_MSG_ALLOC:{
  9623. + int num_chunks, free_chunks;
  9624. + num_chunks = cma_msg->params[0];
  9625. + free_chunks =
  9626. + vc_cma_chunks - vc_cma_chunks_used;
  9627. + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
  9628. + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
  9629. + LOG_ERR
  9630. + ("CMA_MSG_ALLOC - chunk count (%d) "
  9631. + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
  9632. + num_chunks,
  9633. + VC_CMA_MAX_PARAMS_PER_MSG);
  9634. + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
  9635. + }
  9636. +
  9637. + if (num_chunks > free_chunks) {
  9638. + LOG_ERR
  9639. + ("CMA_MSG_ALLOC - chunk count (%d) "
  9640. + "exceeds free chunks (%d)",
  9641. + num_chunks, free_chunks);
  9642. + num_chunks = free_chunks;
  9643. + }
  9644. +
  9645. + vc_cma_alloc_chunks(num_chunks, &reply);
  9646. + }
  9647. + break;
  9648. +
  9649. + case VC_CMA_MSG_FREE:{
  9650. + int chunk_count =
  9651. + (msg_size -
  9652. + offsetof(struct cma_msg,
  9653. + params)) /
  9654. + sizeof(cma_msg->params[0]);
  9655. + int i;
  9656. + BUG_ON(chunk_count <= 0);
  9657. +
  9658. + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
  9659. + chunk_count, cma_msg->params[0]);
  9660. + for (i = 0; i < chunk_count; i++) {
  9661. + int chunk_num = cma_msg->params[i];
  9662. + struct page *page = vc_cma_base_page +
  9663. + chunk_num * PAGES_PER_CHUNK;
  9664. + if (chunk_num >= vc_cma_chunks) {
  9665. + LOG_ERR
  9666. + ("CMA_MSG_FREE - chunk %d of %d"
  9667. + " (value %x) exceeds maximum "
  9668. + "(%x)", i, chunk_count,
  9669. + chunk_num,
  9670. + vc_cma_chunks - 1);
  9671. + break;
  9672. + }
  9673. +
  9674. + if (!dma_release_from_contiguous
  9675. + (NULL /*&vc_cma_device.dev*/, page,
  9676. + PAGES_PER_CHUNK)) {
  9677. + LOG_ERR
  9678. + ("CMA_MSG_FREE - failed to "
  9679. + "release chunk %d (phys %x, "
  9680. + "page %x)", chunk_num,
  9681. + page_to_phys(page),
  9682. + (unsigned int)page);
  9683. + }
  9684. + vc_cma_chunks_used--;
  9685. + }
  9686. + LOG_DBG("CMA released %d chunks -> %d used",
  9687. + i, vc_cma_chunks_used);
  9688. + }
  9689. + break;
  9690. +
  9691. + case VC_CMA_MSG_UPDATE_RESERVE:{
  9692. + int chunks_needed =
  9693. + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
  9694. + 1)
  9695. + / VC_CMA_CHUNK_SIZE) -
  9696. + vc_cma_chunks_reserved;
  9697. +
  9698. + LOG_DBG
  9699. + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
  9700. + chunks_needed);
  9701. +
  9702. + /* Cap the reservations to what is available */
  9703. + if (chunks_needed > 0) {
  9704. + if (chunks_needed >
  9705. + (vc_cma_chunks -
  9706. + vc_cma_chunks_used))
  9707. + chunks_needed =
  9708. + (vc_cma_chunks -
  9709. + vc_cma_chunks_used);
  9710. +
  9711. + chunks_needed =
  9712. + vc_cma_alloc_chunks(chunks_needed,
  9713. + &reply);
  9714. + }
  9715. +
  9716. + LOG_DBG
  9717. + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
  9718. + chunks_needed);
  9719. + vc_cma_chunks_reserved += chunks_needed;
  9720. + }
  9721. + break;
  9722. +
  9723. + default:
  9724. + LOG_ERR("unexpected msg type %d", type);
  9725. + break;
  9726. + }
  9727. + }
  9728. +
  9729. + LOG_DBG("quitting...");
  9730. + return 0;
  9731. +}
  9732. +
  9733. +/****************************************************************************
  9734. +*
  9735. +* vc_cma_connected_init
  9736. +*
  9737. +* This function is called once the videocore has been connected.
  9738. +*
  9739. +***************************************************************************/
  9740. +
  9741. +static void vc_cma_connected_init(void)
  9742. +{
  9743. + VCHIQ_SERVICE_PARAMS_T service_params;
  9744. +
  9745. + LOG_DBG("vc_cma_connected_init");
  9746. +
  9747. + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
  9748. + LOG_ERR("could not create CMA msg queue");
  9749. + goto fail_queue;
  9750. + }
  9751. +
  9752. + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
  9753. + goto fail_vchiq_init;
  9754. +
  9755. + vchiq_connect(cma_instance);
  9756. +
  9757. + service_params.fourcc = VC_CMA_FOURCC;
  9758. + service_params.callback = cma_service_callback;
  9759. + service_params.userdata = NULL;
  9760. + service_params.version = VC_CMA_VERSION;
  9761. + service_params.version_min = VC_CMA_VERSION;
  9762. +
  9763. + if (vchiq_open_service(cma_instance, &service_params,
  9764. + &cma_service) != VCHIQ_SUCCESS) {
  9765. + LOG_ERR("failed to open service - already in use?");
  9766. + goto fail_vchiq_open;
  9767. + }
  9768. +
  9769. + vchiq_release_service(cma_service);
  9770. +
  9771. + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
  9772. + if (!cma_worker) {
  9773. + LOG_ERR("could not create CMA worker thread");
  9774. + goto fail_worker;
  9775. + }
  9776. + set_user_nice(cma_worker, -20);
  9777. + wake_up_process(cma_worker);
  9778. +
  9779. + return;
  9780. +
  9781. +fail_worker:
  9782. + vchiq_close_service(cma_service);
  9783. +fail_vchiq_open:
  9784. + vchiq_shutdown(cma_instance);
  9785. +fail_vchiq_init:
  9786. + vchiu_queue_delete(&cma_msg_queue);
  9787. +fail_queue:
  9788. + return;
  9789. +}
  9790. +
  9791. +void
  9792. +loud_error_header(void)
  9793. +{
  9794. + if (in_loud_error)
  9795. + return;
  9796. +
  9797. + LOG_ERR("============================================================"
  9798. + "================");
  9799. + LOG_ERR("============================================================"
  9800. + "================");
  9801. + LOG_ERR("=====");
  9802. +
  9803. + in_loud_error = 1;
  9804. +}
  9805. +
  9806. +void
  9807. +loud_error_footer(void)
  9808. +{
  9809. + if (!in_loud_error)
  9810. + return;
  9811. +
  9812. + LOG_ERR("=====");
  9813. + LOG_ERR("============================================================"
  9814. + "================");
  9815. + LOG_ERR("============================================================"
  9816. + "================");
  9817. +
  9818. + in_loud_error = 0;
  9819. +}
  9820. +
  9821. +#if 1
  9822. +static int check_cma_config(void) { return 1; }
  9823. +#else
  9824. +static int
  9825. +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
  9826. + const char *symbol,
  9827. + void *buf, size_t bufsize)
  9828. +{
  9829. + VC_MEM_ADDR_T vcMemAddr;
  9830. + size_t vcMemSize;
  9831. + uint8_t *mapAddr;
  9832. + off_t vcMapAddr;
  9833. +
  9834. + if (!LookupVideoCoreSymbol(handle, symbol,
  9835. + &vcMemAddr,
  9836. + &vcMemSize)) {
  9837. + loud_error_header();
  9838. + loud_error(
  9839. + "failed to find VC symbol \"%s\".",
  9840. + symbol);
  9841. + loud_error_footer();
  9842. + return 0;
  9843. + }
  9844. +
  9845. + if (vcMemSize != bufsize) {
  9846. + loud_error_header();
  9847. + loud_error(
  9848. + "VC symbol \"%s\" is the wrong size.",
  9849. + symbol);
  9850. + loud_error_footer();
  9851. + return 0;
  9852. + }
  9853. +
  9854. + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
  9855. + vcMapAddr += mm_vc_mem_phys_addr;
  9856. + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
  9857. + if (mapAddr == 0) {
  9858. + loud_error_header();
  9859. + loud_error(
  9860. + "failed to ioremap \"%s\" @ 0x%x "
  9861. + "(phys: 0x%x, size: %u).",
  9862. + symbol,
  9863. + (unsigned int)vcMapAddr,
  9864. + (unsigned int)vcMemAddr,
  9865. + (unsigned int)vcMemSize);
  9866. + loud_error_footer();
  9867. + return 0;
  9868. + }
  9869. +
  9870. + memcpy(buf, mapAddr, bufsize);
  9871. + iounmap(mapAddr);
  9872. +
  9873. + return 1;
  9874. +}
  9875. +
  9876. +
  9877. +static int
  9878. +check_cma_config(void)
  9879. +{
  9880. + VC_MEM_ACCESS_HANDLE_T mem_hndl;
  9881. + VC_MEM_ADDR_T mempool_start;
  9882. + VC_MEM_ADDR_T mempool_end;
  9883. + VC_MEM_ADDR_T mempool_offline_start;
  9884. + VC_MEM_ADDR_T mempool_offline_end;
  9885. + VC_MEM_ADDR_T cam_alloc_base;
  9886. + VC_MEM_ADDR_T cam_alloc_size;
  9887. + VC_MEM_ADDR_T cam_alloc_end;
  9888. + int success = 0;
  9889. +
  9890. + if (OpenVideoCoreMemory(&mem_hndl) != 0)
  9891. + goto out;
  9892. +
  9893. + /* Read the relevant VideoCore variables */
  9894. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
  9895. + &mempool_start,
  9896. + sizeof(mempool_start)))
  9897. + goto close;
  9898. +
  9899. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
  9900. + &mempool_end,
  9901. + sizeof(mempool_end)))
  9902. + goto close;
  9903. +
  9904. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
  9905. + &mempool_offline_start,
  9906. + sizeof(mempool_offline_start)))
  9907. + goto close;
  9908. +
  9909. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
  9910. + &mempool_offline_end,
  9911. + sizeof(mempool_offline_end)))
  9912. + goto close;
  9913. +
  9914. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
  9915. + &cam_alloc_base,
  9916. + sizeof(cam_alloc_base)))
  9917. + goto close;
  9918. +
  9919. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
  9920. + &cam_alloc_size,
  9921. + sizeof(cam_alloc_size)))
  9922. + goto close;
  9923. +
  9924. + cam_alloc_end = cam_alloc_base + cam_alloc_size;
  9925. +
  9926. + success = 1;
  9927. +
  9928. + /* Now the sanity checks */
  9929. + if (!mempool_offline_start)
  9930. + mempool_offline_start = mempool_start;
  9931. + if (!mempool_offline_end)
  9932. + mempool_offline_end = mempool_end;
  9933. +
  9934. + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
  9935. + loud_error_header();
  9936. + loud_error(
  9937. + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
  9938. + "vc_cma_base(%x)",
  9939. + mempool_offline_start,
  9940. + VCADDR_TO_PHYSADDR(mempool_offline_start),
  9941. + vc_cma_base);
  9942. + success = 0;
  9943. + }
  9944. +
  9945. + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
  9946. + (vc_cma_base + vc_cma_size)) {
  9947. + loud_error_header();
  9948. + loud_error(
  9949. + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
  9950. + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
  9951. + mempool_offline_start,
  9952. + VCADDR_TO_PHYSADDR(mempool_offline_end),
  9953. + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
  9954. + success = 0;
  9955. + }
  9956. +
  9957. + if (mempool_end < mempool_start) {
  9958. + loud_error_header();
  9959. + loud_error(
  9960. + "__MEMPOOL_END(%x) must not be before "
  9961. + "__MEMPOOL_START(%x)",
  9962. + mempool_end,
  9963. + mempool_start);
  9964. + success = 0;
  9965. + }
  9966. +
  9967. + if (mempool_offline_end < mempool_offline_start) {
  9968. + loud_error_header();
  9969. + loud_error(
  9970. + "__MEMPOOL_OFFLINE_END(%x) must not be before "
  9971. + "__MEMPOOL_OFFLINE_START(%x)",
  9972. + mempool_offline_end,
  9973. + mempool_offline_start);
  9974. + success = 0;
  9975. + }
  9976. +
  9977. + if (mempool_offline_start < mempool_start) {
  9978. + loud_error_header();
  9979. + loud_error(
  9980. + "__MEMPOOL_OFFLINE_START(%x) must not be before "
  9981. + "__MEMPOOL_START(%x)",
  9982. + mempool_offline_start,
  9983. + mempool_start);
  9984. + success = 0;
  9985. + }
  9986. +
  9987. + if (mempool_offline_end > mempool_end) {
  9988. + loud_error_header();
  9989. + loud_error(
  9990. + "__MEMPOOL_OFFLINE_END(%x) must not be after "
  9991. + "__MEMPOOL_END(%x)",
  9992. + mempool_offline_end,
  9993. + mempool_end);
  9994. + success = 0;
  9995. + }
  9996. +
  9997. + if ((cam_alloc_base < mempool_end) &&
  9998. + (cam_alloc_end > mempool_start)) {
  9999. + loud_error_header();
  10000. + loud_error(
  10001. + "cam_alloc pool(%x-%x) overlaps "
  10002. + "mempool(%x-%x)",
  10003. + cam_alloc_base, cam_alloc_end,
  10004. + mempool_start, mempool_end);
  10005. + success = 0;
  10006. + }
  10007. +
  10008. + loud_error_footer();
  10009. +
  10010. +close:
  10011. + CloseVideoCoreMemory(mem_hndl);
  10012. +
  10013. +out:
  10014. + return success;
  10015. +}
  10016. +#endif
  10017. +
  10018. +static int vc_cma_init(void)
  10019. +{
  10020. + int rc = -EFAULT;
  10021. + struct device *dev;
  10022. +
  10023. + if (!check_cma_config())
  10024. + goto out_release;
  10025. +
  10026. + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
  10027. + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
  10028. + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
  10029. + vc_cma_size, vc_cma_size / (1024 * 1024));
  10030. + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
  10031. + vc_cma_initial, vc_cma_initial / (1024 * 1024));
  10032. +
  10033. + vc_cma_base_page = phys_to_page(vc_cma_base);
  10034. +
  10035. + if (vc_cma_chunks) {
  10036. + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
  10037. +
  10038. + for (vc_cma_chunks_used = 0;
  10039. + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
  10040. + struct page *chunk;
  10041. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  10042. + PAGES_PER_CHUNK,
  10043. + VC_CMA_CHUNK_ORDER);
  10044. + if (!chunk)
  10045. + break;
  10046. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  10047. + VC_CMA_CHUNK_SIZE) != 0);
  10048. + }
  10049. + if (vc_cma_chunks_used != chunks_needed) {
  10050. + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
  10051. + "bytes, allocation %d of %d)",
  10052. + __func__, VC_CMA_CHUNK_SIZE,
  10053. + vc_cma_chunks_used, chunks_needed);
  10054. + goto out_release;
  10055. + }
  10056. +
  10057. + vchiq_add_connected_callback(vc_cma_connected_init);
  10058. + }
  10059. +
  10060. + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
  10061. + if (rc < 0) {
  10062. + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
  10063. + goto out_release;
  10064. + }
  10065. +
  10066. + cdev_init(&vc_cma_cdev, &vc_cma_fops);
  10067. + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
  10068. + if (rc != 0) {
  10069. + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
  10070. + goto out_unregister;
  10071. + }
  10072. +
  10073. + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
  10074. + if (IS_ERR(vc_cma_class)) {
  10075. + rc = PTR_ERR(vc_cma_class);
  10076. + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
  10077. + goto out_cdev_del;
  10078. + }
  10079. +
  10080. + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
  10081. + DRIVER_NAME);
  10082. + if (IS_ERR(dev)) {
  10083. + rc = PTR_ERR(dev);
  10084. + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
  10085. + goto out_class_destroy;
  10086. + }
  10087. +
  10088. + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
  10089. + if (vc_cma_proc_entry == NULL) {
  10090. + rc = -EFAULT;
  10091. + LOG_ERR("%s: proc_create failed", __func__);
  10092. + goto out_device_destroy;
  10093. + }
  10094. +
  10095. + vc_cma_inited = 1;
  10096. + return 0;
  10097. +
  10098. +out_device_destroy:
  10099. + device_destroy(vc_cma_class, vc_cma_devnum);
  10100. +
  10101. +out_class_destroy:
  10102. + class_destroy(vc_cma_class);
  10103. + vc_cma_class = NULL;
  10104. +
  10105. +out_cdev_del:
  10106. + cdev_del(&vc_cma_cdev);
  10107. +
  10108. +out_unregister:
  10109. + unregister_chrdev_region(vc_cma_devnum, 1);
  10110. +
  10111. +out_release:
  10112. + /* It is tempting to try to clean up by calling
  10113. + dma_release_from_contiguous for all allocated chunks, but it isn't
  10114. + a very safe thing to do. If vc_cma_initial is non-zero it is because
  10115. + VideoCore is already using that memory, so giving it back to Linux
  10116. + is likely to be fatal.
  10117. + */
  10118. + return -1;
  10119. +}
  10120. +
  10121. +/****************************************************************************
  10122. +*
  10123. +* vc_cma_exit
  10124. +*
  10125. +***************************************************************************/
  10126. +
  10127. +static void __exit vc_cma_exit(void)
  10128. +{
  10129. + LOG_DBG("%s: called", __func__);
  10130. +
  10131. + if (vc_cma_inited) {
  10132. + remove_proc_entry(DRIVER_NAME, NULL);
  10133. + device_destroy(vc_cma_class, vc_cma_devnum);
  10134. + class_destroy(vc_cma_class);
  10135. + cdev_del(&vc_cma_cdev);
  10136. + unregister_chrdev_region(vc_cma_devnum, 1);
  10137. + }
  10138. +}
  10139. +
  10140. +module_init(vc_cma_init);
  10141. +module_exit(vc_cma_exit);
  10142. +MODULE_LICENSE("GPL");
  10143. +MODULE_AUTHOR("Broadcom Corporation");
  10144. diff -Nur linux-3.12.13/drivers/char/hw_random/bcm2708-rng.c linux-raspberry-pi/drivers/char/hw_random/bcm2708-rng.c
  10145. --- linux-3.12.13/drivers/char/hw_random/bcm2708-rng.c 1970-01-01 01:00:00.000000000 +0100
  10146. +++ linux-raspberry-pi/drivers/char/hw_random/bcm2708-rng.c 2014-03-11 17:32:20.000000000 +0100
  10147. @@ -0,0 +1,117 @@
  10148. +/**
  10149. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10150. + *
  10151. + * Redistribution and use in source and binary forms, with or without
  10152. + * modification, are permitted provided that the following conditions
  10153. + * are met:
  10154. + * 1. Redistributions of source code must retain the above copyright
  10155. + * notice, this list of conditions, and the following disclaimer,
  10156. + * without modification.
  10157. + * 2. Redistributions in binary form must reproduce the above copyright
  10158. + * notice, this list of conditions and the following disclaimer in the
  10159. + * documentation and/or other materials provided with the distribution.
  10160. + * 3. The names of the above-listed copyright holders may not be used
  10161. + * to endorse or promote products derived from this software without
  10162. + * specific prior written permission.
  10163. + *
  10164. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10165. + * GNU General Public License ("GPL") version 2, as published by the Free
  10166. + * Software Foundation.
  10167. + *
  10168. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10169. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10170. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10171. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10172. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10173. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10174. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10175. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10176. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10177. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10178. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10179. + */
  10180. +
  10181. +#include <linux/kernel.h>
  10182. +#include <linux/module.h>
  10183. +#include <linux/init.h>
  10184. +#include <linux/hw_random.h>
  10185. +#include <linux/printk.h>
  10186. +
  10187. +#include <asm/io.h>
  10188. +#include <mach/hardware.h>
  10189. +#include <mach/platform.h>
  10190. +
  10191. +#define RNG_CTRL (0x0)
  10192. +#define RNG_STATUS (0x4)
  10193. +#define RNG_DATA (0x8)
  10194. +#define RNG_FF_THRESHOLD (0xc)
  10195. +
  10196. +/* enable rng */
  10197. +#define RNG_RBGEN 0x1
  10198. +/* double speed, less random mode */
  10199. +#define RNG_RBG2X 0x2
  10200. +
  10201. +/* the initial numbers generated are "less random" so will be discarded */
  10202. +#define RNG_WARMUP_COUNT 0x40000
  10203. +
  10204. +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
  10205. +{
  10206. + void __iomem *rng_base = (void __iomem *)rng->priv;
  10207. + unsigned words;
  10208. + /* wait for a random number to be in fifo */
  10209. + do {
  10210. + words = __raw_readl(rng_base + RNG_STATUS)>>24;
  10211. + }
  10212. + while (words == 0);
  10213. + /* read the random number */
  10214. + *buffer = __raw_readl(rng_base + RNG_DATA);
  10215. + return 4;
  10216. +}
  10217. +
  10218. +static struct hwrng bcm2708_rng_ops = {
  10219. + .name = "bcm2708",
  10220. + .data_read = bcm2708_rng_data_read,
  10221. +};
  10222. +
  10223. +static int __init bcm2708_rng_init(void)
  10224. +{
  10225. + void __iomem *rng_base;
  10226. + int err;
  10227. +
  10228. + /* map peripheral */
  10229. + rng_base = ioremap(RNG_BASE, 0x10);
  10230. + pr_info("bcm2708_rng_init=%p\n", rng_base);
  10231. + if (!rng_base) {
  10232. + pr_err("bcm2708_rng_init failed to ioremap\n");
  10233. + return -ENOMEM;
  10234. + }
  10235. + bcm2708_rng_ops.priv = (unsigned long)rng_base;
  10236. + /* register driver */
  10237. + err = hwrng_register(&bcm2708_rng_ops);
  10238. + if (err) {
  10239. + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
  10240. + iounmap(rng_base);
  10241. + } else {
  10242. + /* set warm-up count & enable */
  10243. + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  10244. + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  10245. + }
  10246. + return err;
  10247. +}
  10248. +
  10249. +static void __exit bcm2708_rng_exit(void)
  10250. +{
  10251. + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
  10252. + pr_info("bcm2708_rng_exit\n");
  10253. + /* disable rng hardware */
  10254. + __raw_writel(0, rng_base + RNG_CTRL);
  10255. + /* unregister driver */
  10256. + hwrng_unregister(&bcm2708_rng_ops);
  10257. + iounmap(rng_base);
  10258. +}
  10259. +
  10260. +module_init(bcm2708_rng_init);
  10261. +module_exit(bcm2708_rng_exit);
  10262. +
  10263. +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
  10264. +MODULE_LICENSE("GPL and additional rights");
  10265. diff -Nur linux-3.12.13/drivers/char/hw_random/Kconfig linux-raspberry-pi/drivers/char/hw_random/Kconfig
  10266. --- linux-3.12.13/drivers/char/hw_random/Kconfig 2014-02-22 22:32:50.000000000 +0100
  10267. +++ linux-raspberry-pi/drivers/char/hw_random/Kconfig 2014-03-11 17:51:12.000000000 +0100
  10268. @@ -314,3 +314,14 @@
  10269. module will be called tpm-rng.
  10270. If unsure, say Y.
  10271. +
  10272. +config HW_RANDOM_BCM2708
  10273. + tristate "BCM2708 generic true random number generator support"
  10274. + depends on HW_RANDOM && ARCH_BCM2708
  10275. + ---help---
  10276. + This driver provides the kernel-side support for the BCM2708 hardware.
  10277. +
  10278. + To compile this driver as a module, choose M here: the
  10279. + module will be called bcm2708-rng.
  10280. +
  10281. + If unsure, say N.
  10282. diff -Nur linux-3.12.13/drivers/char/hw_random/Makefile linux-raspberry-pi/drivers/char/hw_random/Makefile
  10283. --- linux-3.12.13/drivers/char/hw_random/Makefile 2014-02-22 22:32:50.000000000 +0100
  10284. +++ linux-raspberry-pi/drivers/char/hw_random/Makefile 2014-03-11 17:32:20.000000000 +0100
  10285. @@ -27,3 +27,4 @@
  10286. obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  10287. obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
  10288. obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
  10289. +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
  10290. diff -Nur linux-3.12.13/drivers/char/Kconfig linux-raspberry-pi/drivers/char/Kconfig
  10291. --- linux-3.12.13/drivers/char/Kconfig 2014-02-22 22:32:50.000000000 +0100
  10292. +++ linux-raspberry-pi/drivers/char/Kconfig 2014-03-11 17:51:12.000000000 +0100
  10293. @@ -574,6 +574,8 @@
  10294. source "drivers/s390/char/Kconfig"
  10295. +source "drivers/char/broadcom/Kconfig"
  10296. +
  10297. config MSM_SMD_PKT
  10298. bool "Enable device interface for some SMD packet ports"
  10299. default n
  10300. diff -Nur linux-3.12.13/drivers/char/Makefile linux-raspberry-pi/drivers/char/Makefile
  10301. --- linux-3.12.13/drivers/char/Makefile 2014-02-22 22:32:50.000000000 +0100
  10302. +++ linux-raspberry-pi/drivers/char/Makefile 2014-03-11 17:51:12.000000000 +0100
  10303. @@ -62,3 +62,5 @@
  10304. js-rtc-y = rtc.o
  10305. obj-$(CONFIG_TILE_SROM) += tile-srom.o
  10306. +
  10307. +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
  10308. diff -Nur linux-3.12.13/drivers/cpufreq/bcm2835-cpufreq.c linux-raspberry-pi/drivers/cpufreq/bcm2835-cpufreq.c
  10309. --- linux-3.12.13/drivers/cpufreq/bcm2835-cpufreq.c 1970-01-01 01:00:00.000000000 +0100
  10310. +++ linux-raspberry-pi/drivers/cpufreq/bcm2835-cpufreq.c 2014-03-11 17:51:13.000000000 +0100
  10311. @@ -0,0 +1,239 @@
  10312. +/*****************************************************************************
  10313. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  10314. +*
  10315. +* Unless you and Broadcom execute a separate written software license
  10316. +* agreement governing use of this software, this software is licensed to you
  10317. +* under the terms of the GNU General Public License version 2, available at
  10318. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  10319. +*
  10320. +* Notwithstanding the above, under no circumstances may you combine this
  10321. +* software in any way with any other Broadcom software provided under a
  10322. +* license other than the GPL, without Broadcom's express prior written
  10323. +* consent.
  10324. +*****************************************************************************/
  10325. +
  10326. +/*****************************************************************************
  10327. +* FILENAME: bcm2835-cpufreq.h
  10328. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  10329. +* processor. Messages are sent to Videocore either setting or requesting the
  10330. +* frequency of the ARM in order to match an appropiate frequency to the current
  10331. +* usage of the processor. The policy which selects the frequency to use is
  10332. +* defined in the kernel .config file, but can be changed during runtime.
  10333. +*****************************************************************************/
  10334. +
  10335. +/* ---------- INCLUDES ---------- */
  10336. +#include <linux/kernel.h>
  10337. +#include <linux/init.h>
  10338. +#include <linux/module.h>
  10339. +#include <linux/cpufreq.h>
  10340. +#include <mach/vcio.h>
  10341. +
  10342. +/* ---------- DEFINES ---------- */
  10343. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  10344. +#define MODULE_NAME "bcm2835-cpufreq"
  10345. +
  10346. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  10347. +
  10348. +/* debug printk macros */
  10349. +#ifdef CPUFREQ_DEBUG_ENABLE
  10350. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  10351. +#else
  10352. +#define print_debug(fmt,...)
  10353. +#endif
  10354. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  10355. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  10356. +
  10357. +/* tag part of the message */
  10358. +struct vc_msg_tag {
  10359. + uint32_t tag_id; /* the message id */
  10360. + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
  10361. + uint32_t data_size; /* amount of data being sent or received */
  10362. + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
  10363. + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
  10364. +};
  10365. +
  10366. +/* message structure to be sent to videocore */
  10367. +struct vc_msg {
  10368. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  10369. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  10370. + struct vc_msg_tag tag; /* the tag structure above to make */
  10371. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  10372. +};
  10373. +
  10374. +/* ---------- GLOBALS ---------- */
  10375. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  10376. +
  10377. +/*
  10378. + ===============================================
  10379. + clk_rate either gets or sets the clock rates.
  10380. + ===============================================
  10381. +*/
  10382. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  10383. +{
  10384. + int s, actual_rate=0;
  10385. + struct vc_msg msg;
  10386. +
  10387. + /* wipe all previous message data */
  10388. + memset(&msg, 0, sizeof msg);
  10389. +
  10390. + msg.msg_size = sizeof msg;
  10391. +
  10392. + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
  10393. + msg.tag.buffer_size = 8;
  10394. + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
  10395. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  10396. + msg.tag.val = arm_rate * 1000;
  10397. +
  10398. + /* send the message */
  10399. + s = bcm_mailbox_property(&msg, sizeof msg);
  10400. +
  10401. + /* check if it was all ok and return the rate in KHz */
  10402. + if (s == 0 && (msg.request_code & 0x80000000))
  10403. + actual_rate = msg.tag.val/1000;
  10404. +
  10405. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
  10406. + return actual_rate;
  10407. +}
  10408. +
  10409. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  10410. +{
  10411. + int s;
  10412. + int arm_rate = 0;
  10413. + struct vc_msg msg;
  10414. +
  10415. + /* wipe all previous message data */
  10416. + memset(&msg, 0, sizeof msg);
  10417. +
  10418. + msg.msg_size = sizeof msg;
  10419. + msg.tag.tag_id = tag;
  10420. + msg.tag.buffer_size = 8;
  10421. + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
  10422. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  10423. +
  10424. + /* send the message */
  10425. + s = bcm_mailbox_property(&msg, sizeof msg);
  10426. +
  10427. + /* check if it was all ok and return the rate in KHz */
  10428. + if (s == 0 && (msg.request_code & 0x80000000))
  10429. + arm_rate = msg.tag.val/1000;
  10430. +
  10431. + print_debug("%s frequency = %d\n",
  10432. + tag == VCMSG_GET_CLOCK_RATE ? "Current":
  10433. + tag == VCMSG_GET_MIN_CLOCK ? "Min":
  10434. + tag == VCMSG_GET_MAX_CLOCK ? "Max":
  10435. + "Unexpected", arm_rate);
  10436. +
  10437. + return arm_rate;
  10438. +}
  10439. +
  10440. +/*
  10441. + ====================================================
  10442. + Module Initialisation registers the cpufreq driver
  10443. + ====================================================
  10444. +*/
  10445. +static int __init bcm2835_cpufreq_module_init(void)
  10446. +{
  10447. + print_debug("IN\n");
  10448. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  10449. +}
  10450. +
  10451. +/*
  10452. + =============
  10453. + Module exit
  10454. + =============
  10455. +*/
  10456. +static void __exit bcm2835_cpufreq_module_exit(void)
  10457. +{
  10458. + print_debug("IN\n");
  10459. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  10460. + return;
  10461. +}
  10462. +
  10463. +/*
  10464. + ==============================================================
  10465. + Initialisation function sets up the CPU policy for first use
  10466. + ==============================================================
  10467. +*/
  10468. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  10469. +{
  10470. + /* measured value of how long it takes to change frequency */
  10471. + policy->cpuinfo.transition_latency = 355000; /* ns */
  10472. +
  10473. + /* now find out what the maximum and minimum frequencies are */
  10474. + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
  10475. + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
  10476. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  10477. +
  10478. + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
  10479. + return 0;
  10480. +}
  10481. +
  10482. +/*
  10483. + =================================================================================
  10484. + Target function chooses the most appropriate frequency from the table to enable
  10485. + =================================================================================
  10486. +*/
  10487. +
  10488. +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
  10489. +{
  10490. + unsigned int target = target_freq;
  10491. +#ifdef CPUFREQ_DEBUG_ENABLE
  10492. + unsigned int cur = policy->cur;
  10493. +#endif
  10494. + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
  10495. +
  10496. + /* if we are above min and using ondemand, then just use max */
  10497. + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
  10498. + target = policy->max;
  10499. + /* if the frequency is the same, just quit */
  10500. + if (target == policy->cur)
  10501. + return 0;
  10502. +
  10503. + /* otherwise were good to set the clock frequency */
  10504. + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
  10505. +
  10506. + if (!policy->cur)
  10507. + {
  10508. + print_err("Error occurred setting a new frequency (%d)!\n", target);
  10509. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  10510. + return -EINVAL;
  10511. + }
  10512. + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
  10513. + return 0;
  10514. +}
  10515. +
  10516. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  10517. +{
  10518. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  10519. + print_debug("cpu=%d\n", actual_rate);
  10520. + return actual_rate;
  10521. +}
  10522. +
  10523. +/*
  10524. + =================================================================================
  10525. + Verify ensures that when a policy is changed, it is suitable for the CPU to use
  10526. + =================================================================================
  10527. +*/
  10528. +
  10529. +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
  10530. +{
  10531. + print_info("switching to governor %s\n", policy->governor->name);
  10532. + return 0;
  10533. +}
  10534. +
  10535. +
  10536. +/* the CPUFreq driver */
  10537. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  10538. + .name = "BCM2835 CPUFreq",
  10539. + .init = bcm2835_cpufreq_driver_init,
  10540. + .verify = bcm2835_cpufreq_driver_verify,
  10541. + .target = bcm2835_cpufreq_driver_target,
  10542. + .get = bcm2835_cpufreq_driver_get
  10543. +};
  10544. +
  10545. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  10546. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  10547. +MODULE_LICENSE("GPL");
  10548. +
  10549. +module_init(bcm2835_cpufreq_module_init);
  10550. +module_exit(bcm2835_cpufreq_module_exit);
  10551. diff -Nur linux-3.12.13/drivers/cpufreq/Kconfig.arm linux-raspberry-pi/drivers/cpufreq/Kconfig.arm
  10552. --- linux-3.12.13/drivers/cpufreq/Kconfig.arm 2014-02-22 22:32:50.000000000 +0100
  10553. +++ linux-raspberry-pi/drivers/cpufreq/Kconfig.arm 2014-03-11 17:51:13.000000000 +0100
  10554. @@ -228,6 +228,14 @@
  10555. help
  10556. This adds the CPUFreq driver support for SPEAr SOCs.
  10557. +config ARM_BCM2835_CPUFREQ
  10558. + bool "BCM2835 Driver"
  10559. + default y
  10560. + help
  10561. + This adds the CPUFreq driver for BCM2835
  10562. +
  10563. + If in doubt, say N.
  10564. +
  10565. config ARM_TEGRA_CPUFREQ
  10566. bool "TEGRA CPUFreq support"
  10567. depends on ARCH_TEGRA
  10568. diff -Nur linux-3.12.13/drivers/cpufreq/Makefile linux-raspberry-pi/drivers/cpufreq/Makefile
  10569. --- linux-3.12.13/drivers/cpufreq/Makefile 2014-02-22 22:32:50.000000000 +0100
  10570. +++ linux-raspberry-pi/drivers/cpufreq/Makefile 2014-03-11 17:51:13.000000000 +0100
  10571. @@ -76,6 +76,7 @@
  10572. obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
  10573. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  10574. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  10575. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  10576. obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
  10577. ##################################################################################
  10578. diff -Nur linux-3.12.13/drivers/dma/bcm2708-dmaengine.c linux-raspberry-pi/drivers/dma/bcm2708-dmaengine.c
  10579. --- linux-3.12.13/drivers/dma/bcm2708-dmaengine.c 1970-01-01 01:00:00.000000000 +0100
  10580. +++ linux-raspberry-pi/drivers/dma/bcm2708-dmaengine.c 2014-03-11 17:51:13.000000000 +0100
  10581. @@ -0,0 +1,588 @@
  10582. +/*
  10583. + * BCM2708 DMA engine support
  10584. + *
  10585. + * This driver only supports cyclic DMA transfers
  10586. + * as needed for the I2S module.
  10587. + *
  10588. + * Author: Florian Meier <florian.meier@koalo.de>
  10589. + * Copyright 2013
  10590. + *
  10591. + * Based on
  10592. + * OMAP DMAengine support by Russell King
  10593. + *
  10594. + * BCM2708 DMA Driver
  10595. + * Copyright (C) 2010 Broadcom
  10596. + *
  10597. + * Raspberry Pi PCM I2S ALSA Driver
  10598. + * Copyright (c) by Phil Poole 2013
  10599. + *
  10600. + * MARVELL MMP Peripheral DMA Driver
  10601. + * Copyright 2012 Marvell International Ltd.
  10602. + *
  10603. + * This program is free software; you can redistribute it and/or modify
  10604. + * it under the terms of the GNU General Public License as published by
  10605. + * the Free Software Foundation; either version 2 of the License, or
  10606. + * (at your option) any later version.
  10607. + *
  10608. + * This program is distributed in the hope that it will be useful,
  10609. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10610. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10611. + * GNU General Public License for more details.
  10612. + */
  10613. +#include <linux/dmaengine.h>
  10614. +#include <linux/dma-mapping.h>
  10615. +#include <linux/err.h>
  10616. +#include <linux/init.h>
  10617. +#include <linux/interrupt.h>
  10618. +#include <linux/list.h>
  10619. +#include <linux/module.h>
  10620. +#include <linux/platform_device.h>
  10621. +#include <linux/slab.h>
  10622. +#include <linux/io.h>
  10623. +#include <linux/spinlock.h>
  10624. +#include <linux/irq.h>
  10625. +
  10626. +#include "virt-dma.h"
  10627. +
  10628. +#include <mach/dma.h>
  10629. +#include <mach/irqs.h>
  10630. +
  10631. +struct bcm2708_dmadev {
  10632. + struct dma_device ddev;
  10633. + spinlock_t lock;
  10634. + void __iomem *base;
  10635. + struct device_dma_parameters dma_parms;
  10636. +};
  10637. +
  10638. +struct bcm2708_chan {
  10639. + struct virt_dma_chan vc;
  10640. + struct list_head node;
  10641. +
  10642. + struct dma_slave_config cfg;
  10643. + bool cyclic;
  10644. +
  10645. + int ch;
  10646. + struct bcm2708_desc *desc;
  10647. +
  10648. + void __iomem *chan_base;
  10649. + int irq_number;
  10650. +};
  10651. +
  10652. +struct bcm2708_desc {
  10653. + struct virt_dma_desc vd;
  10654. + enum dma_transfer_direction dir;
  10655. +
  10656. + unsigned int control_block_size;
  10657. + struct bcm2708_dma_cb *control_block_base;
  10658. + dma_addr_t control_block_base_phys;
  10659. +
  10660. + unsigned frames;
  10661. + size_t size;
  10662. +};
  10663. +
  10664. +#define BCM2708_DMA_DATA_TYPE_S8 1
  10665. +#define BCM2708_DMA_DATA_TYPE_S16 2
  10666. +#define BCM2708_DMA_DATA_TYPE_S32 4
  10667. +#define BCM2708_DMA_DATA_TYPE_S128 16
  10668. +
  10669. +static inline struct bcm2708_dmadev *to_bcm2708_dma_dev(struct dma_device *d)
  10670. +{
  10671. + return container_of(d, struct bcm2708_dmadev, ddev);
  10672. +}
  10673. +
  10674. +static inline struct bcm2708_chan *to_bcm2708_dma_chan(struct dma_chan *c)
  10675. +{
  10676. + return container_of(c, struct bcm2708_chan, vc.chan);
  10677. +}
  10678. +
  10679. +static inline struct bcm2708_desc *to_bcm2708_dma_desc(
  10680. + struct dma_async_tx_descriptor *t)
  10681. +{
  10682. + return container_of(t, struct bcm2708_desc, vd.tx);
  10683. +}
  10684. +
  10685. +static void bcm2708_dma_desc_free(struct virt_dma_desc *vd)
  10686. +{
  10687. + struct bcm2708_desc *desc = container_of(vd, struct bcm2708_desc, vd);
  10688. + dma_free_coherent(desc->vd.tx.chan->device->dev,
  10689. + desc->control_block_size,
  10690. + desc->control_block_base,
  10691. + desc->control_block_base_phys);
  10692. + kfree(desc);
  10693. +}
  10694. +
  10695. +static void bcm2708_dma_start_desc(struct bcm2708_chan *c)
  10696. +{
  10697. + struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  10698. + struct bcm2708_desc *d;
  10699. +
  10700. + if (!vd) {
  10701. + c->desc = NULL;
  10702. + return;
  10703. + }
  10704. +
  10705. + list_del(&vd->node);
  10706. +
  10707. + c->desc = d = to_bcm2708_dma_desc(&vd->tx);
  10708. +
  10709. + bcm_dma_start(c->chan_base, d->control_block_base_phys);
  10710. +}
  10711. +
  10712. +static irqreturn_t bcm2708_dma_callback(int irq, void *data)
  10713. +{
  10714. + struct bcm2708_chan *c = data;
  10715. + struct bcm2708_desc *d;
  10716. + unsigned long flags;
  10717. +
  10718. + spin_lock_irqsave(&c->vc.lock, flags);
  10719. +
  10720. + /* Acknowledge interrupt */
  10721. + writel(BCM2708_DMA_INT, c->chan_base + BCM2708_DMA_CS);
  10722. +
  10723. + d = c->desc;
  10724. +
  10725. + if (d) {
  10726. + /* TODO Only works for cyclic DMA */
  10727. + vchan_cyclic_callback(&d->vd);
  10728. + }
  10729. +
  10730. + /* Keep the DMA engine running */
  10731. + dsb(); /* ARM synchronization barrier */
  10732. + writel(BCM2708_DMA_ACTIVE, c->chan_base + BCM2708_DMA_CS);
  10733. +
  10734. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10735. +
  10736. + return IRQ_HANDLED;
  10737. +}
  10738. +
  10739. +static int bcm2708_dma_alloc_chan_resources(struct dma_chan *chan)
  10740. +{
  10741. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10742. +
  10743. + return request_irq(c->irq_number,
  10744. + bcm2708_dma_callback, 0, "DMA IRQ", c);
  10745. +}
  10746. +
  10747. +static void bcm2708_dma_free_chan_resources(struct dma_chan *chan)
  10748. +{
  10749. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10750. +
  10751. + vchan_free_chan_resources(&c->vc);
  10752. + free_irq(c->irq_number, c);
  10753. +
  10754. + dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  10755. +}
  10756. +
  10757. +static size_t bcm2708_dma_desc_size(struct bcm2708_desc *d)
  10758. +{
  10759. + return d->size;
  10760. +}
  10761. +
  10762. +static size_t bcm2708_dma_desc_size_pos(struct bcm2708_desc *d, dma_addr_t addr)
  10763. +{
  10764. + unsigned i;
  10765. + size_t size;
  10766. +
  10767. + for (size = i = 0; i < d->frames; i++) {
  10768. + struct bcm2708_dma_cb *control_block =
  10769. + &d->control_block_base[i];
  10770. + size_t this_size = control_block->length;
  10771. + dma_addr_t dma;
  10772. +
  10773. + if (d->dir == DMA_DEV_TO_MEM)
  10774. + dma = control_block->dst;
  10775. + else
  10776. + dma = control_block->src;
  10777. +
  10778. + if (size)
  10779. + size += this_size;
  10780. + else if (addr >= dma && addr < dma + this_size)
  10781. + size += dma + this_size - addr;
  10782. + }
  10783. +
  10784. + return size;
  10785. +}
  10786. +
  10787. +static enum dma_status bcm2708_dma_tx_status(struct dma_chan *chan,
  10788. + dma_cookie_t cookie, struct dma_tx_state *txstate)
  10789. +{
  10790. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10791. + struct virt_dma_desc *vd;
  10792. + enum dma_status ret;
  10793. + unsigned long flags;
  10794. +
  10795. + ret = dma_cookie_status(chan, cookie, txstate);
  10796. + if (ret == DMA_SUCCESS || !txstate)
  10797. + return ret;
  10798. +
  10799. + spin_lock_irqsave(&c->vc.lock, flags);
  10800. + vd = vchan_find_desc(&c->vc, cookie);
  10801. + if (vd) {
  10802. + txstate->residue =
  10803. + bcm2708_dma_desc_size(to_bcm2708_dma_desc(&vd->tx));
  10804. + } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  10805. + struct bcm2708_desc *d = c->desc;
  10806. + dma_addr_t pos;
  10807. +
  10808. + if (d->dir == DMA_MEM_TO_DEV)
  10809. + pos = readl(c->chan_base + BCM2708_DMA_SOURCE_AD);
  10810. + else if (d->dir == DMA_DEV_TO_MEM)
  10811. + pos = readl(c->chan_base + BCM2708_DMA_DEST_AD);
  10812. + else
  10813. + pos = 0;
  10814. +
  10815. + txstate->residue = bcm2708_dma_desc_size_pos(d, pos);
  10816. + } else {
  10817. + txstate->residue = 0;
  10818. + }
  10819. +
  10820. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10821. +
  10822. + return ret;
  10823. +}
  10824. +
  10825. +static void bcm2708_dma_issue_pending(struct dma_chan *chan)
  10826. +{
  10827. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10828. + unsigned long flags;
  10829. +
  10830. + c->cyclic = true; /* Nothing else is implemented */
  10831. +
  10832. + spin_lock_irqsave(&c->vc.lock, flags);
  10833. + if (vchan_issue_pending(&c->vc) && !c->desc)
  10834. + bcm2708_dma_start_desc(c);
  10835. +
  10836. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10837. +}
  10838. +
  10839. +static struct dma_async_tx_descriptor *bcm2708_dma_prep_dma_cyclic(
  10840. + struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  10841. + size_t period_len, enum dma_transfer_direction direction,
  10842. + unsigned long flags, void *context)
  10843. +{
  10844. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10845. + enum dma_slave_buswidth dev_width;
  10846. + struct bcm2708_desc *d;
  10847. + dma_addr_t dev_addr;
  10848. + unsigned es, sync_type;
  10849. + unsigned frame;
  10850. +
  10851. + /* Grab configuration */
  10852. + if (direction == DMA_DEV_TO_MEM) {
  10853. + dev_addr = c->cfg.src_addr;
  10854. + dev_width = c->cfg.src_addr_width;
  10855. + sync_type = BCM2708_DMA_S_DREQ;
  10856. + } else if (direction == DMA_MEM_TO_DEV) {
  10857. + dev_addr = c->cfg.dst_addr;
  10858. + dev_width = c->cfg.dst_addr_width;
  10859. + sync_type = BCM2708_DMA_D_DREQ;
  10860. + } else {
  10861. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  10862. + return NULL;
  10863. + }
  10864. +
  10865. + /* Bus width translates to the element size (ES) */
  10866. + switch (dev_width) {
  10867. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  10868. + es = BCM2708_DMA_DATA_TYPE_S32;
  10869. + break;
  10870. + default:
  10871. + return NULL;
  10872. + }
  10873. +
  10874. + /* Now allocate and setup the descriptor. */
  10875. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  10876. + if (!d)
  10877. + return NULL;
  10878. +
  10879. + d->dir = direction;
  10880. + d->frames = buf_len / period_len;
  10881. +
  10882. + /* Allocate memory for control blocks */
  10883. + d->control_block_size = d->frames * sizeof(struct bcm2708_dma_cb);
  10884. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  10885. + d->control_block_size, &d->control_block_base_phys,
  10886. + GFP_NOWAIT);
  10887. +
  10888. + if (!d->control_block_base) {
  10889. + kfree(d);
  10890. + return NULL;
  10891. + }
  10892. +
  10893. + /*
  10894. + * Iterate over all frames, create a control block
  10895. + * for each frame and link them together.
  10896. + */
  10897. + for (frame = 0; frame < d->frames; frame++) {
  10898. + struct bcm2708_dma_cb *control_block =
  10899. + &d->control_block_base[frame];
  10900. +
  10901. + /* Setup adresses */
  10902. + if (d->dir == DMA_DEV_TO_MEM) {
  10903. + control_block->info = BCM2708_DMA_D_INC;
  10904. + control_block->src = dev_addr;
  10905. + control_block->dst = buf_addr + frame * period_len;
  10906. + } else {
  10907. + control_block->info = BCM2708_DMA_S_INC;
  10908. + control_block->src = buf_addr + frame * period_len;
  10909. + control_block->dst = dev_addr;
  10910. + }
  10911. +
  10912. + /* Enable interrupt */
  10913. + control_block->info |= BCM2708_DMA_INT_EN;
  10914. +
  10915. + /* Setup synchronization */
  10916. + if (sync_type != 0)
  10917. + control_block->info |= sync_type;
  10918. +
  10919. + /* Setup DREQ channel */
  10920. + if (c->cfg.slave_id != 0)
  10921. + control_block->info |=
  10922. + BCM2708_DMA_PER_MAP(c->cfg.slave_id);
  10923. +
  10924. + /* Length of a frame */
  10925. + control_block->length = period_len;
  10926. + d->size += control_block->length;
  10927. +
  10928. + /*
  10929. + * Next block is the next frame.
  10930. + * This DMA engine driver currently only supports cyclic DMA.
  10931. + * Therefore, wrap around at number of frames.
  10932. + */
  10933. + control_block->next = d->control_block_base_phys +
  10934. + sizeof(struct bcm2708_dma_cb)
  10935. + * ((frame + 1) % d->frames);
  10936. + }
  10937. +
  10938. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  10939. +}
  10940. +
  10941. +static int bcm2708_dma_slave_config(struct bcm2708_chan *c,
  10942. + struct dma_slave_config *cfg)
  10943. +{
  10944. + if ((cfg->direction == DMA_DEV_TO_MEM &&
  10945. + cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  10946. + (cfg->direction == DMA_MEM_TO_DEV &&
  10947. + cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  10948. + !is_slave_direction(cfg->direction)) {
  10949. + return -EINVAL;
  10950. + }
  10951. +
  10952. + c->cfg = *cfg;
  10953. +
  10954. + return 0;
  10955. +}
  10956. +
  10957. +static int bcm2708_dma_terminate_all(struct bcm2708_chan *c)
  10958. +{
  10959. + struct bcm2708_dmadev *d = to_bcm2708_dma_dev(c->vc.chan.device);
  10960. + unsigned long flags;
  10961. + int timeout = 10000;
  10962. + LIST_HEAD(head);
  10963. +
  10964. + spin_lock_irqsave(&c->vc.lock, flags);
  10965. +
  10966. + /* Prevent this channel being scheduled */
  10967. + spin_lock(&d->lock);
  10968. + list_del_init(&c->node);
  10969. + spin_unlock(&d->lock);
  10970. +
  10971. + /*
  10972. + * Stop DMA activity: we assume the callback will not be called
  10973. + * after bcm_dma_abort() returns (even if it does, it will see
  10974. + * c->desc is NULL and exit.)
  10975. + */
  10976. + if (c->desc) {
  10977. + c->desc = NULL;
  10978. + bcm_dma_abort(c->chan_base);
  10979. +
  10980. + /* Wait for stopping */
  10981. + while (timeout > 0) {
  10982. + timeout--;
  10983. + if (!(readl(c->chan_base + BCM2708_DMA_CS) &
  10984. + BCM2708_DMA_ACTIVE))
  10985. + break;
  10986. +
  10987. + cpu_relax();
  10988. + }
  10989. +
  10990. + if (timeout <= 0)
  10991. + dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  10992. + }
  10993. +
  10994. + vchan_get_all_descriptors(&c->vc, &head);
  10995. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10996. + vchan_dma_desc_free_list(&c->vc, &head);
  10997. +
  10998. + return 0;
  10999. +}
  11000. +
  11001. +static int bcm2708_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  11002. + unsigned long arg)
  11003. +{
  11004. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  11005. +
  11006. + switch (cmd) {
  11007. + case DMA_SLAVE_CONFIG:
  11008. + return bcm2708_dma_slave_config(c,
  11009. + (struct dma_slave_config *)arg);
  11010. +
  11011. + case DMA_TERMINATE_ALL:
  11012. + return bcm2708_dma_terminate_all(c);
  11013. +
  11014. + default:
  11015. + return -ENXIO;
  11016. + }
  11017. +}
  11018. +
  11019. +static int bcm2708_dma_chan_init(struct bcm2708_dmadev *d, void __iomem* chan_base,
  11020. + int chan_id, int irq)
  11021. +{
  11022. + struct bcm2708_chan *c;
  11023. +
  11024. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  11025. + if (!c)
  11026. + return -ENOMEM;
  11027. +
  11028. + c->vc.desc_free = bcm2708_dma_desc_free;
  11029. + vchan_init(&c->vc, &d->ddev);
  11030. + INIT_LIST_HEAD(&c->node);
  11031. +
  11032. + d->ddev.chancnt++;
  11033. +
  11034. + c->chan_base = chan_base;
  11035. + c->ch = chan_id;
  11036. + c->irq_number = irq;
  11037. +
  11038. + return 0;
  11039. +}
  11040. +
  11041. +static void bcm2708_dma_free(struct bcm2708_dmadev *od)
  11042. +{
  11043. + while (!list_empty(&od->ddev.channels)) {
  11044. + struct bcm2708_chan *c = list_first_entry(&od->ddev.channels,
  11045. + struct bcm2708_chan, vc.chan.device_node);
  11046. +
  11047. + list_del(&c->vc.chan.device_node);
  11048. + tasklet_kill(&c->vc.task);
  11049. + }
  11050. +}
  11051. +
  11052. +static int bcm2708_dma_probe(struct platform_device *pdev)
  11053. +{
  11054. + struct bcm2708_dmadev *od;
  11055. + int rc, i;
  11056. +
  11057. + if (!pdev->dev.dma_mask)
  11058. + pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  11059. +
  11060. + rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  11061. + if (rc)
  11062. + return rc;
  11063. + dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  11064. +
  11065. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  11066. + if (!od)
  11067. + return -ENOMEM;
  11068. +
  11069. + pdev->dev.dma_parms = &od->dma_parms;
  11070. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  11071. +
  11072. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  11073. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  11074. + od->ddev.device_alloc_chan_resources = bcm2708_dma_alloc_chan_resources;
  11075. + od->ddev.device_free_chan_resources = bcm2708_dma_free_chan_resources;
  11076. + od->ddev.device_tx_status = bcm2708_dma_tx_status;
  11077. + od->ddev.device_issue_pending = bcm2708_dma_issue_pending;
  11078. + od->ddev.device_prep_dma_cyclic = bcm2708_dma_prep_dma_cyclic;
  11079. + od->ddev.device_control = bcm2708_dma_control;
  11080. + od->ddev.dev = &pdev->dev;
  11081. + INIT_LIST_HEAD(&od->ddev.channels);
  11082. + spin_lock_init(&od->lock);
  11083. +
  11084. + platform_set_drvdata(pdev, od);
  11085. +
  11086. + for (i = 0; i < 16; i++) {
  11087. + void __iomem* chan_base;
  11088. + int chan_id, irq;
  11089. +
  11090. + chan_id = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  11091. + &chan_base,
  11092. + &irq);
  11093. +
  11094. + if (chan_id < 0)
  11095. + break;
  11096. +
  11097. + rc = bcm2708_dma_chan_init(od, chan_base, chan_id, irq);
  11098. + if (rc) {
  11099. + bcm2708_dma_free(od);
  11100. + return rc;
  11101. + }
  11102. + }
  11103. +
  11104. + rc = dma_async_device_register(&od->ddev);
  11105. + if (rc) {
  11106. + dev_err(&pdev->dev,
  11107. + "Failed to register slave DMA engine device: %d\n", rc);
  11108. + bcm2708_dma_free(od);
  11109. + return rc;
  11110. + }
  11111. +
  11112. + dev_dbg(&pdev->dev, "Load BCM2708 DMA engine driver\n");
  11113. +
  11114. + return rc;
  11115. +}
  11116. +
  11117. +static int bcm2708_dma_remove(struct platform_device *pdev)
  11118. +{
  11119. + struct bcm2708_dmadev *od = platform_get_drvdata(pdev);
  11120. +
  11121. + dma_async_device_unregister(&od->ddev);
  11122. + bcm2708_dma_free(od);
  11123. +
  11124. + return 0;
  11125. +}
  11126. +
  11127. +static struct platform_driver bcm2708_dma_driver = {
  11128. + .probe = bcm2708_dma_probe,
  11129. + .remove = bcm2708_dma_remove,
  11130. + .driver = {
  11131. + .name = "bcm2708-dmaengine",
  11132. + .owner = THIS_MODULE,
  11133. + },
  11134. +};
  11135. +
  11136. +static struct platform_device *pdev;
  11137. +
  11138. +static const struct platform_device_info bcm2708_dma_dev_info = {
  11139. + .name = "bcm2708-dmaengine",
  11140. + .id = -1,
  11141. +};
  11142. +
  11143. +static int bcm2708_dma_init(void)
  11144. +{
  11145. + int rc = platform_driver_register(&bcm2708_dma_driver);
  11146. +
  11147. + if (rc == 0) {
  11148. + pdev = platform_device_register_full(&bcm2708_dma_dev_info);
  11149. + if (IS_ERR(pdev)) {
  11150. + platform_driver_unregister(&bcm2708_dma_driver);
  11151. + rc = PTR_ERR(pdev);
  11152. + }
  11153. + }
  11154. +
  11155. + return rc;
  11156. +}
  11157. +subsys_initcall(bcm2708_dma_init);
  11158. +
  11159. +static void __exit bcm2708_dma_exit(void)
  11160. +{
  11161. + platform_device_unregister(pdev);
  11162. + platform_driver_unregister(&bcm2708_dma_driver);
  11163. +}
  11164. +module_exit(bcm2708_dma_exit);
  11165. +
  11166. +MODULE_ALIAS("platform:bcm2708-dma");
  11167. +MODULE_DESCRIPTION("BCM2708 DMA engine driver");
  11168. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  11169. +MODULE_LICENSE("GPL v2");
  11170. diff -Nur linux-3.12.13/drivers/dma/Kconfig linux-raspberry-pi/drivers/dma/Kconfig
  11171. --- linux-3.12.13/drivers/dma/Kconfig 2014-02-22 22:32:50.000000000 +0100
  11172. +++ linux-raspberry-pi/drivers/dma/Kconfig 2014-03-11 17:51:13.000000000 +0100
  11173. @@ -296,6 +296,12 @@
  11174. The Communications Port Programming Interface (CPPI) 4.1 DMA engine
  11175. is currently used by the USB driver on AM335x platforms.
  11176. +config DMA_BCM2708
  11177. + tristate "BCM2708 DMA engine support"
  11178. + depends on MACH_BCM2708
  11179. + select DMA_ENGINE
  11180. + select DMA_VIRTUAL_CHANNELS
  11181. +
  11182. config MMP_PDMA
  11183. bool "MMP PDMA support"
  11184. depends on (ARCH_MMP || ARCH_PXA)
  11185. diff -Nur linux-3.12.13/drivers/dma/Makefile linux-raspberry-pi/drivers/dma/Makefile
  11186. --- linux-3.12.13/drivers/dma/Makefile 2014-02-22 22:32:50.000000000 +0100
  11187. +++ linux-raspberry-pi/drivers/dma/Makefile 2014-03-11 17:51:13.000000000 +0100
  11188. @@ -37,6 +37,7 @@
  11189. obj-$(CONFIG_DMA_SA11X0) += sa11x0-dma.o
  11190. obj-$(CONFIG_MMP_TDMA) += mmp_tdma.o
  11191. obj-$(CONFIG_DMA_OMAP) += omap-dma.o
  11192. +obj-$(CONFIG_DMA_BCM2708) += bcm2708-dmaengine.o
  11193. obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o
  11194. obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
  11195. obj-$(CONFIG_TI_CPPI41) += cppi41.o
  11196. diff -Nur linux-3.12.13/drivers/hwmon/bcm2835-hwmon.c linux-raspberry-pi/drivers/hwmon/bcm2835-hwmon.c
  11197. --- linux-3.12.13/drivers/hwmon/bcm2835-hwmon.c 1970-01-01 01:00:00.000000000 +0100
  11198. +++ linux-raspberry-pi/drivers/hwmon/bcm2835-hwmon.c 2014-03-11 17:32:22.000000000 +0100
  11199. @@ -0,0 +1,219 @@
  11200. +/*****************************************************************************
  11201. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  11202. +*
  11203. +* Unless you and Broadcom execute a separate written software license
  11204. +* agreement governing use of this software, this software is licensed to you
  11205. +* under the terms of the GNU General Public License version 2, available at
  11206. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  11207. +*
  11208. +* Notwithstanding the above, under no circumstances may you combine this
  11209. +* software in any way with any other Broadcom software provided under a
  11210. +* license other than the GPL, without Broadcom's express prior written
  11211. +* consent.
  11212. +*****************************************************************************/
  11213. +
  11214. +#include <linux/kernel.h>
  11215. +#include <linux/module.h>
  11216. +#include <linux/init.h>
  11217. +#include <linux/hwmon.h>
  11218. +#include <linux/hwmon-sysfs.h>
  11219. +#include <linux/platform_device.h>
  11220. +#include <linux/sysfs.h>
  11221. +#include <mach/vcio.h>
  11222. +#include <linux/slab.h>
  11223. +#include <linux/err.h>
  11224. +
  11225. +#define MODULE_NAME "bcm2835_hwmon"
  11226. +
  11227. +/*#define HWMON_DEBUG_ENABLE*/
  11228. +
  11229. +#ifdef HWMON_DEBUG_ENABLE
  11230. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  11231. +#else
  11232. +#define print_debug(fmt,...)
  11233. +#endif
  11234. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  11235. +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
  11236. +
  11237. +#define VC_TAG_GET_TEMP 0x00030006
  11238. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  11239. +
  11240. +/* --- STRUCTS --- */
  11241. +struct bcm2835_hwmon_data {
  11242. + struct device *hwmon_dev;
  11243. +};
  11244. +
  11245. +/* tag part of the message */
  11246. +struct vc_msg_tag {
  11247. + uint32_t tag_id; /* the tag ID for the temperature */
  11248. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  11249. + uint32_t request_code; /* identifies message as a request (should be 0) */
  11250. + uint32_t id; /* extra ID field (should be 0) */
  11251. + uint32_t val; /* returned value of the temperature */
  11252. +};
  11253. +
  11254. +/* message structure to be sent to videocore */
  11255. +struct vc_msg {
  11256. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  11257. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  11258. + struct vc_msg_tag tag; /* the tag structure above to make */
  11259. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  11260. +};
  11261. +
  11262. +typedef enum {
  11263. + TEMP,
  11264. + MAX_TEMP,
  11265. +} temp_type;
  11266. +
  11267. +/* --- PROTOTYPES --- */
  11268. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
  11269. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
  11270. +
  11271. +/* --- GLOBALS --- */
  11272. +
  11273. +static struct bcm2835_hwmon_data *bcm2835_data;
  11274. +static struct platform_driver bcm2835_hwmon_driver;
  11275. +
  11276. +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
  11277. +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
  11278. +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
  11279. +
  11280. +static struct attribute* bcm2835_attributes[] = {
  11281. + &sensor_dev_attr_name.dev_attr.attr,
  11282. + &sensor_dev_attr_temp1_input.dev_attr.attr,
  11283. + &sensor_dev_attr_temp1_max.dev_attr.attr,
  11284. + NULL,
  11285. +};
  11286. +
  11287. +static struct attribute_group bcm2835_attr_group = {
  11288. + .attrs = bcm2835_attributes,
  11289. +};
  11290. +
  11291. +/* --- FUNCTIONS --- */
  11292. +
  11293. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
  11294. +{
  11295. + return sprintf(buf,"bcm2835_hwmon\n");
  11296. +}
  11297. +
  11298. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
  11299. +{
  11300. + struct vc_msg msg;
  11301. + int result;
  11302. + uint temp = 0;
  11303. + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
  11304. +
  11305. + print_debug("IN");
  11306. +
  11307. + /* wipe all previous message data */
  11308. + memset(&msg, 0, sizeof msg);
  11309. +
  11310. + /* determine the message type */
  11311. + if(index == TEMP)
  11312. + msg.tag.tag_id = VC_TAG_GET_TEMP;
  11313. + else if (index == MAX_TEMP)
  11314. + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
  11315. + else
  11316. + {
  11317. + print_debug("Unknown temperature message!");
  11318. + return -EINVAL;
  11319. + }
  11320. +
  11321. + msg.msg_size = sizeof msg;
  11322. + msg.tag.buffer_size = 8;
  11323. +
  11324. + /* send the message */
  11325. + result = bcm_mailbox_property(&msg, sizeof msg);
  11326. +
  11327. + /* check if it was all ok and return the rate in milli degrees C */
  11328. + if (result == 0 && (msg.request_code & 0x80000000))
  11329. + temp = (uint)msg.tag.val;
  11330. + #ifdef HWMON_DEBUG_ENABLE
  11331. + else
  11332. + print_debug("Failed to get temperature!");
  11333. + #endif
  11334. + print_debug("Got temperature as %u",temp);
  11335. + print_debug("OUT");
  11336. + return sprintf(buf, "%u\n", temp);
  11337. +}
  11338. +
  11339. +
  11340. +static int bcm2835_hwmon_probe(struct platform_device *pdev)
  11341. +{
  11342. + int err;
  11343. +
  11344. + print_debug("IN");
  11345. + print_debug("HWMON Driver has been probed!");
  11346. +
  11347. + /* check that the device isn't null!*/
  11348. + if(pdev == NULL)
  11349. + {
  11350. + print_debug("Platform device is empty!");
  11351. + return -ENODEV;
  11352. + }
  11353. +
  11354. + /* allocate memory for neccessary data */
  11355. + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
  11356. + if(!bcm2835_data)
  11357. + {
  11358. + print_debug("Unable to allocate memory for hwmon data!");
  11359. + err = -ENOMEM;
  11360. + goto kzalloc_error;
  11361. + }
  11362. +
  11363. + /* create the sysfs files */
  11364. + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
  11365. + {
  11366. + print_debug("Unable to create sysfs files!");
  11367. + err = -EFAULT;
  11368. + goto sysfs_error;
  11369. + }
  11370. +
  11371. + /* register the hwmon device */
  11372. + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  11373. + if (IS_ERR(bcm2835_data->hwmon_dev))
  11374. + {
  11375. + err = PTR_ERR(bcm2835_data->hwmon_dev);
  11376. + goto hwmon_error;
  11377. + }
  11378. + print_debug("OUT");
  11379. + return 0;
  11380. +
  11381. + /* error goto's */
  11382. + hwmon_error:
  11383. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  11384. +
  11385. + sysfs_error:
  11386. + kfree(bcm2835_data);
  11387. +
  11388. + kzalloc_error:
  11389. +
  11390. + return err;
  11391. +
  11392. +}
  11393. +
  11394. +static int bcm2835_hwmon_remove(struct platform_device *pdev)
  11395. +{
  11396. + print_debug("IN");
  11397. + hwmon_device_unregister(bcm2835_data->hwmon_dev);
  11398. +
  11399. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  11400. + print_debug("OUT");
  11401. + return 0;
  11402. +}
  11403. +
  11404. +/* Hwmon Driver */
  11405. +static struct platform_driver bcm2835_hwmon_driver = {
  11406. + .probe = bcm2835_hwmon_probe,
  11407. + .remove = bcm2835_hwmon_remove,
  11408. + .driver = {
  11409. + .name = "bcm2835_hwmon",
  11410. + .owner = THIS_MODULE,
  11411. + },
  11412. +};
  11413. +
  11414. +MODULE_LICENSE("GPL");
  11415. +MODULE_AUTHOR("Dorian Peake");
  11416. +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
  11417. +
  11418. +module_platform_driver(bcm2835_hwmon_driver);
  11419. diff -Nur linux-3.12.13/drivers/hwmon/Kconfig linux-raspberry-pi/drivers/hwmon/Kconfig
  11420. --- linux-3.12.13/drivers/hwmon/Kconfig 2014-02-22 22:32:50.000000000 +0100
  11421. +++ linux-raspberry-pi/drivers/hwmon/Kconfig 2014-03-11 17:51:14.000000000 +0100
  11422. @@ -1553,6 +1553,16 @@
  11423. help
  11424. Support for the A/D converter on MC13783 and MC13892 PMIC.
  11425. +config SENSORS_BCM2835
  11426. + depends on THERMAL_BCM2835=n
  11427. + tristate "Broadcom BCM2835 HWMON Driver"
  11428. + help
  11429. + If you say yes here you get support for the hardware
  11430. + monitoring features of the BCM2835 Chip
  11431. +
  11432. + This driver can also be built as a module. If so, the module
  11433. + will be called bcm2835-hwmon.
  11434. +
  11435. if ACPI
  11436. comment "ACPI drivers"
  11437. diff -Nur linux-3.12.13/drivers/hwmon/Makefile linux-raspberry-pi/drivers/hwmon/Makefile
  11438. --- linux-3.12.13/drivers/hwmon/Makefile 2014-02-22 22:32:50.000000000 +0100
  11439. +++ linux-raspberry-pi/drivers/hwmon/Makefile 2014-03-11 17:51:14.000000000 +0100
  11440. @@ -142,6 +142,7 @@
  11441. obj-$(CONFIG_SENSORS_W83L786NG) += w83l786ng.o
  11442. obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
  11443. obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
  11444. +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
  11445. obj-$(CONFIG_PMBUS) += pmbus/
  11446. diff -Nur linux-3.12.13/drivers/i2c/busses/i2c-bcm2708.c linux-raspberry-pi/drivers/i2c/busses/i2c-bcm2708.c
  11447. --- linux-3.12.13/drivers/i2c/busses/i2c-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  11448. +++ linux-raspberry-pi/drivers/i2c/busses/i2c-bcm2708.c 2014-03-11 17:51:14.000000000 +0100
  11449. @@ -0,0 +1,408 @@
  11450. +/*
  11451. + * Driver for Broadcom BCM2708 BSC Controllers
  11452. + *
  11453. + * Copyright (C) 2012 Chris Boot & Frank Buss
  11454. + *
  11455. + * This driver is inspired by:
  11456. + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  11457. + *
  11458. + * This program is free software; you can redistribute it and/or modify
  11459. + * it under the terms of the GNU General Public License as published by
  11460. + * the Free Software Foundation; either version 2 of the License, or
  11461. + * (at your option) any later version.
  11462. + *
  11463. + * This program is distributed in the hope that it will be useful,
  11464. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11465. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11466. + * GNU General Public License for more details.
  11467. + *
  11468. + * You should have received a copy of the GNU General Public License
  11469. + * along with this program; if not, write to the Free Software
  11470. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  11471. + */
  11472. +
  11473. +#include <linux/kernel.h>
  11474. +#include <linux/module.h>
  11475. +#include <linux/spinlock.h>
  11476. +#include <linux/clk.h>
  11477. +#include <linux/err.h>
  11478. +#include <linux/platform_device.h>
  11479. +#include <linux/io.h>
  11480. +#include <linux/slab.h>
  11481. +#include <linux/i2c.h>
  11482. +#include <linux/interrupt.h>
  11483. +#include <linux/sched.h>
  11484. +#include <linux/wait.h>
  11485. +
  11486. +/* BSC register offsets */
  11487. +#define BSC_C 0x00
  11488. +#define BSC_S 0x04
  11489. +#define BSC_DLEN 0x08
  11490. +#define BSC_A 0x0c
  11491. +#define BSC_FIFO 0x10
  11492. +#define BSC_DIV 0x14
  11493. +#define BSC_DEL 0x18
  11494. +#define BSC_CLKT 0x1c
  11495. +
  11496. +/* Bitfields in BSC_C */
  11497. +#define BSC_C_I2CEN 0x00008000
  11498. +#define BSC_C_INTR 0x00000400
  11499. +#define BSC_C_INTT 0x00000200
  11500. +#define BSC_C_INTD 0x00000100
  11501. +#define BSC_C_ST 0x00000080
  11502. +#define BSC_C_CLEAR_1 0x00000020
  11503. +#define BSC_C_CLEAR_2 0x00000010
  11504. +#define BSC_C_READ 0x00000001
  11505. +
  11506. +/* Bitfields in BSC_S */
  11507. +#define BSC_S_CLKT 0x00000200
  11508. +#define BSC_S_ERR 0x00000100
  11509. +#define BSC_S_RXF 0x00000080
  11510. +#define BSC_S_TXE 0x00000040
  11511. +#define BSC_S_RXD 0x00000020
  11512. +#define BSC_S_TXD 0x00000010
  11513. +#define BSC_S_RXR 0x00000008
  11514. +#define BSC_S_TXW 0x00000004
  11515. +#define BSC_S_DONE 0x00000002
  11516. +#define BSC_S_TA 0x00000001
  11517. +
  11518. +#define I2C_TIMEOUT_MS 150
  11519. +
  11520. +#define DRV_NAME "bcm2708_i2c"
  11521. +
  11522. +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  11523. +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  11524. +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  11525. +
  11526. +
  11527. +struct bcm2708_i2c {
  11528. + struct i2c_adapter adapter;
  11529. +
  11530. + spinlock_t lock;
  11531. + void __iomem *base;
  11532. + int irq;
  11533. + struct clk *clk;
  11534. +
  11535. + struct completion done;
  11536. +
  11537. + struct i2c_msg *msg;
  11538. + int pos;
  11539. + int nmsgs;
  11540. + bool error;
  11541. +};
  11542. +
  11543. +/*
  11544. + * This function sets the ALT mode on the I2C pins so that we can use them with
  11545. + * the BSC hardware.
  11546. + *
  11547. + * FIXME: This is a hack. Use pinmux / pinctrl.
  11548. + */
  11549. +static void bcm2708_i2c_init_pinmode(int id)
  11550. +{
  11551. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  11552. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  11553. +
  11554. + int pin;
  11555. + u32 *gpio = ioremap(0x20200000, SZ_16K);
  11556. +
  11557. + BUG_ON(id != 0 && id != 1);
  11558. + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  11559. + for (pin = id*2+0; pin <= id*2+1; pin++) {
  11560. +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
  11561. + INP_GPIO(pin); /* set mode to GPIO input first */
  11562. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  11563. + }
  11564. +
  11565. + iounmap(gpio);
  11566. +
  11567. +#undef INP_GPIO
  11568. +#undef SET_GPIO_ALT
  11569. +}
  11570. +
  11571. +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  11572. +{
  11573. + return readl(bi->base + reg);
  11574. +}
  11575. +
  11576. +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  11577. +{
  11578. + writel(val, bi->base + reg);
  11579. +}
  11580. +
  11581. +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  11582. +{
  11583. + bcm2708_wr(bi, BSC_C, 0);
  11584. + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  11585. +}
  11586. +
  11587. +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  11588. +{
  11589. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  11590. + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  11591. +}
  11592. +
  11593. +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  11594. +{
  11595. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  11596. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  11597. +}
  11598. +
  11599. +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  11600. +{
  11601. + unsigned long bus_hz;
  11602. + u32 cdiv;
  11603. + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  11604. +
  11605. + bus_hz = clk_get_rate(bi->clk);
  11606. + cdiv = bus_hz / baudrate;
  11607. +
  11608. + if (bi->msg->flags & I2C_M_RD)
  11609. + c |= BSC_C_INTR | BSC_C_READ;
  11610. + else
  11611. + c |= BSC_C_INTT;
  11612. +
  11613. + bcm2708_wr(bi, BSC_DIV, cdiv);
  11614. + bcm2708_wr(bi, BSC_A, bi->msg->addr);
  11615. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  11616. + bcm2708_wr(bi, BSC_C, c);
  11617. +}
  11618. +
  11619. +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  11620. +{
  11621. + struct bcm2708_i2c *bi = dev_id;
  11622. + bool handled = true;
  11623. + u32 s;
  11624. +
  11625. + spin_lock(&bi->lock);
  11626. +
  11627. + /* we may see camera interrupts on the "other" I2C channel
  11628. + Just return if we've not sent anything */
  11629. + if (!bi->nmsgs || !bi->msg )
  11630. + goto early_exit;
  11631. +
  11632. + s = bcm2708_rd(bi, BSC_S);
  11633. +
  11634. + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  11635. + bcm2708_bsc_reset(bi);
  11636. + bi->error = true;
  11637. +
  11638. + /* wake up our bh */
  11639. + complete(&bi->done);
  11640. + } else if (s & BSC_S_DONE) {
  11641. + bi->nmsgs--;
  11642. +
  11643. + if (bi->msg->flags & I2C_M_RD)
  11644. + bcm2708_bsc_fifo_drain(bi);
  11645. +
  11646. + bcm2708_bsc_reset(bi);
  11647. +
  11648. + if (bi->nmsgs) {
  11649. + /* advance to next message */
  11650. + bi->msg++;
  11651. + bi->pos = 0;
  11652. + bcm2708_bsc_setup(bi);
  11653. + } else {
  11654. + /* wake up our bh */
  11655. + complete(&bi->done);
  11656. + }
  11657. + } else if (s & BSC_S_TXW) {
  11658. + bcm2708_bsc_fifo_fill(bi);
  11659. + } else if (s & BSC_S_RXR) {
  11660. + bcm2708_bsc_fifo_drain(bi);
  11661. + } else {
  11662. + handled = false;
  11663. + }
  11664. +
  11665. +early_exit:
  11666. + spin_unlock(&bi->lock);
  11667. +
  11668. + return handled ? IRQ_HANDLED : IRQ_NONE;
  11669. +}
  11670. +
  11671. +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  11672. + struct i2c_msg *msgs, int num)
  11673. +{
  11674. + struct bcm2708_i2c *bi = adap->algo_data;
  11675. + unsigned long flags;
  11676. + int ret;
  11677. +
  11678. + spin_lock_irqsave(&bi->lock, flags);
  11679. +
  11680. + INIT_COMPLETION(bi->done);
  11681. + bi->msg = msgs;
  11682. + bi->pos = 0;
  11683. + bi->nmsgs = num;
  11684. + bi->error = false;
  11685. +
  11686. + spin_unlock_irqrestore(&bi->lock, flags);
  11687. +
  11688. + bcm2708_bsc_setup(bi);
  11689. +
  11690. + ret = wait_for_completion_timeout(&bi->done,
  11691. + msecs_to_jiffies(I2C_TIMEOUT_MS));
  11692. + if (ret == 0) {
  11693. + dev_err(&adap->dev, "transfer timed out\n");
  11694. + spin_lock_irqsave(&bi->lock, flags);
  11695. + bcm2708_bsc_reset(bi);
  11696. + spin_unlock_irqrestore(&bi->lock, flags);
  11697. + return -ETIMEDOUT;
  11698. + }
  11699. +
  11700. + return bi->error ? -EIO : num;
  11701. +}
  11702. +
  11703. +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  11704. +{
  11705. + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  11706. +}
  11707. +
  11708. +static struct i2c_algorithm bcm2708_i2c_algorithm = {
  11709. + .master_xfer = bcm2708_i2c_master_xfer,
  11710. + .functionality = bcm2708_i2c_functionality,
  11711. +};
  11712. +
  11713. +static int bcm2708_i2c_probe(struct platform_device *pdev)
  11714. +{
  11715. + struct resource *regs;
  11716. + int irq, err = -ENOMEM;
  11717. + struct clk *clk;
  11718. + struct bcm2708_i2c *bi;
  11719. + struct i2c_adapter *adap;
  11720. +
  11721. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  11722. + if (!regs) {
  11723. + dev_err(&pdev->dev, "could not get IO memory\n");
  11724. + return -ENXIO;
  11725. + }
  11726. +
  11727. + irq = platform_get_irq(pdev, 0);
  11728. + if (irq < 0) {
  11729. + dev_err(&pdev->dev, "could not get IRQ\n");
  11730. + return irq;
  11731. + }
  11732. +
  11733. + clk = clk_get(&pdev->dev, NULL);
  11734. + if (IS_ERR(clk)) {
  11735. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  11736. + return PTR_ERR(clk);
  11737. + }
  11738. +
  11739. + bcm2708_i2c_init_pinmode(pdev->id);
  11740. +
  11741. + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  11742. + if (!bi)
  11743. + goto out_clk_put;
  11744. +
  11745. + platform_set_drvdata(pdev, bi);
  11746. +
  11747. + adap = &bi->adapter;
  11748. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  11749. + adap->algo = &bcm2708_i2c_algorithm;
  11750. + adap->algo_data = bi;
  11751. + adap->dev.parent = &pdev->dev;
  11752. + adap->nr = pdev->id;
  11753. + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  11754. +
  11755. + switch (pdev->id) {
  11756. + case 0:
  11757. + adap->class = I2C_CLASS_HWMON;
  11758. + break;
  11759. + case 1:
  11760. + adap->class = I2C_CLASS_DDC;
  11761. + break;
  11762. + default:
  11763. + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  11764. + err = -ENXIO;
  11765. + goto out_free_bi;
  11766. + }
  11767. +
  11768. + spin_lock_init(&bi->lock);
  11769. + init_completion(&bi->done);
  11770. +
  11771. + bi->base = ioremap(regs->start, resource_size(regs));
  11772. + if (!bi->base) {
  11773. + dev_err(&pdev->dev, "could not remap memory\n");
  11774. + goto out_free_bi;
  11775. + }
  11776. +
  11777. + bi->irq = irq;
  11778. + bi->clk = clk;
  11779. +
  11780. + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  11781. + dev_name(&pdev->dev), bi);
  11782. + if (err) {
  11783. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  11784. + goto out_iounmap;
  11785. + }
  11786. +
  11787. + bcm2708_bsc_reset(bi);
  11788. +
  11789. + err = i2c_add_numbered_adapter(adap);
  11790. + if (err < 0) {
  11791. + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  11792. + goto out_free_irq;
  11793. + }
  11794. +
  11795. + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %dk)\n",
  11796. + pdev->id, (unsigned long)regs->start, irq, baudrate/1000);
  11797. +
  11798. + return 0;
  11799. +
  11800. +out_free_irq:
  11801. + free_irq(bi->irq, bi);
  11802. +out_iounmap:
  11803. + iounmap(bi->base);
  11804. +out_free_bi:
  11805. + kfree(bi);
  11806. +out_clk_put:
  11807. + clk_put(clk);
  11808. + return err;
  11809. +}
  11810. +
  11811. +static int bcm2708_i2c_remove(struct platform_device *pdev)
  11812. +{
  11813. + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  11814. +
  11815. + platform_set_drvdata(pdev, NULL);
  11816. +
  11817. + i2c_del_adapter(&bi->adapter);
  11818. + free_irq(bi->irq, bi);
  11819. + iounmap(bi->base);
  11820. + clk_disable(bi->clk);
  11821. + clk_put(bi->clk);
  11822. + kfree(bi);
  11823. +
  11824. + return 0;
  11825. +}
  11826. +
  11827. +static struct platform_driver bcm2708_i2c_driver = {
  11828. + .driver = {
  11829. + .name = DRV_NAME,
  11830. + .owner = THIS_MODULE,
  11831. + },
  11832. + .probe = bcm2708_i2c_probe,
  11833. + .remove = bcm2708_i2c_remove,
  11834. +};
  11835. +
  11836. +// module_platform_driver(bcm2708_i2c_driver);
  11837. +
  11838. +
  11839. +static int __init bcm2708_i2c_init(void)
  11840. +{
  11841. + return platform_driver_register(&bcm2708_i2c_driver);
  11842. +}
  11843. +
  11844. +static void __exit bcm2708_i2c_exit(void)
  11845. +{
  11846. + platform_driver_unregister(&bcm2708_i2c_driver);
  11847. +}
  11848. +
  11849. +module_init(bcm2708_i2c_init);
  11850. +module_exit(bcm2708_i2c_exit);
  11851. +
  11852. +
  11853. +
  11854. +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  11855. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  11856. +MODULE_LICENSE("GPL v2");
  11857. +MODULE_ALIAS("platform:" DRV_NAME);
  11858. diff -Nur linux-3.12.13/drivers/i2c/busses/Kconfig linux-raspberry-pi/drivers/i2c/busses/Kconfig
  11859. --- linux-3.12.13/drivers/i2c/busses/Kconfig 2014-02-22 22:32:50.000000000 +0100
  11860. +++ linux-raspberry-pi/drivers/i2c/busses/Kconfig 2014-03-11 17:51:14.000000000 +0100
  11861. @@ -346,6 +346,25 @@
  11862. This support is also available as a module. If so, the module
  11863. will be called i2c-bcm2835.
  11864. +config I2C_BCM2708
  11865. + tristate "BCM2708 BSC"
  11866. + depends on MACH_BCM2708
  11867. + help
  11868. + Enabling this option will add BSC (Broadcom Serial Controller)
  11869. + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
  11870. + with I2C/TWI/SMBus.
  11871. +
  11872. +config I2C_BCM2708_BAUDRATE
  11873. + prompt "BCM2708 I2C baudrate"
  11874. + depends on I2C_BCM2708
  11875. + int
  11876. + default 100000
  11877. + help
  11878. + Set the I2C baudrate. This will alter the default value. A
  11879. + different baudrate can be set by using a module parameter as well. If
  11880. + no parameter is provided when loading, this is the value that will be
  11881. + used.
  11882. +
  11883. config I2C_BLACKFIN_TWI
  11884. tristate "Blackfin TWI I2C support"
  11885. depends on BLACKFIN
  11886. diff -Nur linux-3.12.13/drivers/i2c/busses/Makefile linux-raspberry-pi/drivers/i2c/busses/Makefile
  11887. --- linux-3.12.13/drivers/i2c/busses/Makefile 2014-02-22 22:32:50.000000000 +0100
  11888. +++ linux-raspberry-pi/drivers/i2c/busses/Makefile 2014-03-11 17:51:14.000000000 +0100
  11889. @@ -32,6 +32,7 @@
  11890. obj-$(CONFIG_I2C_AT91) += i2c-at91.o
  11891. obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
  11892. obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
  11893. +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
  11894. obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
  11895. obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
  11896. obj-$(CONFIG_I2C_CPM) += i2c-cpm.o
  11897. diff -Nur linux-3.12.13/drivers/media/dvb-core/dvb-usb-ids.h linux-raspberry-pi/drivers/media/dvb-core/dvb-usb-ids.h
  11898. --- linux-3.12.13/drivers/media/dvb-core/dvb-usb-ids.h 2014-02-22 22:32:50.000000000 +0100
  11899. +++ linux-raspberry-pi/drivers/media/dvb-core/dvb-usb-ids.h 2014-03-11 17:51:15.000000000 +0100
  11900. @@ -366,6 +366,7 @@
  11901. #define USB_PID_TERRATEC_DVBS2CI_V2 0x10ac
  11902. #define USB_PID_TECHNISAT_USB2_HDCI_V1 0x0001
  11903. #define USB_PID_TECHNISAT_USB2_HDCI_V2 0x0002
  11904. +#define USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI 0x0003
  11905. #define USB_PID_TECHNISAT_AIRSTAR_TELESTICK_2 0x0004
  11906. #define USB_PID_TECHNISAT_USB2_DVB_S2 0x0500
  11907. #define USB_PID_CPYTO_REDI_PC50A 0xa803
  11908. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/bcm2835-camera.c linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.c
  11909. --- linux-3.12.13/drivers/media/platform/bcm2835/bcm2835-camera.c 1970-01-01 01:00:00.000000000 +0100
  11910. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.c 2014-03-11 17:51:15.000000000 +0100
  11911. @@ -0,0 +1,1622 @@
  11912. +/*
  11913. + * Broadcom BM2835 V4L2 driver
  11914. + *
  11915. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  11916. + *
  11917. + * This file is subject to the terms and conditions of the GNU General Public
  11918. + * License. See the file COPYING in the main directory of this archive
  11919. + * for more details.
  11920. + *
  11921. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  11922. + * Dave Stevenson <dsteve@broadcom.com>
  11923. + * Simon Mellor <simellor@broadcom.com>
  11924. + * Luke Diamand <luked@broadcom.com>
  11925. + */
  11926. +
  11927. +#include <linux/errno.h>
  11928. +#include <linux/kernel.h>
  11929. +#include <linux/module.h>
  11930. +#include <linux/slab.h>
  11931. +#include <media/videobuf2-vmalloc.h>
  11932. +#include <media/videobuf2-dma-contig.h>
  11933. +#include <media/v4l2-device.h>
  11934. +#include <media/v4l2-ioctl.h>
  11935. +#include <media/v4l2-ctrls.h>
  11936. +#include <media/v4l2-fh.h>
  11937. +#include <media/v4l2-event.h>
  11938. +#include <media/v4l2-common.h>
  11939. +#include <linux/delay.h>
  11940. +
  11941. +#include "mmal-common.h"
  11942. +#include "mmal-encodings.h"
  11943. +#include "mmal-vchiq.h"
  11944. +#include "mmal-msg.h"
  11945. +#include "mmal-parameters.h"
  11946. +#include "bcm2835-camera.h"
  11947. +
  11948. +#define BM2835_MMAL_VERSION "0.0.2"
  11949. +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
  11950. +
  11951. +#define MAX_WIDTH 2592
  11952. +#define MAX_HEIGHT 1944
  11953. +#define MIN_BUFFER_SIZE (80*1024)
  11954. +
  11955. +#define MAX_VIDEO_MODE_WIDTH 1280
  11956. +#define MAX_VIDEO_MODE_HEIGHT 720
  11957. +
  11958. +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
  11959. +MODULE_AUTHOR("Vincent Sanders");
  11960. +MODULE_LICENSE("GPL");
  11961. +MODULE_VERSION(BM2835_MMAL_VERSION);
  11962. +
  11963. +int bcm2835_v4l2_debug;
  11964. +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
  11965. +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
  11966. +
  11967. +static struct bm2835_mmal_dev *gdev; /* global device data */
  11968. +
  11969. +#define FPS_MIN 1
  11970. +#define FPS_MAX 30
  11971. +
  11972. +/* timeperframe: min/max and default */
  11973. +static const struct v4l2_fract
  11974. + tpf_min = {.numerator = 1, .denominator = FPS_MAX},
  11975. + tpf_max = {.numerator = 1, .denominator = FPS_MIN},
  11976. + tpf_default = {.numerator = 1000, .denominator = 30000};
  11977. +
  11978. +/* video formats */
  11979. +static struct mmal_fmt formats[] = {
  11980. + {
  11981. + .name = "4:2:0, packed YUV",
  11982. + .fourcc = V4L2_PIX_FMT_YUV420,
  11983. + .mmal = MMAL_ENCODING_I420,
  11984. + .depth = 12,
  11985. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11986. + },
  11987. + {
  11988. + .name = "4:2:2, packed, YUYV",
  11989. + .fourcc = V4L2_PIX_FMT_YUYV,
  11990. + .mmal = MMAL_ENCODING_YUYV,
  11991. + .depth = 16,
  11992. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11993. + },
  11994. + {
  11995. + .name = "RGB24 (BE)",
  11996. + .fourcc = V4L2_PIX_FMT_BGR24,
  11997. + .mmal = MMAL_ENCODING_BGR24,
  11998. + .depth = 24,
  11999. + .mmal_component = MMAL_COMPONENT_CAMERA,
  12000. + },
  12001. + {
  12002. + .name = "JPEG",
  12003. + .fourcc = V4L2_PIX_FMT_JPEG,
  12004. + .mmal = MMAL_ENCODING_JPEG,
  12005. + .depth = 8,
  12006. + .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
  12007. + },
  12008. + {
  12009. + .name = "H264",
  12010. + .fourcc = V4L2_PIX_FMT_H264,
  12011. + .mmal = MMAL_ENCODING_H264,
  12012. + .depth = 8,
  12013. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  12014. + }
  12015. +};
  12016. +
  12017. +static struct mmal_fmt *get_format(struct v4l2_format *f)
  12018. +{
  12019. + struct mmal_fmt *fmt;
  12020. + unsigned int k;
  12021. +
  12022. + for (k = 0; k < ARRAY_SIZE(formats); k++) {
  12023. + fmt = &formats[k];
  12024. + if (fmt->fourcc == f->fmt.pix.pixelformat)
  12025. + break;
  12026. + }
  12027. +
  12028. + if (k == ARRAY_SIZE(formats))
  12029. + return NULL;
  12030. +
  12031. + return &formats[k];
  12032. +}
  12033. +
  12034. +/* ------------------------------------------------------------------
  12035. + Videobuf queue operations
  12036. + ------------------------------------------------------------------*/
  12037. +
  12038. +static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  12039. + unsigned int *nbuffers, unsigned int *nplanes,
  12040. + unsigned int sizes[], void *alloc_ctxs[])
  12041. +{
  12042. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  12043. + unsigned long size;
  12044. +
  12045. + /* refuse queue setup if port is not configured */
  12046. + if (dev->capture.port == NULL) {
  12047. + v4l2_err(&dev->v4l2_dev,
  12048. + "%s: capture port not configured\n", __func__);
  12049. + return -EINVAL;
  12050. + }
  12051. +
  12052. + size = dev->capture.port->current_buffer.size;
  12053. + if (size == 0) {
  12054. + v4l2_err(&dev->v4l2_dev,
  12055. + "%s: capture port buffer size is zero\n", __func__);
  12056. + return -EINVAL;
  12057. + }
  12058. +
  12059. + if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
  12060. + *nbuffers = (dev->capture.port->current_buffer.num + 2);
  12061. +
  12062. + *nplanes = 1;
  12063. +
  12064. + sizes[0] = size;
  12065. +
  12066. + /*
  12067. + * videobuf2-vmalloc allocator is context-less so no need to set
  12068. + * alloc_ctxs array.
  12069. + */
  12070. +
  12071. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  12072. + __func__, dev);
  12073. +
  12074. + return 0;
  12075. +}
  12076. +
  12077. +static int buffer_prepare(struct vb2_buffer *vb)
  12078. +{
  12079. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  12080. + unsigned long size;
  12081. +
  12082. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  12083. + __func__, dev);
  12084. +
  12085. + BUG_ON(dev->capture.port == NULL);
  12086. + BUG_ON(dev->capture.fmt == NULL);
  12087. +
  12088. + size = dev->capture.stride * dev->capture.height;
  12089. + if (vb2_plane_size(vb, 0) < size) {
  12090. + v4l2_err(&dev->v4l2_dev,
  12091. + "%s data will not fit into plane (%lu < %lu)\n",
  12092. + __func__, vb2_plane_size(vb, 0), size);
  12093. + return -EINVAL;
  12094. + }
  12095. +
  12096. + return 0;
  12097. +}
  12098. +
  12099. +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
  12100. +{
  12101. + return dev->capture.camera_port ==
  12102. + &dev->
  12103. + component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
  12104. +}
  12105. +
  12106. +static void buffer_cb(struct vchiq_mmal_instance *instance,
  12107. + struct vchiq_mmal_port *port,
  12108. + int status,
  12109. + struct mmal_buffer *buf,
  12110. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
  12111. +{
  12112. + struct bm2835_mmal_dev *dev = port->cb_ctx;
  12113. +
  12114. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12115. + "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
  12116. + __func__, status, buf, length, mmal_flags, pts);
  12117. +
  12118. + if (status != 0) {
  12119. + /* error in transfer */
  12120. + if (buf != NULL) {
  12121. + /* there was a buffer with the error so return it */
  12122. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  12123. + }
  12124. + return;
  12125. + } else if (length == 0) {
  12126. + /* stream ended */
  12127. + if (buf != NULL) {
  12128. + /* this should only ever happen if the port is
  12129. + * disabled and there are buffers still queued
  12130. + */
  12131. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  12132. + pr_debug("Empty buffer");
  12133. + } else if (dev->capture.frame_count) {
  12134. + /* grab another frame */
  12135. + if (is_capturing(dev)) {
  12136. + pr_debug("Grab another frame");
  12137. + vchiq_mmal_port_parameter_set(
  12138. + instance,
  12139. + dev->capture.
  12140. + camera_port,
  12141. + MMAL_PARAMETER_CAPTURE,
  12142. + &dev->capture.
  12143. + frame_count,
  12144. + sizeof(dev->capture.frame_count));
  12145. + }
  12146. + } else {
  12147. + /* signal frame completion */
  12148. + complete(&dev->capture.frame_cmplt);
  12149. + }
  12150. + } else {
  12151. + if (dev->capture.frame_count) {
  12152. + if (dev->capture.vc_start_timestamp != -1 &&
  12153. + pts != 0) {
  12154. + s64 runtime_us = pts -
  12155. + dev->capture.vc_start_timestamp;
  12156. + u32 div = 0;
  12157. + u32 rem = 0;
  12158. +
  12159. + div =
  12160. + div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
  12161. + buf->vb.v4l2_buf.timestamp.tv_sec =
  12162. + dev->capture.kernel_start_ts.tv_sec - 1 +
  12163. + div;
  12164. + buf->vb.v4l2_buf.timestamp.tv_usec =
  12165. + dev->capture.kernel_start_ts.tv_usec + rem;
  12166. +
  12167. + if (buf->vb.v4l2_buf.timestamp.tv_usec >=
  12168. + USEC_PER_SEC) {
  12169. + buf->vb.v4l2_buf.timestamp.tv_sec++;
  12170. + buf->vb.v4l2_buf.timestamp.tv_usec -=
  12171. + USEC_PER_SEC;
  12172. + }
  12173. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12174. + "Convert start time %d.%06d and %llu "
  12175. + "with offset %llu to %d.%06d\n",
  12176. + (int)dev->capture.kernel_start_ts.
  12177. + tv_sec,
  12178. + (int)dev->capture.kernel_start_ts.
  12179. + tv_usec,
  12180. + dev->capture.vc_start_timestamp, pts,
  12181. + (int)buf->vb.v4l2_buf.timestamp.tv_sec,
  12182. + (int)buf->vb.v4l2_buf.timestamp.
  12183. + tv_usec);
  12184. + } else {
  12185. + v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  12186. + }
  12187. +
  12188. + vb2_set_plane_payload(&buf->vb, 0, length);
  12189. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  12190. +
  12191. + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
  12192. + is_capturing(dev)) {
  12193. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12194. + "Grab another frame as buffer has EOS");
  12195. + vchiq_mmal_port_parameter_set(
  12196. + instance,
  12197. + dev->capture.
  12198. + camera_port,
  12199. + MMAL_PARAMETER_CAPTURE,
  12200. + &dev->capture.
  12201. + frame_count,
  12202. + sizeof(dev->capture.frame_count));
  12203. + }
  12204. + } else {
  12205. + /* signal frame completion */
  12206. + complete(&dev->capture.frame_cmplt);
  12207. + }
  12208. + }
  12209. +}
  12210. +
  12211. +static int enable_camera(struct bm2835_mmal_dev *dev)
  12212. +{
  12213. + int ret;
  12214. + if (!dev->camera_use_count) {
  12215. + ret = vchiq_mmal_component_enable(
  12216. + dev->instance,
  12217. + dev->component[MMAL_COMPONENT_CAMERA]);
  12218. + if (ret < 0) {
  12219. + v4l2_err(&dev->v4l2_dev,
  12220. + "Failed enabling camera, ret %d\n", ret);
  12221. + return -EINVAL;
  12222. + }
  12223. + }
  12224. + dev->camera_use_count++;
  12225. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12226. + &dev->v4l2_dev, "enabled camera (refcount %d)\n",
  12227. + dev->camera_use_count);
  12228. + return 0;
  12229. +}
  12230. +
  12231. +static int disable_camera(struct bm2835_mmal_dev *dev)
  12232. +{
  12233. + int ret;
  12234. + if (!dev->camera_use_count) {
  12235. + v4l2_err(&dev->v4l2_dev,
  12236. + "Disabled the camera when already disabled\n");
  12237. + return -EINVAL;
  12238. + }
  12239. + dev->camera_use_count--;
  12240. + if (!dev->camera_use_count) {
  12241. + unsigned int i = 0xFFFFFFFF;
  12242. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12243. + "Disabling camera\n");
  12244. + ret =
  12245. + vchiq_mmal_component_disable(
  12246. + dev->instance,
  12247. + dev->component[MMAL_COMPONENT_CAMERA]);
  12248. + if (ret < 0) {
  12249. + v4l2_err(&dev->v4l2_dev,
  12250. + "Failed disabling camera, ret %d\n", ret);
  12251. + return -EINVAL;
  12252. + }
  12253. + vchiq_mmal_port_parameter_set(
  12254. + dev->instance,
  12255. + &dev->component[MMAL_COMPONENT_CAMERA]->control,
  12256. + MMAL_PARAMETER_CAMERA_NUM, &i,
  12257. + sizeof(i));
  12258. + }
  12259. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12260. + "Camera refcount now %d\n", dev->camera_use_count);
  12261. + return 0;
  12262. +}
  12263. +
  12264. +static void buffer_queue(struct vb2_buffer *vb)
  12265. +{
  12266. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  12267. + struct mmal_buffer *buf = container_of(vb, struct mmal_buffer, vb);
  12268. + int ret;
  12269. +
  12270. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12271. + "%s: dev:%p buf:%p\n", __func__, dev, buf);
  12272. +
  12273. + buf->buffer = vb2_plane_vaddr(&buf->vb, 0);
  12274. + buf->buffer_size = vb2_plane_size(&buf->vb, 0);
  12275. +
  12276. + ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
  12277. + if (ret < 0)
  12278. + v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
  12279. + __func__);
  12280. +}
  12281. +
  12282. +static int start_streaming(struct vb2_queue *vq, unsigned int count)
  12283. +{
  12284. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  12285. + int ret;
  12286. + int parameter_size;
  12287. +
  12288. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  12289. + __func__, dev);
  12290. +
  12291. + /* ensure a format has actually been set */
  12292. + if (dev->capture.port == NULL)
  12293. + return -EINVAL;
  12294. +
  12295. + if (enable_camera(dev) < 0) {
  12296. + v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
  12297. + return -EINVAL;
  12298. + }
  12299. +
  12300. + /*init_completion(&dev->capture.frame_cmplt); */
  12301. +
  12302. + /* enable frame capture */
  12303. + dev->capture.frame_count = 1;
  12304. +
  12305. + /* if the preview is not already running, wait for a few frames for AGC
  12306. + * to settle down.
  12307. + */
  12308. + if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
  12309. + msleep(300);
  12310. +
  12311. + /* enable the connection from camera to encoder (if applicable) */
  12312. + if (dev->capture.camera_port != dev->capture.port
  12313. + && dev->capture.camera_port) {
  12314. + ret = vchiq_mmal_port_enable(dev->instance,
  12315. + dev->capture.camera_port, NULL);
  12316. + if (ret) {
  12317. + v4l2_err(&dev->v4l2_dev,
  12318. + "Failed to enable encode tunnel - error %d\n",
  12319. + ret);
  12320. + return -1;
  12321. + }
  12322. + }
  12323. +
  12324. + /* Get VC timestamp at this point in time */
  12325. + parameter_size = sizeof(dev->capture.vc_start_timestamp);
  12326. + if (vchiq_mmal_port_parameter_get(dev->instance,
  12327. + dev->capture.camera_port,
  12328. + MMAL_PARAMETER_SYSTEM_TIME,
  12329. + &dev->capture.vc_start_timestamp,
  12330. + &parameter_size)) {
  12331. + v4l2_err(&dev->v4l2_dev,
  12332. + "Failed to get VC start time - update your VC f/w\n");
  12333. +
  12334. + /* Flag to indicate just to rely on kernel timestamps */
  12335. + dev->capture.vc_start_timestamp = -1;
  12336. + } else
  12337. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12338. + "Start time %lld size %d\n",
  12339. + dev->capture.vc_start_timestamp, parameter_size);
  12340. +
  12341. + v4l2_get_timestamp(&dev->capture.kernel_start_ts);
  12342. +
  12343. + /* enable the camera port */
  12344. + dev->capture.port->cb_ctx = dev;
  12345. + ret =
  12346. + vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
  12347. + if (ret) {
  12348. + v4l2_err(&dev->v4l2_dev,
  12349. + "Failed to enable capture port - error %d. "
  12350. + "Disabling camera port again\n", ret);
  12351. +
  12352. + vchiq_mmal_port_disable(dev->instance,
  12353. + dev->capture.camera_port);
  12354. + if (disable_camera(dev) < 0) {
  12355. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  12356. + return -EINVAL;
  12357. + }
  12358. + return -1;
  12359. + }
  12360. +
  12361. + /* capture the first frame */
  12362. + vchiq_mmal_port_parameter_set(dev->instance,
  12363. + dev->capture.camera_port,
  12364. + MMAL_PARAMETER_CAPTURE,
  12365. + &dev->capture.frame_count,
  12366. + sizeof(dev->capture.frame_count));
  12367. + return 0;
  12368. +}
  12369. +
  12370. +/* abort streaming and wait for last buffer */
  12371. +static int stop_streaming(struct vb2_queue *vq)
  12372. +{
  12373. + int ret;
  12374. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  12375. +
  12376. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  12377. + __func__, dev);
  12378. +
  12379. + init_completion(&dev->capture.frame_cmplt);
  12380. + dev->capture.frame_count = 0;
  12381. +
  12382. + /* ensure a format has actually been set */
  12383. + if (dev->capture.port == NULL)
  12384. + return -EINVAL;
  12385. +
  12386. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
  12387. +
  12388. + /* stop capturing frames */
  12389. + vchiq_mmal_port_parameter_set(dev->instance,
  12390. + dev->capture.camera_port,
  12391. + MMAL_PARAMETER_CAPTURE,
  12392. + &dev->capture.frame_count,
  12393. + sizeof(dev->capture.frame_count));
  12394. +
  12395. + /* wait for last frame to complete */
  12396. + ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
  12397. + if (ret <= 0)
  12398. + v4l2_err(&dev->v4l2_dev,
  12399. + "error %d waiting for frame completion\n", ret);
  12400. +
  12401. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12402. + "disabling connection\n");
  12403. +
  12404. + /* disable the connection from camera to encoder */
  12405. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
  12406. + if (!ret && dev->capture.camera_port != dev->capture.port) {
  12407. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12408. + "disabling port\n");
  12409. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
  12410. + } else if (dev->capture.camera_port != dev->capture.port) {
  12411. + v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
  12412. + ret);
  12413. + }
  12414. +
  12415. + if (disable_camera(dev) < 0) {
  12416. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  12417. + return -EINVAL;
  12418. + }
  12419. +
  12420. + return ret;
  12421. +}
  12422. +
  12423. +static void bm2835_mmal_lock(struct vb2_queue *vq)
  12424. +{
  12425. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  12426. + mutex_lock(&dev->mutex);
  12427. +}
  12428. +
  12429. +static void bm2835_mmal_unlock(struct vb2_queue *vq)
  12430. +{
  12431. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  12432. + mutex_unlock(&dev->mutex);
  12433. +}
  12434. +
  12435. +static struct vb2_ops bm2835_mmal_video_qops = {
  12436. + .queue_setup = queue_setup,
  12437. + .buf_prepare = buffer_prepare,
  12438. + .buf_queue = buffer_queue,
  12439. + .start_streaming = start_streaming,
  12440. + .stop_streaming = stop_streaming,
  12441. + .wait_prepare = bm2835_mmal_unlock,
  12442. + .wait_finish = bm2835_mmal_lock,
  12443. +};
  12444. +
  12445. +/* ------------------------------------------------------------------
  12446. + IOCTL operations
  12447. + ------------------------------------------------------------------*/
  12448. +
  12449. +/* overlay ioctl */
  12450. +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  12451. + struct v4l2_fmtdesc *f)
  12452. +{
  12453. + struct mmal_fmt *fmt;
  12454. +
  12455. + if (f->index >= ARRAY_SIZE(formats))
  12456. + return -EINVAL;
  12457. +
  12458. + fmt = &formats[f->index];
  12459. +
  12460. + strlcpy(f->description, fmt->name, sizeof(f->description));
  12461. + f->pixelformat = fmt->fourcc;
  12462. +
  12463. + return 0;
  12464. +}
  12465. +
  12466. +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
  12467. + struct v4l2_format *f)
  12468. +{
  12469. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12470. +
  12471. + f->fmt.win = dev->overlay;
  12472. +
  12473. + return 0;
  12474. +}
  12475. +
  12476. +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  12477. + struct v4l2_format *f)
  12478. +{
  12479. + /* Only support one format so get the current one. */
  12480. + vidioc_g_fmt_vid_overlay(file, priv, f);
  12481. +
  12482. + /* todo: allow the size and/or offset to be changed. */
  12483. + return 0;
  12484. +}
  12485. +
  12486. +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  12487. + struct v4l2_format *f)
  12488. +{
  12489. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12490. +
  12491. + vidioc_try_fmt_vid_overlay(file, priv, f);
  12492. +
  12493. + dev->overlay = f->fmt.win;
  12494. +
  12495. + /* todo: program the preview port parameters */
  12496. + return 0;
  12497. +}
  12498. +
  12499. +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
  12500. +{
  12501. + int ret;
  12502. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12503. + struct vchiq_mmal_port *src;
  12504. + struct vchiq_mmal_port *dst;
  12505. + struct mmal_parameter_displayregion prev_config = {
  12506. + .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
  12507. + MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
  12508. + .layer = PREVIEW_LAYER,
  12509. + .alpha = 255,
  12510. + .fullscreen = 0,
  12511. + .dest_rect = {
  12512. + .x = dev->overlay.w.left,
  12513. + .y = dev->overlay.w.top,
  12514. + .width = dev->overlay.w.width,
  12515. + .height = dev->overlay.w.height,
  12516. + },
  12517. + };
  12518. +
  12519. + if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
  12520. + (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
  12521. + return 0; /* already in requested state */
  12522. +
  12523. + src =
  12524. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12525. + output[MMAL_CAMERA_PORT_PREVIEW];
  12526. +
  12527. + if (!on) {
  12528. + /* disconnect preview ports and disable component */
  12529. + ret = vchiq_mmal_port_disable(dev->instance, src);
  12530. + if (!ret)
  12531. + ret =
  12532. + vchiq_mmal_port_connect_tunnel(dev->instance, src,
  12533. + NULL);
  12534. + if (ret >= 0)
  12535. + ret = vchiq_mmal_component_disable(
  12536. + dev->instance,
  12537. + dev->component[MMAL_COMPONENT_PREVIEW]);
  12538. +
  12539. + disable_camera(dev);
  12540. + return ret;
  12541. + }
  12542. +
  12543. + /* set preview port format and connect it to output */
  12544. + dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
  12545. +
  12546. + ret = vchiq_mmal_port_set_format(dev->instance, src);
  12547. + if (ret < 0)
  12548. + goto error;
  12549. +
  12550. + ret = vchiq_mmal_port_parameter_set(dev->instance, dst,
  12551. + MMAL_PARAMETER_DISPLAYREGION,
  12552. + &prev_config, sizeof(prev_config));
  12553. + if (ret < 0)
  12554. + goto error;
  12555. +
  12556. + if (enable_camera(dev) < 0)
  12557. + goto error;
  12558. +
  12559. + ret = vchiq_mmal_component_enable(
  12560. + dev->instance,
  12561. + dev->component[MMAL_COMPONENT_PREVIEW]);
  12562. + if (ret < 0)
  12563. + goto error;
  12564. +
  12565. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
  12566. + src, dst);
  12567. + ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
  12568. + if (!ret)
  12569. + ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
  12570. +error:
  12571. + return ret;
  12572. +}
  12573. +
  12574. +static int vidioc_g_fbuf(struct file *file, void *fh,
  12575. + struct v4l2_framebuffer *a)
  12576. +{
  12577. + /* The video overlay must stay within the framebuffer and can't be
  12578. + positioned independently. */
  12579. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12580. + struct vchiq_mmal_port *preview_port =
  12581. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12582. + output[MMAL_CAMERA_PORT_PREVIEW];
  12583. + a->flags = V4L2_FBUF_FLAG_OVERLAY;
  12584. + a->fmt.width = preview_port->es.video.width;
  12585. + a->fmt.height = preview_port->es.video.height;
  12586. + a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
  12587. + a->fmt.bytesperline = (preview_port->es.video.width * 3)>>1;
  12588. + a->fmt.sizeimage = (preview_port->es.video.width *
  12589. + preview_port->es.video.height * 3)>>1;
  12590. + a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
  12591. +
  12592. + return 0;
  12593. +}
  12594. +
  12595. +/* input ioctls */
  12596. +static int vidioc_enum_input(struct file *file, void *priv,
  12597. + struct v4l2_input *inp)
  12598. +{
  12599. + /* only a single camera input */
  12600. + if (inp->index != 0)
  12601. + return -EINVAL;
  12602. +
  12603. + inp->type = V4L2_INPUT_TYPE_CAMERA;
  12604. + sprintf(inp->name, "Camera %u", inp->index);
  12605. + return 0;
  12606. +}
  12607. +
  12608. +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  12609. +{
  12610. + *i = 0;
  12611. + return 0;
  12612. +}
  12613. +
  12614. +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  12615. +{
  12616. + if (i != 0)
  12617. + return -EINVAL;
  12618. +
  12619. + return 0;
  12620. +}
  12621. +
  12622. +/* capture ioctls */
  12623. +static int vidioc_querycap(struct file *file, void *priv,
  12624. + struct v4l2_capability *cap)
  12625. +{
  12626. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12627. + u32 major;
  12628. + u32 minor;
  12629. +
  12630. + vchiq_mmal_version(dev->instance, &major, &minor);
  12631. +
  12632. + strcpy(cap->driver, "bm2835 mmal");
  12633. + snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
  12634. + major, minor);
  12635. +
  12636. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  12637. + "platform:%s", dev->v4l2_dev.name);
  12638. + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
  12639. + V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  12640. + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  12641. +
  12642. + return 0;
  12643. +}
  12644. +
  12645. +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  12646. + struct v4l2_fmtdesc *f)
  12647. +{
  12648. + struct mmal_fmt *fmt;
  12649. +
  12650. + if (f->index >= ARRAY_SIZE(formats))
  12651. + return -EINVAL;
  12652. +
  12653. + fmt = &formats[f->index];
  12654. +
  12655. + strlcpy(f->description, fmt->name, sizeof(f->description));
  12656. + f->pixelformat = fmt->fourcc;
  12657. + return 0;
  12658. +}
  12659. +
  12660. +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  12661. + struct v4l2_format *f)
  12662. +{
  12663. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12664. +
  12665. + f->fmt.pix.width = dev->capture.width;
  12666. + f->fmt.pix.height = dev->capture.height;
  12667. + f->fmt.pix.field = V4L2_FIELD_NONE;
  12668. + f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
  12669. + f->fmt.pix.bytesperline =
  12670. + (f->fmt.pix.width * dev->capture.fmt->depth) >> 3;
  12671. + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  12672. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG
  12673. + && f->fmt.pix.sizeimage < (100 << 10)) {
  12674. + /* Need a minimum size for JPEG to account for EXIF. */
  12675. + f->fmt.pix.sizeimage = (100 << 10);
  12676. + }
  12677. +
  12678. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_YUYV ||
  12679. + dev->capture.fmt->fourcc == V4L2_PIX_FMT_UYVY)
  12680. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  12681. + else
  12682. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  12683. + f->fmt.pix.priv = 0;
  12684. +
  12685. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  12686. + __func__);
  12687. + return 0;
  12688. +}
  12689. +
  12690. +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  12691. + struct v4l2_format *f)
  12692. +{
  12693. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12694. + struct mmal_fmt *mfmt;
  12695. +
  12696. + mfmt = get_format(f);
  12697. + if (!mfmt) {
  12698. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12699. + "Fourcc format (0x%08x) unknown.\n",
  12700. + f->fmt.pix.pixelformat);
  12701. + f->fmt.pix.pixelformat = formats[0].fourcc;
  12702. + mfmt = get_format(f);
  12703. + }
  12704. +
  12705. + f->fmt.pix.field = V4L2_FIELD_NONE;
  12706. + /* image must be a multiple of 32 pixels wide and 16 lines high */
  12707. + v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 5,
  12708. + &f->fmt.pix.height, 32, MAX_HEIGHT, 4, 0);
  12709. + f->fmt.pix.bytesperline = (f->fmt.pix.width * mfmt->depth) >> 3;
  12710. + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  12711. + if (f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
  12712. + f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
  12713. +
  12714. + if (mfmt->fourcc == V4L2_PIX_FMT_YUYV ||
  12715. + mfmt->fourcc == V4L2_PIX_FMT_UYVY)
  12716. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  12717. + else
  12718. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  12719. + f->fmt.pix.priv = 0;
  12720. +
  12721. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  12722. + __func__);
  12723. + return 0;
  12724. +}
  12725. +
  12726. +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
  12727. + struct v4l2_format *f)
  12728. +{
  12729. + int ret;
  12730. + struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
  12731. + struct vchiq_mmal_component *encode_component = NULL;
  12732. + struct mmal_fmt *mfmt = get_format(f);
  12733. +
  12734. + BUG_ON(!mfmt);
  12735. +
  12736. + if (dev->capture.encode_component) {
  12737. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12738. + "vid_cap - disconnect previous tunnel\n");
  12739. +
  12740. + /* Disconnect any previous connection */
  12741. + vchiq_mmal_port_connect_tunnel(dev->instance,
  12742. + dev->capture.camera_port, NULL);
  12743. + dev->capture.camera_port = NULL;
  12744. + ret = vchiq_mmal_component_disable(dev->instance,
  12745. + dev->capture.
  12746. + encode_component);
  12747. + if (ret)
  12748. + v4l2_err(&dev->v4l2_dev,
  12749. + "Failed to disable encode component %d\n",
  12750. + ret);
  12751. +
  12752. + dev->capture.encode_component = NULL;
  12753. + }
  12754. + /* format dependant port setup */
  12755. + switch (mfmt->mmal_component) {
  12756. + case MMAL_COMPONENT_CAMERA:
  12757. + /* Make a further decision on port based on resolution */
  12758. + if (f->fmt.pix.width <= MAX_VIDEO_MODE_WIDTH
  12759. + && f->fmt.pix.height <= MAX_VIDEO_MODE_HEIGHT)
  12760. + camera_port = port =
  12761. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12762. + output[MMAL_CAMERA_PORT_VIDEO];
  12763. + else
  12764. + camera_port = port =
  12765. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12766. + output[MMAL_CAMERA_PORT_CAPTURE];
  12767. + break;
  12768. + case MMAL_COMPONENT_IMAGE_ENCODE:
  12769. + encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
  12770. + port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  12771. + camera_port =
  12772. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12773. + output[MMAL_CAMERA_PORT_CAPTURE];
  12774. + break;
  12775. + case MMAL_COMPONENT_VIDEO_ENCODE:
  12776. + encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
  12777. + port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  12778. + camera_port =
  12779. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12780. + output[MMAL_CAMERA_PORT_VIDEO];
  12781. + break;
  12782. + default:
  12783. + break;
  12784. + }
  12785. +
  12786. + if (!port)
  12787. + return -EINVAL;
  12788. +
  12789. + if (encode_component)
  12790. + camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
  12791. + else
  12792. + camera_port->format.encoding = mfmt->mmal;
  12793. +
  12794. + camera_port->format.encoding_variant = 0;
  12795. + camera_port->es.video.width = f->fmt.pix.width;
  12796. + camera_port->es.video.height = f->fmt.pix.height;
  12797. + camera_port->es.video.crop.x = 0;
  12798. + camera_port->es.video.crop.y = 0;
  12799. + camera_port->es.video.crop.width = f->fmt.pix.width;
  12800. + camera_port->es.video.crop.height = f->fmt.pix.height;
  12801. + camera_port->es.video.frame_rate.num =
  12802. + dev->capture.timeperframe.denominator;
  12803. + camera_port->es.video.frame_rate.den =
  12804. + dev->capture.timeperframe.numerator;
  12805. +
  12806. + ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
  12807. +
  12808. + if (!ret
  12809. + && camera_port ==
  12810. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12811. + output[MMAL_CAMERA_PORT_VIDEO]) {
  12812. + bool overlay_enabled =
  12813. + !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
  12814. + struct vchiq_mmal_port *preview_port =
  12815. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12816. + output[MMAL_CAMERA_PORT_PREVIEW];
  12817. + /* Preview and encode ports need to match on resolution */
  12818. + if (overlay_enabled) {
  12819. + /* Need to disable the overlay before we can update
  12820. + * the resolution
  12821. + */
  12822. + ret =
  12823. + vchiq_mmal_port_disable(dev->instance,
  12824. + preview_port);
  12825. + if (!ret)
  12826. + ret =
  12827. + vchiq_mmal_port_connect_tunnel(
  12828. + dev->instance,
  12829. + preview_port,
  12830. + NULL);
  12831. + }
  12832. + preview_port->es.video.width = f->fmt.pix.width;
  12833. + preview_port->es.video.height = f->fmt.pix.height;
  12834. + preview_port->es.video.crop.x = 0;
  12835. + preview_port->es.video.crop.y = 0;
  12836. + preview_port->es.video.crop.width = f->fmt.pix.width;
  12837. + preview_port->es.video.crop.height = f->fmt.pix.height;
  12838. + preview_port->es.video.frame_rate.num = 30;
  12839. + preview_port->es.video.frame_rate.den = 1;
  12840. + ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
  12841. + if (overlay_enabled) {
  12842. + ret = vchiq_mmal_port_connect_tunnel(
  12843. + dev->instance,
  12844. + preview_port,
  12845. + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
  12846. + if (!ret)
  12847. + ret = vchiq_mmal_port_enable(dev->instance,
  12848. + preview_port,
  12849. + NULL);
  12850. + }
  12851. + }
  12852. +
  12853. + if (ret) {
  12854. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12855. + "%s failed to set format\n", __func__);
  12856. + /* ensure capture is not going to be tried */
  12857. + dev->capture.port = NULL;
  12858. + } else {
  12859. + if (encode_component) {
  12860. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12861. + "vid_cap - set up encode comp\n");
  12862. +
  12863. + /* configure buffering */
  12864. + camera_port->current_buffer.size =
  12865. + camera_port->recommended_buffer.size;
  12866. + camera_port->current_buffer.num =
  12867. + camera_port->recommended_buffer.num;
  12868. +
  12869. + ret =
  12870. + vchiq_mmal_port_connect_tunnel(
  12871. + dev->instance,
  12872. + camera_port,
  12873. + &encode_component->input[0]);
  12874. + if (ret) {
  12875. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12876. + &dev->v4l2_dev,
  12877. + "%s failed to create connection\n",
  12878. + __func__);
  12879. + /* ensure capture is not going to be tried */
  12880. + dev->capture.port = NULL;
  12881. + } else {
  12882. + port->es.video.width = f->fmt.pix.width;
  12883. + port->es.video.height = f->fmt.pix.height;
  12884. + port->es.video.crop.x = 0;
  12885. + port->es.video.crop.y = 0;
  12886. + port->es.video.crop.width = f->fmt.pix.width;
  12887. + port->es.video.crop.height = f->fmt.pix.height;
  12888. + port->es.video.frame_rate.num =
  12889. + dev->capture.timeperframe.denominator;
  12890. + port->es.video.frame_rate.den =
  12891. + dev->capture.timeperframe.numerator;
  12892. +
  12893. + port->format.encoding = mfmt->mmal;
  12894. + port->format.encoding_variant = 0;
  12895. + /* Set any encoding specific parameters */
  12896. + switch (mfmt->mmal_component) {
  12897. + case MMAL_COMPONENT_VIDEO_ENCODE:
  12898. + port->format.bitrate =
  12899. + dev->capture.encode_bitrate;
  12900. + break;
  12901. + case MMAL_COMPONENT_IMAGE_ENCODE:
  12902. + /* Could set EXIF parameters here */
  12903. + break;
  12904. + default:
  12905. + break;
  12906. + }
  12907. + ret = vchiq_mmal_port_set_format(dev->instance,
  12908. + port);
  12909. + if (ret)
  12910. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12911. + &dev->v4l2_dev,
  12912. + "%s failed to set format\n",
  12913. + __func__);
  12914. + }
  12915. +
  12916. + if (!ret) {
  12917. + ret = vchiq_mmal_component_enable(
  12918. + dev->instance,
  12919. + encode_component);
  12920. + if (ret) {
  12921. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12922. + &dev->v4l2_dev,
  12923. + "%s Failed to enable encode components\n",
  12924. + __func__);
  12925. + }
  12926. + }
  12927. + if (!ret) {
  12928. + /* configure buffering */
  12929. + port->current_buffer.num = 1;
  12930. + port->current_buffer.size =
  12931. + f->fmt.pix.sizeimage;
  12932. + if (port->format.encoding ==
  12933. + MMAL_ENCODING_JPEG) {
  12934. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12935. + &dev->v4l2_dev,
  12936. + "JPG - buf size now %d was %d\n",
  12937. + f->fmt.pix.sizeimage,
  12938. + port->current_buffer.size);
  12939. + port->current_buffer.size =
  12940. + (f->fmt.pix.sizeimage <
  12941. + (100 << 10))
  12942. + ? (100 << 10) : f->fmt.pix.
  12943. + sizeimage;
  12944. + }
  12945. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12946. + &dev->v4l2_dev,
  12947. + "vid_cap - cur_buf.size set to %d\n",
  12948. + f->fmt.pix.sizeimage);
  12949. + port->current_buffer.alignment = 0;
  12950. + }
  12951. + } else {
  12952. + /* configure buffering */
  12953. + camera_port->current_buffer.num = 1;
  12954. + camera_port->current_buffer.size = f->fmt.pix.sizeimage;
  12955. + camera_port->current_buffer.alignment = 0;
  12956. + }
  12957. +
  12958. + if (!ret) {
  12959. + dev->capture.fmt = mfmt;
  12960. + dev->capture.stride = f->fmt.pix.bytesperline;
  12961. + dev->capture.width = camera_port->es.video.crop.width;
  12962. + dev->capture.height = camera_port->es.video.crop.height;
  12963. +
  12964. + /* select port for capture */
  12965. + dev->capture.port = port;
  12966. + dev->capture.camera_port = camera_port;
  12967. + dev->capture.encode_component = encode_component;
  12968. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12969. + &dev->v4l2_dev,
  12970. + "Set dev->capture.fmt %08X, %dx%d, stride %d",
  12971. + port->format.encoding,
  12972. + dev->capture.width, dev->capture.height,
  12973. + dev->capture.stride);
  12974. + }
  12975. + }
  12976. +
  12977. + /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
  12978. + return ret;
  12979. +}
  12980. +
  12981. +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  12982. + struct v4l2_format *f)
  12983. +{
  12984. + int ret;
  12985. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12986. + struct mmal_fmt *mfmt;
  12987. +
  12988. + /* try the format to set valid parameters */
  12989. + ret = vidioc_try_fmt_vid_cap(file, priv, f);
  12990. + if (ret) {
  12991. + v4l2_err(&dev->v4l2_dev,
  12992. + "vid_cap - vidioc_try_fmt_vid_cap failed\n");
  12993. + return ret;
  12994. + }
  12995. +
  12996. + /* if a capture is running refuse to set format */
  12997. + if (vb2_is_busy(&dev->capture.vb_vidq)) {
  12998. + v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
  12999. + return -EBUSY;
  13000. + }
  13001. +
  13002. + /* If the format is unsupported v4l2 says we should switch to
  13003. + * a supported one and not return an error. */
  13004. + mfmt = get_format(f);
  13005. + if (!mfmt) {
  13006. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13007. + "Fourcc format (0x%08x) unknown.\n",
  13008. + f->fmt.pix.pixelformat);
  13009. + f->fmt.pix.pixelformat = formats[0].fourcc;
  13010. + mfmt = get_format(f);
  13011. + }
  13012. +
  13013. + ret = mmal_setup_components(dev, f);
  13014. + if (ret != 0)
  13015. + v4l2_err(&dev->v4l2_dev,
  13016. + "%s: failed to setup mmal components: %d\n",
  13017. + __func__, ret);
  13018. +
  13019. + return ret;
  13020. +}
  13021. +
  13022. +/* timeperframe is arbitrary and continous */
  13023. +static int vidioc_enum_frameintervals(struct file *file, void *priv,
  13024. + struct v4l2_frmivalenum *fival)
  13025. +{
  13026. + if (fival->index)
  13027. + return -EINVAL;
  13028. +
  13029. + /* regarding width & height - we support any */
  13030. +
  13031. + fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  13032. +
  13033. + /* fill in stepwise (step=1.0 is requred by V4L2 spec) */
  13034. + fival->stepwise.min = tpf_min;
  13035. + fival->stepwise.max = tpf_max;
  13036. + fival->stepwise.step = (struct v4l2_fract) {1, 1};
  13037. +
  13038. + return 0;
  13039. +}
  13040. +
  13041. +static int vidioc_g_parm(struct file *file, void *priv,
  13042. + struct v4l2_streamparm *parm)
  13043. +{
  13044. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  13045. +
  13046. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  13047. + return -EINVAL;
  13048. +
  13049. + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  13050. + parm->parm.capture.timeperframe = dev->capture.timeperframe;
  13051. + parm->parm.capture.readbuffers = 1;
  13052. + return 0;
  13053. +}
  13054. +
  13055. +#define FRACT_CMP(a, OP, b) \
  13056. + ((u64)(a).numerator * (b).denominator OP \
  13057. + (u64)(b).numerator * (a).denominator)
  13058. +
  13059. +static int vidioc_s_parm(struct file *file, void *priv,
  13060. + struct v4l2_streamparm *parm)
  13061. +{
  13062. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  13063. + struct v4l2_fract tpf;
  13064. + struct mmal_parameter_rational fps_param;
  13065. + int ret;
  13066. +
  13067. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  13068. + return -EINVAL;
  13069. +
  13070. + tpf = parm->parm.capture.timeperframe;
  13071. +
  13072. + /* tpf: {*, 0} resets timing; clip to [min, max]*/
  13073. + tpf = tpf.denominator ? tpf : tpf_default;
  13074. + tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  13075. + tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  13076. +
  13077. + dev->capture.timeperframe = tpf;
  13078. + parm->parm.capture.timeperframe = tpf;
  13079. + parm->parm.capture.readbuffers = 1;
  13080. +
  13081. + fps_param.num = dev->capture.timeperframe.denominator;
  13082. + fps_param.den = dev->capture.timeperframe.numerator;
  13083. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  13084. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13085. + output[MMAL_CAMERA_PORT_PREVIEW],
  13086. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  13087. + &fps_param, sizeof(fps_param));
  13088. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13089. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13090. + output[MMAL_CAMERA_PORT_VIDEO],
  13091. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  13092. + &fps_param, sizeof(fps_param));
  13093. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13094. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13095. + output[MMAL_CAMERA_PORT_CAPTURE],
  13096. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  13097. + &fps_param, sizeof(fps_param));
  13098. + if (ret)
  13099. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13100. + "Failed to set fps ret %d\n",
  13101. + ret);
  13102. +
  13103. + return 0;
  13104. +}
  13105. +
  13106. +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
  13107. + /* overlay */
  13108. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  13109. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  13110. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  13111. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  13112. + .vidioc_overlay = vidioc_overlay,
  13113. + .vidioc_g_fbuf = vidioc_g_fbuf,
  13114. +
  13115. + /* inputs */
  13116. + .vidioc_enum_input = vidioc_enum_input,
  13117. + .vidioc_g_input = vidioc_g_input,
  13118. + .vidioc_s_input = vidioc_s_input,
  13119. +
  13120. + /* capture */
  13121. + .vidioc_querycap = vidioc_querycap,
  13122. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  13123. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  13124. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  13125. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  13126. +
  13127. + /* buffer management */
  13128. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  13129. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  13130. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  13131. + .vidioc_querybuf = vb2_ioctl_querybuf,
  13132. + .vidioc_qbuf = vb2_ioctl_qbuf,
  13133. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  13134. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  13135. + .vidioc_g_parm = vidioc_g_parm,
  13136. + .vidioc_s_parm = vidioc_s_parm,
  13137. + .vidioc_streamon = vb2_ioctl_streamon,
  13138. + .vidioc_streamoff = vb2_ioctl_streamoff,
  13139. +
  13140. + .vidioc_log_status = v4l2_ctrl_log_status,
  13141. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  13142. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  13143. +};
  13144. +
  13145. +/* ------------------------------------------------------------------
  13146. + Driver init/finalise
  13147. + ------------------------------------------------------------------*/
  13148. +
  13149. +static const struct v4l2_file_operations camera0_fops = {
  13150. + .owner = THIS_MODULE,
  13151. + .open = v4l2_fh_open,
  13152. + .release = vb2_fop_release,
  13153. + .read = vb2_fop_read,
  13154. + .poll = vb2_fop_poll,
  13155. + .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  13156. + .mmap = vb2_fop_mmap,
  13157. +};
  13158. +
  13159. +static struct video_device vdev_template = {
  13160. + .name = "camera0",
  13161. + .fops = &camera0_fops,
  13162. + .ioctl_ops = &camera0_ioctl_ops,
  13163. + .release = video_device_release_empty,
  13164. +};
  13165. +
  13166. +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
  13167. + struct vchiq_mmal_component *camera)
  13168. +{
  13169. + int ret;
  13170. + struct mmal_parameter_camera_config cam_config = {
  13171. + .max_stills_w = MAX_WIDTH,
  13172. + .max_stills_h = MAX_HEIGHT,
  13173. + .stills_yuv422 = 1,
  13174. + .one_shot_stills = 1,
  13175. + .max_preview_video_w = 1920,
  13176. + .max_preview_video_h = 1088,
  13177. + .num_preview_video_frames = 3,
  13178. + .stills_capture_circular_buffer_height = 0,
  13179. + .fast_preview_resume = 0,
  13180. + .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
  13181. + };
  13182. +
  13183. + ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
  13184. + MMAL_PARAMETER_CAMERA_CONFIG,
  13185. + &cam_config, sizeof(cam_config));
  13186. + return ret;
  13187. +}
  13188. +
  13189. +/* MMAL instance and component init */
  13190. +static int __init mmal_init(struct bm2835_mmal_dev *dev)
  13191. +{
  13192. + int ret;
  13193. + struct mmal_es_format *format;
  13194. +
  13195. + ret = vchiq_mmal_init(&dev->instance);
  13196. + if (ret < 0)
  13197. + return ret;
  13198. +
  13199. + /* get the camera component ready */
  13200. + ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
  13201. + &dev->component[MMAL_COMPONENT_CAMERA]);
  13202. + if (ret < 0)
  13203. + goto unreg_mmal;
  13204. +
  13205. + if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
  13206. + MMAL_CAMERA_PORT_COUNT) {
  13207. + ret = -EINVAL;
  13208. + goto unreg_camera;
  13209. + }
  13210. +
  13211. + ret = set_camera_parameters(dev->instance,
  13212. + dev->component[MMAL_COMPONENT_CAMERA]);
  13213. + if (ret < 0)
  13214. + goto unreg_camera;
  13215. +
  13216. + format =
  13217. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13218. + output[MMAL_CAMERA_PORT_PREVIEW].format;
  13219. +
  13220. + format->encoding = MMAL_ENCODING_OPAQUE;
  13221. + format->encoding_variant = MMAL_ENCODING_I420;
  13222. +
  13223. + format->es->video.width = 1024;
  13224. + format->es->video.height = 768;
  13225. + format->es->video.crop.x = 0;
  13226. + format->es->video.crop.y = 0;
  13227. + format->es->video.crop.width = 1024;
  13228. + format->es->video.crop.height = 768;
  13229. + format->es->video.frame_rate.num =
  13230. + dev->capture.timeperframe.denominator;
  13231. + format->es->video.frame_rate.den =
  13232. + dev->capture.timeperframe.numerator;
  13233. +
  13234. + format =
  13235. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13236. + output[MMAL_CAMERA_PORT_VIDEO].format;
  13237. +
  13238. + format->encoding = MMAL_ENCODING_OPAQUE;
  13239. + format->encoding_variant = MMAL_ENCODING_I420;
  13240. +
  13241. + format->es->video.width = 1024;
  13242. + format->es->video.height = 768;
  13243. + format->es->video.crop.x = 0;
  13244. + format->es->video.crop.y = 0;
  13245. + format->es->video.crop.width = 1024;
  13246. + format->es->video.crop.height = 768;
  13247. + format->es->video.frame_rate.num =
  13248. + dev->capture.timeperframe.denominator;
  13249. + format->es->video.frame_rate.den =
  13250. + dev->capture.timeperframe.numerator;
  13251. +
  13252. + format =
  13253. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13254. + output[MMAL_CAMERA_PORT_CAPTURE].format;
  13255. +
  13256. + format->encoding = MMAL_ENCODING_OPAQUE;
  13257. +
  13258. + format->es->video.width = 2592;
  13259. + format->es->video.height = 1944;
  13260. + format->es->video.crop.x = 0;
  13261. + format->es->video.crop.y = 0;
  13262. + format->es->video.crop.width = 2592;
  13263. + format->es->video.crop.height = 1944;
  13264. + format->es->video.frame_rate.num = 30;
  13265. + format->es->video.frame_rate.den = 1;
  13266. +
  13267. + dev->capture.width = format->es->video.width;
  13268. + dev->capture.height = format->es->video.height;
  13269. + dev->capture.fmt = &formats[0];
  13270. + dev->capture.encode_component = NULL;
  13271. + dev->capture.timeperframe = tpf_default;
  13272. +
  13273. + /* get the preview component ready */
  13274. + ret = vchiq_mmal_component_init(
  13275. + dev->instance, "ril.video_render",
  13276. + &dev->component[MMAL_COMPONENT_PREVIEW]);
  13277. + if (ret < 0)
  13278. + goto unreg_camera;
  13279. +
  13280. + if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
  13281. + ret = -EINVAL;
  13282. + pr_debug("too few input ports %d needed %d\n",
  13283. + dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
  13284. + goto unreg_preview;
  13285. + }
  13286. +
  13287. + /* get the image encoder component ready */
  13288. + ret = vchiq_mmal_component_init(
  13289. + dev->instance, "ril.image_encode",
  13290. + &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  13291. + if (ret < 0)
  13292. + goto unreg_preview;
  13293. +
  13294. + if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
  13295. + ret = -EINVAL;
  13296. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  13297. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
  13298. + 1);
  13299. + goto unreg_image_encoder;
  13300. + }
  13301. +
  13302. + /* get the video encoder component ready */
  13303. + ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
  13304. + &dev->
  13305. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  13306. + if (ret < 0)
  13307. + goto unreg_image_encoder;
  13308. +
  13309. + if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
  13310. + ret = -EINVAL;
  13311. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  13312. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
  13313. + 1);
  13314. + goto unreg_vid_encoder;
  13315. + }
  13316. +
  13317. + {
  13318. + unsigned int enable = 1;
  13319. + vchiq_mmal_port_parameter_set(
  13320. + dev->instance,
  13321. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  13322. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  13323. + &enable, sizeof(enable));
  13324. +
  13325. + vchiq_mmal_port_parameter_set(dev->instance,
  13326. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  13327. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  13328. + &enable,
  13329. + sizeof(enable));
  13330. + }
  13331. + ret = bm2835_mmal_set_all_camera_controls(dev);
  13332. + if (ret < 0)
  13333. + goto unreg_vid_encoder;
  13334. +
  13335. + return 0;
  13336. +
  13337. +unreg_vid_encoder:
  13338. + pr_err("Cleanup: Destroy video encoder\n");
  13339. + vchiq_mmal_component_finalise(
  13340. + dev->instance,
  13341. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
  13342. +
  13343. +unreg_image_encoder:
  13344. + pr_err("Cleanup: Destroy image encoder\n");
  13345. + vchiq_mmal_component_finalise(
  13346. + dev->instance,
  13347. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  13348. +
  13349. +unreg_preview:
  13350. + pr_err("Cleanup: Destroy video render\n");
  13351. + vchiq_mmal_component_finalise(dev->instance,
  13352. + dev->component[MMAL_COMPONENT_PREVIEW]);
  13353. +
  13354. +unreg_camera:
  13355. + pr_err("Cleanup: Destroy camera\n");
  13356. + vchiq_mmal_component_finalise(dev->instance,
  13357. + dev->component[MMAL_COMPONENT_CAMERA]);
  13358. +
  13359. +unreg_mmal:
  13360. + vchiq_mmal_finalise(dev->instance);
  13361. + return ret;
  13362. +}
  13363. +
  13364. +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
  13365. + struct video_device *vfd)
  13366. +{
  13367. + int ret;
  13368. +
  13369. + *vfd = vdev_template;
  13370. +
  13371. + vfd->v4l2_dev = &dev->v4l2_dev;
  13372. +
  13373. + vfd->lock = &dev->mutex;
  13374. +
  13375. + vfd->queue = &dev->capture.vb_vidq;
  13376. +
  13377. + set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  13378. +
  13379. + /* video device needs to be able to access instance data */
  13380. + video_set_drvdata(vfd, dev);
  13381. +
  13382. + ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  13383. + if (ret < 0)
  13384. + return ret;
  13385. +
  13386. + v4l2_info(vfd->v4l2_dev, "V4L2 device registered as %s\n",
  13387. + video_device_node_name(vfd));
  13388. +
  13389. + return 0;
  13390. +}
  13391. +
  13392. +static struct v4l2_format default_v4l2_format = {
  13393. + .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
  13394. + .fmt.pix.width = 1024,
  13395. + .fmt.pix.bytesperline = 1024 * 3 / 2,
  13396. + .fmt.pix.height = 768,
  13397. + .fmt.pix.sizeimage = 1<<18,
  13398. +};
  13399. +
  13400. +static int __init bm2835_mmal_init(void)
  13401. +{
  13402. + int ret;
  13403. + struct bm2835_mmal_dev *dev;
  13404. + struct vb2_queue *q;
  13405. +
  13406. + dev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  13407. + if (!dev)
  13408. + return -ENOMEM;
  13409. +
  13410. + /* setup device defaults */
  13411. + dev->overlay.w.left = 150;
  13412. + dev->overlay.w.top = 50;
  13413. + dev->overlay.w.width = 1024;
  13414. + dev->overlay.w.height = 768;
  13415. + dev->overlay.clipcount = 0;
  13416. + dev->overlay.field = V4L2_FIELD_NONE;
  13417. +
  13418. + dev->capture.fmt = &formats[3]; /* JPEG */
  13419. +
  13420. + /* v4l device registration */
  13421. + snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  13422. + "%s", BM2835_MMAL_MODULE_NAME);
  13423. + ret = v4l2_device_register(NULL, &dev->v4l2_dev);
  13424. + if (ret)
  13425. + goto free_dev;
  13426. +
  13427. + /* setup v4l controls */
  13428. + ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
  13429. + if (ret < 0)
  13430. + goto unreg_dev;
  13431. + dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  13432. +
  13433. + /* mmal init */
  13434. + ret = mmal_init(dev);
  13435. + if (ret < 0)
  13436. + goto unreg_dev;
  13437. +
  13438. + /* initialize queue */
  13439. + q = &dev->capture.vb_vidq;
  13440. + memset(q, 0, sizeof(*q));
  13441. + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  13442. + q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  13443. + q->drv_priv = dev;
  13444. + q->buf_struct_size = sizeof(struct mmal_buffer);
  13445. + q->ops = &bm2835_mmal_video_qops;
  13446. + q->mem_ops = &vb2_vmalloc_memops;
  13447. + q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  13448. + ret = vb2_queue_init(q);
  13449. + if (ret < 0)
  13450. + goto unreg_dev;
  13451. +
  13452. + /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
  13453. + mutex_init(&dev->mutex);
  13454. +
  13455. + /* initialise video devices */
  13456. + ret = bm2835_mmal_init_device(dev, &dev->vdev);
  13457. + if (ret < 0)
  13458. + goto unreg_dev;
  13459. +
  13460. + ret = mmal_setup_components(dev, &default_v4l2_format);
  13461. + if (ret < 0) {
  13462. + v4l2_err(&dev->v4l2_dev,
  13463. + "%s: could not setup components\n", __func__);
  13464. + goto unreg_dev;
  13465. + }
  13466. +
  13467. + v4l2_info(&dev->v4l2_dev,
  13468. + "Broadcom 2835 MMAL video capture ver %s loaded.\n",
  13469. + BM2835_MMAL_VERSION);
  13470. +
  13471. + gdev = dev;
  13472. + return 0;
  13473. +
  13474. +unreg_dev:
  13475. + v4l2_ctrl_handler_free(&dev->ctrl_handler);
  13476. + v4l2_device_unregister(&dev->v4l2_dev);
  13477. +
  13478. +free_dev:
  13479. + kfree(dev);
  13480. +
  13481. + v4l2_err(&dev->v4l2_dev,
  13482. + "%s: error %d while loading driver\n",
  13483. + BM2835_MMAL_MODULE_NAME, ret);
  13484. +
  13485. + return ret;
  13486. +}
  13487. +
  13488. +static void __exit bm2835_mmal_exit(void)
  13489. +{
  13490. + if (!gdev)
  13491. + return;
  13492. +
  13493. + v4l2_info(&gdev->v4l2_dev, "unregistering %s\n",
  13494. + video_device_node_name(&gdev->vdev));
  13495. +
  13496. + video_unregister_device(&gdev->vdev);
  13497. +
  13498. + if (gdev->capture.encode_component) {
  13499. + v4l2_dbg(1, bcm2835_v4l2_debug, &gdev->v4l2_dev,
  13500. + "mmal_exit - disconnect tunnel\n");
  13501. + vchiq_mmal_port_connect_tunnel(gdev->instance,
  13502. + gdev->capture.camera_port, NULL);
  13503. + vchiq_mmal_component_disable(gdev->instance,
  13504. + gdev->capture.encode_component);
  13505. + }
  13506. + vchiq_mmal_component_disable(gdev->instance,
  13507. + gdev->component[MMAL_COMPONENT_CAMERA]);
  13508. +
  13509. + vchiq_mmal_component_finalise(gdev->instance,
  13510. + gdev->
  13511. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  13512. +
  13513. + vchiq_mmal_component_finalise(gdev->instance,
  13514. + gdev->
  13515. + component[MMAL_COMPONENT_IMAGE_ENCODE]);
  13516. +
  13517. + vchiq_mmal_component_finalise(gdev->instance,
  13518. + gdev->component[MMAL_COMPONENT_PREVIEW]);
  13519. +
  13520. + vchiq_mmal_component_finalise(gdev->instance,
  13521. + gdev->component[MMAL_COMPONENT_CAMERA]);
  13522. +
  13523. + vchiq_mmal_finalise(gdev->instance);
  13524. +
  13525. + v4l2_ctrl_handler_free(&gdev->ctrl_handler);
  13526. +
  13527. + v4l2_device_unregister(&gdev->v4l2_dev);
  13528. +
  13529. + kfree(gdev);
  13530. +}
  13531. +
  13532. +module_init(bm2835_mmal_init);
  13533. +module_exit(bm2835_mmal_exit);
  13534. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/bcm2835-camera.h linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.h
  13535. --- linux-3.12.13/drivers/media/platform/bcm2835/bcm2835-camera.h 1970-01-01 01:00:00.000000000 +0100
  13536. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.h 2014-03-11 17:51:15.000000000 +0100
  13537. @@ -0,0 +1,113 @@
  13538. +/*
  13539. + * Broadcom BM2835 V4L2 driver
  13540. + *
  13541. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  13542. + *
  13543. + * This file is subject to the terms and conditions of the GNU General Public
  13544. + * License. See the file COPYING in the main directory of this archive
  13545. + * for more details.
  13546. + *
  13547. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  13548. + * Dave Stevenson <dsteve@broadcom.com>
  13549. + * Simon Mellor <simellor@broadcom.com>
  13550. + * Luke Diamand <luked@broadcom.com>
  13551. + *
  13552. + * core driver device
  13553. + */
  13554. +
  13555. +#define V4L2_CTRL_COUNT 21 /* number of v4l controls */
  13556. +
  13557. +enum {
  13558. + MMAL_COMPONENT_CAMERA = 0,
  13559. + MMAL_COMPONENT_PREVIEW,
  13560. + MMAL_COMPONENT_IMAGE_ENCODE,
  13561. + MMAL_COMPONENT_VIDEO_ENCODE,
  13562. + MMAL_COMPONENT_COUNT
  13563. +};
  13564. +
  13565. +enum {
  13566. + MMAL_CAMERA_PORT_PREVIEW = 0,
  13567. + MMAL_CAMERA_PORT_VIDEO,
  13568. + MMAL_CAMERA_PORT_CAPTURE,
  13569. + MMAL_CAMERA_PORT_COUNT
  13570. +};
  13571. +
  13572. +#define PREVIEW_LAYER 2
  13573. +
  13574. +extern int bcm2835_v4l2_debug;
  13575. +
  13576. +struct bm2835_mmal_dev {
  13577. + /* v4l2 devices */
  13578. + struct v4l2_device v4l2_dev;
  13579. + struct video_device vdev;
  13580. + struct mutex mutex;
  13581. +
  13582. + /* controls */
  13583. + struct v4l2_ctrl_handler ctrl_handler;
  13584. + struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
  13585. + struct mmal_colourfx colourfx;
  13586. + int hflip;
  13587. + int vflip;
  13588. + enum mmal_parameter_exposuremode exposure_mode;
  13589. + unsigned int manual_shutter_speed;
  13590. +
  13591. + /* allocated mmal instance and components */
  13592. + struct vchiq_mmal_instance *instance;
  13593. + struct vchiq_mmal_component *component[MMAL_COMPONENT_COUNT];
  13594. + int camera_use_count;
  13595. +
  13596. + struct v4l2_window overlay;
  13597. +
  13598. + struct {
  13599. + unsigned int width; /* width */
  13600. + unsigned int height; /* height */
  13601. + unsigned int stride; /* stride */
  13602. + struct mmal_fmt *fmt;
  13603. + struct v4l2_fract timeperframe;
  13604. +
  13605. + /* H264 encode bitrate */
  13606. + int encode_bitrate;
  13607. + /* H264 bitrate mode. CBR/VBR */
  13608. + int encode_bitrate_mode;
  13609. + /* JPEG Q-factor */
  13610. + int q_factor;
  13611. +
  13612. + struct vb2_queue vb_vidq;
  13613. +
  13614. + /* VC start timestamp for streaming */
  13615. + s64 vc_start_timestamp;
  13616. + /* Kernel start timestamp for streaming */
  13617. + struct timeval kernel_start_ts;
  13618. +
  13619. + struct vchiq_mmal_port *port; /* port being used for capture */
  13620. + /* camera port being used for capture */
  13621. + struct vchiq_mmal_port *camera_port;
  13622. + /* component being used for encode */
  13623. + struct vchiq_mmal_component *encode_component;
  13624. + /* number of frames remaining which driver should capture */
  13625. + unsigned int frame_count;
  13626. + /* last frame completion */
  13627. + struct completion frame_cmplt;
  13628. +
  13629. + } capture;
  13630. +
  13631. +};
  13632. +
  13633. +int bm2835_mmal_init_controls(
  13634. + struct bm2835_mmal_dev *dev,
  13635. + struct v4l2_ctrl_handler *hdl);
  13636. +
  13637. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
  13638. +
  13639. +
  13640. +/* Debug helpers */
  13641. +
  13642. +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
  13643. +{ \
  13644. + v4l2_dbg(level, debug, dev, \
  13645. +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
  13646. + desc == NULL ? "" : desc, \
  13647. + (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field, \
  13648. + (pix_fmt)->pixelformat, (pix_fmt)->bytesperline, \
  13649. + (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
  13650. +}
  13651. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/controls.c linux-raspberry-pi/drivers/media/platform/bcm2835/controls.c
  13652. --- linux-3.12.13/drivers/media/platform/bcm2835/controls.c 1970-01-01 01:00:00.000000000 +0100
  13653. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/controls.c 2014-03-11 17:51:15.000000000 +0100
  13654. @@ -0,0 +1,902 @@
  13655. +/*
  13656. + * Broadcom BM2835 V4L2 driver
  13657. + *
  13658. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  13659. + *
  13660. + * This file is subject to the terms and conditions of the GNU General Public
  13661. + * License. See the file COPYING in the main directory of this archive
  13662. + * for more details.
  13663. + *
  13664. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  13665. + * Dave Stevenson <dsteve@broadcom.com>
  13666. + * Simon Mellor <simellor@broadcom.com>
  13667. + * Luke Diamand <luked@broadcom.com>
  13668. + */
  13669. +
  13670. +#include <linux/errno.h>
  13671. +#include <linux/kernel.h>
  13672. +#include <linux/module.h>
  13673. +#include <linux/slab.h>
  13674. +#include <media/videobuf2-vmalloc.h>
  13675. +#include <media/v4l2-device.h>
  13676. +#include <media/v4l2-ioctl.h>
  13677. +#include <media/v4l2-ctrls.h>
  13678. +#include <media/v4l2-fh.h>
  13679. +#include <media/v4l2-event.h>
  13680. +#include <media/v4l2-common.h>
  13681. +
  13682. +#include "mmal-common.h"
  13683. +#include "mmal-vchiq.h"
  13684. +#include "mmal-parameters.h"
  13685. +#include "bcm2835-camera.h"
  13686. +
  13687. +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
  13688. + * MMAL values are in 1/6th increments so the MMAL range is -24 to +24.
  13689. + * V4L2 docs say value "is expressed in terms of EV, drivers should interpret
  13690. + * the values as 0.001 EV units, where the value 1000 stands for +1 EV."
  13691. + * V4L2 is limited to a max of 32 values in a menu, so count in 1/3rds from
  13692. + * -4 to +4
  13693. + */
  13694. +static const s64 ev_bias_qmenu[] = {
  13695. + -4000, -3667, -3333,
  13696. + -3000, -2667, -2333,
  13697. + -2000, -1667, -1333,
  13698. + -1000, -667, -333,
  13699. + 0, 333, 667,
  13700. + 1000, 1333, 1667,
  13701. + 2000, 2333, 2667,
  13702. + 3000, 3333, 3667,
  13703. + 4000
  13704. +};
  13705. +
  13706. +/* Supported ISO values
  13707. + * ISOO = auto ISO
  13708. + */
  13709. +static const s64 iso_qmenu[] = {
  13710. + 0, 100, 200, 400, 800,
  13711. +};
  13712. +
  13713. +static const s64 mains_freq_qmenu[] = {
  13714. + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
  13715. + V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
  13716. + V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  13717. + V4L2_CID_POWER_LINE_FREQUENCY_AUTO
  13718. +};
  13719. +
  13720. +/* Supported video encode modes */
  13721. +static const s64 bitrate_mode_qmenu[] = {
  13722. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  13723. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  13724. +};
  13725. +
  13726. +
  13727. +enum bm2835_mmal_ctrl_type {
  13728. + MMAL_CONTROL_TYPE_STD,
  13729. + MMAL_CONTROL_TYPE_STD_MENU,
  13730. + MMAL_CONTROL_TYPE_INT_MENU,
  13731. + MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
  13732. +};
  13733. +
  13734. +struct bm2835_mmal_v4l2_ctrl;
  13735. +
  13736. +typedef int(bm2835_mmal_v4l2_ctrl_cb)(
  13737. + struct bm2835_mmal_dev *dev,
  13738. + struct v4l2_ctrl *ctrl,
  13739. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
  13740. +
  13741. +struct bm2835_mmal_v4l2_ctrl {
  13742. + u32 id; /* v4l2 control identifier */
  13743. + enum bm2835_mmal_ctrl_type type;
  13744. + /* control minimum value or
  13745. + * mask for MMAL_CONTROL_TYPE_STD_MENU */
  13746. + s32 min;
  13747. + s32 max; /* maximum value of control */
  13748. + s32 def; /* default value of control */
  13749. + s32 step; /* step size of the control */
  13750. + const s64 *imenu; /* integer menu array */
  13751. + u32 mmal_id; /* mmal parameter id */
  13752. + bm2835_mmal_v4l2_ctrl_cb *setter;
  13753. + bool ignore_errors;
  13754. +};
  13755. +
  13756. +struct v4l2_to_mmal_effects_setting {
  13757. + u32 v4l2_effect;
  13758. + u32 mmal_effect;
  13759. + s32 col_fx_enable;
  13760. + s32 col_fx_fixed_cbcr;
  13761. + u32 u;
  13762. + u32 v;
  13763. + u32 num_effect_params;
  13764. + u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
  13765. +};
  13766. +
  13767. +static const struct v4l2_to_mmal_effects_setting
  13768. + v4l2_to_mmal_effects_values[] = {
  13769. + { V4L2_COLORFX_NONE, MMAL_PARAM_IMAGEFX_NONE,
  13770. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13771. + { V4L2_COLORFX_BW, MMAL_PARAM_IMAGEFX_NONE,
  13772. + 1, 0, 128, 128, 0, {0, 0, 0, 0, 0} },
  13773. + { V4L2_COLORFX_SEPIA, MMAL_PARAM_IMAGEFX_NONE,
  13774. + 1, 0, 87, 151, 0, {0, 0, 0, 0, 0} },
  13775. + { V4L2_COLORFX_NEGATIVE, MMAL_PARAM_IMAGEFX_NEGATIVE,
  13776. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13777. + { V4L2_COLORFX_EMBOSS, MMAL_PARAM_IMAGEFX_EMBOSS,
  13778. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13779. + { V4L2_COLORFX_SKETCH, MMAL_PARAM_IMAGEFX_SKETCH,
  13780. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13781. + { V4L2_COLORFX_SKY_BLUE, MMAL_PARAM_IMAGEFX_PASTEL,
  13782. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13783. + { V4L2_COLORFX_GRASS_GREEN, MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  13784. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13785. + { V4L2_COLORFX_SKIN_WHITEN, MMAL_PARAM_IMAGEFX_WASHEDOUT,
  13786. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13787. + { V4L2_COLORFX_VIVID, MMAL_PARAM_IMAGEFX_SATURATION,
  13788. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13789. + { V4L2_COLORFX_AQUA, MMAL_PARAM_IMAGEFX_NONE,
  13790. + 1, 0, 171, 121, 0, {0, 0, 0, 0, 0} },
  13791. + { V4L2_COLORFX_ART_FREEZE, MMAL_PARAM_IMAGEFX_HATCH,
  13792. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13793. + { V4L2_COLORFX_SILHOUETTE, MMAL_PARAM_IMAGEFX_FILM,
  13794. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13795. + { V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
  13796. + 0, 0, 0, 0, 5, {1, 128, 160, 160, 48} },
  13797. + { V4L2_COLORFX_ANTIQUE, MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  13798. + 0, 0, 0, 0, 3, {108, 274, 238, 0, 0} },
  13799. + { V4L2_COLORFX_SET_CBCR, MMAL_PARAM_IMAGEFX_NONE,
  13800. + 1, 1, 0, 0, 0, {0, 0, 0, 0, 0} }
  13801. +};
  13802. +
  13803. +
  13804. +/* control handlers*/
  13805. +
  13806. +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
  13807. + struct v4l2_ctrl *ctrl,
  13808. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13809. +{
  13810. + struct mmal_parameter_rational rational_value;
  13811. + struct vchiq_mmal_port *control;
  13812. +
  13813. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13814. +
  13815. + rational_value.num = ctrl->val;
  13816. + rational_value.den = 100;
  13817. +
  13818. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13819. + mmal_ctrl->mmal_id,
  13820. + &rational_value,
  13821. + sizeof(rational_value));
  13822. +}
  13823. +
  13824. +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
  13825. + struct v4l2_ctrl *ctrl,
  13826. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13827. +{
  13828. + u32 u32_value;
  13829. + struct vchiq_mmal_port *control;
  13830. +
  13831. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13832. +
  13833. + u32_value = ctrl->val;
  13834. +
  13835. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13836. + mmal_ctrl->mmal_id,
  13837. + &u32_value, sizeof(u32_value));
  13838. +}
  13839. +
  13840. +static int ctrl_set_value_menu(struct bm2835_mmal_dev *dev,
  13841. + struct v4l2_ctrl *ctrl,
  13842. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13843. +{
  13844. + u32 u32_value;
  13845. + struct vchiq_mmal_port *control;
  13846. +
  13847. + if (ctrl->val > mmal_ctrl->max || ctrl->val < mmal_ctrl->min)
  13848. + return 1;
  13849. +
  13850. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13851. +
  13852. + u32_value = mmal_ctrl->imenu[ctrl->val];
  13853. +
  13854. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13855. + mmal_ctrl->mmal_id,
  13856. + &u32_value, sizeof(u32_value));
  13857. +}
  13858. +
  13859. +static int ctrl_set_value_ev(struct bm2835_mmal_dev *dev,
  13860. + struct v4l2_ctrl *ctrl,
  13861. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13862. +{
  13863. + s32 s32_value;
  13864. + struct vchiq_mmal_port *control;
  13865. +
  13866. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13867. +
  13868. + s32_value = (ctrl->val-12)*2; /* Convert from index to 1/6ths */
  13869. +
  13870. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13871. + mmal_ctrl->mmal_id,
  13872. + &s32_value, sizeof(s32_value));
  13873. +}
  13874. +
  13875. +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
  13876. + struct v4l2_ctrl *ctrl,
  13877. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13878. +{
  13879. + int ret;
  13880. + u32 u32_value;
  13881. + struct vchiq_mmal_component *camera;
  13882. +
  13883. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  13884. +
  13885. + u32_value = ((ctrl->val % 360) / 90) * 90;
  13886. +
  13887. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  13888. + mmal_ctrl->mmal_id,
  13889. + &u32_value, sizeof(u32_value));
  13890. + if (ret < 0)
  13891. + return ret;
  13892. +
  13893. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  13894. + mmal_ctrl->mmal_id,
  13895. + &u32_value, sizeof(u32_value));
  13896. + if (ret < 0)
  13897. + return ret;
  13898. +
  13899. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  13900. + mmal_ctrl->mmal_id,
  13901. + &u32_value, sizeof(u32_value));
  13902. +
  13903. + return ret;
  13904. +}
  13905. +
  13906. +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
  13907. + struct v4l2_ctrl *ctrl,
  13908. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13909. +{
  13910. + int ret;
  13911. + u32 u32_value;
  13912. + struct vchiq_mmal_component *camera;
  13913. +
  13914. + if (ctrl->id == V4L2_CID_HFLIP)
  13915. + dev->hflip = ctrl->val;
  13916. + else
  13917. + dev->vflip = ctrl->val;
  13918. +
  13919. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  13920. +
  13921. + if (dev->hflip && dev->vflip)
  13922. + u32_value = MMAL_PARAM_MIRROR_BOTH;
  13923. + else if (dev->hflip)
  13924. + u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
  13925. + else if (dev->vflip)
  13926. + u32_value = MMAL_PARAM_MIRROR_VERTICAL;
  13927. + else
  13928. + u32_value = MMAL_PARAM_MIRROR_NONE;
  13929. +
  13930. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  13931. + mmal_ctrl->mmal_id,
  13932. + &u32_value, sizeof(u32_value));
  13933. + if (ret < 0)
  13934. + return ret;
  13935. +
  13936. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  13937. + mmal_ctrl->mmal_id,
  13938. + &u32_value, sizeof(u32_value));
  13939. + if (ret < 0)
  13940. + return ret;
  13941. +
  13942. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  13943. + mmal_ctrl->mmal_id,
  13944. + &u32_value, sizeof(u32_value));
  13945. +
  13946. + return ret;
  13947. +
  13948. +}
  13949. +
  13950. +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
  13951. + struct v4l2_ctrl *ctrl,
  13952. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13953. +{
  13954. + enum mmal_parameter_exposuremode exp_mode = dev->exposure_mode;
  13955. + u32 shutter_speed = 0;
  13956. + struct vchiq_mmal_port *control;
  13957. + int ret = 0;
  13958. +
  13959. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13960. +
  13961. + if (mmal_ctrl->mmal_id == MMAL_PARAMETER_SHUTTER_SPEED) {
  13962. + /* V4L2 is in 100usec increments.
  13963. + * MMAL is 1usec.
  13964. + */
  13965. + dev->manual_shutter_speed = ctrl->val * 100;
  13966. + } else if (mmal_ctrl->mmal_id == MMAL_PARAMETER_EXPOSURE_MODE) {
  13967. + switch (ctrl->val) {
  13968. + case V4L2_EXPOSURE_AUTO:
  13969. + exp_mode = MMAL_PARAM_EXPOSUREMODE_AUTO;
  13970. + break;
  13971. +
  13972. + case V4L2_EXPOSURE_MANUAL:
  13973. + exp_mode = MMAL_PARAM_EXPOSUREMODE_OFF;
  13974. + break;
  13975. +
  13976. + case V4L2_EXPOSURE_SHUTTER_PRIORITY:
  13977. + exp_mode = MMAL_PARAM_EXPOSUREMODE_SPORTS;
  13978. + break;
  13979. +
  13980. + case V4L2_EXPOSURE_APERTURE_PRIORITY:
  13981. + exp_mode = MMAL_PARAM_EXPOSUREMODE_NIGHT;
  13982. + break;
  13983. +
  13984. + }
  13985. + dev->exposure_mode = exp_mode;
  13986. + }
  13987. +
  13988. + if (dev->exposure_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  13989. + shutter_speed = dev->manual_shutter_speed;
  13990. +
  13991. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  13992. + MMAL_PARAMETER_SHUTTER_SPEED,
  13993. + &shutter_speed, sizeof(shutter_speed));
  13994. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  13995. + MMAL_PARAMETER_EXPOSURE_MODE,
  13996. + &exp_mode, sizeof(u32));
  13997. + return ret;
  13998. +}
  13999. +
  14000. +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
  14001. + struct v4l2_ctrl *ctrl,
  14002. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14003. +{
  14004. + u32 u32_value;
  14005. + struct vchiq_mmal_port *control;
  14006. +
  14007. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  14008. +
  14009. + switch (ctrl->val) {
  14010. + case V4L2_EXPOSURE_METERING_AVERAGE:
  14011. + u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  14012. + break;
  14013. +
  14014. + case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
  14015. + u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
  14016. + break;
  14017. +
  14018. + case V4L2_EXPOSURE_METERING_SPOT:
  14019. + u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
  14020. + break;
  14021. +
  14022. + /* todo matrix weighting not added to Linux API till 3.9
  14023. + case V4L2_EXPOSURE_METERING_MATRIX:
  14024. + u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
  14025. + break;
  14026. + */
  14027. +
  14028. + }
  14029. +
  14030. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  14031. + mmal_ctrl->mmal_id,
  14032. + &u32_value, sizeof(u32_value));
  14033. +}
  14034. +
  14035. +static int ctrl_set_flicker_avoidance(struct bm2835_mmal_dev *dev,
  14036. + struct v4l2_ctrl *ctrl,
  14037. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14038. +{
  14039. + u32 u32_value;
  14040. + struct vchiq_mmal_port *control;
  14041. +
  14042. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  14043. +
  14044. + switch (ctrl->val) {
  14045. + case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  14046. + u32_value = MMAL_PARAM_FLICKERAVOID_OFF;
  14047. + break;
  14048. + case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  14049. + u32_value = MMAL_PARAM_FLICKERAVOID_50HZ;
  14050. + break;
  14051. + case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  14052. + u32_value = MMAL_PARAM_FLICKERAVOID_60HZ;
  14053. + break;
  14054. + case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
  14055. + u32_value = MMAL_PARAM_FLICKERAVOID_AUTO;
  14056. + break;
  14057. + }
  14058. +
  14059. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  14060. + mmal_ctrl->mmal_id,
  14061. + &u32_value, sizeof(u32_value));
  14062. +}
  14063. +
  14064. +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
  14065. + struct v4l2_ctrl *ctrl,
  14066. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14067. +{
  14068. + u32 u32_value;
  14069. + struct vchiq_mmal_port *control;
  14070. +
  14071. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  14072. +
  14073. + switch (ctrl->val) {
  14074. + case V4L2_WHITE_BALANCE_MANUAL:
  14075. + u32_value = MMAL_PARAM_AWBMODE_OFF;
  14076. + break;
  14077. +
  14078. + case V4L2_WHITE_BALANCE_AUTO:
  14079. + u32_value = MMAL_PARAM_AWBMODE_AUTO;
  14080. + break;
  14081. +
  14082. + case V4L2_WHITE_BALANCE_INCANDESCENT:
  14083. + u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
  14084. + break;
  14085. +
  14086. + case V4L2_WHITE_BALANCE_FLUORESCENT:
  14087. + u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
  14088. + break;
  14089. +
  14090. + case V4L2_WHITE_BALANCE_FLUORESCENT_H:
  14091. + u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
  14092. + break;
  14093. +
  14094. + case V4L2_WHITE_BALANCE_HORIZON:
  14095. + u32_value = MMAL_PARAM_AWBMODE_HORIZON;
  14096. + break;
  14097. +
  14098. + case V4L2_WHITE_BALANCE_DAYLIGHT:
  14099. + u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
  14100. + break;
  14101. +
  14102. + case V4L2_WHITE_BALANCE_FLASH:
  14103. + u32_value = MMAL_PARAM_AWBMODE_FLASH;
  14104. + break;
  14105. +
  14106. + case V4L2_WHITE_BALANCE_CLOUDY:
  14107. + u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
  14108. + break;
  14109. +
  14110. + case V4L2_WHITE_BALANCE_SHADE:
  14111. + u32_value = MMAL_PARAM_AWBMODE_SHADE;
  14112. + break;
  14113. +
  14114. + }
  14115. +
  14116. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  14117. + mmal_ctrl->mmal_id,
  14118. + &u32_value, sizeof(u32_value));
  14119. +}
  14120. +
  14121. +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
  14122. + struct v4l2_ctrl *ctrl,
  14123. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14124. +{
  14125. + int ret = -EINVAL;
  14126. + int i, j;
  14127. + struct vchiq_mmal_port *control;
  14128. + struct mmal_parameter_imagefx_parameters imagefx;
  14129. +
  14130. + for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
  14131. + if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
  14132. +
  14133. + imagefx.effect =
  14134. + v4l2_to_mmal_effects_values[i].mmal_effect;
  14135. + imagefx.num_effect_params =
  14136. + v4l2_to_mmal_effects_values[i].num_effect_params;
  14137. +
  14138. + if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
  14139. + imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
  14140. +
  14141. + for (j = 0; j < imagefx.num_effect_params; j++)
  14142. + imagefx.effect_parameter[j] =
  14143. + v4l2_to_mmal_effects_values[i].effect_params[j];
  14144. +
  14145. + dev->colourfx.enable =
  14146. + v4l2_to_mmal_effects_values[i].col_fx_enable;
  14147. + if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
  14148. + dev->colourfx.u =
  14149. + v4l2_to_mmal_effects_values[i].u;
  14150. + dev->colourfx.v =
  14151. + v4l2_to_mmal_effects_values[i].v;
  14152. + }
  14153. +
  14154. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  14155. +
  14156. + ret = vchiq_mmal_port_parameter_set(
  14157. + dev->instance, control,
  14158. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  14159. + &imagefx, sizeof(imagefx));
  14160. + if (ret)
  14161. + goto exit;
  14162. +
  14163. + ret = vchiq_mmal_port_parameter_set(
  14164. + dev->instance, control,
  14165. + MMAL_PARAMETER_COLOUR_EFFECT,
  14166. + &dev->colourfx, sizeof(dev->colourfx));
  14167. + }
  14168. + }
  14169. +
  14170. +exit:
  14171. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14172. + "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
  14173. + mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
  14174. + dev->colourfx.enable ? "true" : "false",
  14175. + dev->colourfx.u, dev->colourfx.v,
  14176. + ret, (ret == 0 ? 0 : -EINVAL));
  14177. + return (ret == 0 ? 0 : EINVAL);
  14178. +}
  14179. +
  14180. +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
  14181. + struct v4l2_ctrl *ctrl,
  14182. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14183. +{
  14184. + int ret = -EINVAL;
  14185. + struct vchiq_mmal_port *control;
  14186. +
  14187. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  14188. +
  14189. + dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
  14190. + dev->colourfx.enable = ctrl->val & 0xff;
  14191. +
  14192. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  14193. + MMAL_PARAMETER_COLOUR_EFFECT,
  14194. + &dev->colourfx, sizeof(dev->colourfx));
  14195. +
  14196. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14197. + "After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
  14198. + mmal_ctrl, ctrl->id, ctrl->val, ret,
  14199. + (ret == 0 ? 0 : -EINVAL));
  14200. + return (ret == 0 ? 0 : EINVAL);
  14201. +}
  14202. +
  14203. +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
  14204. + struct v4l2_ctrl *ctrl,
  14205. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14206. +{
  14207. + int ret;
  14208. + struct vchiq_mmal_port *encoder_out;
  14209. +
  14210. + dev->capture.encode_bitrate = ctrl->val;
  14211. +
  14212. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  14213. +
  14214. + ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  14215. + mmal_ctrl->mmal_id,
  14216. + &ctrl->val, sizeof(ctrl->val));
  14217. + ret = 0;
  14218. + return ret;
  14219. +}
  14220. +
  14221. +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
  14222. + struct v4l2_ctrl *ctrl,
  14223. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14224. +{
  14225. + u32 bitrate_mode;
  14226. + struct vchiq_mmal_port *encoder_out;
  14227. +
  14228. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  14229. +
  14230. + dev->capture.encode_bitrate_mode = ctrl->val;
  14231. + switch (ctrl->val) {
  14232. + default:
  14233. + case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
  14234. + bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
  14235. + break;
  14236. + case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
  14237. + bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
  14238. + break;
  14239. + }
  14240. +
  14241. + vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  14242. + mmal_ctrl->mmal_id,
  14243. + &bitrate_mode,
  14244. + sizeof(bitrate_mode));
  14245. + return 0;
  14246. +}
  14247. +
  14248. +static int ctrl_set_image_encode_output(struct bm2835_mmal_dev *dev,
  14249. + struct v4l2_ctrl *ctrl,
  14250. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14251. +{
  14252. + u32 u32_value;
  14253. + struct vchiq_mmal_port *jpeg_out;
  14254. +
  14255. + jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  14256. +
  14257. + u32_value = ctrl->val;
  14258. +
  14259. + return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
  14260. + mmal_ctrl->mmal_id,
  14261. + &u32_value, sizeof(u32_value));
  14262. +}
  14263. +
  14264. +static int ctrl_set_video_encode_param_output(struct bm2835_mmal_dev *dev,
  14265. + struct v4l2_ctrl *ctrl,
  14266. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14267. +{
  14268. + u32 u32_value;
  14269. + struct vchiq_mmal_port *vid_enc_ctl;
  14270. +
  14271. + vid_enc_ctl = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  14272. +
  14273. + u32_value = ctrl->val;
  14274. +
  14275. + return vchiq_mmal_port_parameter_set(dev->instance, vid_enc_ctl,
  14276. + mmal_ctrl->mmal_id,
  14277. + &u32_value, sizeof(u32_value));
  14278. +}
  14279. +
  14280. +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
  14281. +{
  14282. + struct bm2835_mmal_dev *dev =
  14283. + container_of(ctrl->handler, struct bm2835_mmal_dev,
  14284. + ctrl_handler);
  14285. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
  14286. + int ret;
  14287. +
  14288. + if ((mmal_ctrl == NULL) ||
  14289. + (mmal_ctrl->id != ctrl->id) ||
  14290. + (mmal_ctrl->setter == NULL)) {
  14291. + pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
  14292. + return -EINVAL;
  14293. + }
  14294. +
  14295. + ret = mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
  14296. + if (mmal_ctrl->ignore_errors)
  14297. + ret = 0;
  14298. + return ret;
  14299. +}
  14300. +
  14301. +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
  14302. + .s_ctrl = bm2835_mmal_s_ctrl,
  14303. +};
  14304. +
  14305. +
  14306. +
  14307. +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
  14308. + {
  14309. + V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
  14310. + -100, 100, 0, 1, NULL,
  14311. + MMAL_PARAMETER_SATURATION,
  14312. + &ctrl_set_rational,
  14313. + false
  14314. + },
  14315. + {
  14316. + V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
  14317. + -100, 100, 0, 1, NULL,
  14318. + MMAL_PARAMETER_SHARPNESS,
  14319. + &ctrl_set_rational,
  14320. + false
  14321. + },
  14322. + {
  14323. + V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
  14324. + -100, 100, 0, 1, NULL,
  14325. + MMAL_PARAMETER_CONTRAST,
  14326. + &ctrl_set_rational,
  14327. + false
  14328. + },
  14329. + {
  14330. + V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
  14331. + 0, 100, 50, 1, NULL,
  14332. + MMAL_PARAMETER_BRIGHTNESS,
  14333. + &ctrl_set_rational,
  14334. + false
  14335. + },
  14336. + {
  14337. + V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
  14338. + 0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
  14339. + MMAL_PARAMETER_ISO,
  14340. + &ctrl_set_value_menu,
  14341. + false
  14342. + },
  14343. + {
  14344. + V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
  14345. + 0, 1, 0, 1, NULL,
  14346. + MMAL_PARAMETER_VIDEO_STABILISATION,
  14347. + &ctrl_set_value,
  14348. + false
  14349. + },
  14350. +/* {
  14351. + 0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
  14352. + },
  14353. +*/ {
  14354. + V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
  14355. + ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
  14356. + MMAL_PARAMETER_EXPOSURE_MODE,
  14357. + &ctrl_set_exposure,
  14358. + false
  14359. + },
  14360. +/* todo this needs mixing in with set exposure
  14361. + {
  14362. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  14363. + },
  14364. + */
  14365. + {
  14366. + V4L2_CID_EXPOSURE_ABSOLUTE, MMAL_CONTROL_TYPE_STD,
  14367. + /* Units of 100usecs */
  14368. + 1, 1*1000*10, 100*10, 1, NULL,
  14369. + MMAL_PARAMETER_SHUTTER_SPEED,
  14370. + &ctrl_set_exposure,
  14371. + false
  14372. + },
  14373. + {
  14374. + V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
  14375. + 0, ARRAY_SIZE(ev_bias_qmenu) - 1,
  14376. + (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
  14377. + MMAL_PARAMETER_EXPOSURE_COMP,
  14378. + &ctrl_set_value_ev,
  14379. + false
  14380. + },
  14381. + {
  14382. + V4L2_CID_EXPOSURE_METERING,
  14383. + MMAL_CONTROL_TYPE_STD_MENU,
  14384. + ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
  14385. + MMAL_PARAMETER_EXP_METERING_MODE,
  14386. + &ctrl_set_metering_mode,
  14387. + false
  14388. + },
  14389. + {
  14390. + V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
  14391. + MMAL_CONTROL_TYPE_STD_MENU,
  14392. + ~0x3fe, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
  14393. + MMAL_PARAMETER_AWB_MODE,
  14394. + &ctrl_set_awb_mode,
  14395. + false
  14396. + },
  14397. + {
  14398. + V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
  14399. + 0, 15, V4L2_COLORFX_NONE, 0, NULL,
  14400. + MMAL_PARAMETER_IMAGE_EFFECT,
  14401. + &ctrl_set_image_effect,
  14402. + false
  14403. + },
  14404. + {
  14405. + V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
  14406. + 0, 0xffff, 0x8080, 1, NULL,
  14407. + MMAL_PARAMETER_COLOUR_EFFECT,
  14408. + &ctrl_set_colfx,
  14409. + false
  14410. + },
  14411. + {
  14412. + V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
  14413. + 0, 360, 0, 90, NULL,
  14414. + MMAL_PARAMETER_ROTATION,
  14415. + &ctrl_set_rotate,
  14416. + false
  14417. + },
  14418. + {
  14419. + V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
  14420. + 0, 1, 0, 1, NULL,
  14421. + MMAL_PARAMETER_MIRROR,
  14422. + &ctrl_set_flip,
  14423. + false
  14424. + },
  14425. + {
  14426. + V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
  14427. + 0, 1, 0, 1, NULL,
  14428. + MMAL_PARAMETER_MIRROR,
  14429. + &ctrl_set_flip,
  14430. + false
  14431. + },
  14432. + {
  14433. + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  14434. + 0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
  14435. + 0, 0, bitrate_mode_qmenu,
  14436. + MMAL_PARAMETER_RATECONTROL,
  14437. + &ctrl_set_bitrate_mode,
  14438. + false
  14439. + },
  14440. + {
  14441. + V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
  14442. + 25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
  14443. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  14444. + &ctrl_set_bitrate,
  14445. + false
  14446. + },
  14447. + {
  14448. + V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
  14449. + 1, 100,
  14450. + 30, 1, NULL,
  14451. + MMAL_PARAMETER_JPEG_Q_FACTOR,
  14452. + &ctrl_set_image_encode_output,
  14453. + false
  14454. + },
  14455. + {
  14456. + V4L2_CID_POWER_LINE_FREQUENCY, MMAL_CONTROL_TYPE_STD_MENU,
  14457. + 0, ARRAY_SIZE(mains_freq_qmenu) - 1,
  14458. + 1, 1, NULL,
  14459. + MMAL_PARAMETER_FLICKER_AVOID,
  14460. + &ctrl_set_flicker_avoidance,
  14461. + false
  14462. + },
  14463. + {
  14464. + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, MMAL_CONTROL_TYPE_STD,
  14465. + 0, 1,
  14466. + 0, 1, NULL,
  14467. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
  14468. + &ctrl_set_video_encode_param_output,
  14469. + true /* Errors ignored as requires latest firmware to work */
  14470. + },
  14471. +};
  14472. +
  14473. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
  14474. +{
  14475. + int c;
  14476. + int ret = 0;
  14477. +
  14478. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  14479. + if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
  14480. + ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
  14481. + &v4l2_ctrls[c]);
  14482. + if (!v4l2_ctrls[c]. ignore_errors && ret)
  14483. + break;
  14484. + }
  14485. + }
  14486. + return ret;
  14487. +}
  14488. +
  14489. +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
  14490. + struct v4l2_ctrl_handler *hdl)
  14491. +{
  14492. + int c;
  14493. + const struct bm2835_mmal_v4l2_ctrl *ctrl;
  14494. +
  14495. + v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
  14496. +
  14497. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  14498. + ctrl = &v4l2_ctrls[c];
  14499. +
  14500. + switch (ctrl->type) {
  14501. + case MMAL_CONTROL_TYPE_STD:
  14502. + dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
  14503. + &bm2835_mmal_ctrl_ops, ctrl->id,
  14504. + ctrl->min, ctrl->max, ctrl->step, ctrl->def);
  14505. + break;
  14506. +
  14507. + case MMAL_CONTROL_TYPE_STD_MENU:
  14508. + dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
  14509. + &bm2835_mmal_ctrl_ops, ctrl->id,
  14510. + ctrl->max, ctrl->min, ctrl->def);
  14511. + break;
  14512. +
  14513. + case MMAL_CONTROL_TYPE_INT_MENU:
  14514. + dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
  14515. + &bm2835_mmal_ctrl_ops, ctrl->id,
  14516. + ctrl->max, ctrl->def, ctrl->imenu);
  14517. + break;
  14518. +
  14519. + case MMAL_CONTROL_TYPE_CLUSTER:
  14520. + /* skip this entry when constructing controls */
  14521. + continue;
  14522. + }
  14523. +
  14524. + if (hdl->error)
  14525. + break;
  14526. +
  14527. + dev->ctrls[c]->priv = (void *)ctrl;
  14528. + }
  14529. +
  14530. + if (hdl->error) {
  14531. + pr_err("error adding control %d/%d id 0x%x\n", c,
  14532. + V4L2_CTRL_COUNT, ctrl->id);
  14533. + return hdl->error;
  14534. + }
  14535. +
  14536. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  14537. + ctrl = &v4l2_ctrls[c];
  14538. +
  14539. + switch (ctrl->type) {
  14540. + case MMAL_CONTROL_TYPE_CLUSTER:
  14541. + v4l2_ctrl_auto_cluster(ctrl->min,
  14542. + &dev->ctrls[c+1],
  14543. + ctrl->max,
  14544. + ctrl->def);
  14545. + break;
  14546. +
  14547. + case MMAL_CONTROL_TYPE_STD:
  14548. + case MMAL_CONTROL_TYPE_STD_MENU:
  14549. + case MMAL_CONTROL_TYPE_INT_MENU:
  14550. + break;
  14551. + }
  14552. +
  14553. + }
  14554. +
  14555. + return 0;
  14556. +}
  14557. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/Kconfig linux-raspberry-pi/drivers/media/platform/bcm2835/Kconfig
  14558. --- linux-3.12.13/drivers/media/platform/bcm2835/Kconfig 1970-01-01 01:00:00.000000000 +0100
  14559. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/Kconfig 2014-03-11 17:32:31.000000000 +0100
  14560. @@ -0,0 +1,25 @@
  14561. +# Broadcom VideoCore IV v4l2 camera support
  14562. +
  14563. +config VIDEO_BCM2835
  14564. + bool "Broadcom BCM2835 camera interface driver"
  14565. + depends on VIDEO_V4L2 && ARCH_BCM2708
  14566. + ---help---
  14567. + Say Y here to enable camera host interface devices for
  14568. + Broadcom BCM2835 SoC. This operates over the VCHIQ interface
  14569. + to a service running on VideoCore.
  14570. +
  14571. +
  14572. +if VIDEO_BCM2835
  14573. +
  14574. +config VIDEO_BCM2835_MMAL
  14575. + tristate "Broadcom BM2835 MMAL camera interface driver"
  14576. + depends on BCM2708_VCHIQ
  14577. + select VIDEOBUF2_VMALLOC
  14578. + ---help---
  14579. + This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
  14580. +
  14581. + To compile this driver as a module, choose M here: the
  14582. + module will be called bcm2835-v4l2.o
  14583. +
  14584. +
  14585. +endif # VIDEO_BM2835
  14586. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/Makefile linux-raspberry-pi/drivers/media/platform/bcm2835/Makefile
  14587. --- linux-3.12.13/drivers/media/platform/bcm2835/Makefile 1970-01-01 01:00:00.000000000 +0100
  14588. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/Makefile 2014-03-11 17:32:31.000000000 +0100
  14589. @@ -0,0 +1,5 @@
  14590. +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
  14591. +
  14592. +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
  14593. +
  14594. +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  14595. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-common.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-common.h
  14596. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-common.h 1970-01-01 01:00:00.000000000 +0100
  14597. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-common.h 2014-03-11 17:51:15.000000000 +0100
  14598. @@ -0,0 +1,52 @@
  14599. +/*
  14600. + * Broadcom BM2835 V4L2 driver
  14601. + *
  14602. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14603. + *
  14604. + * This file is subject to the terms and conditions of the GNU General Public
  14605. + * License. See the file COPYING in the main directory of this archive
  14606. + * for more details.
  14607. + *
  14608. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14609. + * Dave Stevenson <dsteve@broadcom.com>
  14610. + * Simon Mellor <simellor@broadcom.com>
  14611. + * Luke Diamand <luked@broadcom.com>
  14612. + *
  14613. + * MMAL structures
  14614. + *
  14615. + */
  14616. +
  14617. +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
  14618. +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
  14619. +
  14620. +/** Special value signalling that time is not known */
  14621. +#define MMAL_TIME_UNKNOWN (1LL<<63)
  14622. +
  14623. +/* mapping between v4l and mmal video modes */
  14624. +struct mmal_fmt {
  14625. + char *name;
  14626. + u32 fourcc; /* v4l2 format id */
  14627. + u32 mmal;
  14628. + int depth;
  14629. + u32 mmal_component; /* MMAL component index to be used to encode */
  14630. +};
  14631. +
  14632. +/* buffer for one video frame */
  14633. +struct mmal_buffer {
  14634. + /* v4l buffer data -- must be first */
  14635. + struct vb2_buffer vb;
  14636. +
  14637. + /* list of buffers available */
  14638. + struct list_head list;
  14639. +
  14640. + void *buffer; /* buffer pointer */
  14641. + unsigned long buffer_size; /* size of allocated buffer */
  14642. +};
  14643. +
  14644. +/* */
  14645. +struct mmal_colourfx {
  14646. + s32 enable;
  14647. + u32 u;
  14648. + u32 v;
  14649. +};
  14650. +
  14651. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-encodings.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-encodings.h
  14652. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-encodings.h 1970-01-01 01:00:00.000000000 +0100
  14653. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-encodings.h 2014-03-11 17:51:15.000000000 +0100
  14654. @@ -0,0 +1,93 @@
  14655. +/*
  14656. + * Broadcom BM2835 V4L2 driver
  14657. + *
  14658. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14659. + *
  14660. + * This file is subject to the terms and conditions of the GNU General Public
  14661. + * License. See the file COPYING in the main directory of this archive
  14662. + * for more details.
  14663. + *
  14664. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14665. + * Dave Stevenson <dsteve@broadcom.com>
  14666. + * Simon Mellor <simellor@broadcom.com>
  14667. + * Luke Diamand <luked@broadcom.com>
  14668. + */
  14669. +
  14670. +#define MMAL_ENCODING_H264 MMAL_FOURCC('H', '2', '6', '4')
  14671. +#define MMAL_ENCODING_H263 MMAL_FOURCC('H', '2', '6', '3')
  14672. +#define MMAL_ENCODING_MP4V MMAL_FOURCC('M', 'P', '4', 'V')
  14673. +#define MMAL_ENCODING_MP2V MMAL_FOURCC('M', 'P', '2', 'V')
  14674. +#define MMAL_ENCODING_MP1V MMAL_FOURCC('M', 'P', '1', 'V')
  14675. +#define MMAL_ENCODING_WMV3 MMAL_FOURCC('W', 'M', 'V', '3')
  14676. +#define MMAL_ENCODING_WMV2 MMAL_FOURCC('W', 'M', 'V', '2')
  14677. +#define MMAL_ENCODING_WMV1 MMAL_FOURCC('W', 'M', 'V', '1')
  14678. +#define MMAL_ENCODING_WVC1 MMAL_FOURCC('W', 'V', 'C', '1')
  14679. +#define MMAL_ENCODING_VP8 MMAL_FOURCC('V', 'P', '8', ' ')
  14680. +#define MMAL_ENCODING_VP7 MMAL_FOURCC('V', 'P', '7', ' ')
  14681. +#define MMAL_ENCODING_VP6 MMAL_FOURCC('V', 'P', '6', ' ')
  14682. +#define MMAL_ENCODING_THEORA MMAL_FOURCC('T', 'H', 'E', 'O')
  14683. +#define MMAL_ENCODING_SPARK MMAL_FOURCC('S', 'P', 'R', 'K')
  14684. +
  14685. +#define MMAL_ENCODING_JPEG MMAL_FOURCC('J', 'P', 'E', 'G')
  14686. +#define MMAL_ENCODING_GIF MMAL_FOURCC('G', 'I', 'F', ' ')
  14687. +#define MMAL_ENCODING_PNG MMAL_FOURCC('P', 'N', 'G', ' ')
  14688. +#define MMAL_ENCODING_PPM MMAL_FOURCC('P', 'P', 'M', ' ')
  14689. +#define MMAL_ENCODING_TGA MMAL_FOURCC('T', 'G', 'A', ' ')
  14690. +#define MMAL_ENCODING_BMP MMAL_FOURCC('B', 'M', 'P', ' ')
  14691. +
  14692. +#define MMAL_ENCODING_I420 MMAL_FOURCC('I', '4', '2', '0')
  14693. +#define MMAL_ENCODING_I420_SLICE MMAL_FOURCC('S', '4', '2', '0')
  14694. +#define MMAL_ENCODING_YV12 MMAL_FOURCC('Y', 'V', '1', '2')
  14695. +#define MMAL_ENCODING_I422 MMAL_FOURCC('I', '4', '2', '2')
  14696. +#define MMAL_ENCODING_I422_SLICE MMAL_FOURCC('S', '4', '2', '2')
  14697. +#define MMAL_ENCODING_YUYV MMAL_FOURCC('Y', 'U', 'Y', 'V')
  14698. +#define MMAL_ENCODING_YVYU MMAL_FOURCC('Y', 'V', 'Y', 'U')
  14699. +#define MMAL_ENCODING_UYVY MMAL_FOURCC('U', 'Y', 'V', 'Y')
  14700. +#define MMAL_ENCODING_VYUY MMAL_FOURCC('V', 'Y', 'U', 'Y')
  14701. +#define MMAL_ENCODING_NV12 MMAL_FOURCC('N', 'V', '1', '2')
  14702. +#define MMAL_ENCODING_NV21 MMAL_FOURCC('N', 'V', '2', '1')
  14703. +#define MMAL_ENCODING_ARGB MMAL_FOURCC('A', 'R', 'G', 'B')
  14704. +#define MMAL_ENCODING_RGBA MMAL_FOURCC('R', 'G', 'B', 'A')
  14705. +#define MMAL_ENCODING_ABGR MMAL_FOURCC('A', 'B', 'G', 'R')
  14706. +#define MMAL_ENCODING_BGRA MMAL_FOURCC('B', 'G', 'R', 'A')
  14707. +#define MMAL_ENCODING_RGB16 MMAL_FOURCC('R', 'G', 'B', '2')
  14708. +#define MMAL_ENCODING_RGB24 MMAL_FOURCC('R', 'G', 'B', '3')
  14709. +#define MMAL_ENCODING_RGB32 MMAL_FOURCC('R', 'G', 'B', '4')
  14710. +#define MMAL_ENCODING_BGR16 MMAL_FOURCC('B', 'G', 'R', '2')
  14711. +#define MMAL_ENCODING_BGR24 MMAL_FOURCC('B', 'G', 'R', '3')
  14712. +#define MMAL_ENCODING_BGR32 MMAL_FOURCC('B', 'G', 'R', '4')
  14713. +
  14714. +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
  14715. + * This format is *not* opaque - if requested you will receive full frames
  14716. + * of YUV_UV video.
  14717. + */
  14718. +#define MMAL_ENCODING_YUVUV128 MMAL_FOURCC('S', 'A', 'N', 'D')
  14719. +
  14720. +/** VideoCore opaque image format, image handles are returned to
  14721. + * the host but not the actual image data.
  14722. + */
  14723. +#define MMAL_ENCODING_OPAQUE MMAL_FOURCC('O', 'P', 'Q', 'V')
  14724. +
  14725. +/** An EGL image handle
  14726. + */
  14727. +#define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
  14728. +
  14729. +/* }@ */
  14730. +
  14731. +/** \name Pre-defined audio encodings */
  14732. +/* @{ */
  14733. +#define MMAL_ENCODING_PCM_UNSIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'U')
  14734. +#define MMAL_ENCODING_PCM_UNSIGNED_LE MMAL_FOURCC('p', 'c', 'm', 'u')
  14735. +#define MMAL_ENCODING_PCM_SIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'S')
  14736. +#define MMAL_ENCODING_PCM_SIGNED_LE MMAL_FOURCC('p', 'c', 'm', 's')
  14737. +#define MMAL_ENCODING_PCM_FLOAT_BE MMAL_FOURCC('P', 'C', 'M', 'F')
  14738. +#define MMAL_ENCODING_PCM_FLOAT_LE MMAL_FOURCC('p', 'c', 'm', 'f')
  14739. +
  14740. +/* Pre-defined H264 encoding variants */
  14741. +
  14742. +/** ISO 14496-10 Annex B byte stream format */
  14743. +#define MMAL_ENCODING_VARIANT_H264_DEFAULT 0
  14744. +/** ISO 14496-15 AVC stream format */
  14745. +#define MMAL_ENCODING_VARIANT_H264_AVC1 MMAL_FOURCC('A', 'V', 'C', '1')
  14746. +/** Implicitly delineated NAL units without emulation prevention */
  14747. +#define MMAL_ENCODING_VARIANT_H264_RAW MMAL_FOURCC('R', 'A', 'W', ' ')
  14748. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg-common.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-common.h
  14749. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg-common.h 1970-01-01 01:00:00.000000000 +0100
  14750. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-common.h 2014-03-11 17:32:31.000000000 +0100
  14751. @@ -0,0 +1,50 @@
  14752. +/*
  14753. + * Broadcom BM2835 V4L2 driver
  14754. + *
  14755. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14756. + *
  14757. + * This file is subject to the terms and conditions of the GNU General Public
  14758. + * License. See the file COPYING in the main directory of this archive
  14759. + * for more details.
  14760. + *
  14761. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14762. + * Dave Stevenson <dsteve@broadcom.com>
  14763. + * Simon Mellor <simellor@broadcom.com>
  14764. + * Luke Diamand <luked@broadcom.com>
  14765. + */
  14766. +
  14767. +#ifndef MMAL_MSG_COMMON_H
  14768. +#define MMAL_MSG_COMMON_H
  14769. +
  14770. +enum mmal_msg_status {
  14771. + MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
  14772. + MMAL_MSG_STATUS_ENOMEM, /**< Out of memory */
  14773. + MMAL_MSG_STATUS_ENOSPC, /**< Out of resources other than memory */
  14774. + MMAL_MSG_STATUS_EINVAL, /**< Argument is invalid */
  14775. + MMAL_MSG_STATUS_ENOSYS, /**< Function not implemented */
  14776. + MMAL_MSG_STATUS_ENOENT, /**< No such file or directory */
  14777. + MMAL_MSG_STATUS_ENXIO, /**< No such device or address */
  14778. + MMAL_MSG_STATUS_EIO, /**< I/O error */
  14779. + MMAL_MSG_STATUS_ESPIPE, /**< Illegal seek */
  14780. + MMAL_MSG_STATUS_ECORRUPT, /**< Data is corrupt \attention */
  14781. + MMAL_MSG_STATUS_ENOTREADY, /**< Component is not ready */
  14782. + MMAL_MSG_STATUS_ECONFIG, /**< Component is not configured */
  14783. + MMAL_MSG_STATUS_EISCONN, /**< Port is already connected */
  14784. + MMAL_MSG_STATUS_ENOTCONN, /**< Port is disconnected */
  14785. + MMAL_MSG_STATUS_EAGAIN, /**< Resource temporarily unavailable. */
  14786. + MMAL_MSG_STATUS_EFAULT, /**< Bad address */
  14787. +};
  14788. +
  14789. +struct mmal_rect {
  14790. + s32 x; /**< x coordinate (from left) */
  14791. + s32 y; /**< y coordinate (from top) */
  14792. + s32 width; /**< width */
  14793. + s32 height; /**< height */
  14794. +};
  14795. +
  14796. +struct mmal_rational {
  14797. + s32 num; /**< Numerator */
  14798. + s32 den; /**< Denominator */
  14799. +};
  14800. +
  14801. +#endif /* MMAL_MSG_COMMON_H */
  14802. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg-format.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-format.h
  14803. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg-format.h 1970-01-01 01:00:00.000000000 +0100
  14804. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-format.h 2014-03-11 17:32:31.000000000 +0100
  14805. @@ -0,0 +1,81 @@
  14806. +/*
  14807. + * Broadcom BM2835 V4L2 driver
  14808. + *
  14809. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14810. + *
  14811. + * This file is subject to the terms and conditions of the GNU General Public
  14812. + * License. See the file COPYING in the main directory of this archive
  14813. + * for more details.
  14814. + *
  14815. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14816. + * Dave Stevenson <dsteve@broadcom.com>
  14817. + * Simon Mellor <simellor@broadcom.com>
  14818. + * Luke Diamand <luked@broadcom.com>
  14819. + */
  14820. +
  14821. +#ifndef MMAL_MSG_FORMAT_H
  14822. +#define MMAL_MSG_FORMAT_H
  14823. +
  14824. +#include "mmal-msg-common.h"
  14825. +
  14826. +/* MMAL_ES_FORMAT_T */
  14827. +
  14828. +
  14829. +struct mmal_audio_format {
  14830. + u32 channels; /**< Number of audio channels */
  14831. + u32 sample_rate; /**< Sample rate */
  14832. +
  14833. + u32 bits_per_sample; /**< Bits per sample */
  14834. + u32 block_align; /**< Size of a block of data */
  14835. +};
  14836. +
  14837. +struct mmal_video_format {
  14838. + u32 width; /**< Width of frame in pixels */
  14839. + u32 height; /**< Height of frame in rows of pixels */
  14840. + struct mmal_rect crop; /**< Visible region of the frame */
  14841. + struct mmal_rational frame_rate; /**< Frame rate */
  14842. + struct mmal_rational par; /**< Pixel aspect ratio */
  14843. +
  14844. + /* FourCC specifying the color space of the video stream. See the
  14845. + * \ref MmalColorSpace "pre-defined color spaces" for some examples.
  14846. + */
  14847. + u32 color_space;
  14848. +};
  14849. +
  14850. +struct mmal_subpicture_format {
  14851. + u32 x_offset;
  14852. + u32 y_offset;
  14853. +};
  14854. +
  14855. +union mmal_es_specific_format {
  14856. + struct mmal_audio_format audio;
  14857. + struct mmal_video_format video;
  14858. + struct mmal_subpicture_format subpicture;
  14859. +};
  14860. +
  14861. +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
  14862. +struct mmal_es_format {
  14863. + u32 type; /* enum mmal_es_type */
  14864. +
  14865. + u32 encoding; /* FourCC specifying encoding of the elementary stream.*/
  14866. + u32 encoding_variant; /* FourCC specifying the specific
  14867. + * encoding variant of the elementary
  14868. + * stream.
  14869. + */
  14870. +
  14871. + union mmal_es_specific_format *es; /* TODO: pointers in
  14872. + * message serialisation?!?
  14873. + */
  14874. + /* Type specific
  14875. + * information for the
  14876. + * elementary stream
  14877. + */
  14878. +
  14879. + u32 bitrate; /**< Bitrate in bits per second */
  14880. + u32 flags; /**< Flags describing properties of the elementary stream. */
  14881. +
  14882. + u32 extradata_size; /**< Size of the codec specific data */
  14883. + u8 *extradata; /**< Codec specific data */
  14884. +};
  14885. +
  14886. +#endif /* MMAL_MSG_FORMAT_H */
  14887. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg.h
  14888. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg.h 1970-01-01 01:00:00.000000000 +0100
  14889. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg.h 2014-03-11 17:32:31.000000000 +0100
  14890. @@ -0,0 +1,404 @@
  14891. +/*
  14892. + * Broadcom BM2835 V4L2 driver
  14893. + *
  14894. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14895. + *
  14896. + * This file is subject to the terms and conditions of the GNU General Public
  14897. + * License. See the file COPYING in the main directory of this archive
  14898. + * for more details.
  14899. + *
  14900. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14901. + * Dave Stevenson <dsteve@broadcom.com>
  14902. + * Simon Mellor <simellor@broadcom.com>
  14903. + * Luke Diamand <luked@broadcom.com>
  14904. + */
  14905. +
  14906. +/* all the data structures which serialise the MMAL protocol. note
  14907. + * these are directly mapped onto the recived message data.
  14908. + *
  14909. + * BEWARE: They seem to *assume* pointers are u32 and that there is no
  14910. + * structure padding!
  14911. + *
  14912. + * NOTE: this implementation uses kernel types to ensure sizes. Rather
  14913. + * than assigning values to enums to force their size the
  14914. + * implementation uses fixed size types and not the enums (though the
  14915. + * comments have the actual enum type
  14916. + */
  14917. +
  14918. +#define VC_MMAL_VER 15
  14919. +#define VC_MMAL_MIN_VER 10
  14920. +#define VC_MMAL_SERVER_NAME MAKE_FOURCC("mmal")
  14921. +
  14922. +/* max total message size is 512 bytes */
  14923. +#define MMAL_MSG_MAX_SIZE 512
  14924. +/* with six 32bit header elements max payload is therefore 488 bytes */
  14925. +#define MMAL_MSG_MAX_PAYLOAD 488
  14926. +
  14927. +#include "mmal-msg-common.h"
  14928. +#include "mmal-msg-format.h"
  14929. +#include "mmal-msg-port.h"
  14930. +
  14931. +enum mmal_msg_type {
  14932. + MMAL_MSG_TYPE_QUIT = 1,
  14933. + MMAL_MSG_TYPE_SERVICE_CLOSED,
  14934. + MMAL_MSG_TYPE_GET_VERSION,
  14935. + MMAL_MSG_TYPE_COMPONENT_CREATE,
  14936. + MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
  14937. + MMAL_MSG_TYPE_COMPONENT_ENABLE,
  14938. + MMAL_MSG_TYPE_COMPONENT_DISABLE,
  14939. + MMAL_MSG_TYPE_PORT_INFO_GET,
  14940. + MMAL_MSG_TYPE_PORT_INFO_SET,
  14941. + MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
  14942. + MMAL_MSG_TYPE_BUFFER_FROM_HOST,
  14943. + MMAL_MSG_TYPE_BUFFER_TO_HOST,
  14944. + MMAL_MSG_TYPE_GET_STATS,
  14945. + MMAL_MSG_TYPE_PORT_PARAMETER_SET,
  14946. + MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
  14947. + MMAL_MSG_TYPE_EVENT_TO_HOST,
  14948. + MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
  14949. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
  14950. + MMAL_MSG_TYPE_CONSUME_MEM,
  14951. + MMAL_MSG_TYPE_LMK, /* 20 */
  14952. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
  14953. + MMAL_MSG_TYPE_DRM_GET_LHS32,
  14954. + MMAL_MSG_TYPE_DRM_GET_TIME,
  14955. + MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
  14956. + MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
  14957. + MMAL_MSG_TYPE_HOST_LOG,
  14958. + MMAL_MSG_TYPE_MSG_LAST
  14959. +};
  14960. +
  14961. +/* port action request messages differ depending on the action type */
  14962. +enum mmal_msg_port_action_type {
  14963. + MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0, /* Unkown action */
  14964. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE, /* Enable a port */
  14965. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE, /* Disable a port */
  14966. + MMAL_MSG_PORT_ACTION_TYPE_FLUSH, /* Flush a port */
  14967. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT, /* Connect ports */
  14968. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT, /* Disconnect ports */
  14969. + MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
  14970. +};
  14971. +
  14972. +struct mmal_msg_header {
  14973. + u32 magic;
  14974. + u32 type; /** enum mmal_msg_type */
  14975. +
  14976. + /* Opaque handle to the control service */
  14977. + struct mmal_control_service *control_service;
  14978. +
  14979. + struct mmal_msg_context *context; /** a u32 per message context */
  14980. + u32 status; /** The status of the vchiq operation */
  14981. + u32 padding;
  14982. +};
  14983. +
  14984. +/* Send from VC to host to report version */
  14985. +struct mmal_msg_version {
  14986. + u32 flags;
  14987. + u32 major;
  14988. + u32 minor;
  14989. + u32 minimum;
  14990. +};
  14991. +
  14992. +/* request to VC to create component */
  14993. +struct mmal_msg_component_create {
  14994. + void *client_component; /* component context */
  14995. + char name[128];
  14996. + u32 pid; /* For debug */
  14997. +};
  14998. +
  14999. +/* reply from VC to component creation request */
  15000. +struct mmal_msg_component_create_reply {
  15001. + u32 status; /** enum mmal_msg_status - how does this differ to
  15002. + * the one in the header?
  15003. + */
  15004. + u32 component_handle; /* VideoCore handle for component */
  15005. + u32 input_num; /* Number of input ports */
  15006. + u32 output_num; /* Number of output ports */
  15007. + u32 clock_num; /* Number of clock ports */
  15008. +};
  15009. +
  15010. +/* request to VC to destroy a component */
  15011. +struct mmal_msg_component_destroy {
  15012. + u32 component_handle;
  15013. +};
  15014. +
  15015. +struct mmal_msg_component_destroy_reply {
  15016. + u32 status; /** The component destruction status */
  15017. +};
  15018. +
  15019. +
  15020. +/* request and reply to VC to enable a component */
  15021. +struct mmal_msg_component_enable {
  15022. + u32 component_handle;
  15023. +};
  15024. +
  15025. +struct mmal_msg_component_enable_reply {
  15026. + u32 status; /** The component enable status */
  15027. +};
  15028. +
  15029. +
  15030. +/* request and reply to VC to disable a component */
  15031. +struct mmal_msg_component_disable {
  15032. + u32 component_handle;
  15033. +};
  15034. +
  15035. +struct mmal_msg_component_disable_reply {
  15036. + u32 status; /** The component disable status */
  15037. +};
  15038. +
  15039. +/* request to VC to get port information */
  15040. +struct mmal_msg_port_info_get {
  15041. + u32 component_handle; /* component handle port is associated with */
  15042. + u32 port_type; /* enum mmal_msg_port_type */
  15043. + u32 index; /* port index to query */
  15044. +};
  15045. +
  15046. +/* reply from VC to get port info request */
  15047. +struct mmal_msg_port_info_get_reply {
  15048. + u32 status; /** enum mmal_msg_status */
  15049. + u32 component_handle; /* component handle port is associated with */
  15050. + u32 port_type; /* enum mmal_msg_port_type */
  15051. + u32 port_index; /* port indexed in query */
  15052. + s32 found; /* unused */
  15053. + u32 port_handle; /**< Handle to use for this port */
  15054. + struct mmal_port port;
  15055. + struct mmal_es_format format; /* elementry stream format */
  15056. + union mmal_es_specific_format es; /* es type specific data */
  15057. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
  15058. +};
  15059. +
  15060. +/* request to VC to set port information */
  15061. +struct mmal_msg_port_info_set {
  15062. + u32 component_handle;
  15063. + u32 port_type; /* enum mmal_msg_port_type */
  15064. + u32 port_index; /* port indexed in query */
  15065. + struct mmal_port port;
  15066. + struct mmal_es_format format;
  15067. + union mmal_es_specific_format es;
  15068. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  15069. +};
  15070. +
  15071. +/* reply from VC to port info set request */
  15072. +struct mmal_msg_port_info_set_reply {
  15073. + u32 status;
  15074. + u32 component_handle; /* component handle port is associated with */
  15075. + u32 port_type; /* enum mmal_msg_port_type */
  15076. + u32 index; /* port indexed in query */
  15077. + s32 found; /* unused */
  15078. + u32 port_handle; /**< Handle to use for this port */
  15079. + struct mmal_port port;
  15080. + struct mmal_es_format format;
  15081. + union mmal_es_specific_format es;
  15082. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  15083. +};
  15084. +
  15085. +
  15086. +/* port action requests that take a mmal_port as a parameter */
  15087. +struct mmal_msg_port_action_port {
  15088. + u32 component_handle;
  15089. + u32 port_handle;
  15090. + u32 action; /* enum mmal_msg_port_action_type */
  15091. + struct mmal_port port;
  15092. +};
  15093. +
  15094. +/* port action requests that take handles as a parameter */
  15095. +struct mmal_msg_port_action_handle {
  15096. + u32 component_handle;
  15097. + u32 port_handle;
  15098. + u32 action; /* enum mmal_msg_port_action_type */
  15099. + u32 connect_component_handle;
  15100. + u32 connect_port_handle;
  15101. +};
  15102. +
  15103. +struct mmal_msg_port_action_reply {
  15104. + u32 status; /** The port action operation status */
  15105. +};
  15106. +
  15107. +
  15108. +
  15109. +
  15110. +/* MMAL buffer transfer */
  15111. +
  15112. +/** Size of space reserved in a buffer message for short messages. */
  15113. +#define MMAL_VC_SHORT_DATA 128
  15114. +
  15115. +/** Signals that the current payload is the end of the stream of data */
  15116. +#define MMAL_BUFFER_HEADER_FLAG_EOS (1<<0)
  15117. +/** Signals that the start of the current payload starts a frame */
  15118. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START (1<<1)
  15119. +/** Signals that the end of the current payload ends a frame */
  15120. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END (1<<2)
  15121. +/** Signals that the current payload contains only complete frames (>1) */
  15122. +#define MMAL_BUFFER_HEADER_FLAG_FRAME \
  15123. + (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  15124. +/** Signals that the current payload is a keyframe (i.e. self decodable) */
  15125. +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME (1<<3)
  15126. +/** Signals a discontinuity in the stream of data (e.g. after a seek).
  15127. + * Can be used for instance by a decoder to reset its state */
  15128. +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY (1<<4)
  15129. +/** Signals a buffer containing some kind of config data for the component
  15130. + * (e.g. codec config data) */
  15131. +#define MMAL_BUFFER_HEADER_FLAG_CONFIG (1<<5)
  15132. +/** Signals an encrypted payload */
  15133. +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED (1<<6)
  15134. +/** Signals a buffer containing side information */
  15135. +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO (1<<7)
  15136. +/** Signals a buffer which is the snapshot/postview image from a stills
  15137. + * capture
  15138. + */
  15139. +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT (1<<8)
  15140. +/** Signals a buffer which contains data known to be corrupted */
  15141. +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED (1<<9)
  15142. +/** Signals that a buffer failed to be transmitted */
  15143. +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED (1<<10)
  15144. +
  15145. +struct mmal_driver_buffer {
  15146. + u32 magic;
  15147. + u32 component_handle;
  15148. + u32 port_handle;
  15149. + void *client_context;
  15150. +};
  15151. +
  15152. +/* buffer header */
  15153. +struct mmal_buffer_header {
  15154. + struct mmal_buffer_header *next; /* next header */
  15155. + void *priv; /* framework private data */
  15156. + u32 cmd;
  15157. + void *data;
  15158. + u32 alloc_size;
  15159. + u32 length;
  15160. + u32 offset;
  15161. + u32 flags;
  15162. + s64 pts;
  15163. + s64 dts;
  15164. + void *type;
  15165. + void *user_data;
  15166. +};
  15167. +
  15168. +struct mmal_buffer_header_type_specific {
  15169. + union {
  15170. + struct {
  15171. + u32 planes;
  15172. + u32 offset[4];
  15173. + u32 pitch[4];
  15174. + u32 flags;
  15175. + } video;
  15176. + } u;
  15177. +};
  15178. +
  15179. +struct mmal_msg_buffer_from_host {
  15180. + /* The front 32 bytes of the buffer header are copied
  15181. + * back to us in the reply to allow for context. This
  15182. + * area is used to store two mmal_driver_buffer structures to
  15183. + * allow for multiple concurrent service users.
  15184. + */
  15185. + /* control data */
  15186. + struct mmal_driver_buffer drvbuf;
  15187. +
  15188. + /* referenced control data for passthrough buffer management */
  15189. + struct mmal_driver_buffer drvbuf_ref;
  15190. + struct mmal_buffer_header buffer_header; /* buffer header itself */
  15191. + struct mmal_buffer_header_type_specific buffer_header_type_specific;
  15192. + s32 is_zero_copy;
  15193. + s32 has_reference;
  15194. +
  15195. + /** allows short data to be xfered in control message */
  15196. + u32 payload_in_message;
  15197. + u8 short_data[MMAL_VC_SHORT_DATA];
  15198. +};
  15199. +
  15200. +
  15201. +/* port parameter setting */
  15202. +
  15203. +#define MMAL_WORKER_PORT_PARAMETER_SPACE 96
  15204. +
  15205. +struct mmal_msg_port_parameter_set {
  15206. + u32 component_handle; /* component */
  15207. + u32 port_handle; /* port */
  15208. + u32 id; /* Parameter ID */
  15209. + u32 size; /* Parameter size */
  15210. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  15211. +};
  15212. +
  15213. +struct mmal_msg_port_parameter_set_reply {
  15214. + u32 status; /** enum mmal_msg_status todo: how does this
  15215. + * differ to the one in the header?
  15216. + */
  15217. +};
  15218. +
  15219. +/* port parameter getting */
  15220. +
  15221. +struct mmal_msg_port_parameter_get {
  15222. + u32 component_handle; /* component */
  15223. + u32 port_handle; /* port */
  15224. + u32 id; /* Parameter ID */
  15225. + u32 size; /* Parameter size */
  15226. +};
  15227. +
  15228. +struct mmal_msg_port_parameter_get_reply {
  15229. + u32 status; /* Status of mmal_port_parameter_get call */
  15230. + u32 id; /* Parameter ID */
  15231. + u32 size; /* Parameter size */
  15232. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  15233. +};
  15234. +
  15235. +/* event messages */
  15236. +#define MMAL_WORKER_EVENT_SPACE 256
  15237. +
  15238. +struct mmal_msg_event_to_host {
  15239. + void *client_component; /* component context */
  15240. +
  15241. + u32 port_type;
  15242. + u32 port_num;
  15243. +
  15244. + u32 cmd;
  15245. + u32 length;
  15246. + u8 data[MMAL_WORKER_EVENT_SPACE];
  15247. + struct mmal_buffer_header *delayed_buffer;
  15248. +};
  15249. +
  15250. +/* all mmal messages are serialised through this structure */
  15251. +struct mmal_msg {
  15252. + /* header */
  15253. + struct mmal_msg_header h;
  15254. + /* payload */
  15255. + union {
  15256. + struct mmal_msg_version version;
  15257. +
  15258. + struct mmal_msg_component_create component_create;
  15259. + struct mmal_msg_component_create_reply component_create_reply;
  15260. +
  15261. + struct mmal_msg_component_destroy component_destroy;
  15262. + struct mmal_msg_component_destroy_reply component_destroy_reply;
  15263. +
  15264. + struct mmal_msg_component_enable component_enable;
  15265. + struct mmal_msg_component_enable_reply component_enable_reply;
  15266. +
  15267. + struct mmal_msg_component_disable component_disable;
  15268. + struct mmal_msg_component_disable_reply component_disable_reply;
  15269. +
  15270. + struct mmal_msg_port_info_get port_info_get;
  15271. + struct mmal_msg_port_info_get_reply port_info_get_reply;
  15272. +
  15273. + struct mmal_msg_port_info_set port_info_set;
  15274. + struct mmal_msg_port_info_set_reply port_info_set_reply;
  15275. +
  15276. + struct mmal_msg_port_action_port port_action_port;
  15277. + struct mmal_msg_port_action_handle port_action_handle;
  15278. + struct mmal_msg_port_action_reply port_action_reply;
  15279. +
  15280. + struct mmal_msg_buffer_from_host buffer_from_host;
  15281. +
  15282. + struct mmal_msg_port_parameter_set port_parameter_set;
  15283. + struct mmal_msg_port_parameter_set_reply
  15284. + port_parameter_set_reply;
  15285. + struct mmal_msg_port_parameter_get
  15286. + port_parameter_get;
  15287. + struct mmal_msg_port_parameter_get_reply
  15288. + port_parameter_get_reply;
  15289. +
  15290. + struct mmal_msg_event_to_host event_to_host;
  15291. +
  15292. + u8 payload[MMAL_MSG_MAX_PAYLOAD];
  15293. + } u;
  15294. +};
  15295. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg-port.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-port.h
  15296. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-msg-port.h 1970-01-01 01:00:00.000000000 +0100
  15297. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-port.h 2014-03-11 17:32:31.000000000 +0100
  15298. @@ -0,0 +1,107 @@
  15299. +/*
  15300. + * Broadcom BM2835 V4L2 driver
  15301. + *
  15302. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  15303. + *
  15304. + * This file is subject to the terms and conditions of the GNU General Public
  15305. + * License. See the file COPYING in the main directory of this archive
  15306. + * for more details.
  15307. + *
  15308. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  15309. + * Dave Stevenson <dsteve@broadcom.com>
  15310. + * Simon Mellor <simellor@broadcom.com>
  15311. + * Luke Diamand <luked@broadcom.com>
  15312. + */
  15313. +
  15314. +/* MMAL_PORT_TYPE_T */
  15315. +enum mmal_port_type {
  15316. + MMAL_PORT_TYPE_UNKNOWN = 0, /**< Unknown port type */
  15317. + MMAL_PORT_TYPE_CONTROL, /**< Control port */
  15318. + MMAL_PORT_TYPE_INPUT, /**< Input port */
  15319. + MMAL_PORT_TYPE_OUTPUT, /**< Output port */
  15320. + MMAL_PORT_TYPE_CLOCK, /**< Clock port */
  15321. +};
  15322. +
  15323. +/** The port is pass-through and doesn't need buffer headers allocated */
  15324. +#define MMAL_PORT_CAPABILITY_PASSTHROUGH 0x01
  15325. +/** The port wants to allocate the buffer payloads.
  15326. + * This signals a preference that payload allocation should be done
  15327. + * on this port for efficiency reasons. */
  15328. +#define MMAL_PORT_CAPABILITY_ALLOCATION 0x02
  15329. +/** The port supports format change events.
  15330. + * This applies to input ports and is used to let the client know
  15331. + * whether the port supports being reconfigured via a format
  15332. + * change event (i.e. without having to disable the port). */
  15333. +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE 0x04
  15334. +
  15335. +/* mmal port structure (MMAL_PORT_T)
  15336. + *
  15337. + * most elements are informational only, the pointer values for
  15338. + * interogation messages are generally provided as additional
  15339. + * strucures within the message. When used to set values only teh
  15340. + * buffer_num, buffer_size and userdata parameters are writable.
  15341. + */
  15342. +struct mmal_port {
  15343. + void *priv; /* Private member used by the framework */
  15344. + const char *name; /* Port name. Used for debugging purposes (RO) */
  15345. +
  15346. + u32 type; /* Type of the port (RO) enum mmal_port_type */
  15347. + u16 index; /* Index of the port in its type list (RO) */
  15348. + u16 index_all; /* Index of the port in the list of all ports (RO) */
  15349. +
  15350. + u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
  15351. + struct mmal_es_format *format; /* Format of the elementary stream */
  15352. +
  15353. + u32 buffer_num_min; /* Minimum number of buffers the port
  15354. + * requires (RO). This is set by the
  15355. + * component.
  15356. + */
  15357. +
  15358. + u32 buffer_size_min; /* Minimum size of buffers the port
  15359. + * requires (RO). This is set by the
  15360. + * component.
  15361. + */
  15362. +
  15363. + u32 buffer_alignment_min; /* Minimum alignment requirement for
  15364. + * the buffers (RO). A value of
  15365. + * zero means no special alignment
  15366. + * requirements. This is set by the
  15367. + * component.
  15368. + */
  15369. +
  15370. + u32 buffer_num_recommended; /* Number of buffers the port
  15371. + * recommends for optimal
  15372. + * performance (RO). A value of
  15373. + * zero means no special
  15374. + * recommendation. This is set
  15375. + * by the component.
  15376. + */
  15377. +
  15378. + u32 buffer_size_recommended; /* Size of buffers the port
  15379. + * recommends for optimal
  15380. + * performance (RO). A value of
  15381. + * zero means no special
  15382. + * recommendation. This is set
  15383. + * by the component.
  15384. + */
  15385. +
  15386. + u32 buffer_num; /* Actual number of buffers the port will use.
  15387. + * This is set by the client.
  15388. + */
  15389. +
  15390. + u32 buffer_size; /* Actual maximum size of the buffers that
  15391. + * will be sent to the port. This is set by
  15392. + * the client.
  15393. + */
  15394. +
  15395. + void *component; /* Component this port belongs to (Read Only) */
  15396. +
  15397. + void *userdata; /* Field reserved for use by the client */
  15398. +
  15399. + u32 capabilities; /* Flags describing the capabilities of a
  15400. + * port (RO). Bitwise combination of \ref
  15401. + * portcapabilities "Port capabilities"
  15402. + * values.
  15403. + */
  15404. +
  15405. +};
  15406. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-parameters.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-parameters.h
  15407. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-parameters.h 1970-01-01 01:00:00.000000000 +0100
  15408. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-parameters.h 2014-03-11 17:51:15.000000000 +0100
  15409. @@ -0,0 +1,562 @@
  15410. +/*
  15411. + * Broadcom BM2835 V4L2 driver
  15412. + *
  15413. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  15414. + *
  15415. + * This file is subject to the terms and conditions of the GNU General Public
  15416. + * License. See the file COPYING in the main directory of this archive
  15417. + * for more details.
  15418. + *
  15419. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  15420. + * Dave Stevenson <dsteve@broadcom.com>
  15421. + * Simon Mellor <simellor@broadcom.com>
  15422. + * Luke Diamand <luked@broadcom.com>
  15423. + */
  15424. +
  15425. +/* common parameters */
  15426. +
  15427. +/** @name Parameter groups
  15428. + * Parameters are divided into groups, and then allocated sequentially within
  15429. + * a group using an enum.
  15430. + * @{
  15431. + */
  15432. +
  15433. +/** Common parameter ID group, used with many types of component. */
  15434. +#define MMAL_PARAMETER_GROUP_COMMON (0<<16)
  15435. +/** Camera-specific parameter ID group. */
  15436. +#define MMAL_PARAMETER_GROUP_CAMERA (1<<16)
  15437. +/** Video-specific parameter ID group. */
  15438. +#define MMAL_PARAMETER_GROUP_VIDEO (2<<16)
  15439. +/** Audio-specific parameter ID group. */
  15440. +#define MMAL_PARAMETER_GROUP_AUDIO (3<<16)
  15441. +/** Clock-specific parameter ID group. */
  15442. +#define MMAL_PARAMETER_GROUP_CLOCK (4<<16)
  15443. +/** Miracast-specific parameter ID group. */
  15444. +#define MMAL_PARAMETER_GROUP_MIRACAST (5<<16)
  15445. +
  15446. +/* Common parameters */
  15447. +enum mmal_parameter_common_type {
  15448. + MMAL_PARAMETER_UNUSED /**< Never a valid parameter ID */
  15449. + = MMAL_PARAMETER_GROUP_COMMON,
  15450. + MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
  15451. + MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
  15452. +
  15453. + /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
  15454. + MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
  15455. +
  15456. + /** MMAL_PARAMETER_BOOLEAN_T */
  15457. + MMAL_PARAMETER_ZERO_COPY,
  15458. +
  15459. + /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
  15460. + MMAL_PARAMETER_BUFFER_REQUIREMENTS,
  15461. +
  15462. + MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
  15463. + MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
  15464. + MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
  15465. + MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
  15466. + MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
  15467. + MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
  15468. + MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
  15469. + MMAL_PARAMETER_SYSTEM_TIME /**< MMAL_PARAMETER_UINT64_T */
  15470. +};
  15471. +
  15472. +/* camera parameters */
  15473. +
  15474. +enum mmal_parameter_camera_type {
  15475. + /* 0 */
  15476. + /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
  15477. + MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
  15478. + = MMAL_PARAMETER_GROUP_CAMERA,
  15479. + MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
  15480. + MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
  15481. + MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15482. + MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
  15483. + MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
  15484. + MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
  15485. + MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
  15486. + MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
  15487. + MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
  15488. + MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
  15489. + MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
  15490. + MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
  15491. + MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  15492. + MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
  15493. + MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
  15494. +
  15495. + /* 0x10 */
  15496. + MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
  15497. + MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15498. + MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
  15499. + MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
  15500. + MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
  15501. + MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
  15502. + MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
  15503. + MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
  15504. + MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15505. + MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
  15506. + MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
  15507. + MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
  15508. + MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
  15509. + MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15510. + MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
  15511. + MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15512. +
  15513. + /* 0x20 */
  15514. + MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
  15515. + MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15516. + MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15517. + MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
  15518. + MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
  15519. + MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
  15520. + MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
  15521. + MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
  15522. + MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
  15523. + MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15524. + MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
  15525. + MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
  15526. + MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15527. + MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15528. + MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15529. + MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15530. +
  15531. + /* 0x30 */
  15532. + MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
  15533. + MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15534. +
  15535. + /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
  15536. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  15537. +
  15538. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15539. + MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
  15540. +
  15541. + /** @ref MMAL_PARAMETER_UINT32_T */
  15542. + MMAL_PARAMETER_CAMERA_MIN_ISO,
  15543. +
  15544. + /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
  15545. + MMAL_PARAMETER_CAMERA_USE_CASE,
  15546. +
  15547. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15548. + MMAL_PARAMETER_CAPTURE_STATS_PASS,
  15549. +
  15550. + /** @ref MMAL_PARAMETER_UINT32_T */
  15551. + MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
  15552. +
  15553. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15554. + MMAL_PARAMETER_ENABLE_REGISTER_FILE,
  15555. +
  15556. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15557. + MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
  15558. +
  15559. + /** @ref MMAL_PARAMETER_CONFIGFILE_T */
  15560. + MMAL_PARAMETER_CONFIGFILE_REGISTERS,
  15561. +
  15562. + /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
  15563. + MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
  15564. + MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15565. + MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
  15566. + MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
  15567. + MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  15568. +
  15569. + /* 0x40 */
  15570. + MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15571. + MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15572. + MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15573. + MMAL_PARAMETER_SHUTTER_SPEED /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
  15574. +};
  15575. +
  15576. +struct mmal_parameter_rational {
  15577. + s32 num; /**< Numerator */
  15578. + s32 den; /**< Denominator */
  15579. +};
  15580. +
  15581. +enum mmal_parameter_camera_config_timestamp_mode {
  15582. + MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
  15583. + MMAL_PARAM_TIMESTAMP_MODE_RAW_STC, /* Use the raw STC value
  15584. + * for the frame timestamp
  15585. + */
  15586. + MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
  15587. + * but subtract the
  15588. + * timestamp of the first
  15589. + * frame sent to give a
  15590. + * zero based timestamp.
  15591. + */
  15592. +};
  15593. +
  15594. +/* camera configuration parameter */
  15595. +struct mmal_parameter_camera_config {
  15596. + /* Parameters for setting up the image pools */
  15597. + u32 max_stills_w; /* Max size of stills capture */
  15598. + u32 max_stills_h;
  15599. + u32 stills_yuv422; /* Allow YUV422 stills capture */
  15600. + u32 one_shot_stills; /* Continuous or one shot stills captures. */
  15601. +
  15602. + u32 max_preview_video_w; /* Max size of the preview or video
  15603. + * capture frames
  15604. + */
  15605. + u32 max_preview_video_h;
  15606. + u32 num_preview_video_frames;
  15607. +
  15608. + /** Sets the height of the circular buffer for stills capture. */
  15609. + u32 stills_capture_circular_buffer_height;
  15610. +
  15611. + /** Allows preview/encode to resume as fast as possible after the stills
  15612. + * input frame has been received, and then processes the still frame in
  15613. + * the background whilst preview/encode has resumed.
  15614. + * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
  15615. + */
  15616. + u32 fast_preview_resume;
  15617. +
  15618. + /** Selects algorithm for timestamping frames if
  15619. + * there is no clock component connected.
  15620. + * enum mmal_parameter_camera_config_timestamp_mode
  15621. + */
  15622. + s32 use_stc_timestamp;
  15623. +};
  15624. +
  15625. +
  15626. +enum mmal_parameter_exposuremode {
  15627. + MMAL_PARAM_EXPOSUREMODE_OFF,
  15628. + MMAL_PARAM_EXPOSUREMODE_AUTO,
  15629. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  15630. + MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
  15631. + MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
  15632. + MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
  15633. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  15634. + MMAL_PARAM_EXPOSUREMODE_SNOW,
  15635. + MMAL_PARAM_EXPOSUREMODE_BEACH,
  15636. + MMAL_PARAM_EXPOSUREMODE_VERYLONG,
  15637. + MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
  15638. + MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
  15639. + MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
  15640. +};
  15641. +
  15642. +enum mmal_parameter_exposuremeteringmode {
  15643. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
  15644. + MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
  15645. + MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
  15646. + MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
  15647. +};
  15648. +
  15649. +enum mmal_parameter_awbmode {
  15650. + MMAL_PARAM_AWBMODE_OFF,
  15651. + MMAL_PARAM_AWBMODE_AUTO,
  15652. + MMAL_PARAM_AWBMODE_SUNLIGHT,
  15653. + MMAL_PARAM_AWBMODE_CLOUDY,
  15654. + MMAL_PARAM_AWBMODE_SHADE,
  15655. + MMAL_PARAM_AWBMODE_TUNGSTEN,
  15656. + MMAL_PARAM_AWBMODE_FLUORESCENT,
  15657. + MMAL_PARAM_AWBMODE_INCANDESCENT,
  15658. + MMAL_PARAM_AWBMODE_FLASH,
  15659. + MMAL_PARAM_AWBMODE_HORIZON,
  15660. +};
  15661. +
  15662. +enum mmal_parameter_imagefx {
  15663. + MMAL_PARAM_IMAGEFX_NONE,
  15664. + MMAL_PARAM_IMAGEFX_NEGATIVE,
  15665. + MMAL_PARAM_IMAGEFX_SOLARIZE,
  15666. + MMAL_PARAM_IMAGEFX_POSTERIZE,
  15667. + MMAL_PARAM_IMAGEFX_WHITEBOARD,
  15668. + MMAL_PARAM_IMAGEFX_BLACKBOARD,
  15669. + MMAL_PARAM_IMAGEFX_SKETCH,
  15670. + MMAL_PARAM_IMAGEFX_DENOISE,
  15671. + MMAL_PARAM_IMAGEFX_EMBOSS,
  15672. + MMAL_PARAM_IMAGEFX_OILPAINT,
  15673. + MMAL_PARAM_IMAGEFX_HATCH,
  15674. + MMAL_PARAM_IMAGEFX_GPEN,
  15675. + MMAL_PARAM_IMAGEFX_PASTEL,
  15676. + MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  15677. + MMAL_PARAM_IMAGEFX_FILM,
  15678. + MMAL_PARAM_IMAGEFX_BLUR,
  15679. + MMAL_PARAM_IMAGEFX_SATURATION,
  15680. + MMAL_PARAM_IMAGEFX_COLOURSWAP,
  15681. + MMAL_PARAM_IMAGEFX_WASHEDOUT,
  15682. + MMAL_PARAM_IMAGEFX_POSTERISE,
  15683. + MMAL_PARAM_IMAGEFX_COLOURPOINT,
  15684. + MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  15685. + MMAL_PARAM_IMAGEFX_CARTOON,
  15686. +};
  15687. +
  15688. +enum MMAL_PARAM_FLICKERAVOID_T {
  15689. + MMAL_PARAM_FLICKERAVOID_OFF,
  15690. + MMAL_PARAM_FLICKERAVOID_AUTO,
  15691. + MMAL_PARAM_FLICKERAVOID_50HZ,
  15692. + MMAL_PARAM_FLICKERAVOID_60HZ,
  15693. + MMAL_PARAM_FLICKERAVOID_MAX = 0x7FFFFFFF
  15694. +};
  15695. +
  15696. +/** Manner of video rate control */
  15697. +enum mmal_parameter_rate_control_mode {
  15698. + MMAL_VIDEO_RATECONTROL_DEFAULT,
  15699. + MMAL_VIDEO_RATECONTROL_VARIABLE,
  15700. + MMAL_VIDEO_RATECONTROL_CONSTANT,
  15701. + MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
  15702. + MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
  15703. +};
  15704. +
  15705. +/* video parameters */
  15706. +
  15707. +enum mmal_parameter_video_type {
  15708. + /** @ref MMAL_DISPLAYREGION_T */
  15709. + MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
  15710. +
  15711. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  15712. + MMAL_PARAMETER_SUPPORTED_PROFILES,
  15713. +
  15714. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  15715. + MMAL_PARAMETER_PROFILE,
  15716. +
  15717. + /** @ref MMAL_PARAMETER_UINT32_T */
  15718. + MMAL_PARAMETER_INTRAPERIOD,
  15719. +
  15720. + /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
  15721. + MMAL_PARAMETER_RATECONTROL,
  15722. +
  15723. + /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
  15724. + MMAL_PARAMETER_NALUNITFORMAT,
  15725. +
  15726. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15727. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  15728. +
  15729. + /** @ref MMAL_PARAMETER_UINT32_T.
  15730. + * Setting the value to zero resets to the default (one slice per frame).
  15731. + */
  15732. + MMAL_PARAMETER_MB_ROWS_PER_SLICE,
  15733. +
  15734. + /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
  15735. + MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
  15736. +
  15737. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
  15738. + MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
  15739. +
  15740. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
  15741. + MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
  15742. +
  15743. + /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
  15744. + MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
  15745. + /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
  15746. + MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
  15747. +
  15748. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15749. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  15750. +
  15751. + /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
  15752. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  15753. +
  15754. + /** @ref MMAL_PARAMETER_FRAME_RATE_T */
  15755. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  15756. +
  15757. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15758. + MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
  15759. +
  15760. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15761. + MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
  15762. +
  15763. + /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
  15764. + MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
  15765. +
  15766. + MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
  15767. + /** @ref MMAL_PARAMETER_UINT32_T.
  15768. + * Changing this parameter from the default can reduce frame rate
  15769. + * because image buffers need to be re-pitched.
  15770. + */
  15771. + MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
  15772. +
  15773. + /** @ref MMAL_PARAMETER_UINT32_T.
  15774. + * Changing this parameter from the default can reduce frame rate
  15775. + * because image buffers need to be re-pitched.
  15776. + */
  15777. + MMAL_PARAMETER_VIDEO_ALIGN_VERT,
  15778. +
  15779. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15780. + MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
  15781. +
  15782. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15783. + MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
  15784. +
  15785. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  15786. + MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
  15787. +
  15788. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  15789. + MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
  15790. +
  15791. + /** @ref MMAL_PARAMETER_UINT32_T */
  15792. + MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
  15793. +
  15794. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15795. + MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
  15796. +
  15797. + /* H264 specific parameters */
  15798. +
  15799. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15800. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
  15801. +
  15802. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15803. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
  15804. +
  15805. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15806. + MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
  15807. +
  15808. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15809. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
  15810. +
  15811. + /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
  15812. + MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
  15813. +
  15814. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15815. + MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
  15816. +
  15817. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15818. + MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
  15819. +
  15820. + /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
  15821. + MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
  15822. +
  15823. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15824. + MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
  15825. +
  15826. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15827. + MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
  15828. +
  15829. + /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
  15830. + MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
  15831. +
  15832. + /** @ref MMAL_PARAMETER_BYTES_T */
  15833. + MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3,
  15834. +
  15835. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15836. + MMAL_PARAMETER_VIDEO_ENCODE_H264_VCL_HRD_PARAMETERS,
  15837. +
  15838. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15839. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_DELAY_HRD_FLAG,
  15840. +
  15841. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15842. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER
  15843. +};
  15844. +
  15845. +/** Valid mirror modes */
  15846. +enum mmal_parameter_mirror {
  15847. + MMAL_PARAM_MIRROR_NONE,
  15848. + MMAL_PARAM_MIRROR_VERTICAL,
  15849. + MMAL_PARAM_MIRROR_HORIZONTAL,
  15850. + MMAL_PARAM_MIRROR_BOTH,
  15851. +};
  15852. +
  15853. +enum mmal_parameter_displaytransform {
  15854. + MMAL_DISPLAY_ROT0 = 0,
  15855. + MMAL_DISPLAY_MIRROR_ROT0 = 1,
  15856. + MMAL_DISPLAY_MIRROR_ROT180 = 2,
  15857. + MMAL_DISPLAY_ROT180 = 3,
  15858. + MMAL_DISPLAY_MIRROR_ROT90 = 4,
  15859. + MMAL_DISPLAY_ROT270 = 5,
  15860. + MMAL_DISPLAY_ROT90 = 6,
  15861. + MMAL_DISPLAY_MIRROR_ROT270 = 7,
  15862. +};
  15863. +
  15864. +enum mmal_parameter_displaymode {
  15865. + MMAL_DISPLAY_MODE_FILL = 0,
  15866. + MMAL_DISPLAY_MODE_LETTERBOX = 1,
  15867. +};
  15868. +
  15869. +enum mmal_parameter_displayset {
  15870. + MMAL_DISPLAY_SET_NONE = 0,
  15871. + MMAL_DISPLAY_SET_NUM = 1,
  15872. + MMAL_DISPLAY_SET_FULLSCREEN = 2,
  15873. + MMAL_DISPLAY_SET_TRANSFORM = 4,
  15874. + MMAL_DISPLAY_SET_DEST_RECT = 8,
  15875. + MMAL_DISPLAY_SET_SRC_RECT = 0x10,
  15876. + MMAL_DISPLAY_SET_MODE = 0x20,
  15877. + MMAL_DISPLAY_SET_PIXEL = 0x40,
  15878. + MMAL_DISPLAY_SET_NOASPECT = 0x80,
  15879. + MMAL_DISPLAY_SET_LAYER = 0x100,
  15880. + MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
  15881. + MMAL_DISPLAY_SET_ALPHA = 0x400,
  15882. +};
  15883. +
  15884. +struct mmal_parameter_displayregion {
  15885. + /** Bitfield that indicates which fields are set and should be
  15886. + * used. All other fields will maintain their current value.
  15887. + * \ref MMAL_DISPLAYSET_T defines the bits that can be
  15888. + * combined.
  15889. + */
  15890. + u32 set;
  15891. +
  15892. + /** Describes the display output device, with 0 typically
  15893. + * being a directly connected LCD display. The actual values
  15894. + * will depend on the hardware. Code using hard-wired numbers
  15895. + * (e.g. 2) is certain to fail.
  15896. + */
  15897. +
  15898. + u32 display_num;
  15899. + /** Indicates that we are using the full device screen area,
  15900. + * rather than a window of the display. If zero, then
  15901. + * dest_rect is used to specify a region of the display to
  15902. + * use.
  15903. + */
  15904. +
  15905. + s32 fullscreen;
  15906. + /** Indicates any rotation or flipping used to map frames onto
  15907. + * the natural display orientation.
  15908. + */
  15909. + u32 transform; /* enum mmal_parameter_displaytransform */
  15910. +
  15911. + /** Where to display the frame within the screen, if
  15912. + * fullscreen is zero.
  15913. + */
  15914. + struct vchiq_mmal_rect dest_rect;
  15915. +
  15916. + /** Indicates which area of the frame to display. If all
  15917. + * values are zero, the whole frame will be used.
  15918. + */
  15919. + struct vchiq_mmal_rect src_rect;
  15920. +
  15921. + /** If set to non-zero, indicates that any display scaling
  15922. + * should disregard the aspect ratio of the frame region being
  15923. + * displayed.
  15924. + */
  15925. + s32 noaspect;
  15926. +
  15927. + /** Indicates how the image should be scaled to fit the
  15928. + * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
  15929. + * that the image should fill the screen by potentially
  15930. + * cropping the frames. Setting \code mode \endcode to \code
  15931. + * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
  15932. + * source region should be displayed and black bars added if
  15933. + * necessary.
  15934. + */
  15935. + u32 mode; /* enum mmal_parameter_displaymode */
  15936. +
  15937. + /** If non-zero, defines the width of a source pixel relative
  15938. + * to \code pixel_y \endcode. If zero, then pixels default to
  15939. + * being square.
  15940. + */
  15941. + u32 pixel_x;
  15942. +
  15943. + /** If non-zero, defines the height of a source pixel relative
  15944. + * to \code pixel_x \endcode. If zero, then pixels default to
  15945. + * being square.
  15946. + */
  15947. + u32 pixel_y;
  15948. +
  15949. + /** Sets the relative depth of the images, with greater values
  15950. + * being in front of smaller values.
  15951. + */
  15952. + u32 layer;
  15953. +
  15954. + /** Set to non-zero to ensure copy protection is used on
  15955. + * output.
  15956. + */
  15957. + s32 copyprotect_required;
  15958. +
  15959. + /** Level of opacity of the layer, where zero is fully
  15960. + * transparent and 255 is fully opaque.
  15961. + */
  15962. + u32 alpha;
  15963. +};
  15964. +
  15965. +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
  15966. +
  15967. +struct mmal_parameter_imagefx_parameters {
  15968. + enum mmal_parameter_imagefx effect;
  15969. + u32 num_effect_params;
  15970. + u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
  15971. +};
  15972. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-vchiq.c linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.c
  15973. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  15974. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.c 2014-03-11 17:51:15.000000000 +0100
  15975. @@ -0,0 +1,1916 @@
  15976. +/*
  15977. + * Broadcom BM2835 V4L2 driver
  15978. + *
  15979. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  15980. + *
  15981. + * This file is subject to the terms and conditions of the GNU General Public
  15982. + * License. See the file COPYING in the main directory of this archive
  15983. + * for more details.
  15984. + *
  15985. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  15986. + * Dave Stevenson <dsteve@broadcom.com>
  15987. + * Simon Mellor <simellor@broadcom.com>
  15988. + * Luke Diamand <luked@broadcom.com>
  15989. + *
  15990. + * V4L2 driver MMAL vchiq interface code
  15991. + */
  15992. +
  15993. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15994. +
  15995. +#include <linux/errno.h>
  15996. +#include <linux/kernel.h>
  15997. +#include <linux/mutex.h>
  15998. +#include <linux/mm.h>
  15999. +#include <linux/slab.h>
  16000. +#include <linux/completion.h>
  16001. +#include <linux/vmalloc.h>
  16002. +#include <asm/cacheflush.h>
  16003. +#include <media/videobuf2-vmalloc.h>
  16004. +
  16005. +#include "mmal-common.h"
  16006. +#include "mmal-vchiq.h"
  16007. +#include "mmal-msg.h"
  16008. +
  16009. +#define USE_VCHIQ_ARM
  16010. +#include "interface/vchi/vchi.h"
  16011. +
  16012. +/* maximum number of components supported */
  16013. +#define VCHIQ_MMAL_MAX_COMPONENTS 4
  16014. +
  16015. +/*#define FULL_MSG_DUMP 1*/
  16016. +
  16017. +#ifdef DEBUG
  16018. +static const char *const msg_type_names[] = {
  16019. + "UNKNOWN",
  16020. + "QUIT",
  16021. + "SERVICE_CLOSED",
  16022. + "GET_VERSION",
  16023. + "COMPONENT_CREATE",
  16024. + "COMPONENT_DESTROY",
  16025. + "COMPONENT_ENABLE",
  16026. + "COMPONENT_DISABLE",
  16027. + "PORT_INFO_GET",
  16028. + "PORT_INFO_SET",
  16029. + "PORT_ACTION",
  16030. + "BUFFER_FROM_HOST",
  16031. + "BUFFER_TO_HOST",
  16032. + "GET_STATS",
  16033. + "PORT_PARAMETER_SET",
  16034. + "PORT_PARAMETER_GET",
  16035. + "EVENT_TO_HOST",
  16036. + "GET_CORE_STATS_FOR_PORT",
  16037. + "OPAQUE_ALLOCATOR",
  16038. + "CONSUME_MEM",
  16039. + "LMK",
  16040. + "OPAQUE_ALLOCATOR_DESC",
  16041. + "DRM_GET_LHS32",
  16042. + "DRM_GET_TIME",
  16043. + "BUFFER_FROM_HOST_ZEROLEN",
  16044. + "PORT_FLUSH",
  16045. + "HOST_LOG",
  16046. +};
  16047. +#endif
  16048. +
  16049. +static const char *const port_action_type_names[] = {
  16050. + "UNKNOWN",
  16051. + "ENABLE",
  16052. + "DISABLE",
  16053. + "FLUSH",
  16054. + "CONNECT",
  16055. + "DISCONNECT",
  16056. + "SET_REQUIREMENTS",
  16057. +};
  16058. +
  16059. +#if defined(DEBUG)
  16060. +#if defined(FULL_MSG_DUMP)
  16061. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  16062. + do { \
  16063. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  16064. + msg_type_names[(MSG)->h.type], \
  16065. + (MSG)->h.type, (MSG_LEN)); \
  16066. + print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
  16067. + 16, 4, (MSG), \
  16068. + sizeof(struct mmal_msg_header), 1); \
  16069. + print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
  16070. + 16, 4, \
  16071. + ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
  16072. + (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
  16073. + } while (0)
  16074. +#else
  16075. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  16076. + { \
  16077. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  16078. + msg_type_names[(MSG)->h.type], \
  16079. + (MSG)->h.type, (MSG_LEN)); \
  16080. + }
  16081. +#endif
  16082. +#else
  16083. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
  16084. +#endif
  16085. +
  16086. +/* normal message context */
  16087. +struct mmal_msg_context {
  16088. + union {
  16089. + struct {
  16090. + /* work struct for defered callback - must come first */
  16091. + struct work_struct work;
  16092. + /* mmal instance */
  16093. + struct vchiq_mmal_instance *instance;
  16094. + /* mmal port */
  16095. + struct vchiq_mmal_port *port;
  16096. + /* actual buffer used to store bulk reply */
  16097. + struct mmal_buffer *buffer;
  16098. + /* amount of buffer used */
  16099. + unsigned long buffer_used;
  16100. + /* MMAL buffer flags */
  16101. + u32 mmal_flags;
  16102. + /* Presentation and Decode timestamps */
  16103. + s64 pts;
  16104. + s64 dts;
  16105. +
  16106. + int status; /* context status */
  16107. +
  16108. + } bulk; /* bulk data */
  16109. +
  16110. + struct {
  16111. + /* message handle to release */
  16112. + VCHI_HELD_MSG_T msg_handle;
  16113. + /* pointer to received message */
  16114. + struct mmal_msg *msg;
  16115. + /* received message length */
  16116. + u32 msg_len;
  16117. + /* completion upon reply */
  16118. + struct completion cmplt;
  16119. + } sync; /* synchronous response */
  16120. + } u;
  16121. +
  16122. +};
  16123. +
  16124. +struct vchiq_mmal_instance {
  16125. + VCHI_SERVICE_HANDLE_T handle;
  16126. +
  16127. + /* ensure serialised access to service */
  16128. + struct mutex vchiq_mutex;
  16129. +
  16130. + /* ensure serialised access to bulk operations */
  16131. + struct mutex bulk_mutex;
  16132. +
  16133. + /* vmalloc page to receive scratch bulk xfers into */
  16134. + void *bulk_scratch;
  16135. +
  16136. + /* component to use next */
  16137. + int component_idx;
  16138. + struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
  16139. +};
  16140. +
  16141. +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
  16142. + *instance)
  16143. +{
  16144. + struct mmal_msg_context *msg_context;
  16145. +
  16146. + /* todo: should this be allocated from a pool to avoid kmalloc */
  16147. + msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
  16148. + memset(msg_context, 0, sizeof(*msg_context));
  16149. +
  16150. + return msg_context;
  16151. +}
  16152. +
  16153. +static void release_msg_context(struct mmal_msg_context *msg_context)
  16154. +{
  16155. + kfree(msg_context);
  16156. +}
  16157. +
  16158. +/* deals with receipt of event to host message */
  16159. +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
  16160. + struct mmal_msg *msg, u32 msg_len)
  16161. +{
  16162. + pr_debug("unhandled event\n");
  16163. + pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
  16164. + msg->u.event_to_host.client_component,
  16165. + msg->u.event_to_host.port_type,
  16166. + msg->u.event_to_host.port_num,
  16167. + msg->u.event_to_host.cmd, msg->u.event_to_host.length);
  16168. +}
  16169. +
  16170. +/* workqueue scheduled callback
  16171. + *
  16172. + * we do this because it is important we do not call any other vchiq
  16173. + * sync calls from witin the message delivery thread
  16174. + */
  16175. +static void buffer_work_cb(struct work_struct *work)
  16176. +{
  16177. + struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
  16178. +
  16179. + msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
  16180. + msg_context->u.bulk.port,
  16181. + msg_context->u.bulk.status,
  16182. + msg_context->u.bulk.buffer,
  16183. + msg_context->u.bulk.buffer_used,
  16184. + msg_context->u.bulk.mmal_flags,
  16185. + msg_context->u.bulk.dts,
  16186. + msg_context->u.bulk.pts);
  16187. +
  16188. + /* release message context */
  16189. + release_msg_context(msg_context);
  16190. +}
  16191. +
  16192. +/* enqueue a bulk receive for a given message context */
  16193. +static int bulk_receive(struct vchiq_mmal_instance *instance,
  16194. + struct mmal_msg *msg,
  16195. + struct mmal_msg_context *msg_context)
  16196. +{
  16197. + unsigned long rd_len;
  16198. + unsigned long flags = 0;
  16199. + int ret;
  16200. +
  16201. + /* bulk mutex stops other bulk operations while we have a
  16202. + * receive in progress - released in callback
  16203. + */
  16204. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  16205. + if (ret != 0)
  16206. + return ret;
  16207. +
  16208. + rd_len = msg->u.buffer_from_host.buffer_header.length;
  16209. +
  16210. + /* take buffer from queue */
  16211. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  16212. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  16213. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16214. + pr_err("buffer list empty trying to submit bulk receive\n");
  16215. +
  16216. + /* todo: this is a serious error, we should never have
  16217. + * commited a buffer_to_host operation to the mmal
  16218. + * port without the buffer to back it up (underflow
  16219. + * handling) and there is no obvious way to deal with
  16220. + * this - how is the mmal servie going to react when
  16221. + * we fail to do the xfer and reschedule a buffer when
  16222. + * it arrives? perhaps a starved flag to indicate a
  16223. + * waiting bulk receive?
  16224. + */
  16225. +
  16226. + mutex_unlock(&instance->bulk_mutex);
  16227. +
  16228. + return -EINVAL;
  16229. + }
  16230. +
  16231. + msg_context->u.bulk.buffer =
  16232. + list_entry(msg_context->u.bulk.port->buffers.next,
  16233. + struct mmal_buffer, list);
  16234. + list_del(&msg_context->u.bulk.buffer->list);
  16235. +
  16236. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16237. +
  16238. + /* ensure we do not overrun the available buffer */
  16239. + if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
  16240. + rd_len = msg_context->u.bulk.buffer->buffer_size;
  16241. + pr_warn("short read as not enough receive buffer space\n");
  16242. + /* todo: is this the correct response, what happens to
  16243. + * the rest of the message data?
  16244. + */
  16245. + }
  16246. +
  16247. + /* store length */
  16248. + msg_context->u.bulk.buffer_used = rd_len;
  16249. + msg_context->u.bulk.mmal_flags =
  16250. + msg->u.buffer_from_host.buffer_header.flags;
  16251. + msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
  16252. + msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
  16253. +
  16254. + // only need to flush L1 cache here, as VCHIQ takes care of the L2
  16255. + // cache.
  16256. + __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
  16257. +
  16258. + /* queue the bulk submission */
  16259. + vchi_service_use(instance->handle);
  16260. + ret = vchi_bulk_queue_receive(instance->handle,
  16261. + msg_context->u.bulk.buffer->buffer,
  16262. + /* Actual receive needs to be a multiple
  16263. + * of 4 bytes
  16264. + */
  16265. + (rd_len + 3) & ~3,
  16266. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  16267. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  16268. + msg_context);
  16269. +
  16270. + vchi_service_release(instance->handle);
  16271. +
  16272. + if (ret != 0) {
  16273. + /* callback will not be clearing the mutex */
  16274. + mutex_unlock(&instance->bulk_mutex);
  16275. + }
  16276. +
  16277. + return ret;
  16278. +}
  16279. +
  16280. +/* enque a dummy bulk receive for a given message context */
  16281. +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
  16282. + struct mmal_msg_context *msg_context)
  16283. +{
  16284. + int ret;
  16285. +
  16286. + /* bulk mutex stops other bulk operations while we have a
  16287. + * receive in progress - released in callback
  16288. + */
  16289. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  16290. + if (ret != 0)
  16291. + return ret;
  16292. +
  16293. + /* zero length indicates this was a dummy transfer */
  16294. + msg_context->u.bulk.buffer_used = 0;
  16295. +
  16296. + /* queue the bulk submission */
  16297. + vchi_service_use(instance->handle);
  16298. +
  16299. + ret = vchi_bulk_queue_receive(instance->handle,
  16300. + instance->bulk_scratch,
  16301. + 8,
  16302. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  16303. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  16304. + msg_context);
  16305. +
  16306. + vchi_service_release(instance->handle);
  16307. +
  16308. + if (ret != 0) {
  16309. + /* callback will not be clearing the mutex */
  16310. + mutex_unlock(&instance->bulk_mutex);
  16311. + }
  16312. +
  16313. + return ret;
  16314. +}
  16315. +
  16316. +/* data in message, memcpy from packet into output buffer */
  16317. +static int inline_receive(struct vchiq_mmal_instance *instance,
  16318. + struct mmal_msg *msg,
  16319. + struct mmal_msg_context *msg_context)
  16320. +{
  16321. + unsigned long flags = 0;
  16322. +
  16323. + /* take buffer from queue */
  16324. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  16325. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  16326. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16327. + pr_err("buffer list empty trying to receive inline\n");
  16328. +
  16329. + /* todo: this is a serious error, we should never have
  16330. + * commited a buffer_to_host operation to the mmal
  16331. + * port without the buffer to back it up (with
  16332. + * underflow handling) and there is no obvious way to
  16333. + * deal with this. Less bad than the bulk case as we
  16334. + * can just drop this on the floor but...unhelpful
  16335. + */
  16336. + return -EINVAL;
  16337. + }
  16338. +
  16339. + msg_context->u.bulk.buffer =
  16340. + list_entry(msg_context->u.bulk.port->buffers.next,
  16341. + struct mmal_buffer, list);
  16342. + list_del(&msg_context->u.bulk.buffer->list);
  16343. +
  16344. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16345. +
  16346. + memcpy(msg_context->u.bulk.buffer->buffer,
  16347. + msg->u.buffer_from_host.short_data,
  16348. + msg->u.buffer_from_host.payload_in_message);
  16349. +
  16350. + msg_context->u.bulk.buffer_used =
  16351. + msg->u.buffer_from_host.payload_in_message;
  16352. +
  16353. + return 0;
  16354. +}
  16355. +
  16356. +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
  16357. +static int
  16358. +buffer_from_host(struct vchiq_mmal_instance *instance,
  16359. + struct vchiq_mmal_port *port, struct mmal_buffer *buf)
  16360. +{
  16361. + struct mmal_msg_context *msg_context;
  16362. + struct mmal_msg m;
  16363. + int ret;
  16364. +
  16365. + pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
  16366. +
  16367. + /* bulk mutex stops other bulk operations while we
  16368. + * have a receive in progress
  16369. + */
  16370. + if (mutex_lock_interruptible(&instance->bulk_mutex))
  16371. + return -EINTR;
  16372. +
  16373. + /* get context */
  16374. + msg_context = get_msg_context(instance);
  16375. + if (msg_context == NULL)
  16376. + return -ENOMEM;
  16377. +
  16378. + /* store bulk message context for when data arrives */
  16379. + msg_context->u.bulk.instance = instance;
  16380. + msg_context->u.bulk.port = port;
  16381. + msg_context->u.bulk.buffer = NULL; /* not valid until bulk xfer */
  16382. + msg_context->u.bulk.buffer_used = 0;
  16383. +
  16384. + /* initialise work structure ready to schedule callback */
  16385. + INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
  16386. +
  16387. + /* prep the buffer from host message */
  16388. + memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
  16389. +
  16390. + m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
  16391. + m.h.magic = MMAL_MAGIC;
  16392. + m.h.context = msg_context;
  16393. + m.h.status = 0;
  16394. +
  16395. + /* drvbuf is our private data passed back */
  16396. + m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
  16397. + m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
  16398. + m.u.buffer_from_host.drvbuf.port_handle = port->handle;
  16399. + m.u.buffer_from_host.drvbuf.client_context = msg_context;
  16400. +
  16401. + /* buffer header */
  16402. + m.u.buffer_from_host.buffer_header.cmd = 0;
  16403. + m.u.buffer_from_host.buffer_header.data = buf->buffer;
  16404. + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
  16405. + m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
  16406. + m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
  16407. + m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
  16408. + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
  16409. + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
  16410. +
  16411. + /* clear buffer type sepecific data */
  16412. + memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
  16413. + sizeof(m.u.buffer_from_host.buffer_header_type_specific));
  16414. +
  16415. + /* no payload in message */
  16416. + m.u.buffer_from_host.payload_in_message = 0;
  16417. +
  16418. + vchi_service_use(instance->handle);
  16419. +
  16420. + ret = vchi_msg_queue(instance->handle, &m,
  16421. + sizeof(struct mmal_msg_header) +
  16422. + sizeof(m.u.buffer_from_host),
  16423. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  16424. +
  16425. + if (ret != 0) {
  16426. + release_msg_context(msg_context);
  16427. + /* todo: is this correct error value? */
  16428. + }
  16429. +
  16430. + vchi_service_release(instance->handle);
  16431. +
  16432. + mutex_unlock(&instance->bulk_mutex);
  16433. +
  16434. + return ret;
  16435. +}
  16436. +
  16437. +/* submit a buffer to the mmal sevice
  16438. + *
  16439. + * the buffer_from_host uses size data from the ports next available
  16440. + * mmal_buffer and deals with there being no buffer available by
  16441. + * incrementing the underflow for later
  16442. + */
  16443. +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
  16444. + struct vchiq_mmal_port *port)
  16445. +{
  16446. + int ret;
  16447. + struct mmal_buffer *buf;
  16448. + unsigned long flags = 0;
  16449. +
  16450. + if (!port->enabled)
  16451. + return -EINVAL;
  16452. +
  16453. + /* peek buffer from queue */
  16454. + spin_lock_irqsave(&port->slock, flags);
  16455. + if (list_empty(&port->buffers)) {
  16456. + port->buffer_underflow++;
  16457. + spin_unlock_irqrestore(&port->slock, flags);
  16458. + return -ENOSPC;
  16459. + }
  16460. +
  16461. + buf = list_entry(port->buffers.next, struct mmal_buffer, list);
  16462. +
  16463. + spin_unlock_irqrestore(&port->slock, flags);
  16464. +
  16465. + /* issue buffer to mmal service */
  16466. + ret = buffer_from_host(instance, port, buf);
  16467. + if (ret) {
  16468. + pr_err("adding buffer header failed\n");
  16469. + /* todo: how should this be dealt with */
  16470. + }
  16471. +
  16472. + return ret;
  16473. +}
  16474. +
  16475. +/* deals with receipt of buffer to host message */
  16476. +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
  16477. + struct mmal_msg *msg, u32 msg_len)
  16478. +{
  16479. + struct mmal_msg_context *msg_context;
  16480. +
  16481. + pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
  16482. + instance, msg, msg_len);
  16483. +
  16484. + if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
  16485. + msg_context = msg->u.buffer_from_host.drvbuf.client_context;
  16486. + } else {
  16487. + pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
  16488. + return;
  16489. + }
  16490. +
  16491. + if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
  16492. + /* message reception had an error */
  16493. + pr_warn("error %d in reply\n", msg->h.status);
  16494. +
  16495. + msg_context->u.bulk.status = msg->h.status;
  16496. +
  16497. + } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
  16498. + /* empty buffer */
  16499. + if (msg->u.buffer_from_host.buffer_header.flags &
  16500. + MMAL_BUFFER_HEADER_FLAG_EOS) {
  16501. + msg_context->u.bulk.status =
  16502. + dummy_bulk_receive(instance, msg_context);
  16503. + if (msg_context->u.bulk.status == 0)
  16504. + return; /* successful bulk submission, bulk
  16505. + * completion will trigger callback
  16506. + */
  16507. + } else {
  16508. + /* do callback with empty buffer - not EOS though */
  16509. + msg_context->u.bulk.status = 0;
  16510. + msg_context->u.bulk.buffer_used = 0;
  16511. + }
  16512. + } else if (msg->u.buffer_from_host.payload_in_message == 0) {
  16513. + /* data is not in message, queue a bulk receive */
  16514. + msg_context->u.bulk.status =
  16515. + bulk_receive(instance, msg, msg_context);
  16516. + if (msg_context->u.bulk.status == 0)
  16517. + return; /* successful bulk submission, bulk
  16518. + * completion will trigger callback
  16519. + */
  16520. +
  16521. + /* failed to submit buffer, this will end badly */
  16522. + pr_err("error %d on bulk submission\n",
  16523. + msg_context->u.bulk.status);
  16524. +
  16525. + } else if (msg->u.buffer_from_host.payload_in_message <=
  16526. + MMAL_VC_SHORT_DATA) {
  16527. + /* data payload within message */
  16528. + msg_context->u.bulk.status = inline_receive(instance, msg,
  16529. + msg_context);
  16530. + } else {
  16531. + pr_err("message with invalid short payload\n");
  16532. +
  16533. + /* signal error */
  16534. + msg_context->u.bulk.status = -EINVAL;
  16535. + msg_context->u.bulk.buffer_used =
  16536. + msg->u.buffer_from_host.payload_in_message;
  16537. + }
  16538. +
  16539. + /* replace the buffer header */
  16540. + port_buffer_from_host(instance, msg_context->u.bulk.port);
  16541. +
  16542. + /* schedule the port callback */
  16543. + schedule_work(&msg_context->u.bulk.work);
  16544. +}
  16545. +
  16546. +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
  16547. + struct mmal_msg_context *msg_context)
  16548. +{
  16549. + /* bulk receive operation complete */
  16550. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  16551. +
  16552. + /* replace the buffer header */
  16553. + port_buffer_from_host(msg_context->u.bulk.instance,
  16554. + msg_context->u.bulk.port);
  16555. +
  16556. + msg_context->u.bulk.status = 0;
  16557. +
  16558. + /* schedule the port callback */
  16559. + schedule_work(&msg_context->u.bulk.work);
  16560. +}
  16561. +
  16562. +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
  16563. + struct mmal_msg_context *msg_context)
  16564. +{
  16565. + pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
  16566. +
  16567. + /* bulk receive operation complete */
  16568. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  16569. +
  16570. + /* replace the buffer header */
  16571. + port_buffer_from_host(msg_context->u.bulk.instance,
  16572. + msg_context->u.bulk.port);
  16573. +
  16574. + msg_context->u.bulk.status = -EINTR;
  16575. +
  16576. + schedule_work(&msg_context->u.bulk.work);
  16577. +}
  16578. +
  16579. +/* incoming event service callback */
  16580. +static void service_callback(void *param,
  16581. + const VCHI_CALLBACK_REASON_T reason,
  16582. + void *bulk_ctx)
  16583. +{
  16584. + struct vchiq_mmal_instance *instance = param;
  16585. + int status;
  16586. + u32 msg_len;
  16587. + struct mmal_msg *msg;
  16588. + VCHI_HELD_MSG_T msg_handle;
  16589. +
  16590. + if (!instance) {
  16591. + pr_err("Message callback passed NULL instance\n");
  16592. + return;
  16593. + }
  16594. +
  16595. + switch (reason) {
  16596. + case VCHI_CALLBACK_MSG_AVAILABLE:
  16597. + status = vchi_msg_hold(instance->handle, (void **)&msg,
  16598. + &msg_len, VCHI_FLAGS_NONE, &msg_handle);
  16599. + if (status) {
  16600. + pr_err("Unable to dequeue a message (%d)\n", status);
  16601. + break;
  16602. + }
  16603. +
  16604. + DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
  16605. +
  16606. + /* handling is different for buffer messages */
  16607. + switch (msg->h.type) {
  16608. +
  16609. + case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
  16610. + vchi_held_msg_release(&msg_handle);
  16611. + break;
  16612. +
  16613. + case MMAL_MSG_TYPE_EVENT_TO_HOST:
  16614. + event_to_host_cb(instance, msg, msg_len);
  16615. + vchi_held_msg_release(&msg_handle);
  16616. +
  16617. + break;
  16618. +
  16619. + case MMAL_MSG_TYPE_BUFFER_TO_HOST:
  16620. + buffer_to_host_cb(instance, msg, msg_len);
  16621. + vchi_held_msg_release(&msg_handle);
  16622. + break;
  16623. +
  16624. + default:
  16625. + /* messages dependant on header context to complete */
  16626. +
  16627. + /* todo: the msg.context really ought to be sanity
  16628. + * checked before we just use it, afaict it comes back
  16629. + * and is used raw from the videocore. Perhaps it
  16630. + * should be verified the address lies in the kernel
  16631. + * address space.
  16632. + */
  16633. + if (msg->h.context == NULL) {
  16634. + pr_err("received message context was null!\n");
  16635. + vchi_held_msg_release(&msg_handle);
  16636. + break;
  16637. + }
  16638. +
  16639. + /* fill in context values */
  16640. + msg->h.context->u.sync.msg_handle = msg_handle;
  16641. + msg->h.context->u.sync.msg = msg;
  16642. + msg->h.context->u.sync.msg_len = msg_len;
  16643. +
  16644. + /* todo: should this check (completion_done()
  16645. + * == 1) for no one waiting? or do we need a
  16646. + * flag to tell us the completion has been
  16647. + * interrupted so we can free the message and
  16648. + * its context. This probably also solves the
  16649. + * message arriving after interruption todo
  16650. + * below
  16651. + */
  16652. +
  16653. + /* complete message so caller knows it happened */
  16654. + complete(&msg->h.context->u.sync.cmplt);
  16655. + break;
  16656. + }
  16657. +
  16658. + break;
  16659. +
  16660. + case VCHI_CALLBACK_BULK_RECEIVED:
  16661. + bulk_receive_cb(instance, bulk_ctx);
  16662. + break;
  16663. +
  16664. + case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
  16665. + bulk_abort_cb(instance, bulk_ctx);
  16666. + break;
  16667. +
  16668. + case VCHI_CALLBACK_SERVICE_CLOSED:
  16669. + /* TODO: consider if this requires action if received when
  16670. + * driver is not explicitly closing the service
  16671. + */
  16672. + break;
  16673. +
  16674. + default:
  16675. + pr_err("Received unhandled message reason %d\n", reason);
  16676. + break;
  16677. + }
  16678. +}
  16679. +
  16680. +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
  16681. + struct mmal_msg *msg,
  16682. + unsigned int payload_len,
  16683. + struct mmal_msg **msg_out,
  16684. + VCHI_HELD_MSG_T *msg_handle_out)
  16685. +{
  16686. + struct mmal_msg_context msg_context;
  16687. + int ret;
  16688. +
  16689. + /* payload size must not cause message to exceed max size */
  16690. + if (payload_len >
  16691. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
  16692. + pr_err("payload length %d exceeds max:%d\n", payload_len,
  16693. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
  16694. + return -EINVAL;
  16695. + }
  16696. +
  16697. + init_completion(&msg_context.u.sync.cmplt);
  16698. +
  16699. + msg->h.magic = MMAL_MAGIC;
  16700. + msg->h.context = &msg_context;
  16701. + msg->h.status = 0;
  16702. +
  16703. + DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
  16704. + ">>> sync message");
  16705. +
  16706. + vchi_service_use(instance->handle);
  16707. +
  16708. + ret = vchi_msg_queue(instance->handle,
  16709. + msg,
  16710. + sizeof(struct mmal_msg_header) + payload_len,
  16711. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  16712. +
  16713. + vchi_service_release(instance->handle);
  16714. +
  16715. + if (ret) {
  16716. + pr_err("error %d queuing message\n", ret);
  16717. + return ret;
  16718. + }
  16719. +
  16720. + ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, HZ);
  16721. + if (ret <= 0) {
  16722. + pr_err("error %d waiting for sync completion\n", ret);
  16723. + if (ret == 0)
  16724. + ret = -ETIME;
  16725. + /* todo: what happens if the message arrives after aborting */
  16726. + return ret;
  16727. + }
  16728. +
  16729. + *msg_out = msg_context.u.sync.msg;
  16730. + *msg_handle_out = msg_context.u.sync.msg_handle;
  16731. +
  16732. + return 0;
  16733. +}
  16734. +
  16735. +static void dump_port_info(struct vchiq_mmal_port *port)
  16736. +{
  16737. + pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
  16738. +
  16739. + pr_debug("buffer minimum num:%d size:%d align:%d\n",
  16740. + port->minimum_buffer.num,
  16741. + port->minimum_buffer.size, port->minimum_buffer.alignment);
  16742. +
  16743. + pr_debug("buffer recommended num:%d size:%d align:%d\n",
  16744. + port->recommended_buffer.num,
  16745. + port->recommended_buffer.size,
  16746. + port->recommended_buffer.alignment);
  16747. +
  16748. + pr_debug("buffer current values num:%d size:%d align:%d\n",
  16749. + port->current_buffer.num,
  16750. + port->current_buffer.size, port->current_buffer.alignment);
  16751. +
  16752. + pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
  16753. + port->format.type,
  16754. + port->format.encoding, port->format.encoding_variant);
  16755. +
  16756. + pr_debug(" bitrate:%d flags:0x%x\n",
  16757. + port->format.bitrate, port->format.flags);
  16758. +
  16759. + if (port->format.type == MMAL_ES_TYPE_VIDEO) {
  16760. + pr_debug
  16761. + ("es video format: width:%d height:%d colourspace:0x%x\n",
  16762. + port->es.video.width, port->es.video.height,
  16763. + port->es.video.color_space);
  16764. +
  16765. + pr_debug(" : crop xywh %d,%d,%d,%d\n",
  16766. + port->es.video.crop.x,
  16767. + port->es.video.crop.y,
  16768. + port->es.video.crop.width, port->es.video.crop.height);
  16769. + pr_debug(" : framerate %d/%d aspect %d/%d\n",
  16770. + port->es.video.frame_rate.num,
  16771. + port->es.video.frame_rate.den,
  16772. + port->es.video.par.num, port->es.video.par.den);
  16773. + }
  16774. +}
  16775. +
  16776. +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
  16777. +{
  16778. +
  16779. + /* todo do readonly fields need setting at all? */
  16780. + p->type = port->type;
  16781. + p->index = port->index;
  16782. + p->index_all = 0;
  16783. + p->is_enabled = port->enabled;
  16784. + p->buffer_num_min = port->minimum_buffer.num;
  16785. + p->buffer_size_min = port->minimum_buffer.size;
  16786. + p->buffer_alignment_min = port->minimum_buffer.alignment;
  16787. + p->buffer_num_recommended = port->recommended_buffer.num;
  16788. + p->buffer_size_recommended = port->recommended_buffer.size;
  16789. +
  16790. + /* only three writable fields in a port */
  16791. + p->buffer_num = port->current_buffer.num;
  16792. + p->buffer_size = port->current_buffer.size;
  16793. + p->userdata = port;
  16794. +}
  16795. +
  16796. +static int port_info_set(struct vchiq_mmal_instance *instance,
  16797. + struct vchiq_mmal_port *port)
  16798. +{
  16799. + int ret;
  16800. + struct mmal_msg m;
  16801. + struct mmal_msg *rmsg;
  16802. + VCHI_HELD_MSG_T rmsg_handle;
  16803. +
  16804. + pr_debug("setting port info port %p\n", port);
  16805. + if (!port)
  16806. + return -1;
  16807. + dump_port_info(port);
  16808. +
  16809. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
  16810. +
  16811. + m.u.port_info_set.component_handle = port->component->handle;
  16812. + m.u.port_info_set.port_type = port->type;
  16813. + m.u.port_info_set.port_index = port->index;
  16814. +
  16815. + port_to_mmal_msg(port, &m.u.port_info_set.port);
  16816. +
  16817. + /* elementry stream format setup */
  16818. + m.u.port_info_set.format.type = port->format.type;
  16819. + m.u.port_info_set.format.encoding = port->format.encoding;
  16820. + m.u.port_info_set.format.encoding_variant =
  16821. + port->format.encoding_variant;
  16822. + m.u.port_info_set.format.bitrate = port->format.bitrate;
  16823. + m.u.port_info_set.format.flags = port->format.flags;
  16824. +
  16825. + memcpy(&m.u.port_info_set.es, &port->es,
  16826. + sizeof(union mmal_es_specific_format));
  16827. +
  16828. + m.u.port_info_set.format.extradata_size = port->format.extradata_size;
  16829. + memcpy(rmsg->u.port_info_set.extradata, port->format.extradata,
  16830. + port->format.extradata_size);
  16831. +
  16832. + ret = send_synchronous_mmal_msg(instance, &m,
  16833. + sizeof(m.u.port_info_set),
  16834. + &rmsg, &rmsg_handle);
  16835. + if (ret)
  16836. + return ret;
  16837. +
  16838. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
  16839. + /* got an unexpected message type in reply */
  16840. + ret = -EINVAL;
  16841. + goto release_msg;
  16842. + }
  16843. +
  16844. + /* return operation status */
  16845. + ret = -rmsg->u.port_info_get_reply.status;
  16846. +
  16847. + pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
  16848. + port->component->handle, port->handle);
  16849. +
  16850. +release_msg:
  16851. + vchi_held_msg_release(&rmsg_handle);
  16852. +
  16853. + return ret;
  16854. +
  16855. +}
  16856. +
  16857. +/* use port info get message to retrive port information */
  16858. +static int port_info_get(struct vchiq_mmal_instance *instance,
  16859. + struct vchiq_mmal_port *port)
  16860. +{
  16861. + int ret;
  16862. + struct mmal_msg m;
  16863. + struct mmal_msg *rmsg;
  16864. + VCHI_HELD_MSG_T rmsg_handle;
  16865. +
  16866. + /* port info time */
  16867. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
  16868. + m.u.port_info_get.component_handle = port->component->handle;
  16869. + m.u.port_info_get.port_type = port->type;
  16870. + m.u.port_info_get.index = port->index;
  16871. +
  16872. + ret = send_synchronous_mmal_msg(instance, &m,
  16873. + sizeof(m.u.port_info_get),
  16874. + &rmsg, &rmsg_handle);
  16875. + if (ret)
  16876. + return ret;
  16877. +
  16878. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
  16879. + /* got an unexpected message type in reply */
  16880. + ret = -EINVAL;
  16881. + goto release_msg;
  16882. + }
  16883. +
  16884. + /* return operation status */
  16885. + ret = -rmsg->u.port_info_get_reply.status;
  16886. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  16887. + goto release_msg;
  16888. +
  16889. + if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
  16890. + port->enabled = false;
  16891. + else
  16892. + port->enabled = true;
  16893. +
  16894. + /* copy the values out of the message */
  16895. + port->handle = rmsg->u.port_info_get_reply.port_handle;
  16896. +
  16897. + /* port type and index cached to use on port info set becuase
  16898. + * it does not use a port handle
  16899. + */
  16900. + port->type = rmsg->u.port_info_get_reply.port_type;
  16901. + port->index = rmsg->u.port_info_get_reply.port_index;
  16902. +
  16903. + port->minimum_buffer.num =
  16904. + rmsg->u.port_info_get_reply.port.buffer_num_min;
  16905. + port->minimum_buffer.size =
  16906. + rmsg->u.port_info_get_reply.port.buffer_size_min;
  16907. + port->minimum_buffer.alignment =
  16908. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  16909. +
  16910. + port->recommended_buffer.alignment =
  16911. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  16912. + port->recommended_buffer.num =
  16913. + rmsg->u.port_info_get_reply.port.buffer_num_recommended;
  16914. +
  16915. + port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
  16916. + port->current_buffer.size =
  16917. + rmsg->u.port_info_get_reply.port.buffer_size;
  16918. +
  16919. + /* stream format */
  16920. + port->format.type = rmsg->u.port_info_get_reply.format.type;
  16921. + port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
  16922. + port->format.encoding_variant =
  16923. + rmsg->u.port_info_get_reply.format.encoding_variant;
  16924. + port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
  16925. + port->format.flags = rmsg->u.port_info_get_reply.format.flags;
  16926. +
  16927. + /* elementry stream format */
  16928. + memcpy(&port->es,
  16929. + &rmsg->u.port_info_get_reply.es,
  16930. + sizeof(union mmal_es_specific_format));
  16931. + port->format.es = &port->es;
  16932. +
  16933. + port->format.extradata_size =
  16934. + rmsg->u.port_info_get_reply.format.extradata_size;
  16935. + memcpy(port->format.extradata,
  16936. + rmsg->u.port_info_get_reply.extradata,
  16937. + port->format.extradata_size);
  16938. +
  16939. + pr_debug("received port info\n");
  16940. + dump_port_info(port);
  16941. +
  16942. +release_msg:
  16943. +
  16944. + pr_debug("%s:result:%d component:0x%x port:%d\n",
  16945. + __func__, ret, port->component->handle, port->handle);
  16946. +
  16947. + vchi_held_msg_release(&rmsg_handle);
  16948. +
  16949. + return ret;
  16950. +}
  16951. +
  16952. +/* create comonent on vc */
  16953. +static int create_component(struct vchiq_mmal_instance *instance,
  16954. + struct vchiq_mmal_component *component,
  16955. + const char *name)
  16956. +{
  16957. + int ret;
  16958. + struct mmal_msg m;
  16959. + struct mmal_msg *rmsg;
  16960. + VCHI_HELD_MSG_T rmsg_handle;
  16961. +
  16962. + /* build component create message */
  16963. + m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
  16964. + m.u.component_create.client_component = component;
  16965. + strncpy(m.u.component_create.name, name,
  16966. + sizeof(m.u.component_create.name));
  16967. +
  16968. + ret = send_synchronous_mmal_msg(instance, &m,
  16969. + sizeof(m.u.component_create),
  16970. + &rmsg, &rmsg_handle);
  16971. + if (ret)
  16972. + return ret;
  16973. +
  16974. + if (rmsg->h.type != m.h.type) {
  16975. + /* got an unexpected message type in reply */
  16976. + ret = -EINVAL;
  16977. + goto release_msg;
  16978. + }
  16979. +
  16980. + ret = -rmsg->u.component_create_reply.status;
  16981. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  16982. + goto release_msg;
  16983. +
  16984. + /* a valid component response received */
  16985. + component->handle = rmsg->u.component_create_reply.component_handle;
  16986. + component->inputs = rmsg->u.component_create_reply.input_num;
  16987. + component->outputs = rmsg->u.component_create_reply.output_num;
  16988. + component->clocks = rmsg->u.component_create_reply.clock_num;
  16989. +
  16990. + pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
  16991. + component->handle,
  16992. + component->inputs, component->outputs, component->clocks);
  16993. +
  16994. +release_msg:
  16995. + vchi_held_msg_release(&rmsg_handle);
  16996. +
  16997. + return ret;
  16998. +}
  16999. +
  17000. +/* destroys a component on vc */
  17001. +static int destroy_component(struct vchiq_mmal_instance *instance,
  17002. + struct vchiq_mmal_component *component)
  17003. +{
  17004. + int ret;
  17005. + struct mmal_msg m;
  17006. + struct mmal_msg *rmsg;
  17007. + VCHI_HELD_MSG_T rmsg_handle;
  17008. +
  17009. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
  17010. + m.u.component_destroy.component_handle = component->handle;
  17011. +
  17012. + ret = send_synchronous_mmal_msg(instance, &m,
  17013. + sizeof(m.u.component_destroy),
  17014. + &rmsg, &rmsg_handle);
  17015. + if (ret)
  17016. + return ret;
  17017. +
  17018. + if (rmsg->h.type != m.h.type) {
  17019. + /* got an unexpected message type in reply */
  17020. + ret = -EINVAL;
  17021. + goto release_msg;
  17022. + }
  17023. +
  17024. + ret = -rmsg->u.component_destroy_reply.status;
  17025. +
  17026. +release_msg:
  17027. +
  17028. + vchi_held_msg_release(&rmsg_handle);
  17029. +
  17030. + return ret;
  17031. +}
  17032. +
  17033. +/* enable a component on vc */
  17034. +static int enable_component(struct vchiq_mmal_instance *instance,
  17035. + struct vchiq_mmal_component *component)
  17036. +{
  17037. + int ret;
  17038. + struct mmal_msg m;
  17039. + struct mmal_msg *rmsg;
  17040. + VCHI_HELD_MSG_T rmsg_handle;
  17041. +
  17042. + m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
  17043. + m.u.component_enable.component_handle = component->handle;
  17044. +
  17045. + ret = send_synchronous_mmal_msg(instance, &m,
  17046. + sizeof(m.u.component_enable),
  17047. + &rmsg, &rmsg_handle);
  17048. + if (ret)
  17049. + return ret;
  17050. +
  17051. + if (rmsg->h.type != m.h.type) {
  17052. + /* got an unexpected message type in reply */
  17053. + ret = -EINVAL;
  17054. + goto release_msg;
  17055. + }
  17056. +
  17057. + ret = -rmsg->u.component_enable_reply.status;
  17058. +
  17059. +release_msg:
  17060. + vchi_held_msg_release(&rmsg_handle);
  17061. +
  17062. + return ret;
  17063. +}
  17064. +
  17065. +/* disable a component on vc */
  17066. +static int disable_component(struct vchiq_mmal_instance *instance,
  17067. + struct vchiq_mmal_component *component)
  17068. +{
  17069. + int ret;
  17070. + struct mmal_msg m;
  17071. + struct mmal_msg *rmsg;
  17072. + VCHI_HELD_MSG_T rmsg_handle;
  17073. +
  17074. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
  17075. + m.u.component_disable.component_handle = component->handle;
  17076. +
  17077. + ret = send_synchronous_mmal_msg(instance, &m,
  17078. + sizeof(m.u.component_disable),
  17079. + &rmsg, &rmsg_handle);
  17080. + if (ret)
  17081. + return ret;
  17082. +
  17083. + if (rmsg->h.type != m.h.type) {
  17084. + /* got an unexpected message type in reply */
  17085. + ret = -EINVAL;
  17086. + goto release_msg;
  17087. + }
  17088. +
  17089. + ret = -rmsg->u.component_disable_reply.status;
  17090. +
  17091. +release_msg:
  17092. +
  17093. + vchi_held_msg_release(&rmsg_handle);
  17094. +
  17095. + return ret;
  17096. +}
  17097. +
  17098. +/* get version of mmal implementation */
  17099. +static int get_version(struct vchiq_mmal_instance *instance,
  17100. + u32 *major_out, u32 *minor_out)
  17101. +{
  17102. + int ret;
  17103. + struct mmal_msg m;
  17104. + struct mmal_msg *rmsg;
  17105. + VCHI_HELD_MSG_T rmsg_handle;
  17106. +
  17107. + m.h.type = MMAL_MSG_TYPE_GET_VERSION;
  17108. +
  17109. + ret = send_synchronous_mmal_msg(instance, &m,
  17110. + sizeof(m.u.version),
  17111. + &rmsg, &rmsg_handle);
  17112. + if (ret)
  17113. + return ret;
  17114. +
  17115. + if (rmsg->h.type != m.h.type) {
  17116. + /* got an unexpected message type in reply */
  17117. + ret = -EINVAL;
  17118. + goto release_msg;
  17119. + }
  17120. +
  17121. + *major_out = rmsg->u.version.major;
  17122. + *minor_out = rmsg->u.version.minor;
  17123. +
  17124. +release_msg:
  17125. + vchi_held_msg_release(&rmsg_handle);
  17126. +
  17127. + return ret;
  17128. +}
  17129. +
  17130. +/* do a port action with a port as a parameter */
  17131. +static int port_action_port(struct vchiq_mmal_instance *instance,
  17132. + struct vchiq_mmal_port *port,
  17133. + enum mmal_msg_port_action_type action_type)
  17134. +{
  17135. + int ret;
  17136. + struct mmal_msg m;
  17137. + struct mmal_msg *rmsg;
  17138. + VCHI_HELD_MSG_T rmsg_handle;
  17139. +
  17140. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  17141. + m.u.port_action_port.component_handle = port->component->handle;
  17142. + m.u.port_action_port.port_handle = port->handle;
  17143. + m.u.port_action_port.action = action_type;
  17144. +
  17145. + port_to_mmal_msg(port, &m.u.port_action_port.port);
  17146. +
  17147. + ret = send_synchronous_mmal_msg(instance, &m,
  17148. + sizeof(m.u.port_action_port),
  17149. + &rmsg, &rmsg_handle);
  17150. + if (ret)
  17151. + return ret;
  17152. +
  17153. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  17154. + /* got an unexpected message type in reply */
  17155. + ret = -EINVAL;
  17156. + goto release_msg;
  17157. + }
  17158. +
  17159. + ret = -rmsg->u.port_action_reply.status;
  17160. +
  17161. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
  17162. + __func__,
  17163. + ret, port->component->handle, port->handle,
  17164. + port_action_type_names[action_type], action_type);
  17165. +
  17166. +release_msg:
  17167. + vchi_held_msg_release(&rmsg_handle);
  17168. +
  17169. + return ret;
  17170. +}
  17171. +
  17172. +/* do a port action with handles as parameters */
  17173. +static int port_action_handle(struct vchiq_mmal_instance *instance,
  17174. + struct vchiq_mmal_port *port,
  17175. + enum mmal_msg_port_action_type action_type,
  17176. + u32 connect_component_handle,
  17177. + u32 connect_port_handle)
  17178. +{
  17179. + int ret;
  17180. + struct mmal_msg m;
  17181. + struct mmal_msg *rmsg;
  17182. + VCHI_HELD_MSG_T rmsg_handle;
  17183. +
  17184. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  17185. +
  17186. + m.u.port_action_handle.component_handle = port->component->handle;
  17187. + m.u.port_action_handle.port_handle = port->handle;
  17188. + m.u.port_action_handle.action = action_type;
  17189. +
  17190. + m.u.port_action_handle.connect_component_handle =
  17191. + connect_component_handle;
  17192. + m.u.port_action_handle.connect_port_handle = connect_port_handle;
  17193. +
  17194. + ret = send_synchronous_mmal_msg(instance, &m,
  17195. + sizeof(m.u.port_action_handle),
  17196. + &rmsg, &rmsg_handle);
  17197. + if (ret)
  17198. + return ret;
  17199. +
  17200. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  17201. + /* got an unexpected message type in reply */
  17202. + ret = -EINVAL;
  17203. + goto release_msg;
  17204. + }
  17205. +
  17206. + ret = -rmsg->u.port_action_reply.status;
  17207. +
  17208. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
  17209. + " connect component:0x%x connect port:%d\n",
  17210. + __func__,
  17211. + ret, port->component->handle, port->handle,
  17212. + port_action_type_names[action_type],
  17213. + action_type, connect_component_handle, connect_port_handle);
  17214. +
  17215. +release_msg:
  17216. + vchi_held_msg_release(&rmsg_handle);
  17217. +
  17218. + return ret;
  17219. +}
  17220. +
  17221. +static int port_parameter_set(struct vchiq_mmal_instance *instance,
  17222. + struct vchiq_mmal_port *port,
  17223. + u32 parameter_id, void *value, u32 value_size)
  17224. +{
  17225. + int ret;
  17226. + struct mmal_msg m;
  17227. + struct mmal_msg *rmsg;
  17228. + VCHI_HELD_MSG_T rmsg_handle;
  17229. +
  17230. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
  17231. +
  17232. + m.u.port_parameter_set.component_handle = port->component->handle;
  17233. + m.u.port_parameter_set.port_handle = port->handle;
  17234. + m.u.port_parameter_set.id = parameter_id;
  17235. + m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
  17236. + memcpy(&m.u.port_parameter_set.value, value, value_size);
  17237. +
  17238. + ret = send_synchronous_mmal_msg(instance, &m,
  17239. + (4 * sizeof(u32)) + value_size,
  17240. + &rmsg, &rmsg_handle);
  17241. + if (ret)
  17242. + return ret;
  17243. +
  17244. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
  17245. + /* got an unexpected message type in reply */
  17246. + ret = -EINVAL;
  17247. + goto release_msg;
  17248. + }
  17249. +
  17250. + ret = -rmsg->u.port_parameter_set_reply.status;
  17251. +
  17252. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
  17253. + __func__,
  17254. + ret, port->component->handle, port->handle, parameter_id);
  17255. +
  17256. +release_msg:
  17257. + vchi_held_msg_release(&rmsg_handle);
  17258. +
  17259. + return ret;
  17260. +}
  17261. +
  17262. +static int port_parameter_get(struct vchiq_mmal_instance *instance,
  17263. + struct vchiq_mmal_port *port,
  17264. + u32 parameter_id, void *value, u32 *value_size)
  17265. +{
  17266. + int ret;
  17267. + struct mmal_msg m;
  17268. + struct mmal_msg *rmsg;
  17269. + VCHI_HELD_MSG_T rmsg_handle;
  17270. +
  17271. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
  17272. +
  17273. + m.u.port_parameter_get.component_handle = port->component->handle;
  17274. + m.u.port_parameter_get.port_handle = port->handle;
  17275. + m.u.port_parameter_get.id = parameter_id;
  17276. + m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
  17277. +
  17278. + ret = send_synchronous_mmal_msg(instance, &m,
  17279. + sizeof(struct
  17280. + mmal_msg_port_parameter_get),
  17281. + &rmsg, &rmsg_handle);
  17282. + if (ret)
  17283. + return ret;
  17284. +
  17285. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
  17286. + /* got an unexpected message type in reply */
  17287. + pr_err("Incorrect reply type %d\n", rmsg->h.type);
  17288. + ret = -EINVAL;
  17289. + goto release_msg;
  17290. + }
  17291. +
  17292. + ret = -rmsg->u.port_parameter_get_reply.status;
  17293. + if (ret) {
  17294. + /* Copy only as much as we have space for
  17295. + * but report true size of parameter
  17296. + */
  17297. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  17298. + *value_size);
  17299. + *value_size = rmsg->u.port_parameter_get_reply.size;
  17300. + } else
  17301. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  17302. + rmsg->u.port_parameter_get_reply.size);
  17303. +
  17304. + pr_info("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
  17305. + ret, port->component->handle, port->handle, parameter_id);
  17306. +
  17307. +release_msg:
  17308. + vchi_held_msg_release(&rmsg_handle);
  17309. +
  17310. + return ret;
  17311. +}
  17312. +
  17313. +/* disables a port and drains buffers from it */
  17314. +static int port_disable(struct vchiq_mmal_instance *instance,
  17315. + struct vchiq_mmal_port *port)
  17316. +{
  17317. + int ret;
  17318. + struct list_head *q, *buf_head;
  17319. + unsigned long flags = 0;
  17320. +
  17321. + if (!port->enabled)
  17322. + return 0;
  17323. +
  17324. + port->enabled = false;
  17325. +
  17326. + ret = port_action_port(instance, port,
  17327. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
  17328. + if (ret == 0) {
  17329. +
  17330. + /* drain all queued buffers on port */
  17331. + spin_lock_irqsave(&port->slock, flags);
  17332. +
  17333. + list_for_each_safe(buf_head, q, &port->buffers) {
  17334. + struct mmal_buffer *mmalbuf;
  17335. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  17336. + list);
  17337. + list_del(buf_head);
  17338. + if (port->buffer_cb)
  17339. + port->buffer_cb(instance,
  17340. + port, 0, mmalbuf, 0, 0,
  17341. + MMAL_TIME_UNKNOWN,
  17342. + MMAL_TIME_UNKNOWN);
  17343. + }
  17344. +
  17345. + spin_unlock_irqrestore(&port->slock, flags);
  17346. +
  17347. + ret = port_info_get(instance, port);
  17348. + }
  17349. +
  17350. + return ret;
  17351. +}
  17352. +
  17353. +/* enable a port */
  17354. +static int port_enable(struct vchiq_mmal_instance *instance,
  17355. + struct vchiq_mmal_port *port)
  17356. +{
  17357. + unsigned int hdr_count;
  17358. + struct list_head *buf_head;
  17359. + int ret;
  17360. +
  17361. + if (port->enabled)
  17362. + return 0;
  17363. +
  17364. + /* ensure there are enough buffers queued to cover the buffer headers */
  17365. + if (port->buffer_cb != NULL) {
  17366. + hdr_count = 0;
  17367. + list_for_each(buf_head, &port->buffers) {
  17368. + hdr_count++;
  17369. + }
  17370. + if (hdr_count < port->current_buffer.num)
  17371. + return -ENOSPC;
  17372. + }
  17373. +
  17374. + ret = port_action_port(instance, port,
  17375. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
  17376. + if (ret)
  17377. + goto done;
  17378. +
  17379. + port->enabled = true;
  17380. +
  17381. + if (port->buffer_cb) {
  17382. + /* send buffer headers to videocore */
  17383. + hdr_count = 1;
  17384. + list_for_each(buf_head, &port->buffers) {
  17385. + struct mmal_buffer *mmalbuf;
  17386. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  17387. + list);
  17388. + ret = buffer_from_host(instance, port, mmalbuf);
  17389. + if (ret)
  17390. + goto done;
  17391. +
  17392. + hdr_count++;
  17393. + if (hdr_count > port->current_buffer.num)
  17394. + break;
  17395. + }
  17396. + }
  17397. +
  17398. + ret = port_info_get(instance, port);
  17399. +
  17400. +done:
  17401. + return ret;
  17402. +}
  17403. +
  17404. +/* ------------------------------------------------------------------
  17405. + * Exported API
  17406. + *------------------------------------------------------------------*/
  17407. +
  17408. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  17409. + struct vchiq_mmal_port *port)
  17410. +{
  17411. + int ret;
  17412. +
  17413. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17414. + return -EINTR;
  17415. +
  17416. + ret = port_info_set(instance, port);
  17417. + if (ret)
  17418. + goto release_unlock;
  17419. +
  17420. + /* read what has actually been set */
  17421. + ret = port_info_get(instance, port);
  17422. +
  17423. +release_unlock:
  17424. + mutex_unlock(&instance->vchiq_mutex);
  17425. +
  17426. + return ret;
  17427. +
  17428. +}
  17429. +
  17430. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  17431. + struct vchiq_mmal_port *port,
  17432. + u32 parameter, void *value, u32 value_size)
  17433. +{
  17434. + int ret;
  17435. +
  17436. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17437. + return -EINTR;
  17438. +
  17439. + ret = port_parameter_set(instance, port, parameter, value, value_size);
  17440. +
  17441. + mutex_unlock(&instance->vchiq_mutex);
  17442. +
  17443. + return ret;
  17444. +}
  17445. +
  17446. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  17447. + struct vchiq_mmal_port *port,
  17448. + u32 parameter, void *value, u32 *value_size)
  17449. +{
  17450. + int ret;
  17451. +
  17452. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17453. + return -EINTR;
  17454. +
  17455. + ret = port_parameter_get(instance, port, parameter, value, value_size);
  17456. +
  17457. + mutex_unlock(&instance->vchiq_mutex);
  17458. +
  17459. + return ret;
  17460. +}
  17461. +
  17462. +/* enable a port
  17463. + *
  17464. + * enables a port and queues buffers for satisfying callbacks if we
  17465. + * provide a callback handler
  17466. + */
  17467. +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
  17468. + struct vchiq_mmal_port *port,
  17469. + vchiq_mmal_buffer_cb buffer_cb)
  17470. +{
  17471. + int ret;
  17472. +
  17473. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17474. + return -EINTR;
  17475. +
  17476. + /* already enabled - noop */
  17477. + if (port->enabled) {
  17478. + ret = 0;
  17479. + goto unlock;
  17480. + }
  17481. +
  17482. + port->buffer_cb = buffer_cb;
  17483. +
  17484. + ret = port_enable(instance, port);
  17485. +
  17486. +unlock:
  17487. + mutex_unlock(&instance->vchiq_mutex);
  17488. +
  17489. + return ret;
  17490. +}
  17491. +
  17492. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  17493. + struct vchiq_mmal_port *port)
  17494. +{
  17495. + int ret;
  17496. +
  17497. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17498. + return -EINTR;
  17499. +
  17500. + if (!port->enabled) {
  17501. + mutex_unlock(&instance->vchiq_mutex);
  17502. + return 0;
  17503. + }
  17504. +
  17505. + ret = port_disable(instance, port);
  17506. +
  17507. + mutex_unlock(&instance->vchiq_mutex);
  17508. +
  17509. + return ret;
  17510. +}
  17511. +
  17512. +/* ports will be connected in a tunneled manner so data buffers
  17513. + * are not handled by client.
  17514. + */
  17515. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  17516. + struct vchiq_mmal_port *src,
  17517. + struct vchiq_mmal_port *dst)
  17518. +{
  17519. + int ret;
  17520. +
  17521. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17522. + return -EINTR;
  17523. +
  17524. + /* disconnect ports if connected */
  17525. + if (src->connected != NULL) {
  17526. + ret = port_disable(instance, src);
  17527. + if (ret) {
  17528. + pr_err("failed disabling src port(%d)\n", ret);
  17529. + goto release_unlock;
  17530. + }
  17531. +
  17532. + /* do not need to disable the destination port as they
  17533. + * are connected and it is done automatically
  17534. + */
  17535. +
  17536. + ret = port_action_handle(instance, src,
  17537. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
  17538. + src->connected->component->handle,
  17539. + src->connected->handle);
  17540. + if (ret < 0) {
  17541. + pr_err("failed disconnecting src port\n");
  17542. + goto release_unlock;
  17543. + }
  17544. + src->connected->enabled = false;
  17545. + src->connected = NULL;
  17546. + }
  17547. +
  17548. + if (dst == NULL) {
  17549. + /* do not make new connection */
  17550. + ret = 0;
  17551. + pr_debug("not making new connection\n");
  17552. + goto release_unlock;
  17553. + }
  17554. +
  17555. + /* copy src port format to dst */
  17556. + dst->format.encoding = src->format.encoding;
  17557. + dst->es.video.width = src->es.video.width;
  17558. + dst->es.video.height = src->es.video.height;
  17559. + dst->es.video.crop.x = src->es.video.crop.x;
  17560. + dst->es.video.crop.y = src->es.video.crop.y;
  17561. + dst->es.video.crop.width = src->es.video.crop.width;
  17562. + dst->es.video.crop.height = src->es.video.crop.height;
  17563. + dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
  17564. + dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
  17565. +
  17566. + /* set new format */
  17567. + ret = port_info_set(instance, dst);
  17568. + if (ret) {
  17569. + pr_debug("setting port info failed\n");
  17570. + goto release_unlock;
  17571. + }
  17572. +
  17573. + /* read what has actually been set */
  17574. + ret = port_info_get(instance, dst);
  17575. + if (ret) {
  17576. + pr_debug("read back port info failed\n");
  17577. + goto release_unlock;
  17578. + }
  17579. +
  17580. + /* connect two ports together */
  17581. + ret = port_action_handle(instance, src,
  17582. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
  17583. + dst->component->handle, dst->handle);
  17584. + if (ret < 0) {
  17585. + pr_debug("connecting port %d:%d to %d:%d failed\n",
  17586. + src->component->handle, src->handle,
  17587. + dst->component->handle, dst->handle);
  17588. + goto release_unlock;
  17589. + }
  17590. + src->connected = dst;
  17591. +
  17592. +release_unlock:
  17593. +
  17594. + mutex_unlock(&instance->vchiq_mutex);
  17595. +
  17596. + return ret;
  17597. +}
  17598. +
  17599. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  17600. + struct vchiq_mmal_port *port,
  17601. + struct mmal_buffer *buffer)
  17602. +{
  17603. + unsigned long flags = 0;
  17604. +
  17605. + spin_lock_irqsave(&port->slock, flags);
  17606. + list_add_tail(&buffer->list, &port->buffers);
  17607. + spin_unlock_irqrestore(&port->slock, flags);
  17608. +
  17609. + /* the port previously underflowed because it was missing a
  17610. + * mmal_buffer which has just been added, submit that buffer
  17611. + * to the mmal service.
  17612. + */
  17613. + if (port->buffer_underflow) {
  17614. + port_buffer_from_host(instance, port);
  17615. + port->buffer_underflow--;
  17616. + }
  17617. +
  17618. + return 0;
  17619. +}
  17620. +
  17621. +/* Initialise a mmal component and its ports
  17622. + *
  17623. + */
  17624. +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
  17625. + const char *name,
  17626. + struct vchiq_mmal_component **component_out)
  17627. +{
  17628. + int ret;
  17629. + int idx; /* port index */
  17630. + struct vchiq_mmal_component *component;
  17631. +
  17632. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17633. + return -EINTR;
  17634. +
  17635. + if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
  17636. + ret = -EINVAL; /* todo is this correct error? */
  17637. + goto unlock;
  17638. + }
  17639. +
  17640. + component = &instance->component[instance->component_idx];
  17641. +
  17642. + ret = create_component(instance, component, name);
  17643. + if (ret < 0)
  17644. + goto unlock;
  17645. +
  17646. + /* ports info needs gathering */
  17647. + component->control.type = MMAL_PORT_TYPE_CONTROL;
  17648. + component->control.index = 0;
  17649. + component->control.component = component;
  17650. + spin_lock_init(&component->control.slock);
  17651. + INIT_LIST_HEAD(&component->control.buffers);
  17652. + ret = port_info_get(instance, &component->control);
  17653. + if (ret < 0)
  17654. + goto release_component;
  17655. +
  17656. + for (idx = 0; idx < component->inputs; idx++) {
  17657. + component->input[idx].type = MMAL_PORT_TYPE_INPUT;
  17658. + component->input[idx].index = idx;
  17659. + component->input[idx].component = component;
  17660. + spin_lock_init(&component->input[idx].slock);
  17661. + INIT_LIST_HEAD(&component->input[idx].buffers);
  17662. + ret = port_info_get(instance, &component->input[idx]);
  17663. + if (ret < 0)
  17664. + goto release_component;
  17665. + }
  17666. +
  17667. + for (idx = 0; idx < component->outputs; idx++) {
  17668. + component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
  17669. + component->output[idx].index = idx;
  17670. + component->output[idx].component = component;
  17671. + spin_lock_init(&component->output[idx].slock);
  17672. + INIT_LIST_HEAD(&component->output[idx].buffers);
  17673. + ret = port_info_get(instance, &component->output[idx]);
  17674. + if (ret < 0)
  17675. + goto release_component;
  17676. + }
  17677. +
  17678. + for (idx = 0; idx < component->clocks; idx++) {
  17679. + component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
  17680. + component->clock[idx].index = idx;
  17681. + component->clock[idx].component = component;
  17682. + spin_lock_init(&component->clock[idx].slock);
  17683. + INIT_LIST_HEAD(&component->clock[idx].buffers);
  17684. + ret = port_info_get(instance, &component->clock[idx]);
  17685. + if (ret < 0)
  17686. + goto release_component;
  17687. + }
  17688. +
  17689. + instance->component_idx++;
  17690. +
  17691. + *component_out = component;
  17692. +
  17693. + mutex_unlock(&instance->vchiq_mutex);
  17694. +
  17695. + return 0;
  17696. +
  17697. +release_component:
  17698. + destroy_component(instance, component);
  17699. +unlock:
  17700. + mutex_unlock(&instance->vchiq_mutex);
  17701. +
  17702. + return ret;
  17703. +}
  17704. +
  17705. +/*
  17706. + * cause a mmal component to be destroyed
  17707. + */
  17708. +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
  17709. + struct vchiq_mmal_component *component)
  17710. +{
  17711. + int ret;
  17712. +
  17713. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17714. + return -EINTR;
  17715. +
  17716. + if (component->enabled)
  17717. + ret = disable_component(instance, component);
  17718. +
  17719. + ret = destroy_component(instance, component);
  17720. +
  17721. + mutex_unlock(&instance->vchiq_mutex);
  17722. +
  17723. + return ret;
  17724. +}
  17725. +
  17726. +/*
  17727. + * cause a mmal component to be enabled
  17728. + */
  17729. +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
  17730. + struct vchiq_mmal_component *component)
  17731. +{
  17732. + int ret;
  17733. +
  17734. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17735. + return -EINTR;
  17736. +
  17737. + if (component->enabled) {
  17738. + mutex_unlock(&instance->vchiq_mutex);
  17739. + return 0;
  17740. + }
  17741. +
  17742. + ret = enable_component(instance, component);
  17743. + if (ret == 0)
  17744. + component->enabled = true;
  17745. +
  17746. + mutex_unlock(&instance->vchiq_mutex);
  17747. +
  17748. + return ret;
  17749. +}
  17750. +
  17751. +/*
  17752. + * cause a mmal component to be enabled
  17753. + */
  17754. +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
  17755. + struct vchiq_mmal_component *component)
  17756. +{
  17757. + int ret;
  17758. +
  17759. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17760. + return -EINTR;
  17761. +
  17762. + if (!component->enabled) {
  17763. + mutex_unlock(&instance->vchiq_mutex);
  17764. + return 0;
  17765. + }
  17766. +
  17767. + ret = disable_component(instance, component);
  17768. + if (ret == 0)
  17769. + component->enabled = false;
  17770. +
  17771. + mutex_unlock(&instance->vchiq_mutex);
  17772. +
  17773. + return ret;
  17774. +}
  17775. +
  17776. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  17777. + u32 *major_out, u32 *minor_out)
  17778. +{
  17779. + int ret;
  17780. +
  17781. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17782. + return -EINTR;
  17783. +
  17784. + ret = get_version(instance, major_out, minor_out);
  17785. +
  17786. + mutex_unlock(&instance->vchiq_mutex);
  17787. +
  17788. + return ret;
  17789. +}
  17790. +
  17791. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
  17792. +{
  17793. + int status = 0;
  17794. +
  17795. + if (instance == NULL)
  17796. + return -EINVAL;
  17797. +
  17798. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17799. + return -EINTR;
  17800. +
  17801. + vchi_service_use(instance->handle);
  17802. +
  17803. + status = vchi_service_close(instance->handle);
  17804. + if (status != 0)
  17805. + pr_err("mmal-vchiq: VCHIQ close failed");
  17806. +
  17807. + mutex_unlock(&instance->vchiq_mutex);
  17808. +
  17809. + vfree(instance->bulk_scratch);
  17810. +
  17811. + kfree(instance);
  17812. +
  17813. + return status;
  17814. +}
  17815. +
  17816. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
  17817. +{
  17818. + int status;
  17819. + struct vchiq_mmal_instance *instance;
  17820. + static VCHI_CONNECTION_T *vchi_connection;
  17821. + static VCHI_INSTANCE_T vchi_instance;
  17822. + SERVICE_CREATION_T params = {
  17823. + VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
  17824. + VC_MMAL_SERVER_NAME,
  17825. + vchi_connection,
  17826. + 0, /* rx fifo size (unused) */
  17827. + 0, /* tx fifo size (unused) */
  17828. + service_callback,
  17829. + NULL, /* service callback parameter */
  17830. + 1, /* unaligned bulk receives */
  17831. + 1, /* unaligned bulk transmits */
  17832. + 0 /* want crc check on bulk transfers */
  17833. + };
  17834. +
  17835. + /* compile time checks to ensure structure size as they are
  17836. + * directly (de)serialised from memory.
  17837. + */
  17838. +
  17839. + /* ensure the header structure has packed to the correct size */
  17840. + BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
  17841. +
  17842. + /* ensure message structure does not exceed maximum length */
  17843. + BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
  17844. +
  17845. + /* mmal port struct is correct size */
  17846. + BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
  17847. +
  17848. + /* create a vchi instance */
  17849. + status = vchi_initialise(&vchi_instance);
  17850. + if (status) {
  17851. + pr_err("Failed to initialise VCHI instance (status=%d)\n",
  17852. + status);
  17853. + return -EIO;
  17854. + }
  17855. +
  17856. + status = vchi_connect(NULL, 0, vchi_instance);
  17857. + if (status) {
  17858. + pr_err("Failed to connect VCHI instance (status=%d)\n", status);
  17859. + return -EIO;
  17860. + }
  17861. +
  17862. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  17863. + memset(instance, 0, sizeof(*instance));
  17864. +
  17865. + mutex_init(&instance->vchiq_mutex);
  17866. + mutex_init(&instance->bulk_mutex);
  17867. +
  17868. + instance->bulk_scratch = vmalloc(PAGE_SIZE);
  17869. +
  17870. + params.callback_param = instance;
  17871. +
  17872. + status = vchi_service_open(vchi_instance, &params, &instance->handle);
  17873. + if (status) {
  17874. + pr_err("Failed to open VCHI service connection (status=%d)\n",
  17875. + status);
  17876. + goto err_close_services;
  17877. + }
  17878. +
  17879. + vchi_service_release(instance->handle);
  17880. +
  17881. + *out_instance = instance;
  17882. +
  17883. + return 0;
  17884. +
  17885. +err_close_services:
  17886. +
  17887. + vchi_service_close(instance->handle);
  17888. + vfree(instance->bulk_scratch);
  17889. + kfree(instance);
  17890. + return -ENODEV;
  17891. +}
  17892. diff -Nur linux-3.12.13/drivers/media/platform/bcm2835/mmal-vchiq.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.h
  17893. --- linux-3.12.13/drivers/media/platform/bcm2835/mmal-vchiq.h 1970-01-01 01:00:00.000000000 +0100
  17894. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.h 2014-03-11 17:32:31.000000000 +0100
  17895. @@ -0,0 +1,178 @@
  17896. +/*
  17897. + * Broadcom BM2835 V4L2 driver
  17898. + *
  17899. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  17900. + *
  17901. + * This file is subject to the terms and conditions of the GNU General Public
  17902. + * License. See the file COPYING in the main directory of this archive
  17903. + * for more details.
  17904. + *
  17905. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  17906. + * Dave Stevenson <dsteve@broadcom.com>
  17907. + * Simon Mellor <simellor@broadcom.com>
  17908. + * Luke Diamand <luked@broadcom.com>
  17909. + *
  17910. + * MMAL interface to VCHIQ message passing
  17911. + */
  17912. +
  17913. +#ifndef MMAL_VCHIQ_H
  17914. +#define MMAL_VCHIQ_H
  17915. +
  17916. +#include "mmal-msg-format.h"
  17917. +
  17918. +#define MAX_PORT_COUNT 4
  17919. +
  17920. +/* Maximum size of the format extradata. */
  17921. +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
  17922. +
  17923. +struct vchiq_mmal_instance;
  17924. +
  17925. +enum vchiq_mmal_es_type {
  17926. + MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */
  17927. + MMAL_ES_TYPE_CONTROL, /**< Elementary stream of control commands */
  17928. + MMAL_ES_TYPE_AUDIO, /**< Audio elementary stream */
  17929. + MMAL_ES_TYPE_VIDEO, /**< Video elementary stream */
  17930. + MMAL_ES_TYPE_SUBPICTURE /**< Sub-picture elementary stream */
  17931. +};
  17932. +
  17933. +/* rectangle, used lots so it gets its own struct */
  17934. +struct vchiq_mmal_rect {
  17935. + s32 x;
  17936. + s32 y;
  17937. + s32 width;
  17938. + s32 height;
  17939. +};
  17940. +
  17941. +struct vchiq_mmal_port_buffer {
  17942. + unsigned int num; /* number of buffers */
  17943. + u32 size; /* size of buffers */
  17944. + u32 alignment; /* alignment of buffers */
  17945. +};
  17946. +
  17947. +struct vchiq_mmal_port;
  17948. +
  17949. +typedef void (*vchiq_mmal_buffer_cb)(
  17950. + struct vchiq_mmal_instance *instance,
  17951. + struct vchiq_mmal_port *port,
  17952. + int status, struct mmal_buffer *buffer,
  17953. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
  17954. +
  17955. +struct vchiq_mmal_port {
  17956. + bool enabled;
  17957. + u32 handle;
  17958. + u32 type; /* port type, cached to use on port info set */
  17959. + u32 index; /* port index, cached to use on port info set */
  17960. +
  17961. + /* component port belongs to, allows simple deref */
  17962. + struct vchiq_mmal_component *component;
  17963. +
  17964. + struct vchiq_mmal_port *connected; /* port conencted to */
  17965. +
  17966. + /* buffer info */
  17967. + struct vchiq_mmal_port_buffer minimum_buffer;
  17968. + struct vchiq_mmal_port_buffer recommended_buffer;
  17969. + struct vchiq_mmal_port_buffer current_buffer;
  17970. +
  17971. + /* stream format */
  17972. + struct mmal_es_format format;
  17973. + /* elementry stream format */
  17974. + union mmal_es_specific_format es;
  17975. +
  17976. + /* data buffers to fill */
  17977. + struct list_head buffers;
  17978. + /* lock to serialise adding and removing buffers from list */
  17979. + spinlock_t slock;
  17980. + /* count of how many buffer header refils have failed because
  17981. + * there was no buffer to satisfy them
  17982. + */
  17983. + int buffer_underflow;
  17984. + /* callback on buffer completion */
  17985. + vchiq_mmal_buffer_cb buffer_cb;
  17986. + /* callback context */
  17987. + void *cb_ctx;
  17988. +};
  17989. +
  17990. +struct vchiq_mmal_component {
  17991. + bool enabled;
  17992. + u32 handle; /* VideoCore handle for component */
  17993. + u32 inputs; /* Number of input ports */
  17994. + u32 outputs; /* Number of output ports */
  17995. + u32 clocks; /* Number of clock ports */
  17996. + struct vchiq_mmal_port control; /* control port */
  17997. + struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
  17998. + struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
  17999. + struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
  18000. +};
  18001. +
  18002. +
  18003. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
  18004. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
  18005. +
  18006. +/* Initialise a mmal component and its ports
  18007. +*
  18008. +*/
  18009. +int vchiq_mmal_component_init(
  18010. + struct vchiq_mmal_instance *instance,
  18011. + const char *name,
  18012. + struct vchiq_mmal_component **component_out);
  18013. +
  18014. +int vchiq_mmal_component_finalise(
  18015. + struct vchiq_mmal_instance *instance,
  18016. + struct vchiq_mmal_component *component);
  18017. +
  18018. +int vchiq_mmal_component_enable(
  18019. + struct vchiq_mmal_instance *instance,
  18020. + struct vchiq_mmal_component *component);
  18021. +
  18022. +int vchiq_mmal_component_disable(
  18023. + struct vchiq_mmal_instance *instance,
  18024. + struct vchiq_mmal_component *component);
  18025. +
  18026. +
  18027. +
  18028. +/* enable a mmal port
  18029. + *
  18030. + * enables a port and if a buffer callback provided enque buffer
  18031. + * headers as apropriate for the port.
  18032. + */
  18033. +int vchiq_mmal_port_enable(
  18034. + struct vchiq_mmal_instance *instance,
  18035. + struct vchiq_mmal_port *port,
  18036. + vchiq_mmal_buffer_cb buffer_cb);
  18037. +
  18038. +/* disable a port
  18039. + *
  18040. + * disable a port will dequeue any pending buffers
  18041. + */
  18042. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  18043. + struct vchiq_mmal_port *port);
  18044. +
  18045. +
  18046. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  18047. + struct vchiq_mmal_port *port,
  18048. + u32 parameter,
  18049. + void *value,
  18050. + u32 value_size);
  18051. +
  18052. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  18053. + struct vchiq_mmal_port *port,
  18054. + u32 parameter,
  18055. + void *value,
  18056. + u32 *value_size);
  18057. +
  18058. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  18059. + struct vchiq_mmal_port *port);
  18060. +
  18061. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  18062. + struct vchiq_mmal_port *src,
  18063. + struct vchiq_mmal_port *dst);
  18064. +
  18065. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  18066. + u32 *major_out,
  18067. + u32 *minor_out);
  18068. +
  18069. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  18070. + struct vchiq_mmal_port *port,
  18071. + struct mmal_buffer *buf);
  18072. +
  18073. +#endif /* MMAL_VCHIQ_H */
  18074. diff -Nur linux-3.12.13/drivers/media/platform/Kconfig linux-raspberry-pi/drivers/media/platform/Kconfig
  18075. --- linux-3.12.13/drivers/media/platform/Kconfig 2014-02-22 22:32:50.000000000 +0100
  18076. +++ linux-raspberry-pi/drivers/media/platform/Kconfig 2014-03-11 17:51:15.000000000 +0100
  18077. @@ -124,6 +124,7 @@
  18078. source "drivers/media/platform/soc_camera/Kconfig"
  18079. source "drivers/media/platform/exynos4-is/Kconfig"
  18080. source "drivers/media/platform/s5p-tv/Kconfig"
  18081. +source "drivers/media/platform/bcm2835/Kconfig"
  18082. endif # V4L_PLATFORM_DRIVERS
  18083. diff -Nur linux-3.12.13/drivers/media/platform/Makefile linux-raspberry-pi/drivers/media/platform/Makefile
  18084. --- linux-3.12.13/drivers/media/platform/Makefile 2014-02-22 22:32:50.000000000 +0100
  18085. +++ linux-raspberry-pi/drivers/media/platform/Makefile 2014-03-11 17:51:15.000000000 +0100
  18086. @@ -52,4 +52,6 @@
  18087. obj-$(CONFIG_ARCH_OMAP) += omap/
  18088. +obj-$(CONFIG_VIDEO_BCM2835) += bcm2835/
  18089. +
  18090. ccflags-y += -I$(srctree)/drivers/media/i2c
  18091. diff -Nur linux-3.12.13/drivers/media/usb/dvb-usb-v2/az6007.c linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/az6007.c
  18092. --- linux-3.12.13/drivers/media/usb/dvb-usb-v2/az6007.c 2014-02-22 22:32:50.000000000 +0100
  18093. +++ linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/az6007.c 2014-03-11 17:32:32.000000000 +0100
  18094. @@ -68,6 +68,19 @@
  18095. .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
  18096. };
  18097. +static struct drxk_config cablestar_hdci_drxk = {
  18098. + .adr = 0x29,
  18099. + .parallel_ts = true,
  18100. + .dynamic_clk = true,
  18101. + .single_master = true,
  18102. + .enable_merr_cfg = true,
  18103. + .no_i2c_bridge = false,
  18104. + .chunk_size = 64,
  18105. + .mpeg_out_clk_strength = 0x02,
  18106. + .qam_demod_parameter_count = 2,
  18107. + .microcode_name = "dvb-usb-technisat-cablestar-hdci-drxk.fw",
  18108. +};
  18109. +
  18110. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  18111. {
  18112. struct az6007_device_state *st = fe_to_priv(fe);
  18113. @@ -630,6 +643,27 @@
  18114. return 0;
  18115. }
  18116. +static int az6007_cablestar_hdci_frontend_attach(struct dvb_usb_adapter *adap)
  18117. +{
  18118. + struct az6007_device_state *st = adap_to_priv(adap);
  18119. + struct dvb_usb_device *d = adap_to_d(adap);
  18120. +
  18121. + pr_debug("attaching demod drxk\n");
  18122. +
  18123. + adap->fe[0] = dvb_attach(drxk_attach, &cablestar_hdci_drxk,
  18124. + &d->i2c_adap);
  18125. + if (!adap->fe[0])
  18126. + return -EINVAL;
  18127. +
  18128. + adap->fe[0]->sec_priv = adap;
  18129. + st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
  18130. + adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  18131. +
  18132. + az6007_ci_init(adap);
  18133. +
  18134. + return 0;
  18135. +}
  18136. +
  18137. static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
  18138. {
  18139. struct dvb_usb_device *d = adap_to_d(adap);
  18140. @@ -868,6 +902,29 @@
  18141. }
  18142. };
  18143. +static struct dvb_usb_device_properties az6007_cablestar_hdci_props = {
  18144. + .driver_name = KBUILD_MODNAME,
  18145. + .owner = THIS_MODULE,
  18146. + .firmware = AZ6007_FIRMWARE,
  18147. +
  18148. + .adapter_nr = adapter_nr,
  18149. + .size_of_priv = sizeof(struct az6007_device_state),
  18150. + .i2c_algo = &az6007_i2c_algo,
  18151. + .tuner_attach = az6007_tuner_attach,
  18152. + .frontend_attach = az6007_cablestar_hdci_frontend_attach,
  18153. + .streaming_ctrl = az6007_streaming_ctrl,
  18154. +/* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
  18155. + .get_rc_config = NULL,
  18156. + .read_mac_address = az6007_read_mac_addr,
  18157. + .download_firmware = az6007_download_firmware,
  18158. + .identify_state = az6007_identify_state,
  18159. + .power_ctrl = az6007_power_ctrl,
  18160. + .num_adapters = 1,
  18161. + .adapter = {
  18162. + { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
  18163. + }
  18164. +};
  18165. +
  18166. static struct usb_device_id az6007_usb_table[] = {
  18167. {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
  18168. &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
  18169. @@ -875,6 +932,8 @@
  18170. &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
  18171. {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
  18172. &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
  18173. + {DVB_USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI,
  18174. + &az6007_cablestar_hdci_props, "Technisat CableStar Combo HD CI", RC_MAP_EMPTY)},
  18175. {0},
  18176. };
  18177. diff -Nur linux-3.12.13/drivers/media/usb/dvb-usb-v2/rtl28xxu.c linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
  18178. --- linux-3.12.13/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-02-22 22:32:50.000000000 +0100
  18179. +++ linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-03-11 17:51:16.000000000 +0100
  18180. @@ -1384,6 +1384,10 @@
  18181. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  18182. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  18183. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  18184. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xb803 /*USB_PID_AUGUST_DVBT205*/,
  18185. + &rtl2832u_props, "August DVB-T 205", NULL) },
  18186. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xa803 /*USB_PID_AUGUST_DVBT205*/,
  18187. + &rtl2832u_props, "August DVB-T 205", NULL) },
  18188. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
  18189. &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
  18190. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
  18191. diff -Nur linux-3.12.13/drivers/misc/Kconfig linux-raspberry-pi/drivers/misc/Kconfig
  18192. --- linux-3.12.13/drivers/misc/Kconfig 2014-02-22 22:32:50.000000000 +0100
  18193. +++ linux-raspberry-pi/drivers/misc/Kconfig 2014-03-11 17:51:16.000000000 +0100
  18194. @@ -537,4 +537,5 @@
  18195. source "drivers/misc/altera-stapl/Kconfig"
  18196. source "drivers/misc/mei/Kconfig"
  18197. source "drivers/misc/vmw_vmci/Kconfig"
  18198. +source "drivers/misc/vc04_services/Kconfig"
  18199. endmenu
  18200. diff -Nur linux-3.12.13/drivers/misc/Makefile linux-raspberry-pi/drivers/misc/Makefile
  18201. --- linux-3.12.13/drivers/misc/Makefile 2014-02-22 22:32:50.000000000 +0100
  18202. +++ linux-raspberry-pi/drivers/misc/Makefile 2014-03-11 17:32:36.000000000 +0100
  18203. @@ -53,3 +53,4 @@
  18204. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  18205. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  18206. obj-$(CONFIG_SRAM) += sram.o
  18207. +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
  18208. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchi/connections/connection.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  18209. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchi/connections/connection.h 1970-01-01 01:00:00.000000000 +0100
  18210. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/connections/connection.h 2014-03-11 17:32:37.000000000 +0100
  18211. @@ -0,0 +1,328 @@
  18212. +/**
  18213. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18214. + *
  18215. + * Redistribution and use in source and binary forms, with or without
  18216. + * modification, are permitted provided that the following conditions
  18217. + * are met:
  18218. + * 1. Redistributions of source code must retain the above copyright
  18219. + * notice, this list of conditions, and the following disclaimer,
  18220. + * without modification.
  18221. + * 2. Redistributions in binary form must reproduce the above copyright
  18222. + * notice, this list of conditions and the following disclaimer in the
  18223. + * documentation and/or other materials provided with the distribution.
  18224. + * 3. The names of the above-listed copyright holders may not be used
  18225. + * to endorse or promote products derived from this software without
  18226. + * specific prior written permission.
  18227. + *
  18228. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18229. + * GNU General Public License ("GPL") version 2, as published by the Free
  18230. + * Software Foundation.
  18231. + *
  18232. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18233. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18234. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18235. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18236. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18237. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18238. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18239. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18240. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18241. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18242. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18243. + */
  18244. +
  18245. +#ifndef CONNECTION_H_
  18246. +#define CONNECTION_H_
  18247. +
  18248. +#include <linux/kernel.h>
  18249. +#include <linux/types.h>
  18250. +#include <linux/semaphore.h>
  18251. +
  18252. +#include "interface/vchi/vchi_cfg_internal.h"
  18253. +#include "interface/vchi/vchi_common.h"
  18254. +#include "interface/vchi/message_drivers/message.h"
  18255. +
  18256. +/******************************************************************************
  18257. + Global defs
  18258. + *****************************************************************************/
  18259. +
  18260. +// Opaque handle for a connection / service pair
  18261. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  18262. +
  18263. +// opaque handle to the connection state information
  18264. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  18265. +
  18266. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  18267. +
  18268. +
  18269. +/******************************************************************************
  18270. + API
  18271. + *****************************************************************************/
  18272. +
  18273. +// Routine to init a connection with a particular low level driver
  18274. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  18275. + const VCHI_MESSAGE_DRIVER_T * driver );
  18276. +
  18277. +// Routine to control CRC enabling at a connection level
  18278. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  18279. + VCHI_CRC_CONTROL_T control );
  18280. +
  18281. +// Routine to create a service
  18282. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  18283. + int32_t service_id,
  18284. + uint32_t rx_fifo_size,
  18285. + uint32_t tx_fifo_size,
  18286. + int server,
  18287. + VCHI_CALLBACK_T callback,
  18288. + void *callback_param,
  18289. + int32_t want_crc,
  18290. + int32_t want_unaligned_bulk_rx,
  18291. + int32_t want_unaligned_bulk_tx,
  18292. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  18293. +
  18294. +// Routine to close a service
  18295. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  18296. +
  18297. +// Routine to queue a message
  18298. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18299. + const void *data,
  18300. + uint32_t data_size,
  18301. + VCHI_FLAGS_T flags,
  18302. + void *msg_handle );
  18303. +
  18304. +// scatter-gather (vector) message queueing
  18305. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18306. + VCHI_MSG_VECTOR_T *vector,
  18307. + uint32_t count,
  18308. + VCHI_FLAGS_T flags,
  18309. + void *msg_handle );
  18310. +
  18311. +// Routine to dequeue a message
  18312. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18313. + void *data,
  18314. + uint32_t max_data_size_to_read,
  18315. + uint32_t *actual_msg_size,
  18316. + VCHI_FLAGS_T flags );
  18317. +
  18318. +// Routine to peek at a message
  18319. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18320. + void **data,
  18321. + uint32_t *msg_size,
  18322. + VCHI_FLAGS_T flags );
  18323. +
  18324. +// Routine to hold a message
  18325. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18326. + void **data,
  18327. + uint32_t *msg_size,
  18328. + VCHI_FLAGS_T flags,
  18329. + void **message_handle );
  18330. +
  18331. +// Routine to initialise a received message iterator
  18332. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18333. + VCHI_MSG_ITER_T *iter,
  18334. + VCHI_FLAGS_T flags );
  18335. +
  18336. +// Routine to release a held message
  18337. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18338. + void *message_handle );
  18339. +
  18340. +// Routine to get info on a held message
  18341. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18342. + void *message_handle,
  18343. + void **data,
  18344. + int32_t *msg_size,
  18345. + uint32_t *tx_timestamp,
  18346. + uint32_t *rx_timestamp );
  18347. +
  18348. +// Routine to check whether the iterator has a next message
  18349. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18350. + const VCHI_MSG_ITER_T *iter );
  18351. +
  18352. +// Routine to advance the iterator
  18353. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18354. + VCHI_MSG_ITER_T *iter,
  18355. + void **data,
  18356. + uint32_t *msg_size );
  18357. +
  18358. +// Routine to remove the last message returned by the iterator
  18359. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18360. + VCHI_MSG_ITER_T *iter );
  18361. +
  18362. +// Routine to hold the last message returned by the iterator
  18363. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18364. + VCHI_MSG_ITER_T *iter,
  18365. + void **msg_handle );
  18366. +
  18367. +// Routine to transmit bulk data
  18368. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18369. + const void *data_src,
  18370. + uint32_t data_size,
  18371. + VCHI_FLAGS_T flags,
  18372. + void *bulk_handle );
  18373. +
  18374. +// Routine to receive data
  18375. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18376. + void *data_dst,
  18377. + uint32_t data_size,
  18378. + VCHI_FLAGS_T flags,
  18379. + void *bulk_handle );
  18380. +
  18381. +// Routine to report if a server is available
  18382. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  18383. +
  18384. +// Routine to report the number of RX slots available
  18385. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  18386. +
  18387. +// Routine to report the RX slot size
  18388. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  18389. +
  18390. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  18391. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  18392. + int32_t service,
  18393. + uint32_t length,
  18394. + MESSAGE_TX_CHANNEL_T channel,
  18395. + uint32_t channel_params,
  18396. + uint32_t data_length,
  18397. + uint32_t data_offset);
  18398. +
  18399. +// Callback to inform a service that a Xon or Xoff message has been received
  18400. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  18401. +
  18402. +// Callback to inform a service that a server available reply message has been received
  18403. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  18404. +
  18405. +// Callback to indicate that bulk auxiliary messages have arrived
  18406. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  18407. +
  18408. +// Callback to indicate that bulk auxiliary messages have arrived
  18409. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  18410. +
  18411. +// Callback with all the connection info you require
  18412. +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);
  18413. +
  18414. +// Callback to inform of a disconnect
  18415. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  18416. +
  18417. +// Callback to inform of a power control request
  18418. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  18419. +
  18420. +// allocate memory suitably aligned for this connection
  18421. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  18422. +
  18423. +// free memory allocated by buffer_allocate
  18424. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  18425. +
  18426. +
  18427. +/******************************************************************************
  18428. + System driver struct
  18429. + *****************************************************************************/
  18430. +
  18431. +struct opaque_vchi_connection_api_t
  18432. +{
  18433. + // Routine to init the connection
  18434. + VCHI_CONNECTION_INIT_T init;
  18435. +
  18436. + // Connection-level CRC control
  18437. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  18438. +
  18439. + // Routine to connect to or create service
  18440. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  18441. +
  18442. + // Routine to disconnect from a service
  18443. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  18444. +
  18445. + // Routine to queue a message
  18446. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  18447. +
  18448. + // scatter-gather (vector) message queue
  18449. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  18450. +
  18451. + // Routine to dequeue a message
  18452. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  18453. +
  18454. + // Routine to peek at a message
  18455. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  18456. +
  18457. + // Routine to hold a message
  18458. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  18459. +
  18460. + // Routine to initialise a received message iterator
  18461. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  18462. +
  18463. + // Routine to release a message
  18464. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  18465. +
  18466. + // Routine to get information on a held message
  18467. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  18468. +
  18469. + // Routine to check for next message on iterator
  18470. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  18471. +
  18472. + // Routine to get next message on iterator
  18473. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  18474. +
  18475. + // Routine to remove the last message returned by iterator
  18476. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  18477. +
  18478. + // Routine to hold the last message returned by iterator
  18479. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  18480. +
  18481. + // Routine to transmit bulk data
  18482. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  18483. +
  18484. + // Routine to receive data
  18485. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  18486. +
  18487. + // Routine to report the available servers
  18488. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  18489. +
  18490. + // Routine to report the number of RX slots available
  18491. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  18492. +
  18493. + // Routine to report the RX slot size
  18494. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  18495. +
  18496. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  18497. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  18498. +
  18499. + // Callback to inform a service that a Xon or Xoff message has been received
  18500. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  18501. +
  18502. + // Callback to inform a service that a server available reply message has been received
  18503. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  18504. +
  18505. + // Callback to indicate that bulk auxiliary messages have arrived
  18506. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  18507. +
  18508. + // Callback to indicate that a bulk auxiliary message has been transmitted
  18509. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  18510. +
  18511. + // Callback to provide information about the connection
  18512. + VCHI_CONNECTION_INFO connection_info;
  18513. +
  18514. + // Callback to notify that peer has requested disconnect
  18515. + VCHI_CONNECTION_DISCONNECT disconnect;
  18516. +
  18517. + // Callback to notify that peer has requested power change
  18518. + VCHI_CONNECTION_POWER_CONTROL power_control;
  18519. +
  18520. + // allocate memory suitably aligned for this connection
  18521. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  18522. +
  18523. + // free memory allocated by buffer_allocate
  18524. + VCHI_BUFFER_FREE buffer_free;
  18525. +
  18526. +};
  18527. +
  18528. +struct vchi_connection_t {
  18529. + const VCHI_CONNECTION_API_T *api;
  18530. + VCHI_CONNECTION_STATE_T *state;
  18531. +#ifdef VCHI_COARSE_LOCKING
  18532. + struct semaphore sem;
  18533. +#endif
  18534. +};
  18535. +
  18536. +
  18537. +#endif /* CONNECTION_H_ */
  18538. +
  18539. +/****************************** End of file **********************************/
  18540. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  18541. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 1970-01-01 01:00:00.000000000 +0100
  18542. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 2014-03-11 17:32:37.000000000 +0100
  18543. @@ -0,0 +1,204 @@
  18544. +/**
  18545. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18546. + *
  18547. + * Redistribution and use in source and binary forms, with or without
  18548. + * modification, are permitted provided that the following conditions
  18549. + * are met:
  18550. + * 1. Redistributions of source code must retain the above copyright
  18551. + * notice, this list of conditions, and the following disclaimer,
  18552. + * without modification.
  18553. + * 2. Redistributions in binary form must reproduce the above copyright
  18554. + * notice, this list of conditions and the following disclaimer in the
  18555. + * documentation and/or other materials provided with the distribution.
  18556. + * 3. The names of the above-listed copyright holders may not be used
  18557. + * to endorse or promote products derived from this software without
  18558. + * specific prior written permission.
  18559. + *
  18560. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18561. + * GNU General Public License ("GPL") version 2, as published by the Free
  18562. + * Software Foundation.
  18563. + *
  18564. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18565. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18566. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18567. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18568. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18569. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18570. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18571. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18572. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18573. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18574. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18575. + */
  18576. +
  18577. +#ifndef _VCHI_MESSAGE_H_
  18578. +#define _VCHI_MESSAGE_H_
  18579. +
  18580. +#include <linux/kernel.h>
  18581. +#include <linux/types.h>
  18582. +#include <linux/semaphore.h>
  18583. +
  18584. +#include "interface/vchi/vchi_cfg_internal.h"
  18585. +#include "interface/vchi/vchi_common.h"
  18586. +
  18587. +
  18588. +typedef enum message_event_type {
  18589. + MESSAGE_EVENT_NONE,
  18590. + MESSAGE_EVENT_NOP,
  18591. + MESSAGE_EVENT_MESSAGE,
  18592. + MESSAGE_EVENT_SLOT_COMPLETE,
  18593. + MESSAGE_EVENT_RX_BULK_PAUSED,
  18594. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  18595. + MESSAGE_EVENT_TX_COMPLETE,
  18596. + MESSAGE_EVENT_MSG_DISCARDED
  18597. +} MESSAGE_EVENT_TYPE_T;
  18598. +
  18599. +typedef enum vchi_msg_flags
  18600. +{
  18601. + VCHI_MSG_FLAGS_NONE = 0x0,
  18602. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  18603. +} VCHI_MSG_FLAGS_T;
  18604. +
  18605. +typedef enum message_tx_channel
  18606. +{
  18607. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  18608. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  18609. +} MESSAGE_TX_CHANNEL_T;
  18610. +
  18611. +// Macros used for cycling through bulk channels
  18612. +#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)
  18613. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  18614. +
  18615. +typedef enum message_rx_channel
  18616. +{
  18617. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  18618. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  18619. +} MESSAGE_RX_CHANNEL_T;
  18620. +
  18621. +// Message receive slot information
  18622. +typedef struct rx_msg_slot_info {
  18623. +
  18624. + struct rx_msg_slot_info *next;
  18625. + //struct slot_info *prev;
  18626. +#if !defined VCHI_COARSE_LOCKING
  18627. + struct semaphore sem;
  18628. +#endif
  18629. +
  18630. + uint8_t *addr; // base address of slot
  18631. + uint32_t len; // length of slot in bytes
  18632. +
  18633. + uint32_t write_ptr; // hardware causes this to advance
  18634. + uint32_t read_ptr; // this module does the reading
  18635. + int active; // is this slot in the hardware dma fifo?
  18636. + uint32_t msgs_parsed; // count how many messages are in this slot
  18637. + uint32_t msgs_released; // how many messages have been released
  18638. + void *state; // connection state information
  18639. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  18640. +} RX_MSG_SLOTINFO_T;
  18641. +
  18642. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  18643. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  18644. +// driver will be tasked with sending the aligned core section.
  18645. +typedef struct rx_bulk_slotinfo_t {
  18646. + struct rx_bulk_slotinfo_t *next;
  18647. +
  18648. + struct semaphore *blocking;
  18649. +
  18650. + // needed by DMA
  18651. + void *addr;
  18652. + uint32_t len;
  18653. +
  18654. + // needed for the callback
  18655. + void *service;
  18656. + void *handle;
  18657. + VCHI_FLAGS_T flags;
  18658. +} RX_BULK_SLOTINFO_T;
  18659. +
  18660. +
  18661. +/* ----------------------------------------------------------------------
  18662. + * each connection driver will have a pool of the following struct.
  18663. + *
  18664. + * the pool will be managed by vchi_qman_*
  18665. + * this means there will be multiple queues (single linked lists)
  18666. + * a given struct message_info will be on exactly one of these queues
  18667. + * at any one time
  18668. + * -------------------------------------------------------------------- */
  18669. +typedef struct rx_message_info {
  18670. +
  18671. + struct message_info *next;
  18672. + //struct message_info *prev;
  18673. +
  18674. + uint8_t *addr;
  18675. + uint32_t len;
  18676. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  18677. + uint32_t tx_timestamp;
  18678. + uint32_t rx_timestamp;
  18679. +
  18680. +} RX_MESSAGE_INFO_T;
  18681. +
  18682. +typedef struct {
  18683. + MESSAGE_EVENT_TYPE_T type;
  18684. +
  18685. + struct {
  18686. + // for messages
  18687. + void *addr; // address of message
  18688. + uint16_t slot_delta; // whether this message indicated slot delta
  18689. + uint32_t len; // length of message
  18690. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  18691. + int32_t service; // service id this message is destined for
  18692. + uint32_t tx_timestamp; // timestamp from the header
  18693. + uint32_t rx_timestamp; // timestamp when we parsed it
  18694. + } message;
  18695. +
  18696. + // FIXME: cleanup slot reporting...
  18697. + RX_MSG_SLOTINFO_T *rx_msg;
  18698. + RX_BULK_SLOTINFO_T *rx_bulk;
  18699. + void *tx_handle;
  18700. + MESSAGE_TX_CHANNEL_T tx_channel;
  18701. +
  18702. +} MESSAGE_EVENT_T;
  18703. +
  18704. +
  18705. +// callbacks
  18706. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  18707. +
  18708. +typedef struct {
  18709. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  18710. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  18711. +
  18712. +
  18713. +// handle to this instance of message driver (as returned by ->open)
  18714. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  18715. +
  18716. +struct opaque_vchi_message_driver_t {
  18717. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  18718. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  18719. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  18720. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  18721. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  18722. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  18723. + 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)
  18724. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  18725. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  18726. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  18727. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  18728. +
  18729. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  18730. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  18731. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  18732. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  18733. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  18734. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  18735. +
  18736. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18737. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18738. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18739. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  18740. + 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 );
  18741. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  18742. +};
  18743. +
  18744. +
  18745. +#endif // _VCHI_MESSAGE_H_
  18746. +
  18747. +/****************************** End of file ***********************************/
  18748. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  18749. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 1970-01-01 01:00:00.000000000 +0100
  18750. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 2014-03-11 17:32:37.000000000 +0100
  18751. @@ -0,0 +1,224 @@
  18752. +/**
  18753. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18754. + *
  18755. + * Redistribution and use in source and binary forms, with or without
  18756. + * modification, are permitted provided that the following conditions
  18757. + * are met:
  18758. + * 1. Redistributions of source code must retain the above copyright
  18759. + * notice, this list of conditions, and the following disclaimer,
  18760. + * without modification.
  18761. + * 2. Redistributions in binary form must reproduce the above copyright
  18762. + * notice, this list of conditions and the following disclaimer in the
  18763. + * documentation and/or other materials provided with the distribution.
  18764. + * 3. The names of the above-listed copyright holders may not be used
  18765. + * to endorse or promote products derived from this software without
  18766. + * specific prior written permission.
  18767. + *
  18768. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18769. + * GNU General Public License ("GPL") version 2, as published by the Free
  18770. + * Software Foundation.
  18771. + *
  18772. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18773. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18774. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18775. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18776. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18777. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18778. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18779. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18780. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18781. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18782. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18783. + */
  18784. +
  18785. +#ifndef VCHI_CFG_H_
  18786. +#define VCHI_CFG_H_
  18787. +
  18788. +/****************************************************************************************
  18789. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  18790. + * services.
  18791. + ***************************************************************************************/
  18792. +
  18793. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  18794. +/* Really determined by the message driver, and should be available from a run-time call. */
  18795. +#ifndef VCHI_BULK_ALIGN
  18796. +# if __VCCOREVER__ >= 0x04000000
  18797. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  18798. +# else
  18799. +# define VCHI_BULK_ALIGN 16
  18800. +# endif
  18801. +#endif
  18802. +
  18803. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  18804. +/* May be less than or greater than VCHI_BULK_ALIGN */
  18805. +/* Really determined by the message driver, and should be available from a run-time call. */
  18806. +#ifndef VCHI_BULK_GRANULARITY
  18807. +# if __VCCOREVER__ >= 0x04000000
  18808. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  18809. +# else
  18810. +# define VCHI_BULK_GRANULARITY 16
  18811. +# endif
  18812. +#endif
  18813. +
  18814. +/* The largest possible message to be queued with vchi_msg_queue. */
  18815. +#ifndef VCHI_MAX_MSG_SIZE
  18816. +# if defined VCHI_LOCAL_HOST_PORT
  18817. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  18818. +# else
  18819. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  18820. +# endif
  18821. +#endif
  18822. +
  18823. +/******************************************************************************************
  18824. + * Defines below are system configuration options, and should not be used by VCHI services.
  18825. + *****************************************************************************************/
  18826. +
  18827. +/* How many connections can we support? A localhost implementation uses 2 connections,
  18828. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  18829. + * driver. */
  18830. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  18831. +# define VCHI_MAX_NUM_CONNECTIONS 3
  18832. +#endif
  18833. +
  18834. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  18835. + * amount of static memory. */
  18836. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  18837. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  18838. +#endif
  18839. +
  18840. +/* Adjust if using a message driver that supports more logical TX channels */
  18841. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  18842. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  18843. +#endif
  18844. +
  18845. +/* Adjust if using a message driver that supports more logical RX channels */
  18846. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  18847. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  18848. +#endif
  18849. +
  18850. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  18851. + * receive queue space, less message headers. */
  18852. +#ifndef VCHI_NUM_READ_SLOTS
  18853. +# if defined(VCHI_LOCAL_HOST_PORT)
  18854. +# define VCHI_NUM_READ_SLOTS 4
  18855. +# else
  18856. +# define VCHI_NUM_READ_SLOTS 48
  18857. +# endif
  18858. +#endif
  18859. +
  18860. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  18861. + * performance. Only define on VideoCore end, talking to host.
  18862. + */
  18863. +//#define VCHI_MSG_RX_OVERRUN
  18864. +
  18865. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  18866. + * underneath VCHI will usually have its own buffering. */
  18867. +#ifndef VCHI_NUM_WRITE_SLOTS
  18868. +# define VCHI_NUM_WRITE_SLOTS 4
  18869. +#endif
  18870. +
  18871. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  18872. + * then it's taking up too much buffer space, and the peer service will be told to stop
  18873. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  18874. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  18875. + * is too high. */
  18876. +#ifndef VCHI_XOFF_THRESHOLD
  18877. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  18878. +#endif
  18879. +
  18880. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  18881. + * service has dequeued/released enough messages that it's now occupying
  18882. + * VCHI_XON_THRESHOLD slots or fewer. */
  18883. +#ifndef VCHI_XON_THRESHOLD
  18884. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  18885. +#endif
  18886. +
  18887. +/* A size below which a bulk transfer omits the handshake completely and always goes
  18888. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  18889. + * can guarantee this by enabling unaligned transmits).
  18890. + * Not API. */
  18891. +#ifndef VCHI_MIN_BULK_SIZE
  18892. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  18893. +#endif
  18894. +
  18895. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  18896. + * speed and latency; the smaller the chunk size the better change of messages and other
  18897. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  18898. + * break transmissions into chunks.
  18899. + */
  18900. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  18901. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  18902. +#endif
  18903. +
  18904. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  18905. + * with multiple-line frames. Only use if the receiver can cope. */
  18906. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  18907. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  18908. +#endif
  18909. +
  18910. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  18911. + * vchi_msg_queue will be blocked. */
  18912. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  18913. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  18914. +#endif
  18915. +
  18916. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  18917. + * will be suspended until older messages are dequeued/released. */
  18918. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  18919. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  18920. +#endif
  18921. +
  18922. +/* Really should be able to cope if we run out of received message descriptors, by
  18923. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  18924. + * under the carpet. */
  18925. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  18926. +# undef VCHI_RX_MSG_QUEUE_SIZE
  18927. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  18928. +#endif
  18929. +
  18930. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  18931. + * will be blocked. */
  18932. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  18933. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  18934. +#endif
  18935. +
  18936. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  18937. + * will be blocked. */
  18938. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  18939. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  18940. +#endif
  18941. +
  18942. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  18943. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  18944. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  18945. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  18946. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  18947. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  18948. +#endif
  18949. +
  18950. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  18951. + * transmitter on and off.
  18952. + */
  18953. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  18954. +
  18955. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  18956. +
  18957. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  18958. + * negative for no IDLE.
  18959. + */
  18960. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  18961. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  18962. +# endif
  18963. +
  18964. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  18965. + * negative for no OFF.
  18966. + */
  18967. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  18968. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  18969. +# endif
  18970. +
  18971. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  18972. +
  18973. +#endif /* VCHI_CFG_H_ */
  18974. +
  18975. +/****************************** End of file **********************************/
  18976. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  18977. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 1970-01-01 01:00:00.000000000 +0100
  18978. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 2014-03-11 17:32:37.000000000 +0100
  18979. @@ -0,0 +1,71 @@
  18980. +/**
  18981. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18982. + *
  18983. + * Redistribution and use in source and binary forms, with or without
  18984. + * modification, are permitted provided that the following conditions
  18985. + * are met:
  18986. + * 1. Redistributions of source code must retain the above copyright
  18987. + * notice, this list of conditions, and the following disclaimer,
  18988. + * without modification.
  18989. + * 2. Redistributions in binary form must reproduce the above copyright
  18990. + * notice, this list of conditions and the following disclaimer in the
  18991. + * documentation and/or other materials provided with the distribution.
  18992. + * 3. The names of the above-listed copyright holders may not be used
  18993. + * to endorse or promote products derived from this software without
  18994. + * specific prior written permission.
  18995. + *
  18996. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18997. + * GNU General Public License ("GPL") version 2, as published by the Free
  18998. + * Software Foundation.
  18999. + *
  19000. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19001. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19002. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19003. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19004. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19005. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19006. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19007. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19008. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19009. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19010. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19011. + */
  19012. +
  19013. +#ifndef VCHI_CFG_INTERNAL_H_
  19014. +#define VCHI_CFG_INTERNAL_H_
  19015. +
  19016. +/****************************************************************************************
  19017. + * Control optimisation attempts.
  19018. + ***************************************************************************************/
  19019. +
  19020. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  19021. +#define VCHI_COARSE_LOCKING
  19022. +
  19023. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  19024. +// (only relevant if VCHI_COARSE_LOCKING)
  19025. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  19026. +
  19027. +// Avoid lock on non-blocking peek
  19028. +// (only relevant if VCHI_COARSE_LOCKING)
  19029. +#define VCHI_AVOID_PEEK_LOCK
  19030. +
  19031. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  19032. +#define VCHI_MULTIPLE_HANDLER_THREADS
  19033. +
  19034. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  19035. +// our way through the pool of descriptors.
  19036. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  19037. +
  19038. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  19039. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  19040. +
  19041. +// Don't use message descriptors for TX messages that don't need them
  19042. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  19043. +
  19044. +// Nano-locks for multiqueue
  19045. +//#define VCHI_MQUEUE_NANOLOCKS
  19046. +
  19047. +// Lock-free(er) dequeuing
  19048. +//#define VCHI_RX_NANOLOCKS
  19049. +
  19050. +#endif /*VCHI_CFG_INTERNAL_H_*/
  19051. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_common.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  19052. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_common.h 1970-01-01 01:00:00.000000000 +0100
  19053. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_common.h 2014-03-11 17:32:37.000000000 +0100
  19054. @@ -0,0 +1,163 @@
  19055. +/**
  19056. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19057. + *
  19058. + * Redistribution and use in source and binary forms, with or without
  19059. + * modification, are permitted provided that the following conditions
  19060. + * are met:
  19061. + * 1. Redistributions of source code must retain the above copyright
  19062. + * notice, this list of conditions, and the following disclaimer,
  19063. + * without modification.
  19064. + * 2. Redistributions in binary form must reproduce the above copyright
  19065. + * notice, this list of conditions and the following disclaimer in the
  19066. + * documentation and/or other materials provided with the distribution.
  19067. + * 3. The names of the above-listed copyright holders may not be used
  19068. + * to endorse or promote products derived from this software without
  19069. + * specific prior written permission.
  19070. + *
  19071. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19072. + * GNU General Public License ("GPL") version 2, as published by the Free
  19073. + * Software Foundation.
  19074. + *
  19075. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19076. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19077. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19078. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19079. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19080. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19081. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19082. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19083. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19084. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19085. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19086. + */
  19087. +
  19088. +#ifndef VCHI_COMMON_H_
  19089. +#define VCHI_COMMON_H_
  19090. +
  19091. +
  19092. +//flags used when sending messages (must be bitmapped)
  19093. +typedef enum
  19094. +{
  19095. + VCHI_FLAGS_NONE = 0x0,
  19096. + 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)
  19097. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  19098. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  19099. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  19100. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  19101. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  19102. +
  19103. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  19104. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  19105. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  19106. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  19107. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  19108. + VCHI_FLAGS_INTERNAL = 0xFF0000
  19109. +} VCHI_FLAGS_T;
  19110. +
  19111. +// constants for vchi_crc_control()
  19112. +typedef enum {
  19113. + VCHI_CRC_NOTHING = -1,
  19114. + VCHI_CRC_PER_SERVICE = 0,
  19115. + VCHI_CRC_EVERYTHING = 1,
  19116. +} VCHI_CRC_CONTROL_T;
  19117. +
  19118. +//callback reasons when an event occurs on a service
  19119. +typedef enum
  19120. +{
  19121. + VCHI_CALLBACK_REASON_MIN,
  19122. +
  19123. + //This indicates that there is data available
  19124. + //handle is the msg id that was transmitted with the data
  19125. + // When a message is received and there was no FULL message available previously, send callback
  19126. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  19127. + VCHI_CALLBACK_MSG_AVAILABLE,
  19128. + VCHI_CALLBACK_MSG_SENT,
  19129. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  19130. +
  19131. + // This indicates that a transfer from the other side has completed
  19132. + VCHI_CALLBACK_BULK_RECEIVED,
  19133. + //This indicates that data queued up to be sent has now gone
  19134. + //handle is the msg id that was used when sending the data
  19135. + VCHI_CALLBACK_BULK_SENT,
  19136. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  19137. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  19138. +
  19139. + VCHI_CALLBACK_SERVICE_CLOSED,
  19140. +
  19141. + // this side has sent XOFF to peer due to lack of data consumption by service
  19142. + // (suggests the service may need to take some recovery action if it has
  19143. + // been deliberately holding off consuming data)
  19144. + VCHI_CALLBACK_SENT_XOFF,
  19145. + VCHI_CALLBACK_SENT_XON,
  19146. +
  19147. + // indicates that a bulk transfer has finished reading the source buffer
  19148. + VCHI_CALLBACK_BULK_DATA_READ,
  19149. +
  19150. + // power notification events (currently host side only)
  19151. + VCHI_CALLBACK_PEER_OFF,
  19152. + VCHI_CALLBACK_PEER_SUSPENDED,
  19153. + VCHI_CALLBACK_PEER_ON,
  19154. + VCHI_CALLBACK_PEER_RESUMED,
  19155. + VCHI_CALLBACK_FORCED_POWER_OFF,
  19156. +
  19157. +#ifdef USE_VCHIQ_ARM
  19158. + // some extra notifications provided by vchiq_arm
  19159. + VCHI_CALLBACK_SERVICE_OPENED,
  19160. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  19161. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  19162. +#endif
  19163. +
  19164. + VCHI_CALLBACK_REASON_MAX
  19165. +} VCHI_CALLBACK_REASON_T;
  19166. +
  19167. +//Calback used by all services / bulk transfers
  19168. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  19169. + VCHI_CALLBACK_REASON_T reason,
  19170. + void *handle ); //for transmitting msg's only
  19171. +
  19172. +
  19173. +
  19174. +/*
  19175. + * Define vector struct for scatter-gather (vector) operations
  19176. + * Vectors can be nested - if a vector element has negative length, then
  19177. + * the data pointer is treated as pointing to another vector array, with
  19178. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  19179. + * you can do this:
  19180. + *
  19181. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  19182. + * {
  19183. + * VCHI_MSG_VECTOR_T nv[2];
  19184. + * nv[0].vec_base = my_header;
  19185. + * nv[0].vec_len = sizeof my_header;
  19186. + * nv[1].vec_base = v;
  19187. + * nv[1].vec_len = -n;
  19188. + * ...
  19189. + *
  19190. + */
  19191. +typedef struct vchi_msg_vector {
  19192. + const void *vec_base;
  19193. + int32_t vec_len;
  19194. +} VCHI_MSG_VECTOR_T;
  19195. +
  19196. +// Opaque type for a connection API
  19197. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  19198. +
  19199. +// Opaque type for a message driver
  19200. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  19201. +
  19202. +
  19203. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  19204. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  19205. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  19206. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  19207. +// is used again after messages for that service are removed/dequeued by any
  19208. +// means other than vchi_msg_iter_... calls on the iterator itself.
  19209. +typedef struct {
  19210. + struct opaque_vchi_service_t *service;
  19211. + void *last;
  19212. + void *next;
  19213. + void *remove;
  19214. +} VCHI_MSG_ITER_T;
  19215. +
  19216. +
  19217. +#endif // VCHI_COMMON_H_
  19218. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi.h
  19219. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi.h 1970-01-01 01:00:00.000000000 +0100
  19220. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi.h 2014-03-11 17:32:37.000000000 +0100
  19221. @@ -0,0 +1,373 @@
  19222. +/**
  19223. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19224. + *
  19225. + * Redistribution and use in source and binary forms, with or without
  19226. + * modification, are permitted provided that the following conditions
  19227. + * are met:
  19228. + * 1. Redistributions of source code must retain the above copyright
  19229. + * notice, this list of conditions, and the following disclaimer,
  19230. + * without modification.
  19231. + * 2. Redistributions in binary form must reproduce the above copyright
  19232. + * notice, this list of conditions and the following disclaimer in the
  19233. + * documentation and/or other materials provided with the distribution.
  19234. + * 3. The names of the above-listed copyright holders may not be used
  19235. + * to endorse or promote products derived from this software without
  19236. + * specific prior written permission.
  19237. + *
  19238. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19239. + * GNU General Public License ("GPL") version 2, as published by the Free
  19240. + * Software Foundation.
  19241. + *
  19242. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19243. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19244. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19245. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19246. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19247. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19248. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19249. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19250. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19251. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19252. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19253. + */
  19254. +
  19255. +#ifndef VCHI_H_
  19256. +#define VCHI_H_
  19257. +
  19258. +#include "interface/vchi/vchi_cfg.h"
  19259. +#include "interface/vchi/vchi_common.h"
  19260. +#include "interface/vchi/connections/connection.h"
  19261. +#include "vchi_mh.h"
  19262. +
  19263. +
  19264. +/******************************************************************************
  19265. + Global defs
  19266. + *****************************************************************************/
  19267. +
  19268. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  19269. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  19270. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  19271. +
  19272. +#ifdef USE_VCHIQ_ARM
  19273. +#define VCHI_BULK_ALIGNED(x) 1
  19274. +#else
  19275. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  19276. +#endif
  19277. +
  19278. +struct vchi_version {
  19279. + uint32_t version;
  19280. + uint32_t version_min;
  19281. +};
  19282. +#define VCHI_VERSION(v_) { v_, v_ }
  19283. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  19284. +
  19285. +typedef enum
  19286. +{
  19287. + VCHI_VEC_POINTER,
  19288. + VCHI_VEC_HANDLE,
  19289. + VCHI_VEC_LIST
  19290. +} VCHI_MSG_VECTOR_TYPE_T;
  19291. +
  19292. +typedef struct vchi_msg_vector_ex {
  19293. +
  19294. + VCHI_MSG_VECTOR_TYPE_T type;
  19295. + union
  19296. + {
  19297. + // a memory handle
  19298. + struct
  19299. + {
  19300. + VCHI_MEM_HANDLE_T handle;
  19301. + uint32_t offset;
  19302. + int32_t vec_len;
  19303. + } handle;
  19304. +
  19305. + // an ordinary data pointer
  19306. + struct
  19307. + {
  19308. + const void *vec_base;
  19309. + int32_t vec_len;
  19310. + } ptr;
  19311. +
  19312. + // a nested vector list
  19313. + struct
  19314. + {
  19315. + struct vchi_msg_vector_ex *vec;
  19316. + uint32_t vec_len;
  19317. + } list;
  19318. + } u;
  19319. +} VCHI_MSG_VECTOR_EX_T;
  19320. +
  19321. +
  19322. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  19323. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  19324. +
  19325. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  19326. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  19327. +
  19328. +// Macros to manipulate 'FOURCC' values
  19329. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  19330. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  19331. +
  19332. +
  19333. +// Opaque service information
  19334. +struct opaque_vchi_service_t;
  19335. +
  19336. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  19337. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  19338. +typedef struct
  19339. +{
  19340. + struct opaque_vchi_service_t *service;
  19341. + void *message;
  19342. +} VCHI_HELD_MSG_T;
  19343. +
  19344. +
  19345. +
  19346. +// structure used to provide the information needed to open a server or a client
  19347. +typedef struct {
  19348. + struct vchi_version version;
  19349. + int32_t service_id;
  19350. + VCHI_CONNECTION_T *connection;
  19351. + uint32_t rx_fifo_size;
  19352. + uint32_t tx_fifo_size;
  19353. + VCHI_CALLBACK_T callback;
  19354. + void *callback_param;
  19355. + /* client intends to receive bulk transfers of
  19356. + odd lengths or into unaligned buffers */
  19357. + int32_t want_unaligned_bulk_rx;
  19358. + /* client intends to transmit bulk transfers of
  19359. + odd lengths or out of unaligned buffers */
  19360. + int32_t want_unaligned_bulk_tx;
  19361. + /* client wants to check CRCs on (bulk) xfers.
  19362. + Only needs to be set at 1 end - will do both directions. */
  19363. + int32_t want_crc;
  19364. +} SERVICE_CREATION_T;
  19365. +
  19366. +// Opaque handle for a VCHI instance
  19367. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  19368. +
  19369. +// Opaque handle for a server or client
  19370. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  19371. +
  19372. +// Service registration & startup
  19373. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  19374. +
  19375. +typedef struct service_info_tag {
  19376. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  19377. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  19378. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  19379. +} SERVICE_INFO_T;
  19380. +
  19381. +/******************************************************************************
  19382. + Global funcs - implementation is specific to which side you are on (local / remote)
  19383. + *****************************************************************************/
  19384. +
  19385. +#ifdef __cplusplus
  19386. +extern "C" {
  19387. +#endif
  19388. +
  19389. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  19390. + const VCHI_MESSAGE_DRIVER_T * low_level);
  19391. +
  19392. +
  19393. +// Routine used to initialise the vchi on both local + remote connections
  19394. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  19395. +
  19396. +extern int32_t vchi_exit( void );
  19397. +
  19398. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  19399. + const uint32_t num_connections,
  19400. + VCHI_INSTANCE_T instance_handle );
  19401. +
  19402. +//When this is called, ensure that all services have no data pending.
  19403. +//Bulk transfers can remain 'queued'
  19404. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  19405. +
  19406. +// Global control over bulk CRC checking
  19407. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  19408. + VCHI_CRC_CONTROL_T control );
  19409. +
  19410. +// helper functions
  19411. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  19412. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  19413. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  19414. +
  19415. +
  19416. +/******************************************************************************
  19417. + Global service API
  19418. + *****************************************************************************/
  19419. +// Routine to create a named service
  19420. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  19421. + SERVICE_CREATION_T *setup,
  19422. + VCHI_SERVICE_HANDLE_T *handle );
  19423. +
  19424. +// Routine to destory a service
  19425. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  19426. +
  19427. +// Routine to open a named service
  19428. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  19429. + SERVICE_CREATION_T *setup,
  19430. + VCHI_SERVICE_HANDLE_T *handle);
  19431. +
  19432. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  19433. + short *peer_version );
  19434. +
  19435. +// Routine to close a named service
  19436. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  19437. +
  19438. +// Routine to increment ref count on a named service
  19439. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  19440. +
  19441. +// Routine to decrement ref count on a named service
  19442. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  19443. +
  19444. +// Routine to send a message accross a service
  19445. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  19446. + const void *data,
  19447. + uint32_t data_size,
  19448. + VCHI_FLAGS_T flags,
  19449. + void *msg_handle );
  19450. +
  19451. +// scatter-gather (vector) and send message
  19452. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  19453. + VCHI_MSG_VECTOR_EX_T *vector,
  19454. + uint32_t count,
  19455. + VCHI_FLAGS_T flags,
  19456. + void *msg_handle );
  19457. +
  19458. +// legacy scatter-gather (vector) and send message, only handles pointers
  19459. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  19460. + VCHI_MSG_VECTOR_T *vector,
  19461. + uint32_t count,
  19462. + VCHI_FLAGS_T flags,
  19463. + void *msg_handle );
  19464. +
  19465. +// Routine to receive a msg from a service
  19466. +// Dequeue is equivalent to hold, copy into client buffer, release
  19467. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  19468. + void *data,
  19469. + uint32_t max_data_size_to_read,
  19470. + uint32_t *actual_msg_size,
  19471. + VCHI_FLAGS_T flags );
  19472. +
  19473. +// Routine to look at a message in place.
  19474. +// The message is not dequeued, so a subsequent call to peek or dequeue
  19475. +// will return the same message.
  19476. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  19477. + void **data,
  19478. + uint32_t *msg_size,
  19479. + VCHI_FLAGS_T flags );
  19480. +
  19481. +// Routine to remove a message after it has been read in place with peek
  19482. +// The first message on the queue is dequeued.
  19483. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  19484. +
  19485. +// Routine to look at a message in place.
  19486. +// The message is dequeued, so the caller is left holding it; the descriptor is
  19487. +// filled in and must be released when the user has finished with the message.
  19488. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  19489. + void **data, // } may be NULL, as info can be
  19490. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  19491. + VCHI_FLAGS_T flags,
  19492. + VCHI_HELD_MSG_T *message_descriptor );
  19493. +
  19494. +// Initialise an iterator to look through messages in place
  19495. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  19496. + VCHI_MSG_ITER_T *iter,
  19497. + VCHI_FLAGS_T flags );
  19498. +
  19499. +/******************************************************************************
  19500. + Global service support API - operations on held messages and message iterators
  19501. + *****************************************************************************/
  19502. +
  19503. +// Routine to get the address of a held message
  19504. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  19505. +
  19506. +// Routine to get the size of a held message
  19507. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  19508. +
  19509. +// Routine to get the transmit timestamp as written into the header by the peer
  19510. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  19511. +
  19512. +// Routine to get the reception timestamp, written as we parsed the header
  19513. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  19514. +
  19515. +// Routine to release a held message after it has been processed
  19516. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  19517. +
  19518. +// Indicates whether the iterator has a next message.
  19519. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  19520. +
  19521. +// Return the pointer and length for the next message and advance the iterator.
  19522. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  19523. + void **data,
  19524. + uint32_t *msg_size );
  19525. +
  19526. +// Remove the last message returned by vchi_msg_iter_next.
  19527. +// Can only be called once after each call to vchi_msg_iter_next.
  19528. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  19529. +
  19530. +// Hold the last message returned by vchi_msg_iter_next.
  19531. +// Can only be called once after each call to vchi_msg_iter_next.
  19532. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  19533. + VCHI_HELD_MSG_T *message );
  19534. +
  19535. +// Return information for the next message, and hold it, advancing the iterator.
  19536. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  19537. + void **data, // } may be NULL
  19538. + uint32_t *msg_size, // }
  19539. + VCHI_HELD_MSG_T *message );
  19540. +
  19541. +
  19542. +/******************************************************************************
  19543. + Global bulk API
  19544. + *****************************************************************************/
  19545. +
  19546. +// Routine to prepare interface for a transfer from the other side
  19547. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  19548. + void *data_dst,
  19549. + uint32_t data_size,
  19550. + VCHI_FLAGS_T flags,
  19551. + void *transfer_handle );
  19552. +
  19553. +
  19554. +// Prepare interface for a transfer from the other side into relocatable memory.
  19555. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  19556. + VCHI_MEM_HANDLE_T h_dst,
  19557. + uint32_t offset,
  19558. + uint32_t data_size,
  19559. + const VCHI_FLAGS_T flags,
  19560. + void * const bulk_handle );
  19561. +
  19562. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  19563. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  19564. + const void *data_src,
  19565. + uint32_t data_size,
  19566. + VCHI_FLAGS_T flags,
  19567. + void *transfer_handle );
  19568. +
  19569. +
  19570. +/******************************************************************************
  19571. + Configuration plumbing
  19572. + *****************************************************************************/
  19573. +
  19574. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  19575. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  19576. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  19577. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  19578. +
  19579. +// declare all message drivers here
  19580. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  19581. +
  19582. +#ifdef __cplusplus
  19583. +}
  19584. +#endif
  19585. +
  19586. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  19587. + VCHI_MEM_HANDLE_T h_src,
  19588. + uint32_t offset,
  19589. + uint32_t data_size,
  19590. + VCHI_FLAGS_T flags,
  19591. + void *transfer_handle );
  19592. +#endif /* VCHI_H_ */
  19593. +
  19594. +/****************************** End of file **********************************/
  19595. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_mh.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  19596. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 1970-01-01 01:00:00.000000000 +0100
  19597. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 2014-03-11 17:32:37.000000000 +0100
  19598. @@ -0,0 +1,42 @@
  19599. +/**
  19600. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19601. + *
  19602. + * Redistribution and use in source and binary forms, with or without
  19603. + * modification, are permitted provided that the following conditions
  19604. + * are met:
  19605. + * 1. Redistributions of source code must retain the above copyright
  19606. + * notice, this list of conditions, and the following disclaimer,
  19607. + * without modification.
  19608. + * 2. Redistributions in binary form must reproduce the above copyright
  19609. + * notice, this list of conditions and the following disclaimer in the
  19610. + * documentation and/or other materials provided with the distribution.
  19611. + * 3. The names of the above-listed copyright holders may not be used
  19612. + * to endorse or promote products derived from this software without
  19613. + * specific prior written permission.
  19614. + *
  19615. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19616. + * GNU General Public License ("GPL") version 2, as published by the Free
  19617. + * Software Foundation.
  19618. + *
  19619. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19620. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19621. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19622. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19623. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19624. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19625. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19626. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19627. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19628. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19629. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19630. + */
  19631. +
  19632. +#ifndef VCHI_MH_H_
  19633. +#define VCHI_MH_H_
  19634. +
  19635. +#include <linux/types.h>
  19636. +
  19637. +typedef int32_t VCHI_MEM_HANDLE_T;
  19638. +#define VCHI_MEM_HANDLE_INVALID 0
  19639. +
  19640. +#endif
  19641. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  19642. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 1970-01-01 01:00:00.000000000 +0100
  19643. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 2014-03-11 17:32:37.000000000 +0100
  19644. @@ -0,0 +1,561 @@
  19645. +/**
  19646. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19647. + *
  19648. + * Redistribution and use in source and binary forms, with or without
  19649. + * modification, are permitted provided that the following conditions
  19650. + * are met:
  19651. + * 1. Redistributions of source code must retain the above copyright
  19652. + * notice, this list of conditions, and the following disclaimer,
  19653. + * without modification.
  19654. + * 2. Redistributions in binary form must reproduce the above copyright
  19655. + * notice, this list of conditions and the following disclaimer in the
  19656. + * documentation and/or other materials provided with the distribution.
  19657. + * 3. The names of the above-listed copyright holders may not be used
  19658. + * to endorse or promote products derived from this software without
  19659. + * specific prior written permission.
  19660. + *
  19661. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19662. + * GNU General Public License ("GPL") version 2, as published by the Free
  19663. + * Software Foundation.
  19664. + *
  19665. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19666. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19667. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19668. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19669. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19670. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19671. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19672. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19673. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19674. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19675. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19676. + */
  19677. +
  19678. +#include <linux/kernel.h>
  19679. +#include <linux/types.h>
  19680. +#include <linux/errno.h>
  19681. +#include <linux/interrupt.h>
  19682. +#include <linux/irq.h>
  19683. +#include <linux/pagemap.h>
  19684. +#include <linux/dma-mapping.h>
  19685. +#include <linux/version.h>
  19686. +#include <linux/io.h>
  19687. +#include <linux/uaccess.h>
  19688. +#include <asm/pgtable.h>
  19689. +
  19690. +#include <mach/irqs.h>
  19691. +
  19692. +#include <mach/platform.h>
  19693. +#include <mach/vcio.h>
  19694. +
  19695. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  19696. +
  19697. +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
  19698. +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
  19699. +
  19700. +#include "vchiq_arm.h"
  19701. +#include "vchiq_2835.h"
  19702. +#include "vchiq_connected.h"
  19703. +
  19704. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  19705. +
  19706. +typedef struct vchiq_2835_state_struct {
  19707. + int inited;
  19708. + VCHIQ_ARM_STATE_T arm_state;
  19709. +} VCHIQ_2835_ARM_STATE_T;
  19710. +
  19711. +static char *g_slot_mem;
  19712. +static int g_slot_mem_size;
  19713. +dma_addr_t g_slot_phys;
  19714. +static FRAGMENTS_T *g_fragments_base;
  19715. +static FRAGMENTS_T *g_free_fragments;
  19716. +struct semaphore g_free_fragments_sema;
  19717. +
  19718. +extern int vchiq_arm_log_level;
  19719. +
  19720. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  19721. +
  19722. +static irqreturn_t
  19723. +vchiq_doorbell_irq(int irq, void *dev_id);
  19724. +
  19725. +static int
  19726. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  19727. + struct task_struct *task, PAGELIST_T ** ppagelist);
  19728. +
  19729. +static void
  19730. +free_pagelist(PAGELIST_T *pagelist, int actual);
  19731. +
  19732. +int __init
  19733. +vchiq_platform_init(VCHIQ_STATE_T *state)
  19734. +{
  19735. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  19736. + int frag_mem_size;
  19737. + int err;
  19738. + int i;
  19739. +
  19740. + /* Allocate space for the channels in coherent memory */
  19741. + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  19742. + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
  19743. +
  19744. + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
  19745. + &g_slot_phys, GFP_ATOMIC);
  19746. +
  19747. + if (!g_slot_mem) {
  19748. + vchiq_log_error(vchiq_arm_log_level,
  19749. + "Unable to allocate channel memory");
  19750. + err = -ENOMEM;
  19751. + goto failed_alloc;
  19752. + }
  19753. +
  19754. + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
  19755. +
  19756. + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
  19757. + if (!vchiq_slot_zero) {
  19758. + err = -EINVAL;
  19759. + goto failed_init_slots;
  19760. + }
  19761. +
  19762. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  19763. + (int)g_slot_phys + g_slot_mem_size;
  19764. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  19765. + MAX_FRAGMENTS;
  19766. +
  19767. + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
  19768. + g_slot_mem_size += frag_mem_size;
  19769. +
  19770. + g_free_fragments = g_fragments_base;
  19771. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  19772. + *(FRAGMENTS_T **)&g_fragments_base[i] =
  19773. + &g_fragments_base[i + 1];
  19774. + }
  19775. + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
  19776. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  19777. +
  19778. + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
  19779. + VCHIQ_SUCCESS) {
  19780. + err = -EINVAL;
  19781. + goto failed_vchiq_init;
  19782. + }
  19783. +
  19784. + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
  19785. + IRQF_IRQPOLL, "VCHIQ doorbell",
  19786. + state);
  19787. + if (err < 0) {
  19788. + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
  19789. + "irq=%d err=%d", __func__,
  19790. + VCHIQ_DOORBELL_IRQ, err);
  19791. + goto failed_request_irq;
  19792. + }
  19793. +
  19794. + /* Send the base address of the slots to VideoCore */
  19795. +
  19796. + dsb(); /* Ensure all writes have completed */
  19797. +
  19798. + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
  19799. +
  19800. + vchiq_log_info(vchiq_arm_log_level,
  19801. + "vchiq_init - done (slots %x, phys %x)",
  19802. + (unsigned int)vchiq_slot_zero, g_slot_phys);
  19803. +
  19804. + vchiq_call_connected_callbacks();
  19805. +
  19806. + return 0;
  19807. +
  19808. +failed_request_irq:
  19809. +failed_vchiq_init:
  19810. +failed_init_slots:
  19811. + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
  19812. +
  19813. +failed_alloc:
  19814. + return err;
  19815. +}
  19816. +
  19817. +void __exit
  19818. +vchiq_platform_exit(VCHIQ_STATE_T *state)
  19819. +{
  19820. + free_irq(VCHIQ_DOORBELL_IRQ, state);
  19821. + dma_free_coherent(NULL, g_slot_mem_size,
  19822. + g_slot_mem, g_slot_phys);
  19823. +}
  19824. +
  19825. +
  19826. +VCHIQ_STATUS_T
  19827. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  19828. +{
  19829. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  19830. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  19831. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  19832. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  19833. + if(status != VCHIQ_SUCCESS)
  19834. + {
  19835. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  19836. + }
  19837. + return status;
  19838. +}
  19839. +
  19840. +VCHIQ_ARM_STATE_T*
  19841. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  19842. +{
  19843. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  19844. + {
  19845. + BUG();
  19846. + }
  19847. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  19848. +}
  19849. +
  19850. +void
  19851. +remote_event_signal(REMOTE_EVENT_T *event)
  19852. +{
  19853. + wmb();
  19854. +
  19855. + event->fired = 1;
  19856. +
  19857. + dsb(); /* data barrier operation */
  19858. +
  19859. + if (event->armed) {
  19860. + /* trigger vc interrupt */
  19861. +
  19862. + writel(0, __io_address(ARM_0_BELL2));
  19863. + }
  19864. +}
  19865. +
  19866. +int
  19867. +vchiq_copy_from_user(void *dst, const void *src, int size)
  19868. +{
  19869. + if ((uint32_t)src < TASK_SIZE) {
  19870. + return copy_from_user(dst, src, size);
  19871. + } else {
  19872. + memcpy(dst, src, size);
  19873. + return 0;
  19874. + }
  19875. +}
  19876. +
  19877. +VCHIQ_STATUS_T
  19878. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  19879. + void *offset, int size, int dir)
  19880. +{
  19881. + PAGELIST_T *pagelist;
  19882. + int ret;
  19883. +
  19884. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  19885. +
  19886. + ret = create_pagelist((char __user *)offset, size,
  19887. + (dir == VCHIQ_BULK_RECEIVE)
  19888. + ? PAGELIST_READ
  19889. + : PAGELIST_WRITE,
  19890. + current,
  19891. + &pagelist);
  19892. + if (ret != 0)
  19893. + return VCHIQ_ERROR;
  19894. +
  19895. + bulk->handle = memhandle;
  19896. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  19897. +
  19898. + /* Store the pagelist address in remote_data, which isn't used by the
  19899. + slave. */
  19900. + bulk->remote_data = pagelist;
  19901. +
  19902. + return VCHIQ_SUCCESS;
  19903. +}
  19904. +
  19905. +void
  19906. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  19907. +{
  19908. + if (bulk && bulk->remote_data && bulk->actual)
  19909. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  19910. +}
  19911. +
  19912. +void
  19913. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  19914. +{
  19915. + /*
  19916. + * This should only be called on the master (VideoCore) side, but
  19917. + * provide an implementation to avoid the need for ifdefery.
  19918. + */
  19919. + BUG();
  19920. +}
  19921. +
  19922. +void
  19923. +vchiq_dump_platform_state(void *dump_context)
  19924. +{
  19925. + char buf[80];
  19926. + int len;
  19927. + len = snprintf(buf, sizeof(buf),
  19928. + " Platform: 2835 (VC master)");
  19929. + vchiq_dump(dump_context, buf, len + 1);
  19930. +}
  19931. +
  19932. +VCHIQ_STATUS_T
  19933. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  19934. +{
  19935. + return VCHIQ_ERROR;
  19936. +}
  19937. +
  19938. +VCHIQ_STATUS_T
  19939. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  19940. +{
  19941. + return VCHIQ_SUCCESS;
  19942. +}
  19943. +
  19944. +void
  19945. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  19946. +{
  19947. +}
  19948. +
  19949. +void
  19950. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  19951. +{
  19952. +}
  19953. +
  19954. +int
  19955. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  19956. +{
  19957. + return 1; // autosuspend not supported - videocore always wanted
  19958. +}
  19959. +
  19960. +int
  19961. +vchiq_platform_use_suspend_timer(void)
  19962. +{
  19963. + return 0;
  19964. +}
  19965. +void
  19966. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  19967. +{
  19968. + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
  19969. +}
  19970. +void
  19971. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  19972. +{
  19973. + (void)state;
  19974. +}
  19975. +/*
  19976. + * Local functions
  19977. + */
  19978. +
  19979. +static irqreturn_t
  19980. +vchiq_doorbell_irq(int irq, void *dev_id)
  19981. +{
  19982. + VCHIQ_STATE_T *state = dev_id;
  19983. + irqreturn_t ret = IRQ_NONE;
  19984. + unsigned int status;
  19985. +
  19986. + /* Read (and clear) the doorbell */
  19987. + status = readl(__io_address(ARM_0_BELL0));
  19988. +
  19989. + if (status & 0x4) { /* Was the doorbell rung? */
  19990. + remote_event_pollall(state);
  19991. + ret = IRQ_HANDLED;
  19992. + }
  19993. +
  19994. + return ret;
  19995. +}
  19996. +
  19997. +/* There is a potential problem with partial cache lines (pages?)
  19998. +** at the ends of the block when reading. If the CPU accessed anything in
  19999. +** the same line (page?) then it may have pulled old data into the cache,
  20000. +** obscuring the new data underneath. We can solve this by transferring the
  20001. +** partial cache lines separately, and allowing the ARM to copy into the
  20002. +** cached area.
  20003. +
  20004. +** N.B. This implementation plays slightly fast and loose with the Linux
  20005. +** driver programming rules, e.g. its use of __virt_to_bus instead of
  20006. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  20007. +** from increased speed as a result.
  20008. +*/
  20009. +
  20010. +static int
  20011. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  20012. + struct task_struct *task, PAGELIST_T ** ppagelist)
  20013. +{
  20014. + PAGELIST_T *pagelist;
  20015. + struct page **pages;
  20016. + struct page *page;
  20017. + unsigned long *addrs;
  20018. + unsigned int num_pages, offset, i;
  20019. + char *addr, *base_addr, *next_addr;
  20020. + int run, addridx, actual_pages;
  20021. + unsigned long *need_release;
  20022. +
  20023. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  20024. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  20025. +
  20026. + *ppagelist = NULL;
  20027. +
  20028. + /* Allocate enough storage to hold the page pointers and the page
  20029. + ** list
  20030. + */
  20031. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  20032. + (num_pages * sizeof(unsigned long)) +
  20033. + sizeof(unsigned long) +
  20034. + (num_pages * sizeof(pages[0])),
  20035. + GFP_KERNEL);
  20036. +
  20037. + vchiq_log_trace(vchiq_arm_log_level,
  20038. + "create_pagelist - %x", (unsigned int)pagelist);
  20039. + if (!pagelist)
  20040. + return -ENOMEM;
  20041. +
  20042. + addrs = pagelist->addrs;
  20043. + need_release = (unsigned long *)(addrs + num_pages);
  20044. + pages = (struct page **)(addrs + num_pages + 1);
  20045. +
  20046. + if (is_vmalloc_addr(buf)) {
  20047. + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
  20048. + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
  20049. + }
  20050. + *need_release = 0; /* do not try and release vmalloc pages */
  20051. + } else {
  20052. + down_read(&task->mm->mmap_sem);
  20053. + actual_pages = get_user_pages(task, task->mm,
  20054. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  20055. + num_pages,
  20056. + (type == PAGELIST_READ) /*Write */ ,
  20057. + 0 /*Force */ ,
  20058. + pages,
  20059. + NULL /*vmas */);
  20060. + up_read(&task->mm->mmap_sem);
  20061. +
  20062. + if (actual_pages != num_pages) {
  20063. + vchiq_log_info(vchiq_arm_log_level,
  20064. + "create_pagelist - only %d/%d pages locked",
  20065. + actual_pages,
  20066. + num_pages);
  20067. +
  20068. + /* This is probably due to the process being killed */
  20069. + while (actual_pages > 0)
  20070. + {
  20071. + actual_pages--;
  20072. + page_cache_release(pages[actual_pages]);
  20073. + }
  20074. + kfree(pagelist);
  20075. + if (actual_pages == 0)
  20076. + actual_pages = -ENOMEM;
  20077. + return actual_pages;
  20078. + }
  20079. + *need_release = 1; /* release user pages */
  20080. + }
  20081. +
  20082. + pagelist->length = count;
  20083. + pagelist->type = type;
  20084. + pagelist->offset = offset;
  20085. +
  20086. + /* Group the pages into runs of contiguous pages */
  20087. +
  20088. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  20089. + next_addr = base_addr + PAGE_SIZE;
  20090. + addridx = 0;
  20091. + run = 0;
  20092. +
  20093. + for (i = 1; i < num_pages; i++) {
  20094. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  20095. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  20096. + next_addr += PAGE_SIZE;
  20097. + run++;
  20098. + } else {
  20099. + addrs[addridx] = (unsigned long)base_addr + run;
  20100. + addridx++;
  20101. + base_addr = addr;
  20102. + next_addr = addr + PAGE_SIZE;
  20103. + run = 0;
  20104. + }
  20105. + }
  20106. +
  20107. + addrs[addridx] = (unsigned long)base_addr + run;
  20108. + addridx++;
  20109. +
  20110. + /* Partial cache lines (fragments) require special measures */
  20111. + if ((type == PAGELIST_READ) &&
  20112. + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
  20113. + ((pagelist->offset + pagelist->length) &
  20114. + (CACHE_LINE_SIZE - 1)))) {
  20115. + FRAGMENTS_T *fragments;
  20116. +
  20117. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  20118. + kfree(pagelist);
  20119. + return -EINTR;
  20120. + }
  20121. +
  20122. + WARN_ON(g_free_fragments == NULL);
  20123. +
  20124. + down(&g_free_fragments_mutex);
  20125. + fragments = (FRAGMENTS_T *) g_free_fragments;
  20126. + WARN_ON(fragments == NULL);
  20127. + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
  20128. + up(&g_free_fragments_mutex);
  20129. + pagelist->type =
  20130. + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
  20131. + g_fragments_base);
  20132. + }
  20133. +
  20134. + for (page = virt_to_page(pagelist);
  20135. + page <= virt_to_page(addrs + num_pages - 1); page++) {
  20136. + flush_dcache_page(page);
  20137. + }
  20138. +
  20139. + *ppagelist = pagelist;
  20140. +
  20141. + return 0;
  20142. +}
  20143. +
  20144. +static void
  20145. +free_pagelist(PAGELIST_T *pagelist, int actual)
  20146. +{
  20147. + unsigned long *need_release;
  20148. + struct page **pages;
  20149. + unsigned int num_pages, i;
  20150. +
  20151. + vchiq_log_trace(vchiq_arm_log_level,
  20152. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  20153. +
  20154. + num_pages =
  20155. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  20156. + PAGE_SIZE;
  20157. +
  20158. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  20159. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  20160. +
  20161. + /* Deal with any partial cache lines (fragments) */
  20162. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  20163. + FRAGMENTS_T *fragments = g_fragments_base +
  20164. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
  20165. + int head_bytes, tail_bytes;
  20166. + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
  20167. + (CACHE_LINE_SIZE - 1);
  20168. + tail_bytes = (pagelist->offset + actual) &
  20169. + (CACHE_LINE_SIZE - 1);
  20170. +
  20171. + if ((actual >= 0) && (head_bytes != 0)) {
  20172. + if (head_bytes > actual)
  20173. + head_bytes = actual;
  20174. +
  20175. + memcpy((char *)page_address(pages[0]) +
  20176. + pagelist->offset,
  20177. + fragments->headbuf,
  20178. + head_bytes);
  20179. + }
  20180. + if ((actual >= 0) && (head_bytes < actual) &&
  20181. + (tail_bytes != 0)) {
  20182. + memcpy((char *)page_address(pages[num_pages - 1]) +
  20183. + ((pagelist->offset + actual) &
  20184. + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
  20185. + fragments->tailbuf, tail_bytes);
  20186. + }
  20187. +
  20188. + down(&g_free_fragments_mutex);
  20189. + *(FRAGMENTS_T **) fragments = g_free_fragments;
  20190. + g_free_fragments = fragments;
  20191. + up(&g_free_fragments_mutex);
  20192. + up(&g_free_fragments_sema);
  20193. + }
  20194. +
  20195. + if (*need_release) {
  20196. + for (i = 0; i < num_pages; i++) {
  20197. + if (pagelist->type != PAGELIST_WRITE)
  20198. + set_page_dirty(pages[i]);
  20199. +
  20200. + page_cache_release(pages[i]);
  20201. + }
  20202. + }
  20203. +
  20204. + kfree(pagelist);
  20205. +}
  20206. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  20207. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 1970-01-01 01:00:00.000000000 +0100
  20208. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 2014-03-11 17:32:37.000000000 +0100
  20209. @@ -0,0 +1,42 @@
  20210. +/**
  20211. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  20212. + *
  20213. + * Redistribution and use in source and binary forms, with or without
  20214. + * modification, are permitted provided that the following conditions
  20215. + * are met:
  20216. + * 1. Redistributions of source code must retain the above copyright
  20217. + * notice, this list of conditions, and the following disclaimer,
  20218. + * without modification.
  20219. + * 2. Redistributions in binary form must reproduce the above copyright
  20220. + * notice, this list of conditions and the following disclaimer in the
  20221. + * documentation and/or other materials provided with the distribution.
  20222. + * 3. The names of the above-listed copyright holders may not be used
  20223. + * to endorse or promote products derived from this software without
  20224. + * specific prior written permission.
  20225. + *
  20226. + * ALTERNATIVELY, this software may be distributed under the terms of the
  20227. + * GNU General Public License ("GPL") version 2, as published by the Free
  20228. + * Software Foundation.
  20229. + *
  20230. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20231. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20232. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20233. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20234. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20235. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20236. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20237. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20238. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20239. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20240. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20241. + */
  20242. +
  20243. +#ifndef VCHIQ_2835_H
  20244. +#define VCHIQ_2835_H
  20245. +
  20246. +#include "vchiq_pagelist.h"
  20247. +
  20248. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  20249. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  20250. +
  20251. +#endif /* VCHIQ_2835_H */
  20252. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  20253. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 1970-01-01 01:00:00.000000000 +0100
  20254. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 2014-03-11 17:32:37.000000000 +0100
  20255. @@ -0,0 +1,2813 @@
  20256. +/**
  20257. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  20258. + *
  20259. + * Redistribution and use in source and binary forms, with or without
  20260. + * modification, are permitted provided that the following conditions
  20261. + * are met:
  20262. + * 1. Redistributions of source code must retain the above copyright
  20263. + * notice, this list of conditions, and the following disclaimer,
  20264. + * without modification.
  20265. + * 2. Redistributions in binary form must reproduce the above copyright
  20266. + * notice, this list of conditions and the following disclaimer in the
  20267. + * documentation and/or other materials provided with the distribution.
  20268. + * 3. The names of the above-listed copyright holders may not be used
  20269. + * to endorse or promote products derived from this software without
  20270. + * specific prior written permission.
  20271. + *
  20272. + * ALTERNATIVELY, this software may be distributed under the terms of the
  20273. + * GNU General Public License ("GPL") version 2, as published by the Free
  20274. + * Software Foundation.
  20275. + *
  20276. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20277. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20278. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20279. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20280. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20281. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20282. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20283. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20284. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20285. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20286. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20287. + */
  20288. +
  20289. +#include <linux/kernel.h>
  20290. +#include <linux/module.h>
  20291. +#include <linux/types.h>
  20292. +#include <linux/errno.h>
  20293. +#include <linux/cdev.h>
  20294. +#include <linux/fs.h>
  20295. +#include <linux/device.h>
  20296. +#include <linux/mm.h>
  20297. +#include <linux/highmem.h>
  20298. +#include <linux/pagemap.h>
  20299. +#include <linux/bug.h>
  20300. +#include <linux/semaphore.h>
  20301. +#include <linux/list.h>
  20302. +#include <linux/proc_fs.h>
  20303. +
  20304. +#include "vchiq_core.h"
  20305. +#include "vchiq_ioctl.h"
  20306. +#include "vchiq_arm.h"
  20307. +
  20308. +#define DEVICE_NAME "vchiq"
  20309. +
  20310. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  20311. +#undef MODULE_PARAM_PREFIX
  20312. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  20313. +
  20314. +#define VCHIQ_MINOR 0
  20315. +
  20316. +/* Some per-instance constants */
  20317. +#define MAX_COMPLETIONS 16
  20318. +#define MAX_SERVICES 64
  20319. +#define MAX_ELEMENTS 8
  20320. +#define MSG_QUEUE_SIZE 64
  20321. +
  20322. +#define KEEPALIVE_VER 1
  20323. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  20324. +
  20325. +/* Run time control of log level, based on KERN_XXX level. */
  20326. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  20327. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  20328. +
  20329. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  20330. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  20331. +
  20332. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  20333. +static const char *const suspend_state_names[] = {
  20334. + "VC_SUSPEND_FORCE_CANCELED",
  20335. + "VC_SUSPEND_REJECTED",
  20336. + "VC_SUSPEND_FAILED",
  20337. + "VC_SUSPEND_IDLE",
  20338. + "VC_SUSPEND_REQUESTED",
  20339. + "VC_SUSPEND_IN_PROGRESS",
  20340. + "VC_SUSPEND_SUSPENDED"
  20341. +};
  20342. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  20343. +static const char *const resume_state_names[] = {
  20344. + "VC_RESUME_FAILED",
  20345. + "VC_RESUME_IDLE",
  20346. + "VC_RESUME_REQUESTED",
  20347. + "VC_RESUME_IN_PROGRESS",
  20348. + "VC_RESUME_RESUMED"
  20349. +};
  20350. +/* The number of times we allow force suspend to timeout before actually
  20351. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  20352. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  20353. +*/
  20354. +#define FORCE_SUSPEND_FAIL_MAX 8
  20355. +
  20356. +/* The time in ms allowed for videocore to go idle when force suspend has been
  20357. + * requested */
  20358. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  20359. +
  20360. +
  20361. +static void suspend_timer_callback(unsigned long context);
  20362. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance);
  20363. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance);
  20364. +
  20365. +
  20366. +typedef struct user_service_struct {
  20367. + VCHIQ_SERVICE_T *service;
  20368. + void *userdata;
  20369. + VCHIQ_INSTANCE_T instance;
  20370. + int is_vchi;
  20371. + int dequeue_pending;
  20372. + int message_available_pos;
  20373. + int msg_insert;
  20374. + int msg_remove;
  20375. + struct semaphore insert_event;
  20376. + struct semaphore remove_event;
  20377. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  20378. +} USER_SERVICE_T;
  20379. +
  20380. +struct bulk_waiter_node {
  20381. + struct bulk_waiter bulk_waiter;
  20382. + int pid;
  20383. + struct list_head list;
  20384. +};
  20385. +
  20386. +struct vchiq_instance_struct {
  20387. + VCHIQ_STATE_T *state;
  20388. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  20389. + int completion_insert;
  20390. + int completion_remove;
  20391. + struct semaphore insert_event;
  20392. + struct semaphore remove_event;
  20393. + struct mutex completion_mutex;
  20394. +
  20395. + int connected;
  20396. + int closing;
  20397. + int pid;
  20398. + int mark;
  20399. +
  20400. + struct list_head bulk_waiter_list;
  20401. + struct mutex bulk_waiter_list_mutex;
  20402. +
  20403. + struct proc_dir_entry *proc_entry;
  20404. +};
  20405. +
  20406. +typedef struct dump_context_struct {
  20407. + char __user *buf;
  20408. + size_t actual;
  20409. + size_t space;
  20410. + loff_t offset;
  20411. +} DUMP_CONTEXT_T;
  20412. +
  20413. +static struct cdev vchiq_cdev;
  20414. +static dev_t vchiq_devid;
  20415. +static VCHIQ_STATE_T g_state;
  20416. +static struct class *vchiq_class;
  20417. +static struct device *vchiq_dev;
  20418. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  20419. +
  20420. +static const char *const ioctl_names[] = {
  20421. + "CONNECT",
  20422. + "SHUTDOWN",
  20423. + "CREATE_SERVICE",
  20424. + "REMOVE_SERVICE",
  20425. + "QUEUE_MESSAGE",
  20426. + "QUEUE_BULK_TRANSMIT",
  20427. + "QUEUE_BULK_RECEIVE",
  20428. + "AWAIT_COMPLETION",
  20429. + "DEQUEUE_MESSAGE",
  20430. + "GET_CLIENT_ID",
  20431. + "GET_CONFIG",
  20432. + "CLOSE_SERVICE",
  20433. + "USE_SERVICE",
  20434. + "RELEASE_SERVICE",
  20435. + "SET_SERVICE_OPTION",
  20436. + "DUMP_PHYS_MEM"
  20437. +};
  20438. +
  20439. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  20440. + (VCHIQ_IOC_MAX + 1));
  20441. +
  20442. +static void
  20443. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  20444. +
  20445. +/****************************************************************************
  20446. +*
  20447. +* add_completion
  20448. +*
  20449. +***************************************************************************/
  20450. +
  20451. +static VCHIQ_STATUS_T
  20452. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  20453. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  20454. + void *bulk_userdata)
  20455. +{
  20456. + VCHIQ_COMPLETION_DATA_T *completion;
  20457. + DEBUG_INITIALISE(g_state.local)
  20458. +
  20459. + while (instance->completion_insert ==
  20460. + (instance->completion_remove + MAX_COMPLETIONS)) {
  20461. + /* Out of space - wait for the client */
  20462. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20463. + vchiq_log_trace(vchiq_arm_log_level,
  20464. + "add_completion - completion queue full");
  20465. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  20466. + if (down_interruptible(&instance->remove_event) != 0) {
  20467. + vchiq_log_info(vchiq_arm_log_level,
  20468. + "service_callback interrupted");
  20469. + return VCHIQ_RETRY;
  20470. + } else if (instance->closing) {
  20471. + vchiq_log_info(vchiq_arm_log_level,
  20472. + "service_callback closing");
  20473. + return VCHIQ_ERROR;
  20474. + }
  20475. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20476. + }
  20477. +
  20478. + completion =
  20479. + &instance->completions[instance->completion_insert &
  20480. + (MAX_COMPLETIONS - 1)];
  20481. +
  20482. + completion->header = header;
  20483. + completion->reason = reason;
  20484. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  20485. + completion->service_userdata = user_service->service;
  20486. + completion->bulk_userdata = bulk_userdata;
  20487. +
  20488. + if (reason == VCHIQ_SERVICE_CLOSED)
  20489. + /* Take an extra reference, to be held until
  20490. + this CLOSED notification is delivered. */
  20491. + lock_service(user_service->service);
  20492. +
  20493. + /* A write barrier is needed here to ensure that the entire completion
  20494. + record is written out before the insert point. */
  20495. + wmb();
  20496. +
  20497. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  20498. + user_service->message_available_pos =
  20499. + instance->completion_insert;
  20500. + instance->completion_insert++;
  20501. +
  20502. + up(&instance->insert_event);
  20503. +
  20504. + return VCHIQ_SUCCESS;
  20505. +}
  20506. +
  20507. +/****************************************************************************
  20508. +*
  20509. +* service_callback
  20510. +*
  20511. +***************************************************************************/
  20512. +
  20513. +static VCHIQ_STATUS_T
  20514. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  20515. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  20516. +{
  20517. + /* How do we ensure the callback goes to the right client?
  20518. + ** The service_user data points to a USER_SERVICE_T record containing
  20519. + ** the original callback and the user state structure, which contains a
  20520. + ** circular buffer for completion records.
  20521. + */
  20522. + USER_SERVICE_T *user_service;
  20523. + VCHIQ_SERVICE_T *service;
  20524. + VCHIQ_INSTANCE_T instance;
  20525. + DEBUG_INITIALISE(g_state.local)
  20526. +
  20527. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20528. +
  20529. + service = handle_to_service(handle);
  20530. + BUG_ON(!service);
  20531. + user_service = (USER_SERVICE_T *)service->base.userdata;
  20532. + instance = user_service->instance;
  20533. +
  20534. + if (!instance || instance->closing)
  20535. + return VCHIQ_SUCCESS;
  20536. +
  20537. + vchiq_log_trace(vchiq_arm_log_level,
  20538. + "service_callback - service %lx(%d), reason %d, header %lx, "
  20539. + "instance %lx, bulk_userdata %lx",
  20540. + (unsigned long)user_service,
  20541. + service->localport,
  20542. + reason, (unsigned long)header,
  20543. + (unsigned long)instance, (unsigned long)bulk_userdata);
  20544. +
  20545. + if (header && user_service->is_vchi) {
  20546. + spin_lock(&msg_queue_spinlock);
  20547. + while (user_service->msg_insert ==
  20548. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  20549. + spin_unlock(&msg_queue_spinlock);
  20550. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20551. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  20552. + vchiq_log_trace(vchiq_arm_log_level,
  20553. + "service_callback - msg queue full");
  20554. + /* If there is no MESSAGE_AVAILABLE in the completion
  20555. + ** queue, add one
  20556. + */
  20557. + if ((user_service->message_available_pos -
  20558. + instance->completion_remove) < 0) {
  20559. + VCHIQ_STATUS_T status;
  20560. + vchiq_log_info(vchiq_arm_log_level,
  20561. + "Inserting extra MESSAGE_AVAILABLE");
  20562. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20563. + status = add_completion(instance, reason,
  20564. + NULL, user_service, bulk_userdata);
  20565. + if (status != VCHIQ_SUCCESS) {
  20566. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20567. + return status;
  20568. + }
  20569. + }
  20570. +
  20571. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20572. + if (down_interruptible(&user_service->remove_event)
  20573. + != 0) {
  20574. + vchiq_log_info(vchiq_arm_log_level,
  20575. + "service_callback interrupted");
  20576. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20577. + return VCHIQ_RETRY;
  20578. + } else if (instance->closing) {
  20579. + vchiq_log_info(vchiq_arm_log_level,
  20580. + "service_callback closing");
  20581. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20582. + return VCHIQ_ERROR;
  20583. + }
  20584. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20585. + spin_lock(&msg_queue_spinlock);
  20586. + }
  20587. +
  20588. + user_service->msg_queue[user_service->msg_insert &
  20589. + (MSG_QUEUE_SIZE - 1)] = header;
  20590. + user_service->msg_insert++;
  20591. + spin_unlock(&msg_queue_spinlock);
  20592. +
  20593. + up(&user_service->insert_event);
  20594. +
  20595. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  20596. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  20597. + ** bypass the completion queue.
  20598. + */
  20599. + if (((user_service->message_available_pos -
  20600. + instance->completion_remove) >= 0) ||
  20601. + user_service->dequeue_pending) {
  20602. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20603. + user_service->dequeue_pending = 0;
  20604. + return VCHIQ_SUCCESS;
  20605. + }
  20606. +
  20607. + header = NULL;
  20608. + }
  20609. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20610. +
  20611. + return add_completion(instance, reason, header, user_service,
  20612. + bulk_userdata);
  20613. +}
  20614. +
  20615. +/****************************************************************************
  20616. +*
  20617. +* user_service_free
  20618. +*
  20619. +***************************************************************************/
  20620. +static void
  20621. +user_service_free(void *userdata)
  20622. +{
  20623. + kfree(userdata);
  20624. +}
  20625. +
  20626. +/****************************************************************************
  20627. +*
  20628. +* vchiq_ioctl
  20629. +*
  20630. +***************************************************************************/
  20631. +
  20632. +static long
  20633. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  20634. +{
  20635. + VCHIQ_INSTANCE_T instance = file->private_data;
  20636. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  20637. + VCHIQ_SERVICE_T *service = NULL;
  20638. + long ret = 0;
  20639. + int i, rc;
  20640. + DEBUG_INITIALISE(g_state.local)
  20641. +
  20642. + vchiq_log_trace(vchiq_arm_log_level,
  20643. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  20644. + (unsigned int)instance,
  20645. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  20646. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  20647. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  20648. +
  20649. + switch (cmd) {
  20650. + case VCHIQ_IOC_SHUTDOWN:
  20651. + if (!instance->connected)
  20652. + break;
  20653. +
  20654. + /* Remove all services */
  20655. + i = 0;
  20656. + while ((service = next_service_by_instance(instance->state,
  20657. + instance, &i)) != NULL) {
  20658. + status = vchiq_remove_service(service->handle);
  20659. + unlock_service(service);
  20660. + if (status != VCHIQ_SUCCESS)
  20661. + break;
  20662. + }
  20663. + service = NULL;
  20664. +
  20665. + if (status == VCHIQ_SUCCESS) {
  20666. + /* Wake the completion thread and ask it to exit */
  20667. + instance->closing = 1;
  20668. + up(&instance->insert_event);
  20669. + }
  20670. +
  20671. + break;
  20672. +
  20673. + case VCHIQ_IOC_CONNECT:
  20674. + if (instance->connected) {
  20675. + ret = -EINVAL;
  20676. + break;
  20677. + }
  20678. + rc = mutex_lock_interruptible(&instance->state->mutex);
  20679. + if (rc != 0) {
  20680. + vchiq_log_error(vchiq_arm_log_level,
  20681. + "vchiq: connect: could not lock mutex for "
  20682. + "state %d: %d",
  20683. + instance->state->id, rc);
  20684. + ret = -EINTR;
  20685. + break;
  20686. + }
  20687. + status = vchiq_connect_internal(instance->state, instance);
  20688. + mutex_unlock(&instance->state->mutex);
  20689. +
  20690. + if (status == VCHIQ_SUCCESS)
  20691. + instance->connected = 1;
  20692. + else
  20693. + vchiq_log_error(vchiq_arm_log_level,
  20694. + "vchiq: could not connect: %d", status);
  20695. + break;
  20696. +
  20697. + case VCHIQ_IOC_CREATE_SERVICE: {
  20698. + VCHIQ_CREATE_SERVICE_T args;
  20699. + USER_SERVICE_T *user_service = NULL;
  20700. + void *userdata;
  20701. + int srvstate;
  20702. +
  20703. + if (copy_from_user
  20704. + (&args, (const void __user *)arg,
  20705. + sizeof(args)) != 0) {
  20706. + ret = -EFAULT;
  20707. + break;
  20708. + }
  20709. +
  20710. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  20711. + if (!user_service) {
  20712. + ret = -ENOMEM;
  20713. + break;
  20714. + }
  20715. +
  20716. + if (args.is_open) {
  20717. + if (!instance->connected) {
  20718. + ret = -ENOTCONN;
  20719. + kfree(user_service);
  20720. + break;
  20721. + }
  20722. + srvstate = VCHIQ_SRVSTATE_OPENING;
  20723. + } else {
  20724. + srvstate =
  20725. + instance->connected ?
  20726. + VCHIQ_SRVSTATE_LISTENING :
  20727. + VCHIQ_SRVSTATE_HIDDEN;
  20728. + }
  20729. +
  20730. + userdata = args.params.userdata;
  20731. + args.params.callback = service_callback;
  20732. + args.params.userdata = user_service;
  20733. + service = vchiq_add_service_internal(
  20734. + instance->state,
  20735. + &args.params, srvstate,
  20736. + instance, user_service_free);
  20737. +
  20738. + if (service != NULL) {
  20739. + user_service->service = service;
  20740. + user_service->userdata = userdata;
  20741. + user_service->instance = instance;
  20742. + user_service->is_vchi = args.is_vchi;
  20743. + user_service->dequeue_pending = 0;
  20744. + user_service->message_available_pos =
  20745. + instance->completion_remove - 1;
  20746. + user_service->msg_insert = 0;
  20747. + user_service->msg_remove = 0;
  20748. + sema_init(&user_service->insert_event, 0);
  20749. + sema_init(&user_service->remove_event, 0);
  20750. +
  20751. + if (args.is_open) {
  20752. + status = vchiq_open_service_internal
  20753. + (service, instance->pid);
  20754. + if (status != VCHIQ_SUCCESS) {
  20755. + vchiq_remove_service(service->handle);
  20756. + service = NULL;
  20757. + ret = (status == VCHIQ_RETRY) ?
  20758. + -EINTR : -EIO;
  20759. + break;
  20760. + }
  20761. + }
  20762. +
  20763. + if (copy_to_user((void __user *)
  20764. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  20765. + arg)->handle),
  20766. + (const void *)&service->handle,
  20767. + sizeof(service->handle)) != 0) {
  20768. + ret = -EFAULT;
  20769. + vchiq_remove_service(service->handle);
  20770. + }
  20771. +
  20772. + service = NULL;
  20773. + } else {
  20774. + ret = -EEXIST;
  20775. + kfree(user_service);
  20776. + }
  20777. + } break;
  20778. +
  20779. + case VCHIQ_IOC_CLOSE_SERVICE: {
  20780. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20781. +
  20782. + service = find_service_for_instance(instance, handle);
  20783. + if (service != NULL)
  20784. + status = vchiq_close_service(service->handle);
  20785. + else
  20786. + ret = -EINVAL;
  20787. + } break;
  20788. +
  20789. + case VCHIQ_IOC_REMOVE_SERVICE: {
  20790. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20791. +
  20792. + service = find_service_for_instance(instance, handle);
  20793. + if (service != NULL)
  20794. + status = vchiq_remove_service(service->handle);
  20795. + else
  20796. + ret = -EINVAL;
  20797. + } break;
  20798. +
  20799. + case VCHIQ_IOC_USE_SERVICE:
  20800. + case VCHIQ_IOC_RELEASE_SERVICE: {
  20801. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20802. +
  20803. + service = find_service_for_instance(instance, handle);
  20804. + if (service != NULL) {
  20805. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  20806. + vchiq_use_service_internal(service) :
  20807. + vchiq_release_service_internal(service);
  20808. + if (status != VCHIQ_SUCCESS) {
  20809. + vchiq_log_error(vchiq_susp_log_level,
  20810. + "%s: cmd %s returned error %d for "
  20811. + "service %c%c%c%c:%03d",
  20812. + __func__,
  20813. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  20814. + "VCHIQ_IOC_USE_SERVICE" :
  20815. + "VCHIQ_IOC_RELEASE_SERVICE",
  20816. + status,
  20817. + VCHIQ_FOURCC_AS_4CHARS(
  20818. + service->base.fourcc),
  20819. + service->client_id);
  20820. + ret = -EINVAL;
  20821. + }
  20822. + } else
  20823. + ret = -EINVAL;
  20824. + } break;
  20825. +
  20826. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  20827. + VCHIQ_QUEUE_MESSAGE_T args;
  20828. + if (copy_from_user
  20829. + (&args, (const void __user *)arg,
  20830. + sizeof(args)) != 0) {
  20831. + ret = -EFAULT;
  20832. + break;
  20833. + }
  20834. +
  20835. + service = find_service_for_instance(instance, args.handle);
  20836. +
  20837. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  20838. + /* Copy elements into kernel space */
  20839. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  20840. + if (copy_from_user(elements, args.elements,
  20841. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  20842. + status = vchiq_queue_message
  20843. + (args.handle,
  20844. + elements, args.count);
  20845. + else
  20846. + ret = -EFAULT;
  20847. + } else {
  20848. + ret = -EINVAL;
  20849. + }
  20850. + } break;
  20851. +
  20852. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  20853. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  20854. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  20855. + struct bulk_waiter_node *waiter = NULL;
  20856. + VCHIQ_BULK_DIR_T dir =
  20857. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  20858. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  20859. +
  20860. + if (copy_from_user
  20861. + (&args, (const void __user *)arg,
  20862. + sizeof(args)) != 0) {
  20863. + ret = -EFAULT;
  20864. + break;
  20865. + }
  20866. +
  20867. + service = find_service_for_instance(instance, args.handle);
  20868. + if (!service) {
  20869. + ret = -EINVAL;
  20870. + break;
  20871. + }
  20872. +
  20873. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  20874. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  20875. + GFP_KERNEL);
  20876. + if (!waiter) {
  20877. + ret = -ENOMEM;
  20878. + break;
  20879. + }
  20880. + args.userdata = &waiter->bulk_waiter;
  20881. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  20882. + struct list_head *pos;
  20883. + mutex_lock(&instance->bulk_waiter_list_mutex);
  20884. + list_for_each(pos, &instance->bulk_waiter_list) {
  20885. + if (list_entry(pos, struct bulk_waiter_node,
  20886. + list)->pid == current->pid) {
  20887. + waiter = list_entry(pos,
  20888. + struct bulk_waiter_node,
  20889. + list);
  20890. + list_del(pos);
  20891. + break;
  20892. + }
  20893. +
  20894. + }
  20895. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  20896. + if (!waiter) {
  20897. + vchiq_log_error(vchiq_arm_log_level,
  20898. + "no bulk_waiter found for pid %d",
  20899. + current->pid);
  20900. + ret = -ESRCH;
  20901. + break;
  20902. + }
  20903. + vchiq_log_info(vchiq_arm_log_level,
  20904. + "found bulk_waiter %x for pid %d",
  20905. + (unsigned int)waiter, current->pid);
  20906. + args.userdata = &waiter->bulk_waiter;
  20907. + }
  20908. + status = vchiq_bulk_transfer
  20909. + (args.handle,
  20910. + VCHI_MEM_HANDLE_INVALID,
  20911. + args.data, args.size,
  20912. + args.userdata, args.mode,
  20913. + dir);
  20914. + if (!waiter)
  20915. + break;
  20916. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  20917. + !waiter->bulk_waiter.bulk) {
  20918. + if (waiter->bulk_waiter.bulk) {
  20919. + /* Cancel the signal when the transfer
  20920. + ** completes. */
  20921. + spin_lock(&bulk_waiter_spinlock);
  20922. + waiter->bulk_waiter.bulk->userdata = NULL;
  20923. + spin_unlock(&bulk_waiter_spinlock);
  20924. + }
  20925. + kfree(waiter);
  20926. + } else {
  20927. + const VCHIQ_BULK_MODE_T mode_waiting =
  20928. + VCHIQ_BULK_MODE_WAITING;
  20929. + waiter->pid = current->pid;
  20930. + mutex_lock(&instance->bulk_waiter_list_mutex);
  20931. + list_add(&waiter->list, &instance->bulk_waiter_list);
  20932. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  20933. + vchiq_log_info(vchiq_arm_log_level,
  20934. + "saved bulk_waiter %x for pid %d",
  20935. + (unsigned int)waiter, current->pid);
  20936. +
  20937. + if (copy_to_user((void __user *)
  20938. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  20939. + arg)->mode),
  20940. + (const void *)&mode_waiting,
  20941. + sizeof(mode_waiting)) != 0)
  20942. + ret = -EFAULT;
  20943. + }
  20944. + } break;
  20945. +
  20946. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  20947. + VCHIQ_AWAIT_COMPLETION_T args;
  20948. +
  20949. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20950. + if (!instance->connected) {
  20951. + ret = -ENOTCONN;
  20952. + break;
  20953. + }
  20954. +
  20955. + if (copy_from_user(&args, (const void __user *)arg,
  20956. + sizeof(args)) != 0) {
  20957. + ret = -EFAULT;
  20958. + break;
  20959. + }
  20960. +
  20961. + mutex_lock(&instance->completion_mutex);
  20962. +
  20963. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20964. + while ((instance->completion_remove ==
  20965. + instance->completion_insert)
  20966. + && !instance->closing) {
  20967. + int rc;
  20968. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20969. + mutex_unlock(&instance->completion_mutex);
  20970. + rc = down_interruptible(&instance->insert_event);
  20971. + mutex_lock(&instance->completion_mutex);
  20972. + if (rc != 0) {
  20973. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20974. + vchiq_log_info(vchiq_arm_log_level,
  20975. + "AWAIT_COMPLETION interrupted");
  20976. + ret = -EINTR;
  20977. + break;
  20978. + }
  20979. + }
  20980. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20981. +
  20982. + /* A read memory barrier is needed to stop prefetch of a stale
  20983. + ** completion record
  20984. + */
  20985. + rmb();
  20986. +
  20987. + if (ret == 0) {
  20988. + int msgbufcount = args.msgbufcount;
  20989. + for (ret = 0; ret < args.count; ret++) {
  20990. + VCHIQ_COMPLETION_DATA_T *completion;
  20991. + VCHIQ_SERVICE_T *service;
  20992. + USER_SERVICE_T *user_service;
  20993. + VCHIQ_HEADER_T *header;
  20994. + if (instance->completion_remove ==
  20995. + instance->completion_insert)
  20996. + break;
  20997. + completion = &instance->completions[
  20998. + instance->completion_remove &
  20999. + (MAX_COMPLETIONS - 1)];
  21000. +
  21001. + service = completion->service_userdata;
  21002. + user_service = service->base.userdata;
  21003. + completion->service_userdata =
  21004. + user_service->userdata;
  21005. +
  21006. + header = completion->header;
  21007. + if (header) {
  21008. + void __user *msgbuf;
  21009. + int msglen;
  21010. +
  21011. + msglen = header->size +
  21012. + sizeof(VCHIQ_HEADER_T);
  21013. + /* This must be a VCHIQ-style service */
  21014. + if (args.msgbufsize < msglen) {
  21015. + vchiq_log_error(
  21016. + vchiq_arm_log_level,
  21017. + "header %x: msgbufsize"
  21018. + " %x < msglen %x",
  21019. + (unsigned int)header,
  21020. + args.msgbufsize,
  21021. + msglen);
  21022. + WARN(1, "invalid message "
  21023. + "size\n");
  21024. + if (ret == 0)
  21025. + ret = -EMSGSIZE;
  21026. + break;
  21027. + }
  21028. + if (msgbufcount <= 0)
  21029. + /* Stall here for lack of a
  21030. + ** buffer for the message. */
  21031. + break;
  21032. + /* Get the pointer from user space */
  21033. + msgbufcount--;
  21034. + if (copy_from_user(&msgbuf,
  21035. + (const void __user *)
  21036. + &args.msgbufs[msgbufcount],
  21037. + sizeof(msgbuf)) != 0) {
  21038. + if (ret == 0)
  21039. + ret = -EFAULT;
  21040. + break;
  21041. + }
  21042. +
  21043. + /* Copy the message to user space */
  21044. + if (copy_to_user(msgbuf, header,
  21045. + msglen) != 0) {
  21046. + if (ret == 0)
  21047. + ret = -EFAULT;
  21048. + break;
  21049. + }
  21050. +
  21051. + /* Now it has been copied, the message
  21052. + ** can be released. */
  21053. + vchiq_release_message(service->handle,
  21054. + header);
  21055. +
  21056. + /* The completion must point to the
  21057. + ** msgbuf. */
  21058. + completion->header = msgbuf;
  21059. + }
  21060. +
  21061. + if (completion->reason ==
  21062. + VCHIQ_SERVICE_CLOSED)
  21063. + unlock_service(service);
  21064. +
  21065. + if (copy_to_user((void __user *)(
  21066. + (size_t)args.buf +
  21067. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  21068. + completion,
  21069. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  21070. + if (ret == 0)
  21071. + ret = -EFAULT;
  21072. + break;
  21073. + }
  21074. +
  21075. + instance->completion_remove++;
  21076. + }
  21077. +
  21078. + if (msgbufcount != args.msgbufcount) {
  21079. + if (copy_to_user((void __user *)
  21080. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  21081. + msgbufcount,
  21082. + &msgbufcount,
  21083. + sizeof(msgbufcount)) != 0) {
  21084. + ret = -EFAULT;
  21085. + }
  21086. + }
  21087. + }
  21088. +
  21089. + if (ret != 0)
  21090. + up(&instance->remove_event);
  21091. + mutex_unlock(&instance->completion_mutex);
  21092. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  21093. + } break;
  21094. +
  21095. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  21096. + VCHIQ_DEQUEUE_MESSAGE_T args;
  21097. + USER_SERVICE_T *user_service;
  21098. + VCHIQ_HEADER_T *header;
  21099. +
  21100. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21101. + if (copy_from_user
  21102. + (&args, (const void __user *)arg,
  21103. + sizeof(args)) != 0) {
  21104. + ret = -EFAULT;
  21105. + break;
  21106. + }
  21107. + service = find_service_for_instance(instance, args.handle);
  21108. + if (!service) {
  21109. + ret = -EINVAL;
  21110. + break;
  21111. + }
  21112. + user_service = (USER_SERVICE_T *)service->base.userdata;
  21113. + if (user_service->is_vchi == 0) {
  21114. + ret = -EINVAL;
  21115. + break;
  21116. + }
  21117. +
  21118. + spin_lock(&msg_queue_spinlock);
  21119. + if (user_service->msg_remove == user_service->msg_insert) {
  21120. + if (!args.blocking) {
  21121. + spin_unlock(&msg_queue_spinlock);
  21122. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21123. + ret = -EWOULDBLOCK;
  21124. + break;
  21125. + }
  21126. + user_service->dequeue_pending = 1;
  21127. + do {
  21128. + spin_unlock(&msg_queue_spinlock);
  21129. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21130. + if (down_interruptible(
  21131. + &user_service->insert_event) != 0) {
  21132. + vchiq_log_info(vchiq_arm_log_level,
  21133. + "DEQUEUE_MESSAGE interrupted");
  21134. + ret = -EINTR;
  21135. + break;
  21136. + }
  21137. + spin_lock(&msg_queue_spinlock);
  21138. + } while (user_service->msg_remove ==
  21139. + user_service->msg_insert);
  21140. +
  21141. + if (ret)
  21142. + break;
  21143. + }
  21144. +
  21145. + BUG_ON((int)(user_service->msg_insert -
  21146. + user_service->msg_remove) < 0);
  21147. +
  21148. + header = user_service->msg_queue[user_service->msg_remove &
  21149. + (MSG_QUEUE_SIZE - 1)];
  21150. + user_service->msg_remove++;
  21151. + spin_unlock(&msg_queue_spinlock);
  21152. +
  21153. + up(&user_service->remove_event);
  21154. + if (header == NULL)
  21155. + ret = -ENOTCONN;
  21156. + else if (header->size <= args.bufsize) {
  21157. + /* Copy to user space if msgbuf is not NULL */
  21158. + if ((args.buf == NULL) ||
  21159. + (copy_to_user((void __user *)args.buf,
  21160. + header->data,
  21161. + header->size) == 0)) {
  21162. + ret = header->size;
  21163. + vchiq_release_message(
  21164. + service->handle,
  21165. + header);
  21166. + } else
  21167. + ret = -EFAULT;
  21168. + } else {
  21169. + vchiq_log_error(vchiq_arm_log_level,
  21170. + "header %x: bufsize %x < size %x",
  21171. + (unsigned int)header, args.bufsize,
  21172. + header->size);
  21173. + WARN(1, "invalid size\n");
  21174. + ret = -EMSGSIZE;
  21175. + }
  21176. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21177. + } break;
  21178. +
  21179. + case VCHIQ_IOC_GET_CLIENT_ID: {
  21180. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  21181. +
  21182. + ret = vchiq_get_client_id(handle);
  21183. + } break;
  21184. +
  21185. + case VCHIQ_IOC_GET_CONFIG: {
  21186. + VCHIQ_GET_CONFIG_T args;
  21187. + VCHIQ_CONFIG_T config;
  21188. +
  21189. + if (copy_from_user(&args, (const void __user *)arg,
  21190. + sizeof(args)) != 0) {
  21191. + ret = -EFAULT;
  21192. + break;
  21193. + }
  21194. + if (args.config_size > sizeof(config)) {
  21195. + ret = -EINVAL;
  21196. + break;
  21197. + }
  21198. + status = vchiq_get_config(instance, args.config_size, &config);
  21199. + if (status == VCHIQ_SUCCESS) {
  21200. + if (copy_to_user((void __user *)args.pconfig,
  21201. + &config, args.config_size) != 0) {
  21202. + ret = -EFAULT;
  21203. + break;
  21204. + }
  21205. + }
  21206. + } break;
  21207. +
  21208. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  21209. + VCHIQ_SET_SERVICE_OPTION_T args;
  21210. +
  21211. + if (copy_from_user(
  21212. + &args, (const void __user *)arg,
  21213. + sizeof(args)) != 0) {
  21214. + ret = -EFAULT;
  21215. + break;
  21216. + }
  21217. +
  21218. + service = find_service_for_instance(instance, args.handle);
  21219. + if (!service) {
  21220. + ret = -EINVAL;
  21221. + break;
  21222. + }
  21223. +
  21224. + status = vchiq_set_service_option(
  21225. + args.handle, args.option, args.value);
  21226. + } break;
  21227. +
  21228. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  21229. + VCHIQ_DUMP_MEM_T args;
  21230. +
  21231. + if (copy_from_user
  21232. + (&args, (const void __user *)arg,
  21233. + sizeof(args)) != 0) {
  21234. + ret = -EFAULT;
  21235. + break;
  21236. + }
  21237. + dump_phys_mem(args.virt_addr, args.num_bytes);
  21238. + } break;
  21239. +
  21240. + default:
  21241. + ret = -ENOTTY;
  21242. + break;
  21243. + }
  21244. +
  21245. + if (service)
  21246. + unlock_service(service);
  21247. +
  21248. + if (ret == 0) {
  21249. + if (status == VCHIQ_ERROR)
  21250. + ret = -EIO;
  21251. + else if (status == VCHIQ_RETRY)
  21252. + ret = -EINTR;
  21253. + }
  21254. +
  21255. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  21256. + (ret != -EWOULDBLOCK))
  21257. + vchiq_log_info(vchiq_arm_log_level,
  21258. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  21259. + (unsigned long)instance,
  21260. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  21261. + ioctl_names[_IOC_NR(cmd)] :
  21262. + "<invalid>",
  21263. + status, ret);
  21264. + else
  21265. + vchiq_log_trace(vchiq_arm_log_level,
  21266. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  21267. + (unsigned long)instance,
  21268. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  21269. + ioctl_names[_IOC_NR(cmd)] :
  21270. + "<invalid>",
  21271. + status, ret);
  21272. +
  21273. + return ret;
  21274. +}
  21275. +
  21276. +/****************************************************************************
  21277. +*
  21278. +* vchiq_open
  21279. +*
  21280. +***************************************************************************/
  21281. +
  21282. +static int
  21283. +vchiq_open(struct inode *inode, struct file *file)
  21284. +{
  21285. + int dev = iminor(inode) & 0x0f;
  21286. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  21287. + switch (dev) {
  21288. + case VCHIQ_MINOR: {
  21289. + int ret;
  21290. + VCHIQ_STATE_T *state = vchiq_get_state();
  21291. + VCHIQ_INSTANCE_T instance;
  21292. +
  21293. + if (!state) {
  21294. + vchiq_log_error(vchiq_arm_log_level,
  21295. + "vchiq has no connection to VideoCore");
  21296. + return -ENOTCONN;
  21297. + }
  21298. +
  21299. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  21300. + if (!instance)
  21301. + return -ENOMEM;
  21302. +
  21303. + instance->state = state;
  21304. + instance->pid = current->tgid;
  21305. +
  21306. + ret = vchiq_proc_add_instance(instance);
  21307. + if (ret != 0) {
  21308. + kfree(instance);
  21309. + return ret;
  21310. + }
  21311. +
  21312. + sema_init(&instance->insert_event, 0);
  21313. + sema_init(&instance->remove_event, 0);
  21314. + mutex_init(&instance->completion_mutex);
  21315. + mutex_init(&instance->bulk_waiter_list_mutex);
  21316. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  21317. +
  21318. + file->private_data = instance;
  21319. + } break;
  21320. +
  21321. + default:
  21322. + vchiq_log_error(vchiq_arm_log_level,
  21323. + "Unknown minor device: %d", dev);
  21324. + return -ENXIO;
  21325. + }
  21326. +
  21327. + return 0;
  21328. +}
  21329. +
  21330. +/****************************************************************************
  21331. +*
  21332. +* vchiq_release
  21333. +*
  21334. +***************************************************************************/
  21335. +
  21336. +static int
  21337. +vchiq_release(struct inode *inode, struct file *file)
  21338. +{
  21339. + int dev = iminor(inode) & 0x0f;
  21340. + int ret = 0;
  21341. + switch (dev) {
  21342. + case VCHIQ_MINOR: {
  21343. + VCHIQ_INSTANCE_T instance = file->private_data;
  21344. + VCHIQ_STATE_T *state = vchiq_get_state();
  21345. + VCHIQ_SERVICE_T *service;
  21346. + int i;
  21347. +
  21348. + vchiq_log_info(vchiq_arm_log_level,
  21349. + "vchiq_release: instance=%lx",
  21350. + (unsigned long)instance);
  21351. +
  21352. + if (!state) {
  21353. + ret = -EPERM;
  21354. + goto out;
  21355. + }
  21356. +
  21357. + /* Ensure videocore is awake to allow termination. */
  21358. + vchiq_use_internal(instance->state, NULL,
  21359. + USE_TYPE_VCHIQ);
  21360. +
  21361. + mutex_lock(&instance->completion_mutex);
  21362. +
  21363. + /* Wake the completion thread and ask it to exit */
  21364. + instance->closing = 1;
  21365. + up(&instance->insert_event);
  21366. +
  21367. + mutex_unlock(&instance->completion_mutex);
  21368. +
  21369. + /* Wake the slot handler if the completion queue is full. */
  21370. + up(&instance->remove_event);
  21371. +
  21372. + /* Mark all services for termination... */
  21373. + i = 0;
  21374. + while ((service = next_service_by_instance(state, instance,
  21375. + &i)) != NULL) {
  21376. + USER_SERVICE_T *user_service = service->base.userdata;
  21377. +
  21378. + /* Wake the slot handler if the msg queue is full. */
  21379. + up(&user_service->remove_event);
  21380. +
  21381. + vchiq_terminate_service_internal(service);
  21382. + unlock_service(service);
  21383. + }
  21384. +
  21385. + /* ...and wait for them to die */
  21386. + i = 0;
  21387. + while ((service = next_service_by_instance(state, instance, &i))
  21388. + != NULL) {
  21389. + USER_SERVICE_T *user_service = service->base.userdata;
  21390. +
  21391. + down(&service->remove_event);
  21392. +
  21393. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  21394. +
  21395. + spin_lock(&msg_queue_spinlock);
  21396. +
  21397. + while (user_service->msg_remove !=
  21398. + user_service->msg_insert) {
  21399. + VCHIQ_HEADER_T *header = user_service->
  21400. + msg_queue[user_service->msg_remove &
  21401. + (MSG_QUEUE_SIZE - 1)];
  21402. + user_service->msg_remove++;
  21403. + spin_unlock(&msg_queue_spinlock);
  21404. +
  21405. + if (header)
  21406. + vchiq_release_message(
  21407. + service->handle,
  21408. + header);
  21409. + spin_lock(&msg_queue_spinlock);
  21410. + }
  21411. +
  21412. + spin_unlock(&msg_queue_spinlock);
  21413. +
  21414. + unlock_service(service);
  21415. + }
  21416. +
  21417. + /* Release any closed services */
  21418. + while (instance->completion_remove !=
  21419. + instance->completion_insert) {
  21420. + VCHIQ_COMPLETION_DATA_T *completion;
  21421. + VCHIQ_SERVICE_T *service;
  21422. + completion = &instance->completions[
  21423. + instance->completion_remove &
  21424. + (MAX_COMPLETIONS - 1)];
  21425. + service = completion->service_userdata;
  21426. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  21427. + unlock_service(service);
  21428. + instance->completion_remove++;
  21429. + }
  21430. +
  21431. + /* Release the PEER service count. */
  21432. + vchiq_release_internal(instance->state, NULL);
  21433. +
  21434. + {
  21435. + struct list_head *pos, *next;
  21436. + list_for_each_safe(pos, next,
  21437. + &instance->bulk_waiter_list) {
  21438. + struct bulk_waiter_node *waiter;
  21439. + waiter = list_entry(pos,
  21440. + struct bulk_waiter_node,
  21441. + list);
  21442. + list_del(pos);
  21443. + vchiq_log_info(vchiq_arm_log_level,
  21444. + "bulk_waiter - cleaned up %x "
  21445. + "for pid %d",
  21446. + (unsigned int)waiter, waiter->pid);
  21447. + kfree(waiter);
  21448. + }
  21449. + }
  21450. +
  21451. + vchiq_proc_remove_instance(instance);
  21452. +
  21453. + kfree(instance);
  21454. + file->private_data = NULL;
  21455. + } break;
  21456. +
  21457. + default:
  21458. + vchiq_log_error(vchiq_arm_log_level,
  21459. + "Unknown minor device: %d", dev);
  21460. + ret = -ENXIO;
  21461. + }
  21462. +
  21463. +out:
  21464. + return ret;
  21465. +}
  21466. +
  21467. +/****************************************************************************
  21468. +*
  21469. +* vchiq_dump
  21470. +*
  21471. +***************************************************************************/
  21472. +
  21473. +void
  21474. +vchiq_dump(void *dump_context, const char *str, int len)
  21475. +{
  21476. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  21477. +
  21478. + if (context->actual < context->space) {
  21479. + int copy_bytes;
  21480. + if (context->offset > 0) {
  21481. + int skip_bytes = min(len, (int)context->offset);
  21482. + str += skip_bytes;
  21483. + len -= skip_bytes;
  21484. + context->offset -= skip_bytes;
  21485. + if (context->offset > 0)
  21486. + return;
  21487. + }
  21488. + copy_bytes = min(len, (int)(context->space - context->actual));
  21489. + if (copy_bytes == 0)
  21490. + return;
  21491. + if (copy_to_user(context->buf + context->actual, str,
  21492. + copy_bytes))
  21493. + context->actual = -EFAULT;
  21494. + context->actual += copy_bytes;
  21495. + len -= copy_bytes;
  21496. +
  21497. + /* If tne terminating NUL is included in the length, then it
  21498. + ** marks the end of a line and should be replaced with a
  21499. + ** carriage return. */
  21500. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  21501. + char cr = '\n';
  21502. + if (copy_to_user(context->buf + context->actual - 1,
  21503. + &cr, 1))
  21504. + context->actual = -EFAULT;
  21505. + }
  21506. + }
  21507. +}
  21508. +
  21509. +/****************************************************************************
  21510. +*
  21511. +* vchiq_dump_platform_instance_state
  21512. +*
  21513. +***************************************************************************/
  21514. +
  21515. +void
  21516. +vchiq_dump_platform_instances(void *dump_context)
  21517. +{
  21518. + VCHIQ_STATE_T *state = vchiq_get_state();
  21519. + char buf[80];
  21520. + int len;
  21521. + int i;
  21522. +
  21523. + /* There is no list of instances, so instead scan all services,
  21524. + marking those that have been dumped. */
  21525. +
  21526. + for (i = 0; i < state->unused_service; i++) {
  21527. + VCHIQ_SERVICE_T *service = state->services[i];
  21528. + VCHIQ_INSTANCE_T instance;
  21529. +
  21530. + if (service && (service->base.callback == service_callback)) {
  21531. + instance = service->instance;
  21532. + if (instance)
  21533. + instance->mark = 0;
  21534. + }
  21535. + }
  21536. +
  21537. + for (i = 0; i < state->unused_service; i++) {
  21538. + VCHIQ_SERVICE_T *service = state->services[i];
  21539. + VCHIQ_INSTANCE_T instance;
  21540. +
  21541. + if (service && (service->base.callback == service_callback)) {
  21542. + instance = service->instance;
  21543. + if (instance && !instance->mark) {
  21544. + len = snprintf(buf, sizeof(buf),
  21545. + "Instance %x: pid %d,%s completions "
  21546. + "%d/%d",
  21547. + (unsigned int)instance, instance->pid,
  21548. + instance->connected ? " connected, " :
  21549. + "",
  21550. + instance->completion_insert -
  21551. + instance->completion_remove,
  21552. + MAX_COMPLETIONS);
  21553. +
  21554. + vchiq_dump(dump_context, buf, len + 1);
  21555. +
  21556. + instance->mark = 1;
  21557. + }
  21558. + }
  21559. + }
  21560. +}
  21561. +
  21562. +/****************************************************************************
  21563. +*
  21564. +* vchiq_dump_platform_service_state
  21565. +*
  21566. +***************************************************************************/
  21567. +
  21568. +void
  21569. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  21570. +{
  21571. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  21572. + char buf[80];
  21573. + int len;
  21574. +
  21575. + len = snprintf(buf, sizeof(buf), " instance %x",
  21576. + (unsigned int)service->instance);
  21577. +
  21578. + if ((service->base.callback == service_callback) &&
  21579. + user_service->is_vchi) {
  21580. + len += snprintf(buf + len, sizeof(buf) - len,
  21581. + ", %d/%d messages",
  21582. + user_service->msg_insert - user_service->msg_remove,
  21583. + MSG_QUEUE_SIZE);
  21584. +
  21585. + if (user_service->dequeue_pending)
  21586. + len += snprintf(buf + len, sizeof(buf) - len,
  21587. + " (dequeue pending)");
  21588. + }
  21589. +
  21590. + vchiq_dump(dump_context, buf, len + 1);
  21591. +}
  21592. +
  21593. +/****************************************************************************
  21594. +*
  21595. +* dump_user_mem
  21596. +*
  21597. +***************************************************************************/
  21598. +
  21599. +static void
  21600. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  21601. +{
  21602. + int rc;
  21603. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  21604. + int num_pages;
  21605. + int offset;
  21606. + int end_offset;
  21607. + int page_idx;
  21608. + int prev_idx;
  21609. + struct page *page;
  21610. + struct page **pages;
  21611. + uint8_t *kmapped_virt_ptr;
  21612. +
  21613. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  21614. +
  21615. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  21616. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  21617. + ~0x0fuL);
  21618. +
  21619. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  21620. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  21621. +
  21622. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  21623. +
  21624. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  21625. + if (pages == NULL) {
  21626. + vchiq_log_error(vchiq_arm_log_level,
  21627. + "Unable to allocation memory for %d pages\n",
  21628. + num_pages);
  21629. + return;
  21630. + }
  21631. +
  21632. + down_read(&current->mm->mmap_sem);
  21633. + rc = get_user_pages(current, /* task */
  21634. + current->mm, /* mm */
  21635. + (unsigned long)virt_addr, /* start */
  21636. + num_pages, /* len */
  21637. + 0, /* write */
  21638. + 0, /* force */
  21639. + pages, /* pages (array of page pointers) */
  21640. + NULL); /* vmas */
  21641. + up_read(&current->mm->mmap_sem);
  21642. +
  21643. + prev_idx = -1;
  21644. + page = NULL;
  21645. +
  21646. + while (offset < end_offset) {
  21647. +
  21648. + int page_offset = offset % PAGE_SIZE;
  21649. + page_idx = offset / PAGE_SIZE;
  21650. +
  21651. + if (page_idx != prev_idx) {
  21652. +
  21653. + if (page != NULL)
  21654. + kunmap(page);
  21655. + page = pages[page_idx];
  21656. + kmapped_virt_ptr = kmap(page);
  21657. +
  21658. + prev_idx = page_idx;
  21659. + }
  21660. +
  21661. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  21662. + vchiq_log_dump_mem("ph",
  21663. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  21664. + page_offset],
  21665. + &kmapped_virt_ptr[page_offset], 16);
  21666. +
  21667. + offset += 16;
  21668. + }
  21669. + if (page != NULL)
  21670. + kunmap(page);
  21671. +
  21672. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  21673. + page_cache_release(pages[page_idx]);
  21674. +
  21675. + kfree(pages);
  21676. +}
  21677. +
  21678. +/****************************************************************************
  21679. +*
  21680. +* vchiq_read
  21681. +*
  21682. +***************************************************************************/
  21683. +
  21684. +static ssize_t
  21685. +vchiq_read(struct file *file, char __user *buf,
  21686. + size_t count, loff_t *ppos)
  21687. +{
  21688. + DUMP_CONTEXT_T context;
  21689. + context.buf = buf;
  21690. + context.actual = 0;
  21691. + context.space = count;
  21692. + context.offset = *ppos;
  21693. +
  21694. + vchiq_dump_state(&context, &g_state);
  21695. +
  21696. + *ppos += context.actual;
  21697. +
  21698. + return context.actual;
  21699. +}
  21700. +
  21701. +VCHIQ_STATE_T *
  21702. +vchiq_get_state(void)
  21703. +{
  21704. +
  21705. + if (g_state.remote == NULL)
  21706. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  21707. + else if (g_state.remote->initialised != 1)
  21708. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  21709. + __func__, g_state.remote->initialised);
  21710. +
  21711. + return ((g_state.remote != NULL) &&
  21712. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  21713. +}
  21714. +
  21715. +static const struct file_operations
  21716. +vchiq_fops = {
  21717. + .owner = THIS_MODULE,
  21718. + .unlocked_ioctl = vchiq_ioctl,
  21719. + .open = vchiq_open,
  21720. + .release = vchiq_release,
  21721. + .read = vchiq_read
  21722. +};
  21723. +
  21724. +/*
  21725. + * Autosuspend related functionality
  21726. + */
  21727. +
  21728. +int
  21729. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  21730. +{
  21731. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21732. + if (!arm_state)
  21733. + /* autosuspend not supported - always return wanted */
  21734. + return 1;
  21735. + else if (arm_state->blocked_count)
  21736. + return 1;
  21737. + else if (!arm_state->videocore_use_count)
  21738. + /* usage count zero - check for override unless we're forcing */
  21739. + if (arm_state->resume_blocked)
  21740. + return 0;
  21741. + else
  21742. + return vchiq_platform_videocore_wanted(state);
  21743. + else
  21744. + /* non-zero usage count - videocore still required */
  21745. + return 1;
  21746. +}
  21747. +
  21748. +static VCHIQ_STATUS_T
  21749. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  21750. + VCHIQ_HEADER_T *header,
  21751. + VCHIQ_SERVICE_HANDLE_T service_user,
  21752. + void *bulk_user)
  21753. +{
  21754. + vchiq_log_error(vchiq_susp_log_level,
  21755. + "%s callback reason %d", __func__, reason);
  21756. + return 0;
  21757. +}
  21758. +
  21759. +static int
  21760. +vchiq_keepalive_thread_func(void *v)
  21761. +{
  21762. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  21763. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21764. +
  21765. + VCHIQ_STATUS_T status;
  21766. + VCHIQ_INSTANCE_T instance;
  21767. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  21768. +
  21769. + VCHIQ_SERVICE_PARAMS_T params = {
  21770. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  21771. + .callback = vchiq_keepalive_vchiq_callback,
  21772. + .version = KEEPALIVE_VER,
  21773. + .version_min = KEEPALIVE_VER_MIN
  21774. + };
  21775. +
  21776. + status = vchiq_initialise(&instance);
  21777. + if (status != VCHIQ_SUCCESS) {
  21778. + vchiq_log_error(vchiq_susp_log_level,
  21779. + "%s vchiq_initialise failed %d", __func__, status);
  21780. + goto exit;
  21781. + }
  21782. +
  21783. + status = vchiq_connect(instance);
  21784. + if (status != VCHIQ_SUCCESS) {
  21785. + vchiq_log_error(vchiq_susp_log_level,
  21786. + "%s vchiq_connect failed %d", __func__, status);
  21787. + goto shutdown;
  21788. + }
  21789. +
  21790. + status = vchiq_add_service(instance, &params, &ka_handle);
  21791. + if (status != VCHIQ_SUCCESS) {
  21792. + vchiq_log_error(vchiq_susp_log_level,
  21793. + "%s vchiq_open_service failed %d", __func__, status);
  21794. + goto shutdown;
  21795. + }
  21796. +
  21797. + while (1) {
  21798. + long rc = 0, uc = 0;
  21799. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  21800. + != 0) {
  21801. + vchiq_log_error(vchiq_susp_log_level,
  21802. + "%s interrupted", __func__);
  21803. + flush_signals(current);
  21804. + continue;
  21805. + }
  21806. +
  21807. + /* read and clear counters. Do release_count then use_count to
  21808. + * prevent getting more releases than uses */
  21809. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  21810. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  21811. +
  21812. + /* Call use/release service the requisite number of times.
  21813. + * Process use before release so use counts don't go negative */
  21814. + while (uc--) {
  21815. + atomic_inc(&arm_state->ka_use_ack_count);
  21816. + status = vchiq_use_service(ka_handle);
  21817. + if (status != VCHIQ_SUCCESS) {
  21818. + vchiq_log_error(vchiq_susp_log_level,
  21819. + "%s vchiq_use_service error %d",
  21820. + __func__, status);
  21821. + }
  21822. + }
  21823. + while (rc--) {
  21824. + status = vchiq_release_service(ka_handle);
  21825. + if (status != VCHIQ_SUCCESS) {
  21826. + vchiq_log_error(vchiq_susp_log_level,
  21827. + "%s vchiq_release_service error %d",
  21828. + __func__, status);
  21829. + }
  21830. + }
  21831. + }
  21832. +
  21833. +shutdown:
  21834. + vchiq_shutdown(instance);
  21835. +exit:
  21836. + return 0;
  21837. +}
  21838. +
  21839. +
  21840. +
  21841. +VCHIQ_STATUS_T
  21842. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  21843. +{
  21844. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  21845. +
  21846. + if (arm_state) {
  21847. + rwlock_init(&arm_state->susp_res_lock);
  21848. +
  21849. + init_completion(&arm_state->ka_evt);
  21850. + atomic_set(&arm_state->ka_use_count, 0);
  21851. + atomic_set(&arm_state->ka_use_ack_count, 0);
  21852. + atomic_set(&arm_state->ka_release_count, 0);
  21853. +
  21854. + init_completion(&arm_state->vc_suspend_complete);
  21855. +
  21856. + init_completion(&arm_state->vc_resume_complete);
  21857. + /* Initialise to 'done' state. We only want to block on resume
  21858. + * completion while videocore is suspended. */
  21859. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  21860. +
  21861. + init_completion(&arm_state->resume_blocker);
  21862. + /* Initialise to 'done' state. We only want to block on this
  21863. + * completion while resume is blocked */
  21864. + complete_all(&arm_state->resume_blocker);
  21865. +
  21866. + init_completion(&arm_state->blocked_blocker);
  21867. + /* Initialise to 'done' state. We only want to block on this
  21868. + * completion while things are waiting on the resume blocker */
  21869. + complete_all(&arm_state->blocked_blocker);
  21870. +
  21871. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  21872. + arm_state->suspend_timer_running = 0;
  21873. + init_timer(&arm_state->suspend_timer);
  21874. + arm_state->suspend_timer.data = (unsigned long)(state);
  21875. + arm_state->suspend_timer.function = suspend_timer_callback;
  21876. +
  21877. + arm_state->first_connect = 0;
  21878. +
  21879. + }
  21880. + return status;
  21881. +}
  21882. +
  21883. +/*
  21884. +** Functions to modify the state variables;
  21885. +** set_suspend_state
  21886. +** set_resume_state
  21887. +**
  21888. +** There are more state variables than we might like, so ensure they remain in
  21889. +** step. Suspend and resume state are maintained separately, since most of
  21890. +** these state machines can operate independently. However, there are a few
  21891. +** states where state transitions in one state machine cause a reset to the
  21892. +** other state machine. In addition, there are some completion events which
  21893. +** need to occur on state machine reset and end-state(s), so these are also
  21894. +** dealt with in these functions.
  21895. +**
  21896. +** In all states we set the state variable according to the input, but in some
  21897. +** cases we perform additional steps outlined below;
  21898. +**
  21899. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  21900. +** The suspend completion is completed after any suspend
  21901. +** attempt. When we reset the state machine we also reset
  21902. +** the completion. This reset occurs when videocore is
  21903. +** resumed, and also if we initiate suspend after a suspend
  21904. +** failure.
  21905. +**
  21906. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  21907. +** suspend - ie from this point on we must try to suspend
  21908. +** before resuming can occur. We therefore also reset the
  21909. +** resume state machine to VC_RESUME_IDLE in this state.
  21910. +**
  21911. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  21912. +** complete_all on the suspend completion to notify
  21913. +** anything waiting for suspend to happen.
  21914. +**
  21915. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  21916. +** initiate resume, so no need to alter resume state.
  21917. +** We call complete_all on the suspend completion to notify
  21918. +** of suspend rejection.
  21919. +**
  21920. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  21921. +** suspend completion and reset the resume state machine.
  21922. +**
  21923. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  21924. +** resume completion is in it's 'done' state whenever
  21925. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  21926. +** implies that videocore is suspended.
  21927. +** Hence, any thread which needs to wait until videocore is
  21928. +** running can wait on this completion - it will only block
  21929. +** if videocore is suspended.
  21930. +**
  21931. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  21932. +** Call complete_all on the resume completion to unblock
  21933. +** any threads waiting for resume. Also reset the suspend
  21934. +** state machine to it's idle state.
  21935. +**
  21936. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  21937. +*/
  21938. +
  21939. +inline void
  21940. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  21941. + enum vc_suspend_status new_state)
  21942. +{
  21943. + /* set the state in all cases */
  21944. + arm_state->vc_suspend_state = new_state;
  21945. +
  21946. + /* state specific additional actions */
  21947. + switch (new_state) {
  21948. + case VC_SUSPEND_FORCE_CANCELED:
  21949. + complete_all(&arm_state->vc_suspend_complete);
  21950. + break;
  21951. + case VC_SUSPEND_REJECTED:
  21952. + complete_all(&arm_state->vc_suspend_complete);
  21953. + break;
  21954. + case VC_SUSPEND_FAILED:
  21955. + complete_all(&arm_state->vc_suspend_complete);
  21956. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  21957. + complete_all(&arm_state->vc_resume_complete);
  21958. + break;
  21959. + case VC_SUSPEND_IDLE:
  21960. + INIT_COMPLETION(arm_state->vc_suspend_complete);
  21961. + break;
  21962. + case VC_SUSPEND_REQUESTED:
  21963. + break;
  21964. + case VC_SUSPEND_IN_PROGRESS:
  21965. + set_resume_state(arm_state, VC_RESUME_IDLE);
  21966. + break;
  21967. + case VC_SUSPEND_SUSPENDED:
  21968. + complete_all(&arm_state->vc_suspend_complete);
  21969. + break;
  21970. + default:
  21971. + BUG();
  21972. + break;
  21973. + }
  21974. +}
  21975. +
  21976. +inline void
  21977. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  21978. + enum vc_resume_status new_state)
  21979. +{
  21980. + /* set the state in all cases */
  21981. + arm_state->vc_resume_state = new_state;
  21982. +
  21983. + /* state specific additional actions */
  21984. + switch (new_state) {
  21985. + case VC_RESUME_FAILED:
  21986. + break;
  21987. + case VC_RESUME_IDLE:
  21988. + INIT_COMPLETION(arm_state->vc_resume_complete);
  21989. + break;
  21990. + case VC_RESUME_REQUESTED:
  21991. + break;
  21992. + case VC_RESUME_IN_PROGRESS:
  21993. + break;
  21994. + case VC_RESUME_RESUMED:
  21995. + complete_all(&arm_state->vc_resume_complete);
  21996. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  21997. + break;
  21998. + default:
  21999. + BUG();
  22000. + break;
  22001. + }
  22002. +}
  22003. +
  22004. +
  22005. +/* should be called with the write lock held */
  22006. +inline void
  22007. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  22008. +{
  22009. + del_timer(&arm_state->suspend_timer);
  22010. + arm_state->suspend_timer.expires = jiffies +
  22011. + msecs_to_jiffies(arm_state->
  22012. + suspend_timer_timeout);
  22013. + add_timer(&arm_state->suspend_timer);
  22014. + arm_state->suspend_timer_running = 1;
  22015. +}
  22016. +
  22017. +/* should be called with the write lock held */
  22018. +static inline void
  22019. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  22020. +{
  22021. + if (arm_state->suspend_timer_running) {
  22022. + del_timer(&arm_state->suspend_timer);
  22023. + arm_state->suspend_timer_running = 0;
  22024. + }
  22025. +}
  22026. +
  22027. +static inline int
  22028. +need_resume(VCHIQ_STATE_T *state)
  22029. +{
  22030. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22031. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  22032. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  22033. + vchiq_videocore_wanted(state);
  22034. +}
  22035. +
  22036. +static int
  22037. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  22038. +{
  22039. + int status = VCHIQ_SUCCESS;
  22040. + const unsigned long timeout_val =
  22041. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  22042. + int resume_count = 0;
  22043. +
  22044. + /* Allow any threads which were blocked by the last force suspend to
  22045. + * complete if they haven't already. Only give this one shot; if
  22046. + * blocked_count is incremented after blocked_blocker is completed
  22047. + * (which only happens when blocked_count hits 0) then those threads
  22048. + * will have to wait until next time around */
  22049. + if (arm_state->blocked_count) {
  22050. + INIT_COMPLETION(arm_state->blocked_blocker);
  22051. + write_unlock_bh(&arm_state->susp_res_lock);
  22052. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  22053. + "blocked clients", __func__);
  22054. + if (wait_for_completion_interruptible_timeout(
  22055. + &arm_state->blocked_blocker, timeout_val)
  22056. + <= 0) {
  22057. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  22058. + "previously blocked clients failed" , __func__);
  22059. + status = VCHIQ_ERROR;
  22060. + write_lock_bh(&arm_state->susp_res_lock);
  22061. + goto out;
  22062. + }
  22063. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  22064. + "clients resumed", __func__);
  22065. + write_lock_bh(&arm_state->susp_res_lock);
  22066. + }
  22067. +
  22068. + /* We need to wait for resume to complete if it's in process */
  22069. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  22070. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  22071. + if (resume_count > 1) {
  22072. + status = VCHIQ_ERROR;
  22073. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  22074. + "many times for resume" , __func__);
  22075. + goto out;
  22076. + }
  22077. + write_unlock_bh(&arm_state->susp_res_lock);
  22078. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  22079. + __func__);
  22080. + if (wait_for_completion_interruptible_timeout(
  22081. + &arm_state->vc_resume_complete, timeout_val)
  22082. + <= 0) {
  22083. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  22084. + "resume failed (%s)", __func__,
  22085. + resume_state_names[arm_state->vc_resume_state +
  22086. + VC_RESUME_NUM_OFFSET]);
  22087. + status = VCHIQ_ERROR;
  22088. + write_lock_bh(&arm_state->susp_res_lock);
  22089. + goto out;
  22090. + }
  22091. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  22092. + write_lock_bh(&arm_state->susp_res_lock);
  22093. + resume_count++;
  22094. + }
  22095. + INIT_COMPLETION(arm_state->resume_blocker);
  22096. + arm_state->resume_blocked = 1;
  22097. +
  22098. +out:
  22099. + return status;
  22100. +}
  22101. +
  22102. +static inline void
  22103. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  22104. +{
  22105. + complete_all(&arm_state->resume_blocker);
  22106. + arm_state->resume_blocked = 0;
  22107. +}
  22108. +
  22109. +/* Initiate suspend via slot handler. Should be called with the write lock
  22110. + * held */
  22111. +VCHIQ_STATUS_T
  22112. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  22113. +{
  22114. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  22115. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22116. +
  22117. + if (!arm_state)
  22118. + goto out;
  22119. +
  22120. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22121. + status = VCHIQ_SUCCESS;
  22122. +
  22123. +
  22124. + switch (arm_state->vc_suspend_state) {
  22125. + case VC_SUSPEND_REQUESTED:
  22126. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  22127. + "requested", __func__);
  22128. + break;
  22129. + case VC_SUSPEND_IN_PROGRESS:
  22130. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  22131. + "progress", __func__);
  22132. + break;
  22133. +
  22134. + default:
  22135. + /* We don't expect to be in other states, so log but continue
  22136. + * anyway */
  22137. + vchiq_log_error(vchiq_susp_log_level,
  22138. + "%s unexpected suspend state %s", __func__,
  22139. + suspend_state_names[arm_state->vc_suspend_state +
  22140. + VC_SUSPEND_NUM_OFFSET]);
  22141. + /* fall through */
  22142. + case VC_SUSPEND_REJECTED:
  22143. + case VC_SUSPEND_FAILED:
  22144. + /* Ensure any idle state actions have been run */
  22145. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  22146. + /* fall through */
  22147. + case VC_SUSPEND_IDLE:
  22148. + vchiq_log_info(vchiq_susp_log_level,
  22149. + "%s: suspending", __func__);
  22150. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  22151. + /* kick the slot handler thread to initiate suspend */
  22152. + request_poll(state, NULL, 0);
  22153. + break;
  22154. + }
  22155. +
  22156. +out:
  22157. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  22158. + return status;
  22159. +}
  22160. +
  22161. +void
  22162. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  22163. +{
  22164. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22165. + int susp = 0;
  22166. +
  22167. + if (!arm_state)
  22168. + goto out;
  22169. +
  22170. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22171. +
  22172. + write_lock_bh(&arm_state->susp_res_lock);
  22173. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  22174. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  22175. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  22176. + susp = 1;
  22177. + }
  22178. + write_unlock_bh(&arm_state->susp_res_lock);
  22179. +
  22180. + if (susp)
  22181. + vchiq_platform_suspend(state);
  22182. +
  22183. +out:
  22184. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22185. + return;
  22186. +}
  22187. +
  22188. +
  22189. +static void
  22190. +output_timeout_error(VCHIQ_STATE_T *state)
  22191. +{
  22192. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22193. + char service_err[50] = "";
  22194. + int vc_use_count = arm_state->videocore_use_count;
  22195. + int active_services = state->unused_service;
  22196. + int i;
  22197. +
  22198. + if (!arm_state->videocore_use_count) {
  22199. + snprintf(service_err, 50, " Videocore usecount is 0");
  22200. + goto output_msg;
  22201. + }
  22202. + for (i = 0; i < active_services; i++) {
  22203. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  22204. + if (service_ptr && service_ptr->service_use_count &&
  22205. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  22206. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  22207. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  22208. + service_ptr->base.fourcc),
  22209. + service_ptr->client_id,
  22210. + service_ptr->service_use_count,
  22211. + service_ptr->service_use_count ==
  22212. + vc_use_count ? "" : " (+ more)");
  22213. + break;
  22214. + }
  22215. + }
  22216. +
  22217. +output_msg:
  22218. + vchiq_log_error(vchiq_susp_log_level,
  22219. + "timed out waiting for vc suspend (%d).%s",
  22220. + arm_state->autosuspend_override, service_err);
  22221. +
  22222. +}
  22223. +
  22224. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  22225. +** We don't actually force suspend, since videocore may get into a bad state
  22226. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  22227. +** determine a good point to suspend. If this doesn't happen within 100ms we
  22228. +** report failure.
  22229. +**
  22230. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  22231. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  22232. +*/
  22233. +VCHIQ_STATUS_T
  22234. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  22235. +{
  22236. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22237. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  22238. + long rc = 0;
  22239. + int repeat = -1;
  22240. +
  22241. + if (!arm_state)
  22242. + goto out;
  22243. +
  22244. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22245. +
  22246. + write_lock_bh(&arm_state->susp_res_lock);
  22247. +
  22248. + status = block_resume(arm_state);
  22249. + if (status != VCHIQ_SUCCESS)
  22250. + goto unlock;
  22251. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  22252. + /* Already suspended - just block resume and exit */
  22253. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  22254. + __func__);
  22255. + status = VCHIQ_SUCCESS;
  22256. + goto unlock;
  22257. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  22258. + /* initiate suspend immediately in the case that we're waiting
  22259. + * for the timeout */
  22260. + stop_suspend_timer(arm_state);
  22261. + if (!vchiq_videocore_wanted(state)) {
  22262. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  22263. + "idle, initiating suspend", __func__);
  22264. + status = vchiq_arm_vcsuspend(state);
  22265. + } else if (arm_state->autosuspend_override <
  22266. + FORCE_SUSPEND_FAIL_MAX) {
  22267. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  22268. + "videocore go idle", __func__);
  22269. + status = VCHIQ_SUCCESS;
  22270. + } else {
  22271. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  22272. + "many times - attempting suspend", __func__);
  22273. + status = vchiq_arm_vcsuspend(state);
  22274. + }
  22275. + } else {
  22276. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  22277. + "in progress - wait for completion", __func__);
  22278. + status = VCHIQ_SUCCESS;
  22279. + }
  22280. +
  22281. + /* Wait for suspend to happen due to system idle (not forced..) */
  22282. + if (status != VCHIQ_SUCCESS)
  22283. + goto unblock_resume;
  22284. +
  22285. + do {
  22286. + write_unlock_bh(&arm_state->susp_res_lock);
  22287. +
  22288. + rc = wait_for_completion_interruptible_timeout(
  22289. + &arm_state->vc_suspend_complete,
  22290. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  22291. +
  22292. + write_lock_bh(&arm_state->susp_res_lock);
  22293. + if (rc < 0) {
  22294. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  22295. + "interrupted waiting for suspend", __func__);
  22296. + status = VCHIQ_ERROR;
  22297. + goto unblock_resume;
  22298. + } else if (rc == 0) {
  22299. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  22300. + /* Repeat timeout once if in progress */
  22301. + if (repeat < 0) {
  22302. + repeat = 1;
  22303. + continue;
  22304. + }
  22305. + }
  22306. + arm_state->autosuspend_override++;
  22307. + output_timeout_error(state);
  22308. +
  22309. + status = VCHIQ_RETRY;
  22310. + goto unblock_resume;
  22311. + }
  22312. + } while (0 < (repeat--));
  22313. +
  22314. + /* Check and report state in case we need to abort ARM suspend */
  22315. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  22316. + status = VCHIQ_RETRY;
  22317. + vchiq_log_error(vchiq_susp_log_level,
  22318. + "%s videocore suspend failed (state %s)", __func__,
  22319. + suspend_state_names[arm_state->vc_suspend_state +
  22320. + VC_SUSPEND_NUM_OFFSET]);
  22321. + /* Reset the state only if it's still in an error state.
  22322. + * Something could have already initiated another suspend. */
  22323. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  22324. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  22325. +
  22326. + goto unblock_resume;
  22327. + }
  22328. +
  22329. + /* successfully suspended - unlock and exit */
  22330. + goto unlock;
  22331. +
  22332. +unblock_resume:
  22333. + /* all error states need to unblock resume before exit */
  22334. + unblock_resume(arm_state);
  22335. +
  22336. +unlock:
  22337. + write_unlock_bh(&arm_state->susp_res_lock);
  22338. +
  22339. +out:
  22340. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  22341. + return status;
  22342. +}
  22343. +
  22344. +void
  22345. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  22346. +{
  22347. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22348. +
  22349. + if (!arm_state)
  22350. + goto out;
  22351. +
  22352. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22353. +
  22354. + write_lock_bh(&arm_state->susp_res_lock);
  22355. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  22356. + arm_state->first_connect &&
  22357. + !vchiq_videocore_wanted(state)) {
  22358. + vchiq_arm_vcsuspend(state);
  22359. + }
  22360. + write_unlock_bh(&arm_state->susp_res_lock);
  22361. +
  22362. +out:
  22363. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22364. + return;
  22365. +}
  22366. +
  22367. +
  22368. +int
  22369. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  22370. +{
  22371. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22372. + int resume = 0;
  22373. + int ret = -1;
  22374. +
  22375. + if (!arm_state)
  22376. + goto out;
  22377. +
  22378. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22379. +
  22380. + write_lock_bh(&arm_state->susp_res_lock);
  22381. + unblock_resume(arm_state);
  22382. + resume = vchiq_check_resume(state);
  22383. + write_unlock_bh(&arm_state->susp_res_lock);
  22384. +
  22385. + if (resume) {
  22386. + if (wait_for_completion_interruptible(
  22387. + &arm_state->vc_resume_complete) < 0) {
  22388. + vchiq_log_error(vchiq_susp_log_level,
  22389. + "%s interrupted", __func__);
  22390. + /* failed, cannot accurately derive suspend
  22391. + * state, so exit early. */
  22392. + goto out;
  22393. + }
  22394. + }
  22395. +
  22396. + read_lock_bh(&arm_state->susp_res_lock);
  22397. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  22398. + vchiq_log_info(vchiq_susp_log_level,
  22399. + "%s: Videocore remains suspended", __func__);
  22400. + } else {
  22401. + vchiq_log_info(vchiq_susp_log_level,
  22402. + "%s: Videocore resumed", __func__);
  22403. + ret = 0;
  22404. + }
  22405. + read_unlock_bh(&arm_state->susp_res_lock);
  22406. +out:
  22407. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  22408. + return ret;
  22409. +}
  22410. +
  22411. +/* This function should be called with the write lock held */
  22412. +int
  22413. +vchiq_check_resume(VCHIQ_STATE_T *state)
  22414. +{
  22415. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22416. + int resume = 0;
  22417. +
  22418. + if (!arm_state)
  22419. + goto out;
  22420. +
  22421. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22422. +
  22423. + if (need_resume(state)) {
  22424. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  22425. + request_poll(state, NULL, 0);
  22426. + resume = 1;
  22427. + }
  22428. +
  22429. +out:
  22430. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22431. + return resume;
  22432. +}
  22433. +
  22434. +void
  22435. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  22436. +{
  22437. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22438. + int res = 0;
  22439. +
  22440. + if (!arm_state)
  22441. + goto out;
  22442. +
  22443. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22444. +
  22445. + write_lock_bh(&arm_state->susp_res_lock);
  22446. + if (arm_state->wake_address == 0) {
  22447. + vchiq_log_info(vchiq_susp_log_level,
  22448. + "%s: already awake", __func__);
  22449. + goto unlock;
  22450. + }
  22451. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  22452. + vchiq_log_info(vchiq_susp_log_level,
  22453. + "%s: already resuming", __func__);
  22454. + goto unlock;
  22455. + }
  22456. +
  22457. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  22458. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  22459. + res = 1;
  22460. + } else
  22461. + vchiq_log_trace(vchiq_susp_log_level,
  22462. + "%s: not resuming (resume state %s)", __func__,
  22463. + resume_state_names[arm_state->vc_resume_state +
  22464. + VC_RESUME_NUM_OFFSET]);
  22465. +
  22466. +unlock:
  22467. + write_unlock_bh(&arm_state->susp_res_lock);
  22468. +
  22469. + if (res)
  22470. + vchiq_platform_resume(state);
  22471. +
  22472. +out:
  22473. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22474. + return;
  22475. +
  22476. +}
  22477. +
  22478. +
  22479. +
  22480. +VCHIQ_STATUS_T
  22481. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  22482. + enum USE_TYPE_E use_type)
  22483. +{
  22484. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22485. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  22486. + char entity[16];
  22487. + int *entity_uc;
  22488. + int local_uc, local_entity_uc;
  22489. +
  22490. + if (!arm_state)
  22491. + goto out;
  22492. +
  22493. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22494. +
  22495. + if (use_type == USE_TYPE_VCHIQ) {
  22496. + sprintf(entity, "VCHIQ: ");
  22497. + entity_uc = &arm_state->peer_use_count;
  22498. + } else if (service) {
  22499. + sprintf(entity, "%c%c%c%c:%03d",
  22500. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  22501. + service->client_id);
  22502. + entity_uc = &service->service_use_count;
  22503. + } else {
  22504. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  22505. + "ptr", __func__);
  22506. + ret = VCHIQ_ERROR;
  22507. + goto out;
  22508. + }
  22509. +
  22510. + write_lock_bh(&arm_state->susp_res_lock);
  22511. + while (arm_state->resume_blocked) {
  22512. + /* If we call 'use' while force suspend is waiting for suspend,
  22513. + * then we're about to block the thread which the force is
  22514. + * waiting to complete, so we're bound to just time out. In this
  22515. + * case, set the suspend state such that the wait will be
  22516. + * canceled, so we can complete as quickly as possible. */
  22517. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  22518. + VC_SUSPEND_IDLE) {
  22519. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  22520. + break;
  22521. + }
  22522. + /* If suspend is already in progress then we need to block */
  22523. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  22524. + /* Indicate that there are threads waiting on the resume
  22525. + * blocker. These need to be allowed to complete before
  22526. + * a _second_ call to force suspend can complete,
  22527. + * otherwise low priority threads might never actually
  22528. + * continue */
  22529. + arm_state->blocked_count++;
  22530. + write_unlock_bh(&arm_state->susp_res_lock);
  22531. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  22532. + "blocked - waiting...", __func__, entity);
  22533. + if (wait_for_completion_killable(
  22534. + &arm_state->resume_blocker) != 0) {
  22535. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  22536. + "wait for resume blocker interrupted",
  22537. + __func__, entity);
  22538. + ret = VCHIQ_ERROR;
  22539. + write_lock_bh(&arm_state->susp_res_lock);
  22540. + arm_state->blocked_count--;
  22541. + write_unlock_bh(&arm_state->susp_res_lock);
  22542. + goto out;
  22543. + }
  22544. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  22545. + "unblocked", __func__, entity);
  22546. + write_lock_bh(&arm_state->susp_res_lock);
  22547. + if (--arm_state->blocked_count == 0)
  22548. + complete_all(&arm_state->blocked_blocker);
  22549. + }
  22550. + }
  22551. +
  22552. + stop_suspend_timer(arm_state);
  22553. +
  22554. + local_uc = ++arm_state->videocore_use_count;
  22555. + local_entity_uc = ++(*entity_uc);
  22556. +
  22557. + /* If there's a pending request which hasn't yet been serviced then
  22558. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  22559. + * vc_resume_complete will block until we either resume or fail to
  22560. + * suspend */
  22561. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  22562. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  22563. +
  22564. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  22565. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  22566. + vchiq_log_info(vchiq_susp_log_level,
  22567. + "%s %s count %d, state count %d",
  22568. + __func__, entity, local_entity_uc, local_uc);
  22569. + request_poll(state, NULL, 0);
  22570. + } else
  22571. + vchiq_log_trace(vchiq_susp_log_level,
  22572. + "%s %s count %d, state count %d",
  22573. + __func__, entity, *entity_uc, local_uc);
  22574. +
  22575. +
  22576. + write_unlock_bh(&arm_state->susp_res_lock);
  22577. +
  22578. + /* Completion is in a done state when we're not suspended, so this won't
  22579. + * block for the non-suspended case. */
  22580. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  22581. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  22582. + __func__, entity);
  22583. + if (wait_for_completion_killable(
  22584. + &arm_state->vc_resume_complete) != 0) {
  22585. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  22586. + "resume interrupted", __func__, entity);
  22587. + ret = VCHIQ_ERROR;
  22588. + goto out;
  22589. + }
  22590. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  22591. + entity);
  22592. + }
  22593. +
  22594. + if (ret == VCHIQ_SUCCESS) {
  22595. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  22596. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  22597. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  22598. + /* Send the use notify to videocore */
  22599. + status = vchiq_send_remote_use_active(state);
  22600. + if (status == VCHIQ_SUCCESS)
  22601. + ack_cnt--;
  22602. + else
  22603. + atomic_add(ack_cnt,
  22604. + &arm_state->ka_use_ack_count);
  22605. + }
  22606. + }
  22607. +
  22608. +out:
  22609. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  22610. + return ret;
  22611. +}
  22612. +
  22613. +VCHIQ_STATUS_T
  22614. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  22615. +{
  22616. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22617. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  22618. + char entity[16];
  22619. + int *entity_uc;
  22620. + int local_uc, local_entity_uc;
  22621. +
  22622. + if (!arm_state)
  22623. + goto out;
  22624. +
  22625. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22626. +
  22627. + if (service) {
  22628. + sprintf(entity, "%c%c%c%c:%03d",
  22629. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  22630. + service->client_id);
  22631. + entity_uc = &service->service_use_count;
  22632. + } else {
  22633. + sprintf(entity, "PEER: ");
  22634. + entity_uc = &arm_state->peer_use_count;
  22635. + }
  22636. +
  22637. + write_lock_bh(&arm_state->susp_res_lock);
  22638. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  22639. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  22640. + WARN_ON(!arm_state->videocore_use_count);
  22641. + WARN_ON(!(*entity_uc));
  22642. + ret = VCHIQ_ERROR;
  22643. + goto unlock;
  22644. + }
  22645. + local_uc = --arm_state->videocore_use_count;
  22646. + local_entity_uc = --(*entity_uc);
  22647. +
  22648. + if (!vchiq_videocore_wanted(state)) {
  22649. + if (vchiq_platform_use_suspend_timer() &&
  22650. + !arm_state->resume_blocked) {
  22651. + /* Only use the timer if we're not trying to force
  22652. + * suspend (=> resume_blocked) */
  22653. + start_suspend_timer(arm_state);
  22654. + } else {
  22655. + vchiq_log_info(vchiq_susp_log_level,
  22656. + "%s %s count %d, state count %d - suspending",
  22657. + __func__, entity, *entity_uc,
  22658. + arm_state->videocore_use_count);
  22659. + vchiq_arm_vcsuspend(state);
  22660. + }
  22661. + } else
  22662. + vchiq_log_trace(vchiq_susp_log_level,
  22663. + "%s %s count %d, state count %d",
  22664. + __func__, entity, *entity_uc,
  22665. + arm_state->videocore_use_count);
  22666. +
  22667. +unlock:
  22668. + write_unlock_bh(&arm_state->susp_res_lock);
  22669. +
  22670. +out:
  22671. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  22672. + return ret;
  22673. +}
  22674. +
  22675. +void
  22676. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  22677. +{
  22678. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22679. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22680. + atomic_inc(&arm_state->ka_use_count);
  22681. + complete(&arm_state->ka_evt);
  22682. +}
  22683. +
  22684. +void
  22685. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  22686. +{
  22687. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22688. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22689. + atomic_inc(&arm_state->ka_release_count);
  22690. + complete(&arm_state->ka_evt);
  22691. +}
  22692. +
  22693. +VCHIQ_STATUS_T
  22694. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  22695. +{
  22696. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  22697. +}
  22698. +
  22699. +VCHIQ_STATUS_T
  22700. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  22701. +{
  22702. + return vchiq_release_internal(service->state, service);
  22703. +}
  22704. +
  22705. +static void suspend_timer_callback(unsigned long context)
  22706. +{
  22707. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  22708. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22709. + if (!arm_state)
  22710. + goto out;
  22711. + vchiq_log_info(vchiq_susp_log_level,
  22712. + "%s - suspend timer expired - check suspend", __func__);
  22713. + vchiq_check_suspend(state);
  22714. +out:
  22715. + return;
  22716. +}
  22717. +
  22718. +VCHIQ_STATUS_T
  22719. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  22720. +{
  22721. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22722. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22723. + if (service) {
  22724. + ret = vchiq_use_internal(service->state, service,
  22725. + USE_TYPE_SERVICE_NO_RESUME);
  22726. + unlock_service(service);
  22727. + }
  22728. + return ret;
  22729. +}
  22730. +
  22731. +VCHIQ_STATUS_T
  22732. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  22733. +{
  22734. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22735. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22736. + if (service) {
  22737. + ret = vchiq_use_internal(service->state, service,
  22738. + USE_TYPE_SERVICE);
  22739. + unlock_service(service);
  22740. + }
  22741. + return ret;
  22742. +}
  22743. +
  22744. +VCHIQ_STATUS_T
  22745. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  22746. +{
  22747. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22748. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22749. + if (service) {
  22750. + ret = vchiq_release_internal(service->state, service);
  22751. + unlock_service(service);
  22752. + }
  22753. + return ret;
  22754. +}
  22755. +
  22756. +void
  22757. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  22758. +{
  22759. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22760. + int i, j = 0;
  22761. + /* Only dump 64 services */
  22762. + static const int local_max_services = 64;
  22763. + /* If there's more than 64 services, only dump ones with
  22764. + * non-zero counts */
  22765. + int only_nonzero = 0;
  22766. + static const char *nz = "<-- preventing suspend";
  22767. +
  22768. + enum vc_suspend_status vc_suspend_state;
  22769. + enum vc_resume_status vc_resume_state;
  22770. + int peer_count;
  22771. + int vc_use_count;
  22772. + int active_services;
  22773. + struct service_data_struct {
  22774. + int fourcc;
  22775. + int clientid;
  22776. + int use_count;
  22777. + } service_data[local_max_services];
  22778. +
  22779. + if (!arm_state)
  22780. + return;
  22781. +
  22782. + read_lock_bh(&arm_state->susp_res_lock);
  22783. + vc_suspend_state = arm_state->vc_suspend_state;
  22784. + vc_resume_state = arm_state->vc_resume_state;
  22785. + peer_count = arm_state->peer_use_count;
  22786. + vc_use_count = arm_state->videocore_use_count;
  22787. + active_services = state->unused_service;
  22788. + if (active_services > local_max_services)
  22789. + only_nonzero = 1;
  22790. +
  22791. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  22792. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  22793. + if (!service_ptr)
  22794. + continue;
  22795. +
  22796. + if (only_nonzero && !service_ptr->service_use_count)
  22797. + continue;
  22798. +
  22799. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  22800. + service_data[j].fourcc = service_ptr->base.fourcc;
  22801. + service_data[j].clientid = service_ptr->client_id;
  22802. + service_data[j++].use_count = service_ptr->
  22803. + service_use_count;
  22804. + }
  22805. + }
  22806. +
  22807. + read_unlock_bh(&arm_state->susp_res_lock);
  22808. +
  22809. + vchiq_log_warning(vchiq_susp_log_level,
  22810. + "-- Videcore suspend state: %s --",
  22811. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  22812. + vchiq_log_warning(vchiq_susp_log_level,
  22813. + "-- Videcore resume state: %s --",
  22814. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  22815. +
  22816. + if (only_nonzero)
  22817. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  22818. + "services (%d). Only dumping up to first %d services "
  22819. + "with non-zero use-count", active_services,
  22820. + local_max_services);
  22821. +
  22822. + for (i = 0; i < j; i++) {
  22823. + vchiq_log_warning(vchiq_susp_log_level,
  22824. + "----- %c%c%c%c:%d service count %d %s",
  22825. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  22826. + service_data[i].clientid,
  22827. + service_data[i].use_count,
  22828. + service_data[i].use_count ? nz : "");
  22829. + }
  22830. + vchiq_log_warning(vchiq_susp_log_level,
  22831. + "----- VCHIQ use count count %d", peer_count);
  22832. + vchiq_log_warning(vchiq_susp_log_level,
  22833. + "--- Overall vchiq instance use count %d", vc_use_count);
  22834. +
  22835. + vchiq_dump_platform_use_state(state);
  22836. +}
  22837. +
  22838. +VCHIQ_STATUS_T
  22839. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  22840. +{
  22841. + VCHIQ_ARM_STATE_T *arm_state;
  22842. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22843. +
  22844. + if (!service || !service->state)
  22845. + goto out;
  22846. +
  22847. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22848. +
  22849. + arm_state = vchiq_platform_get_arm_state(service->state);
  22850. +
  22851. + read_lock_bh(&arm_state->susp_res_lock);
  22852. + if (service->service_use_count)
  22853. + ret = VCHIQ_SUCCESS;
  22854. + read_unlock_bh(&arm_state->susp_res_lock);
  22855. +
  22856. + if (ret == VCHIQ_ERROR) {
  22857. + vchiq_log_error(vchiq_susp_log_level,
  22858. + "%s ERROR - %c%c%c%c:%d service count %d, "
  22859. + "state count %d, videocore suspend state %s", __func__,
  22860. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  22861. + service->client_id, service->service_use_count,
  22862. + arm_state->videocore_use_count,
  22863. + suspend_state_names[arm_state->vc_suspend_state +
  22864. + VC_SUSPEND_NUM_OFFSET]);
  22865. + vchiq_dump_service_use_state(service->state);
  22866. + }
  22867. +out:
  22868. + return ret;
  22869. +}
  22870. +
  22871. +/* stub functions */
  22872. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  22873. +{
  22874. + (void)state;
  22875. +}
  22876. +
  22877. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  22878. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  22879. +{
  22880. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22881. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  22882. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  22883. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  22884. + write_lock_bh(&arm_state->susp_res_lock);
  22885. + if (!arm_state->first_connect) {
  22886. + char threadname[10];
  22887. + arm_state->first_connect = 1;
  22888. + write_unlock_bh(&arm_state->susp_res_lock);
  22889. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  22890. + state->id);
  22891. + arm_state->ka_thread = kthread_create(
  22892. + &vchiq_keepalive_thread_func,
  22893. + (void *)state,
  22894. + threadname);
  22895. + if (arm_state->ka_thread == NULL) {
  22896. + vchiq_log_error(vchiq_susp_log_level,
  22897. + "vchiq: FATAL: couldn't create thread %s",
  22898. + threadname);
  22899. + } else {
  22900. + wake_up_process(arm_state->ka_thread);
  22901. + }
  22902. + } else
  22903. + write_unlock_bh(&arm_state->susp_res_lock);
  22904. + }
  22905. +}
  22906. +
  22907. +
  22908. +/****************************************************************************
  22909. +*
  22910. +* vchiq_init - called when the module is loaded.
  22911. +*
  22912. +***************************************************************************/
  22913. +
  22914. +static int __init
  22915. +vchiq_init(void)
  22916. +{
  22917. + int err;
  22918. + void *ptr_err;
  22919. +
  22920. + /* create proc entries */
  22921. + err = vchiq_proc_init();
  22922. + if (err != 0)
  22923. + goto failed_proc_init;
  22924. +
  22925. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  22926. + if (err != 0) {
  22927. + vchiq_log_error(vchiq_arm_log_level,
  22928. + "Unable to allocate device number");
  22929. + goto failed_alloc_chrdev;
  22930. + }
  22931. + cdev_init(&vchiq_cdev, &vchiq_fops);
  22932. + vchiq_cdev.owner = THIS_MODULE;
  22933. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  22934. + if (err != 0) {
  22935. + vchiq_log_error(vchiq_arm_log_level,
  22936. + "Unable to register device");
  22937. + goto failed_cdev_add;
  22938. + }
  22939. +
  22940. + /* create sysfs entries */
  22941. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  22942. + ptr_err = vchiq_class;
  22943. + if (IS_ERR(ptr_err))
  22944. + goto failed_class_create;
  22945. +
  22946. + vchiq_dev = device_create(vchiq_class, NULL,
  22947. + vchiq_devid, NULL, "vchiq");
  22948. + ptr_err = vchiq_dev;
  22949. + if (IS_ERR(ptr_err))
  22950. + goto failed_device_create;
  22951. +
  22952. + err = vchiq_platform_init(&g_state);
  22953. + if (err != 0)
  22954. + goto failed_platform_init;
  22955. +
  22956. + vchiq_log_info(vchiq_arm_log_level,
  22957. + "vchiq: initialised - version %d (min %d), device %d.%d",
  22958. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  22959. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  22960. +
  22961. + return 0;
  22962. +
  22963. +failed_platform_init:
  22964. + device_destroy(vchiq_class, vchiq_devid);
  22965. +failed_device_create:
  22966. + class_destroy(vchiq_class);
  22967. +failed_class_create:
  22968. + cdev_del(&vchiq_cdev);
  22969. + err = PTR_ERR(ptr_err);
  22970. +failed_cdev_add:
  22971. + unregister_chrdev_region(vchiq_devid, 1);
  22972. +failed_alloc_chrdev:
  22973. + vchiq_proc_deinit();
  22974. +failed_proc_init:
  22975. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  22976. + return err;
  22977. +}
  22978. +
  22979. +static int vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  22980. +{
  22981. + VCHIQ_SERVICE_T *service;
  22982. + int use_count = 0, i;
  22983. + i = 0;
  22984. + while ((service = next_service_by_instance(instance->state,
  22985. + instance, &i)) != NULL) {
  22986. + use_count += service->service_use_count;
  22987. + unlock_service(service);
  22988. + }
  22989. + return use_count;
  22990. +}
  22991. +
  22992. +/* read the per-process use-count */
  22993. +static int proc_read_use_count(char *page, char **start,
  22994. + off_t off, int count,
  22995. + int *eof, void *data)
  22996. +{
  22997. + VCHIQ_INSTANCE_T instance = data;
  22998. + int len, use_count;
  22999. +
  23000. + use_count = vchiq_instance_get_use_count(instance);
  23001. + len = snprintf(page+off, count, "%d\n", use_count);
  23002. +
  23003. + return len;
  23004. +}
  23005. +
  23006. +/* add an instance (process) to the proc entries */
  23007. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance)
  23008. +{
  23009. +#if 1
  23010. + return 0;
  23011. +#else
  23012. + char pidstr[32];
  23013. + struct proc_dir_entry *top, *use_count;
  23014. + struct proc_dir_entry *clients = vchiq_clients_top();
  23015. + int pid = instance->pid;
  23016. +
  23017. + snprintf(pidstr, sizeof(pidstr), "%d", pid);
  23018. + top = proc_mkdir(pidstr, clients);
  23019. + if (!top)
  23020. + goto fail_top;
  23021. +
  23022. + use_count = create_proc_read_entry("use_count",
  23023. + 0444, top,
  23024. + proc_read_use_count,
  23025. + instance);
  23026. + if (!use_count)
  23027. + goto fail_use_count;
  23028. +
  23029. + instance->proc_entry = top;
  23030. +
  23031. + return 0;
  23032. +
  23033. +fail_use_count:
  23034. + remove_proc_entry(top->name, clients);
  23035. +fail_top:
  23036. + return -ENOMEM;
  23037. +#endif
  23038. +}
  23039. +
  23040. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance)
  23041. +{
  23042. +#if 0
  23043. + struct proc_dir_entry *clients = vchiq_clients_top();
  23044. + remove_proc_entry("use_count", instance->proc_entry);
  23045. + remove_proc_entry(instance->proc_entry->name, clients);
  23046. +#endif
  23047. +}
  23048. +
  23049. +/****************************************************************************
  23050. +*
  23051. +* vchiq_exit - called when the module is unloaded.
  23052. +*
  23053. +***************************************************************************/
  23054. +
  23055. +static void __exit
  23056. +vchiq_exit(void)
  23057. +{
  23058. + vchiq_platform_exit(&g_state);
  23059. + device_destroy(vchiq_class, vchiq_devid);
  23060. + class_destroy(vchiq_class);
  23061. + cdev_del(&vchiq_cdev);
  23062. + unregister_chrdev_region(vchiq_devid, 1);
  23063. +}
  23064. +
  23065. +module_init(vchiq_init);
  23066. +module_exit(vchiq_exit);
  23067. +MODULE_LICENSE("GPL");
  23068. +MODULE_AUTHOR("Broadcom Corporation");
  23069. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  23070. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 1970-01-01 01:00:00.000000000 +0100
  23071. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 2014-03-11 17:32:37.000000000 +0100
  23072. @@ -0,0 +1,212 @@
  23073. +/**
  23074. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23075. + *
  23076. + * Redistribution and use in source and binary forms, with or without
  23077. + * modification, are permitted provided that the following conditions
  23078. + * are met:
  23079. + * 1. Redistributions of source code must retain the above copyright
  23080. + * notice, this list of conditions, and the following disclaimer,
  23081. + * without modification.
  23082. + * 2. Redistributions in binary form must reproduce the above copyright
  23083. + * notice, this list of conditions and the following disclaimer in the
  23084. + * documentation and/or other materials provided with the distribution.
  23085. + * 3. The names of the above-listed copyright holders may not be used
  23086. + * to endorse or promote products derived from this software without
  23087. + * specific prior written permission.
  23088. + *
  23089. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23090. + * GNU General Public License ("GPL") version 2, as published by the Free
  23091. + * Software Foundation.
  23092. + *
  23093. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23094. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23095. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23096. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23097. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23098. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23099. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23100. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23101. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23102. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23103. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23104. + */
  23105. +
  23106. +#ifndef VCHIQ_ARM_H
  23107. +#define VCHIQ_ARM_H
  23108. +
  23109. +#include <linux/mutex.h>
  23110. +#include <linux/semaphore.h>
  23111. +#include <linux/atomic.h>
  23112. +#include "vchiq_core.h"
  23113. +
  23114. +
  23115. +enum vc_suspend_status {
  23116. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  23117. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  23118. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  23119. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  23120. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  23121. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  23122. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  23123. +};
  23124. +
  23125. +enum vc_resume_status {
  23126. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  23127. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  23128. + VC_RESUME_REQUESTED, /* User has requested resume */
  23129. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  23130. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  23131. +};
  23132. +
  23133. +
  23134. +enum USE_TYPE_E {
  23135. + USE_TYPE_SERVICE,
  23136. + USE_TYPE_SERVICE_NO_RESUME,
  23137. + USE_TYPE_VCHIQ
  23138. +};
  23139. +
  23140. +
  23141. +
  23142. +typedef struct vchiq_arm_state_struct {
  23143. + /* Keepalive-related data */
  23144. + struct task_struct *ka_thread;
  23145. + struct completion ka_evt;
  23146. + atomic_t ka_use_count;
  23147. + atomic_t ka_use_ack_count;
  23148. + atomic_t ka_release_count;
  23149. +
  23150. + struct completion vc_suspend_complete;
  23151. + struct completion vc_resume_complete;
  23152. +
  23153. + rwlock_t susp_res_lock;
  23154. + enum vc_suspend_status vc_suspend_state;
  23155. + enum vc_resume_status vc_resume_state;
  23156. +
  23157. + unsigned int wake_address;
  23158. +
  23159. + struct timer_list suspend_timer;
  23160. + int suspend_timer_timeout;
  23161. + int suspend_timer_running;
  23162. +
  23163. + /* Global use count for videocore.
  23164. + ** This is equal to the sum of the use counts for all services. When
  23165. + ** this hits zero the videocore suspend procedure will be initiated.
  23166. + */
  23167. + int videocore_use_count;
  23168. +
  23169. + /* Use count to track requests from videocore peer.
  23170. + ** This use count is not associated with a service, so needs to be
  23171. + ** tracked separately with the state.
  23172. + */
  23173. + int peer_use_count;
  23174. +
  23175. + /* Flag to indicate whether resume is blocked. This happens when the
  23176. + ** ARM is suspending
  23177. + */
  23178. + struct completion resume_blocker;
  23179. + int resume_blocked;
  23180. + struct completion blocked_blocker;
  23181. + int blocked_count;
  23182. +
  23183. + int autosuspend_override;
  23184. +
  23185. + /* Flag to indicate that the first vchiq connect has made it through.
  23186. + ** This means that both sides should be fully ready, and we should
  23187. + ** be able to suspend after this point.
  23188. + */
  23189. + int first_connect;
  23190. +
  23191. + unsigned long long suspend_start_time;
  23192. + unsigned long long sleep_start_time;
  23193. + unsigned long long resume_start_time;
  23194. + unsigned long long last_wake_time;
  23195. +
  23196. +} VCHIQ_ARM_STATE_T;
  23197. +
  23198. +extern int vchiq_arm_log_level;
  23199. +extern int vchiq_susp_log_level;
  23200. +
  23201. +extern int __init
  23202. +vchiq_platform_init(VCHIQ_STATE_T *state);
  23203. +
  23204. +extern void __exit
  23205. +vchiq_platform_exit(VCHIQ_STATE_T *state);
  23206. +
  23207. +extern VCHIQ_STATE_T *
  23208. +vchiq_get_state(void);
  23209. +
  23210. +extern VCHIQ_STATUS_T
  23211. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  23212. +
  23213. +extern VCHIQ_STATUS_T
  23214. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  23215. +
  23216. +extern int
  23217. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  23218. +
  23219. +extern VCHIQ_STATUS_T
  23220. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  23221. +
  23222. +extern VCHIQ_STATUS_T
  23223. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  23224. +
  23225. +extern int
  23226. +vchiq_check_resume(VCHIQ_STATE_T *state);
  23227. +
  23228. +extern void
  23229. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  23230. +
  23231. +extern VCHIQ_STATUS_T
  23232. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  23233. +
  23234. +extern VCHIQ_STATUS_T
  23235. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  23236. +
  23237. +extern VCHIQ_STATUS_T
  23238. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  23239. +
  23240. +extern VCHIQ_STATUS_T
  23241. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  23242. +
  23243. +extern int
  23244. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  23245. +
  23246. +extern int
  23247. +vchiq_platform_use_suspend_timer(void);
  23248. +
  23249. +extern void
  23250. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  23251. +
  23252. +extern void
  23253. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  23254. +
  23255. +extern VCHIQ_ARM_STATE_T*
  23256. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  23257. +
  23258. +extern int
  23259. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  23260. +
  23261. +extern VCHIQ_STATUS_T
  23262. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  23263. + enum USE_TYPE_E use_type);
  23264. +extern VCHIQ_STATUS_T
  23265. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  23266. +
  23267. +void
  23268. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  23269. + enum vc_suspend_status new_state);
  23270. +
  23271. +void
  23272. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  23273. + enum vc_resume_status new_state);
  23274. +
  23275. +void
  23276. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  23277. +
  23278. +extern int vchiq_proc_init(void);
  23279. +extern void vchiq_proc_deinit(void);
  23280. +extern struct proc_dir_entry *vchiq_proc_top(void);
  23281. +extern struct proc_dir_entry *vchiq_clients_top(void);
  23282. +
  23283. +
  23284. +#endif /* VCHIQ_ARM_H */
  23285. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  23286. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 1970-01-01 01:00:00.000000000 +0100
  23287. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 2014-03-11 17:32:37.000000000 +0100
  23288. @@ -0,0 +1,37 @@
  23289. +/**
  23290. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23291. + *
  23292. + * Redistribution and use in source and binary forms, with or without
  23293. + * modification, are permitted provided that the following conditions
  23294. + * are met:
  23295. + * 1. Redistributions of source code must retain the above copyright
  23296. + * notice, this list of conditions, and the following disclaimer,
  23297. + * without modification.
  23298. + * 2. Redistributions in binary form must reproduce the above copyright
  23299. + * notice, this list of conditions and the following disclaimer in the
  23300. + * documentation and/or other materials provided with the distribution.
  23301. + * 3. The names of the above-listed copyright holders may not be used
  23302. + * to endorse or promote products derived from this software without
  23303. + * specific prior written permission.
  23304. + *
  23305. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23306. + * GNU General Public License ("GPL") version 2, as published by the Free
  23307. + * Software Foundation.
  23308. + *
  23309. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23310. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23311. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23312. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23313. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23314. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23315. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23316. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23317. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23318. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23319. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23320. + */
  23321. +
  23322. +const char *vchiq_get_build_hostname(void);
  23323. +const char *vchiq_get_build_version(void);
  23324. +const char *vchiq_get_build_time(void);
  23325. +const char *vchiq_get_build_date(void);
  23326. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  23327. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 1970-01-01 01:00:00.000000000 +0100
  23328. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 2014-03-11 17:32:37.000000000 +0100
  23329. @@ -0,0 +1,60 @@
  23330. +/**
  23331. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23332. + *
  23333. + * Redistribution and use in source and binary forms, with or without
  23334. + * modification, are permitted provided that the following conditions
  23335. + * are met:
  23336. + * 1. Redistributions of source code must retain the above copyright
  23337. + * notice, this list of conditions, and the following disclaimer,
  23338. + * without modification.
  23339. + * 2. Redistributions in binary form must reproduce the above copyright
  23340. + * notice, this list of conditions and the following disclaimer in the
  23341. + * documentation and/or other materials provided with the distribution.
  23342. + * 3. The names of the above-listed copyright holders may not be used
  23343. + * to endorse or promote products derived from this software without
  23344. + * specific prior written permission.
  23345. + *
  23346. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23347. + * GNU General Public License ("GPL") version 2, as published by the Free
  23348. + * Software Foundation.
  23349. + *
  23350. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23351. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23352. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23353. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23354. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23355. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23356. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23357. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23358. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23359. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23360. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23361. + */
  23362. +
  23363. +#ifndef VCHIQ_CFG_H
  23364. +#define VCHIQ_CFG_H
  23365. +
  23366. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  23367. +/* The version of VCHIQ - change with any non-trivial change */
  23368. +#define VCHIQ_VERSION 6
  23369. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  23370. +** incompatible change */
  23371. +#define VCHIQ_VERSION_MIN 3
  23372. +
  23373. +#define VCHIQ_MAX_STATES 1
  23374. +#define VCHIQ_MAX_SERVICES 4096
  23375. +#define VCHIQ_MAX_SLOTS 128
  23376. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  23377. +
  23378. +#define VCHIQ_NUM_CURRENT_BULKS 32
  23379. +#define VCHIQ_NUM_SERVICE_BULKS 4
  23380. +
  23381. +#ifndef VCHIQ_ENABLE_DEBUG
  23382. +#define VCHIQ_ENABLE_DEBUG 1
  23383. +#endif
  23384. +
  23385. +#ifndef VCHIQ_ENABLE_STATS
  23386. +#define VCHIQ_ENABLE_STATS 1
  23387. +#endif
  23388. +
  23389. +#endif /* VCHIQ_CFG_H */
  23390. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  23391. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 1970-01-01 01:00:00.000000000 +0100
  23392. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 2014-03-11 17:32:37.000000000 +0100
  23393. @@ -0,0 +1,119 @@
  23394. +/**
  23395. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23396. + *
  23397. + * Redistribution and use in source and binary forms, with or without
  23398. + * modification, are permitted provided that the following conditions
  23399. + * are met:
  23400. + * 1. Redistributions of source code must retain the above copyright
  23401. + * notice, this list of conditions, and the following disclaimer,
  23402. + * without modification.
  23403. + * 2. Redistributions in binary form must reproduce the above copyright
  23404. + * notice, this list of conditions and the following disclaimer in the
  23405. + * documentation and/or other materials provided with the distribution.
  23406. + * 3. The names of the above-listed copyright holders may not be used
  23407. + * to endorse or promote products derived from this software without
  23408. + * specific prior written permission.
  23409. + *
  23410. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23411. + * GNU General Public License ("GPL") version 2, as published by the Free
  23412. + * Software Foundation.
  23413. + *
  23414. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23415. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23416. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23417. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23418. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23419. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23420. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23421. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23422. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23423. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23424. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23425. + */
  23426. +
  23427. +#include "vchiq_connected.h"
  23428. +#include "vchiq_core.h"
  23429. +#include <linux/module.h>
  23430. +#include <linux/mutex.h>
  23431. +
  23432. +#define MAX_CALLBACKS 10
  23433. +
  23434. +static int g_connected;
  23435. +static int g_num_deferred_callbacks;
  23436. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  23437. +static int g_once_init;
  23438. +static struct mutex g_connected_mutex;
  23439. +
  23440. +/****************************************************************************
  23441. +*
  23442. +* Function to initialize our lock.
  23443. +*
  23444. +***************************************************************************/
  23445. +
  23446. +static void connected_init(void)
  23447. +{
  23448. + if (!g_once_init) {
  23449. + mutex_init(&g_connected_mutex);
  23450. + g_once_init = 1;
  23451. + }
  23452. +}
  23453. +
  23454. +/****************************************************************************
  23455. +*
  23456. +* This function is used to defer initialization until the vchiq stack is
  23457. +* initialized. If the stack is already initialized, then the callback will
  23458. +* be made immediately, otherwise it will be deferred until
  23459. +* vchiq_call_connected_callbacks is called.
  23460. +*
  23461. +***************************************************************************/
  23462. +
  23463. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  23464. +{
  23465. + connected_init();
  23466. +
  23467. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  23468. + return;
  23469. +
  23470. + if (g_connected)
  23471. + /* We're already connected. Call the callback immediately. */
  23472. +
  23473. + callback();
  23474. + else {
  23475. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  23476. + vchiq_log_error(vchiq_core_log_level,
  23477. + "There already %d callback registered - "
  23478. + "please increase MAX_CALLBACKS",
  23479. + g_num_deferred_callbacks);
  23480. + else {
  23481. + g_deferred_callback[g_num_deferred_callbacks] =
  23482. + callback;
  23483. + g_num_deferred_callbacks++;
  23484. + }
  23485. + }
  23486. + mutex_unlock(&g_connected_mutex);
  23487. +}
  23488. +
  23489. +/****************************************************************************
  23490. +*
  23491. +* This function is called by the vchiq stack once it has been connected to
  23492. +* the videocore and clients can start to use the stack.
  23493. +*
  23494. +***************************************************************************/
  23495. +
  23496. +void vchiq_call_connected_callbacks(void)
  23497. +{
  23498. + int i;
  23499. +
  23500. + connected_init();
  23501. +
  23502. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  23503. + return;
  23504. +
  23505. + for (i = 0; i < g_num_deferred_callbacks; i++)
  23506. + g_deferred_callback[i]();
  23507. +
  23508. + g_num_deferred_callbacks = 0;
  23509. + g_connected = 1;
  23510. + mutex_unlock(&g_connected_mutex);
  23511. +}
  23512. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  23513. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  23514. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 1970-01-01 01:00:00.000000000 +0100
  23515. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 2014-03-11 17:51:16.000000000 +0100
  23516. @@ -0,0 +1,50 @@
  23517. +/**
  23518. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23519. + *
  23520. + * Redistribution and use in source and binary forms, with or without
  23521. + * modification, are permitted provided that the following conditions
  23522. + * are met:
  23523. + * 1. Redistributions of source code must retain the above copyright
  23524. + * notice, this list of conditions, and the following disclaimer,
  23525. + * without modification.
  23526. + * 2. Redistributions in binary form must reproduce the above copyright
  23527. + * notice, this list of conditions and the following disclaimer in the
  23528. + * documentation and/or other materials provided with the distribution.
  23529. + * 3. The names of the above-listed copyright holders may not be used
  23530. + * to endorse or promote products derived from this software without
  23531. + * specific prior written permission.
  23532. + *
  23533. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23534. + * GNU General Public License ("GPL") version 2, as published by the Free
  23535. + * Software Foundation.
  23536. + *
  23537. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23538. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23539. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23540. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23541. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23542. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23543. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23544. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23545. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23546. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23547. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23548. + */
  23549. +
  23550. +#ifndef VCHIQ_CONNECTED_H
  23551. +#define VCHIQ_CONNECTED_H
  23552. +
  23553. +/* ---- Include Files ----------------------------------------------------- */
  23554. +
  23555. +/* ---- Constants and Types ---------------------------------------------- */
  23556. +
  23557. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  23558. +
  23559. +/* ---- Variable Externs ------------------------------------------------- */
  23560. +
  23561. +/* ---- Function Prototypes ---------------------------------------------- */
  23562. +
  23563. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  23564. +void vchiq_call_connected_callbacks(void);
  23565. +
  23566. +#endif /* VCHIQ_CONNECTED_H */
  23567. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  23568. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 1970-01-01 01:00:00.000000000 +0100
  23569. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 2014-03-11 17:32:37.000000000 +0100
  23570. @@ -0,0 +1,3824 @@
  23571. +/**
  23572. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23573. + *
  23574. + * Redistribution and use in source and binary forms, with or without
  23575. + * modification, are permitted provided that the following conditions
  23576. + * are met:
  23577. + * 1. Redistributions of source code must retain the above copyright
  23578. + * notice, this list of conditions, and the following disclaimer,
  23579. + * without modification.
  23580. + * 2. Redistributions in binary form must reproduce the above copyright
  23581. + * notice, this list of conditions and the following disclaimer in the
  23582. + * documentation and/or other materials provided with the distribution.
  23583. + * 3. The names of the above-listed copyright holders may not be used
  23584. + * to endorse or promote products derived from this software without
  23585. + * specific prior written permission.
  23586. + *
  23587. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23588. + * GNU General Public License ("GPL") version 2, as published by the Free
  23589. + * Software Foundation.
  23590. + *
  23591. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23592. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23593. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23594. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23595. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23596. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23597. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23598. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23599. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23600. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23601. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23602. + */
  23603. +
  23604. +#include "vchiq_core.h"
  23605. +
  23606. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  23607. +
  23608. +#define HANDLE_STATE_SHIFT 12
  23609. +
  23610. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  23611. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  23612. +#define SLOT_INDEX_FROM_DATA(state, data) \
  23613. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  23614. + VCHIQ_SLOT_SIZE)
  23615. +#define SLOT_INDEX_FROM_INFO(state, info) \
  23616. + ((unsigned int)(info - state->slot_info))
  23617. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  23618. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  23619. +
  23620. +
  23621. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  23622. +
  23623. +
  23624. +struct vchiq_open_payload {
  23625. + int fourcc;
  23626. + int client_id;
  23627. + short version;
  23628. + short version_min;
  23629. +};
  23630. +
  23631. +struct vchiq_openack_payload {
  23632. + short version;
  23633. +};
  23634. +
  23635. +/* we require this for consistency between endpoints */
  23636. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  23637. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  23638. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  23639. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  23640. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  23641. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  23642. +
  23643. +/* Run time control of log level, based on KERN_XXX level. */
  23644. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  23645. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  23646. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  23647. +
  23648. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  23649. +
  23650. +static DEFINE_SPINLOCK(service_spinlock);
  23651. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  23652. +DEFINE_SPINLOCK(quota_spinlock);
  23653. +
  23654. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  23655. +static unsigned int handle_seq;
  23656. +
  23657. +static const char *const srvstate_names[] = {
  23658. + "FREE",
  23659. + "HIDDEN",
  23660. + "LISTENING",
  23661. + "OPENING",
  23662. + "OPEN",
  23663. + "OPENSYNC",
  23664. + "CLOSESENT",
  23665. + "CLOSERECVD",
  23666. + "CLOSEWAIT",
  23667. + "CLOSED"
  23668. +};
  23669. +
  23670. +static const char *const reason_names[] = {
  23671. + "SERVICE_OPENED",
  23672. + "SERVICE_CLOSED",
  23673. + "MESSAGE_AVAILABLE",
  23674. + "BULK_TRANSMIT_DONE",
  23675. + "BULK_RECEIVE_DONE",
  23676. + "BULK_TRANSMIT_ABORTED",
  23677. + "BULK_RECEIVE_ABORTED"
  23678. +};
  23679. +
  23680. +static const char *const conn_state_names[] = {
  23681. + "DISCONNECTED",
  23682. + "CONNECTING",
  23683. + "CONNECTED",
  23684. + "PAUSING",
  23685. + "PAUSE_SENT",
  23686. + "PAUSED",
  23687. + "RESUMING",
  23688. + "PAUSE_TIMEOUT",
  23689. + "RESUME_TIMEOUT"
  23690. +};
  23691. +
  23692. +
  23693. +static void
  23694. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  23695. +
  23696. +static const char *msg_type_str(unsigned int msg_type)
  23697. +{
  23698. + switch (msg_type) {
  23699. + case VCHIQ_MSG_PADDING: return "PADDING";
  23700. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  23701. + case VCHIQ_MSG_OPEN: return "OPEN";
  23702. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  23703. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  23704. + case VCHIQ_MSG_DATA: return "DATA";
  23705. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  23706. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  23707. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  23708. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  23709. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  23710. + case VCHIQ_MSG_RESUME: return "RESUME";
  23711. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  23712. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  23713. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  23714. + }
  23715. + return "???";
  23716. +}
  23717. +
  23718. +static inline void
  23719. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  23720. +{
  23721. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  23722. + service->state->id, service->localport,
  23723. + srvstate_names[service->srvstate],
  23724. + srvstate_names[newstate]);
  23725. + service->srvstate = newstate;
  23726. +}
  23727. +
  23728. +VCHIQ_SERVICE_T *
  23729. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  23730. +{
  23731. + VCHIQ_SERVICE_T *service;
  23732. +
  23733. + spin_lock(&service_spinlock);
  23734. + service = handle_to_service(handle);
  23735. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23736. + (service->handle == handle)) {
  23737. + BUG_ON(service->ref_count == 0);
  23738. + service->ref_count++;
  23739. + } else
  23740. + service = NULL;
  23741. + spin_unlock(&service_spinlock);
  23742. +
  23743. + if (!service)
  23744. + vchiq_log_info(vchiq_core_log_level,
  23745. + "Invalid service handle 0x%x", handle);
  23746. +
  23747. + return service;
  23748. +}
  23749. +
  23750. +VCHIQ_SERVICE_T *
  23751. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  23752. +{
  23753. + VCHIQ_SERVICE_T *service = NULL;
  23754. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  23755. + spin_lock(&service_spinlock);
  23756. + service = state->services[localport];
  23757. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  23758. + BUG_ON(service->ref_count == 0);
  23759. + service->ref_count++;
  23760. + } else
  23761. + service = NULL;
  23762. + spin_unlock(&service_spinlock);
  23763. + }
  23764. +
  23765. + if (!service)
  23766. + vchiq_log_info(vchiq_core_log_level,
  23767. + "Invalid port %d", localport);
  23768. +
  23769. + return service;
  23770. +}
  23771. +
  23772. +VCHIQ_SERVICE_T *
  23773. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  23774. + VCHIQ_SERVICE_HANDLE_T handle) {
  23775. + VCHIQ_SERVICE_T *service;
  23776. +
  23777. + spin_lock(&service_spinlock);
  23778. + service = handle_to_service(handle);
  23779. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23780. + (service->handle == handle) &&
  23781. + (service->instance == instance)) {
  23782. + BUG_ON(service->ref_count == 0);
  23783. + service->ref_count++;
  23784. + } else
  23785. + service = NULL;
  23786. + spin_unlock(&service_spinlock);
  23787. +
  23788. + if (!service)
  23789. + vchiq_log_info(vchiq_core_log_level,
  23790. + "Invalid service handle 0x%x", handle);
  23791. +
  23792. + return service;
  23793. +}
  23794. +
  23795. +VCHIQ_SERVICE_T *
  23796. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  23797. + int *pidx)
  23798. +{
  23799. + VCHIQ_SERVICE_T *service = NULL;
  23800. + int idx = *pidx;
  23801. +
  23802. + spin_lock(&service_spinlock);
  23803. + while (idx < state->unused_service) {
  23804. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  23805. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23806. + (srv->instance == instance)) {
  23807. + service = srv;
  23808. + BUG_ON(service->ref_count == 0);
  23809. + service->ref_count++;
  23810. + break;
  23811. + }
  23812. + }
  23813. + spin_unlock(&service_spinlock);
  23814. +
  23815. + *pidx = idx;
  23816. +
  23817. + return service;
  23818. +}
  23819. +
  23820. +void
  23821. +lock_service(VCHIQ_SERVICE_T *service)
  23822. +{
  23823. + spin_lock(&service_spinlock);
  23824. + BUG_ON(!service || (service->ref_count == 0));
  23825. + if (service)
  23826. + service->ref_count++;
  23827. + spin_unlock(&service_spinlock);
  23828. +}
  23829. +
  23830. +void
  23831. +unlock_service(VCHIQ_SERVICE_T *service)
  23832. +{
  23833. + VCHIQ_STATE_T *state = service->state;
  23834. + spin_lock(&service_spinlock);
  23835. + BUG_ON(!service || (service->ref_count == 0));
  23836. + if (service && service->ref_count) {
  23837. + service->ref_count--;
  23838. + if (!service->ref_count) {
  23839. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  23840. + state->services[service->localport] = NULL;
  23841. + } else
  23842. + service = NULL;
  23843. + }
  23844. + spin_unlock(&service_spinlock);
  23845. +
  23846. + if (service && service->userdata_term)
  23847. + service->userdata_term(service->base.userdata);
  23848. +
  23849. + kfree(service);
  23850. +}
  23851. +
  23852. +int
  23853. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  23854. +{
  23855. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  23856. + int id;
  23857. +
  23858. + id = service ? service->client_id : 0;
  23859. + if (service)
  23860. + unlock_service(service);
  23861. +
  23862. + return id;
  23863. +}
  23864. +
  23865. +void *
  23866. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  23867. +{
  23868. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  23869. +
  23870. + return service ? service->base.userdata : NULL;
  23871. +}
  23872. +
  23873. +int
  23874. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  23875. +{
  23876. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  23877. +
  23878. + return service ? service->base.fourcc : 0;
  23879. +}
  23880. +
  23881. +static void
  23882. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  23883. +{
  23884. + VCHIQ_STATE_T *state = service->state;
  23885. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  23886. +
  23887. + service->closing = 1;
  23888. +
  23889. + /* Synchronise with other threads. */
  23890. + mutex_lock(&state->recycle_mutex);
  23891. + mutex_unlock(&state->recycle_mutex);
  23892. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  23893. + /* If we're pausing then the slot_mutex is held until resume
  23894. + * by the slot handler. Therefore don't try to acquire this
  23895. + * mutex if we're the slot handler and in the pause sent state.
  23896. + * We don't need to in this case anyway. */
  23897. + mutex_lock(&state->slot_mutex);
  23898. + mutex_unlock(&state->slot_mutex);
  23899. + }
  23900. +
  23901. + /* Unblock any sending thread. */
  23902. + service_quota = &state->service_quotas[service->localport];
  23903. + up(&service_quota->quota_event);
  23904. +}
  23905. +
  23906. +static void
  23907. +mark_service_closing(VCHIQ_SERVICE_T *service)
  23908. +{
  23909. + mark_service_closing_internal(service, 0);
  23910. +}
  23911. +
  23912. +static inline VCHIQ_STATUS_T
  23913. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  23914. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  23915. +{
  23916. + VCHIQ_STATUS_T status;
  23917. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  23918. + service->state->id, service->localport, reason_names[reason],
  23919. + (unsigned int)header, (unsigned int)bulk_userdata);
  23920. + status = service->base.callback(reason, header, service->handle,
  23921. + bulk_userdata);
  23922. + if (status == VCHIQ_ERROR) {
  23923. + vchiq_log_warning(vchiq_core_log_level,
  23924. + "%d: ignoring ERROR from callback to service %x",
  23925. + service->state->id, service->handle);
  23926. + status = VCHIQ_SUCCESS;
  23927. + }
  23928. + return status;
  23929. +}
  23930. +
  23931. +inline void
  23932. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  23933. +{
  23934. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  23935. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  23936. + conn_state_names[oldstate],
  23937. + conn_state_names[newstate]);
  23938. + state->conn_state = newstate;
  23939. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  23940. +}
  23941. +
  23942. +static inline void
  23943. +remote_event_create(REMOTE_EVENT_T *event)
  23944. +{
  23945. + event->armed = 0;
  23946. + /* Don't clear the 'fired' flag because it may already have been set
  23947. + ** by the other side. */
  23948. + sema_init(event->event, 0);
  23949. +}
  23950. +
  23951. +static inline void
  23952. +remote_event_destroy(REMOTE_EVENT_T *event)
  23953. +{
  23954. + (void)event;
  23955. +}
  23956. +
  23957. +static inline int
  23958. +remote_event_wait(REMOTE_EVENT_T *event)
  23959. +{
  23960. + if (!event->fired) {
  23961. + event->armed = 1;
  23962. + dsb();
  23963. + if (!event->fired) {
  23964. + if (down_interruptible(event->event) != 0) {
  23965. + event->armed = 0;
  23966. + return 0;
  23967. + }
  23968. + }
  23969. + event->armed = 0;
  23970. + wmb();
  23971. + }
  23972. +
  23973. + event->fired = 0;
  23974. + return 1;
  23975. +}
  23976. +
  23977. +static inline void
  23978. +remote_event_signal_local(REMOTE_EVENT_T *event)
  23979. +{
  23980. + event->armed = 0;
  23981. + up(event->event);
  23982. +}
  23983. +
  23984. +static inline void
  23985. +remote_event_poll(REMOTE_EVENT_T *event)
  23986. +{
  23987. + if (event->fired && event->armed)
  23988. + remote_event_signal_local(event);
  23989. +}
  23990. +
  23991. +void
  23992. +remote_event_pollall(VCHIQ_STATE_T *state)
  23993. +{
  23994. + remote_event_poll(&state->local->sync_trigger);
  23995. + remote_event_poll(&state->local->sync_release);
  23996. + remote_event_poll(&state->local->trigger);
  23997. + remote_event_poll(&state->local->recycle);
  23998. +}
  23999. +
  24000. +/* Round up message sizes so that any space at the end of a slot is always big
  24001. +** enough for a header. This relies on header size being a power of two, which
  24002. +** has been verified earlier by a static assertion. */
  24003. +
  24004. +static inline unsigned int
  24005. +calc_stride(unsigned int size)
  24006. +{
  24007. + /* Allow room for the header */
  24008. + size += sizeof(VCHIQ_HEADER_T);
  24009. +
  24010. + /* Round up */
  24011. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  24012. + - 1);
  24013. +}
  24014. +
  24015. +/* Called by the slot handler thread */
  24016. +static VCHIQ_SERVICE_T *
  24017. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  24018. +{
  24019. + int i;
  24020. +
  24021. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  24022. +
  24023. + for (i = 0; i < state->unused_service; i++) {
  24024. + VCHIQ_SERVICE_T *service = state->services[i];
  24025. + if (service &&
  24026. + (service->public_fourcc == fourcc) &&
  24027. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  24028. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  24029. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  24030. + lock_service(service);
  24031. + return service;
  24032. + }
  24033. + }
  24034. +
  24035. + return NULL;
  24036. +}
  24037. +
  24038. +/* Called by the slot handler thread */
  24039. +static VCHIQ_SERVICE_T *
  24040. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  24041. +{
  24042. + int i;
  24043. + for (i = 0; i < state->unused_service; i++) {
  24044. + VCHIQ_SERVICE_T *service = state->services[i];
  24045. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  24046. + && (service->remoteport == port)) {
  24047. + lock_service(service);
  24048. + return service;
  24049. + }
  24050. + }
  24051. + return NULL;
  24052. +}
  24053. +
  24054. +inline void
  24055. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  24056. +{
  24057. + uint32_t value;
  24058. +
  24059. + if (service) {
  24060. + do {
  24061. + value = atomic_read(&service->poll_flags);
  24062. + } while (atomic_cmpxchg(&service->poll_flags, value,
  24063. + value | (1 << poll_type)) != value);
  24064. +
  24065. + do {
  24066. + value = atomic_read(&state->poll_services[
  24067. + service->localport>>5]);
  24068. + } while (atomic_cmpxchg(
  24069. + &state->poll_services[service->localport>>5],
  24070. + value, value | (1 << (service->localport & 0x1f)))
  24071. + != value);
  24072. + }
  24073. +
  24074. + state->poll_needed = 1;
  24075. + wmb();
  24076. +
  24077. + /* ... and ensure the slot handler runs. */
  24078. + remote_event_signal_local(&state->local->trigger);
  24079. +}
  24080. +
  24081. +/* Called from queue_message, by the slot handler and application threads,
  24082. +** with slot_mutex held */
  24083. +static VCHIQ_HEADER_T *
  24084. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  24085. +{
  24086. + VCHIQ_SHARED_STATE_T *local = state->local;
  24087. + int tx_pos = state->local_tx_pos;
  24088. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  24089. +
  24090. + if (space > slot_space) {
  24091. + VCHIQ_HEADER_T *header;
  24092. + /* Fill the remaining space with padding */
  24093. + WARN_ON(state->tx_data == NULL);
  24094. + header = (VCHIQ_HEADER_T *)
  24095. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  24096. + header->msgid = VCHIQ_MSGID_PADDING;
  24097. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  24098. +
  24099. + tx_pos += slot_space;
  24100. + }
  24101. +
  24102. + /* If necessary, get the next slot. */
  24103. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  24104. + int slot_index;
  24105. +
  24106. + /* If there is no free slot... */
  24107. +
  24108. + if (down_trylock(&state->slot_available_event) != 0) {
  24109. + /* ...wait for one. */
  24110. +
  24111. + VCHIQ_STATS_INC(state, slot_stalls);
  24112. +
  24113. + /* But first, flush through the last slot. */
  24114. + state->local_tx_pos = tx_pos;
  24115. + local->tx_pos = tx_pos;
  24116. + remote_event_signal(&state->remote->trigger);
  24117. +
  24118. + if (!is_blocking ||
  24119. + (down_interruptible(
  24120. + &state->slot_available_event) != 0))
  24121. + return NULL; /* No space available */
  24122. + }
  24123. +
  24124. + BUG_ON(tx_pos ==
  24125. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  24126. +
  24127. + slot_index = local->slot_queue[
  24128. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  24129. + VCHIQ_SLOT_QUEUE_MASK];
  24130. + state->tx_data =
  24131. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  24132. + }
  24133. +
  24134. + state->local_tx_pos = tx_pos + space;
  24135. +
  24136. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  24137. +}
  24138. +
  24139. +/* Called by the recycle thread. */
  24140. +static void
  24141. +process_free_queue(VCHIQ_STATE_T *state)
  24142. +{
  24143. + VCHIQ_SHARED_STATE_T *local = state->local;
  24144. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  24145. + int slot_queue_available;
  24146. +
  24147. + /* Use a read memory barrier to ensure that any state that may have
  24148. + ** been modified by another thread is not masked by stale prefetched
  24149. + ** values. */
  24150. + rmb();
  24151. +
  24152. + /* Find slots which have been freed by the other side, and return them
  24153. + ** to the available queue. */
  24154. + slot_queue_available = state->slot_queue_available;
  24155. +
  24156. + while (slot_queue_available != local->slot_queue_recycle) {
  24157. + unsigned int pos;
  24158. + int slot_index = local->slot_queue[slot_queue_available++ &
  24159. + VCHIQ_SLOT_QUEUE_MASK];
  24160. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  24161. + int data_found = 0;
  24162. +
  24163. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  24164. + state->id, slot_index, (unsigned int)data,
  24165. + local->slot_queue_recycle, slot_queue_available);
  24166. +
  24167. + /* Initialise the bitmask for services which have used this
  24168. + ** slot */
  24169. + BITSET_ZERO(service_found);
  24170. +
  24171. + pos = 0;
  24172. +
  24173. + while (pos < VCHIQ_SLOT_SIZE) {
  24174. + VCHIQ_HEADER_T *header =
  24175. + (VCHIQ_HEADER_T *)(data + pos);
  24176. + int msgid = header->msgid;
  24177. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  24178. + int port = VCHIQ_MSG_SRCPORT(msgid);
  24179. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  24180. + &state->service_quotas[port];
  24181. + int count;
  24182. + spin_lock(&quota_spinlock);
  24183. + count = service_quota->message_use_count;
  24184. + if (count > 0)
  24185. + service_quota->message_use_count =
  24186. + count - 1;
  24187. + spin_unlock(&quota_spinlock);
  24188. +
  24189. + if (count == service_quota->message_quota)
  24190. + /* Signal the service that it
  24191. + ** has dropped below its quota
  24192. + */
  24193. + up(&service_quota->quota_event);
  24194. + else if (count == 0) {
  24195. + vchiq_log_error(vchiq_core_log_level,
  24196. + "service %d "
  24197. + "message_use_count=%d "
  24198. + "(header %x, msgid %x, "
  24199. + "header->msgid %x, "
  24200. + "header->size %x)",
  24201. + port,
  24202. + service_quota->
  24203. + message_use_count,
  24204. + (unsigned int)header, msgid,
  24205. + header->msgid,
  24206. + header->size);
  24207. + WARN(1, "invalid message use count\n");
  24208. + }
  24209. + if (!BITSET_IS_SET(service_found, port)) {
  24210. + /* Set the found bit for this service */
  24211. + BITSET_SET(service_found, port);
  24212. +
  24213. + spin_lock(&quota_spinlock);
  24214. + count = service_quota->slot_use_count;
  24215. + if (count > 0)
  24216. + service_quota->slot_use_count =
  24217. + count - 1;
  24218. + spin_unlock(&quota_spinlock);
  24219. +
  24220. + if (count > 0) {
  24221. + /* Signal the service in case
  24222. + ** it has dropped below its
  24223. + ** quota */
  24224. + up(&service_quota->quota_event);
  24225. + vchiq_log_trace(
  24226. + vchiq_core_log_level,
  24227. + "%d: pfq:%d %x@%x - "
  24228. + "slot_use->%d",
  24229. + state->id, port,
  24230. + header->size,
  24231. + (unsigned int)header,
  24232. + count - 1);
  24233. + } else {
  24234. + vchiq_log_error(
  24235. + vchiq_core_log_level,
  24236. + "service %d "
  24237. + "slot_use_count"
  24238. + "=%d (header %x"
  24239. + ", msgid %x, "
  24240. + "header->msgid"
  24241. + " %x, header->"
  24242. + "size %x)",
  24243. + port, count,
  24244. + (unsigned int)header,
  24245. + msgid,
  24246. + header->msgid,
  24247. + header->size);
  24248. + WARN(1, "bad slot use count\n");
  24249. + }
  24250. + }
  24251. +
  24252. + data_found = 1;
  24253. + }
  24254. +
  24255. + pos += calc_stride(header->size);
  24256. + if (pos > VCHIQ_SLOT_SIZE) {
  24257. + vchiq_log_error(vchiq_core_log_level,
  24258. + "pfq - pos %x: header %x, msgid %x, "
  24259. + "header->msgid %x, header->size %x",
  24260. + pos, (unsigned int)header, msgid,
  24261. + header->msgid, header->size);
  24262. + WARN(1, "invalid slot position\n");
  24263. + }
  24264. + }
  24265. +
  24266. + if (data_found) {
  24267. + int count;
  24268. + spin_lock(&quota_spinlock);
  24269. + count = state->data_use_count;
  24270. + if (count > 0)
  24271. + state->data_use_count =
  24272. + count - 1;
  24273. + spin_unlock(&quota_spinlock);
  24274. + if (count == state->data_quota)
  24275. + up(&state->data_quota_event);
  24276. + }
  24277. +
  24278. + state->slot_queue_available = slot_queue_available;
  24279. + up(&state->slot_available_event);
  24280. + }
  24281. +}
  24282. +
  24283. +/* Called by the slot handler and application threads */
  24284. +static VCHIQ_STATUS_T
  24285. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  24286. + int msgid, const VCHIQ_ELEMENT_T *elements,
  24287. + int count, int size, int is_blocking)
  24288. +{
  24289. + VCHIQ_SHARED_STATE_T *local;
  24290. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  24291. + VCHIQ_HEADER_T *header;
  24292. + int type = VCHIQ_MSG_TYPE(msgid);
  24293. +
  24294. + unsigned int stride;
  24295. +
  24296. + local = state->local;
  24297. +
  24298. + stride = calc_stride(size);
  24299. +
  24300. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  24301. +
  24302. + if ((type != VCHIQ_MSG_RESUME) &&
  24303. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  24304. + return VCHIQ_RETRY;
  24305. +
  24306. + if (type == VCHIQ_MSG_DATA) {
  24307. + int tx_end_index;
  24308. +
  24309. + BUG_ON(!service);
  24310. +
  24311. + if (service->closing) {
  24312. + /* The service has been closed */
  24313. + mutex_unlock(&state->slot_mutex);
  24314. + return VCHIQ_ERROR;
  24315. + }
  24316. +
  24317. + service_quota = &state->service_quotas[service->localport];
  24318. +
  24319. + spin_lock(&quota_spinlock);
  24320. +
  24321. + /* Ensure this service doesn't use more than its quota of
  24322. + ** messages or slots */
  24323. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  24324. + state->local_tx_pos + stride - 1);
  24325. +
  24326. + /* Ensure data messages don't use more than their quota of
  24327. + ** slots */
  24328. + while ((tx_end_index != state->previous_data_index) &&
  24329. + (state->data_use_count == state->data_quota)) {
  24330. + VCHIQ_STATS_INC(state, data_stalls);
  24331. + spin_unlock(&quota_spinlock);
  24332. + mutex_unlock(&state->slot_mutex);
  24333. +
  24334. + if (down_interruptible(&state->data_quota_event)
  24335. + != 0)
  24336. + return VCHIQ_RETRY;
  24337. +
  24338. + mutex_lock(&state->slot_mutex);
  24339. + spin_lock(&quota_spinlock);
  24340. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  24341. + state->local_tx_pos + stride - 1);
  24342. + if ((tx_end_index == state->previous_data_index) ||
  24343. + (state->data_use_count < state->data_quota)) {
  24344. + /* Pass the signal on to other waiters */
  24345. + up(&state->data_quota_event);
  24346. + break;
  24347. + }
  24348. + }
  24349. +
  24350. + while ((service_quota->message_use_count ==
  24351. + service_quota->message_quota) ||
  24352. + ((tx_end_index != service_quota->previous_tx_index) &&
  24353. + (service_quota->slot_use_count ==
  24354. + service_quota->slot_quota))) {
  24355. + spin_unlock(&quota_spinlock);
  24356. + vchiq_log_trace(vchiq_core_log_level,
  24357. + "%d: qm:%d %s,%x - quota stall "
  24358. + "(msg %d, slot %d)",
  24359. + state->id, service->localport,
  24360. + msg_type_str(type), size,
  24361. + service_quota->message_use_count,
  24362. + service_quota->slot_use_count);
  24363. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  24364. + mutex_unlock(&state->slot_mutex);
  24365. + if (down_interruptible(&service_quota->quota_event)
  24366. + != 0)
  24367. + return VCHIQ_RETRY;
  24368. + if (service->closing)
  24369. + return VCHIQ_ERROR;
  24370. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  24371. + return VCHIQ_RETRY;
  24372. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  24373. + /* The service has been closed */
  24374. + mutex_unlock(&state->slot_mutex);
  24375. + return VCHIQ_ERROR;
  24376. + }
  24377. + spin_lock(&quota_spinlock);
  24378. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  24379. + state->local_tx_pos + stride - 1);
  24380. + }
  24381. +
  24382. + spin_unlock(&quota_spinlock);
  24383. + }
  24384. +
  24385. + header = reserve_space(state, stride, is_blocking);
  24386. +
  24387. + if (!header) {
  24388. + if (service)
  24389. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  24390. + mutex_unlock(&state->slot_mutex);
  24391. + return VCHIQ_RETRY;
  24392. + }
  24393. +
  24394. + if (type == VCHIQ_MSG_DATA) {
  24395. + int i, pos;
  24396. + int tx_end_index;
  24397. + int slot_use_count;
  24398. +
  24399. + vchiq_log_info(vchiq_core_log_level,
  24400. + "%d: qm %s@%x,%x (%d->%d)",
  24401. + state->id,
  24402. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24403. + (unsigned int)header, size,
  24404. + VCHIQ_MSG_SRCPORT(msgid),
  24405. + VCHIQ_MSG_DSTPORT(msgid));
  24406. +
  24407. + BUG_ON(!service);
  24408. +
  24409. + for (i = 0, pos = 0; i < (unsigned int)count;
  24410. + pos += elements[i++].size)
  24411. + if (elements[i].size) {
  24412. + if (vchiq_copy_from_user
  24413. + (header->data + pos, elements[i].data,
  24414. + (size_t) elements[i].size) !=
  24415. + VCHIQ_SUCCESS) {
  24416. + mutex_unlock(&state->slot_mutex);
  24417. + VCHIQ_SERVICE_STATS_INC(service,
  24418. + error_count);
  24419. + return VCHIQ_ERROR;
  24420. + }
  24421. + if (i == 0) {
  24422. + if (vchiq_core_msg_log_level >=
  24423. + VCHIQ_LOG_INFO)
  24424. + vchiq_log_dump_mem("Sent", 0,
  24425. + header->data + pos,
  24426. + min(64u,
  24427. + elements[0].size));
  24428. + }
  24429. + }
  24430. +
  24431. + spin_lock(&quota_spinlock);
  24432. + service_quota->message_use_count++;
  24433. +
  24434. + tx_end_index =
  24435. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  24436. +
  24437. + /* If this transmission can't fit in the last slot used by any
  24438. + ** service, the data_use_count must be increased. */
  24439. + if (tx_end_index != state->previous_data_index) {
  24440. + state->previous_data_index = tx_end_index;
  24441. + state->data_use_count++;
  24442. + }
  24443. +
  24444. + /* If this isn't the same slot last used by this service,
  24445. + ** the service's slot_use_count must be increased. */
  24446. + if (tx_end_index != service_quota->previous_tx_index) {
  24447. + service_quota->previous_tx_index = tx_end_index;
  24448. + slot_use_count = ++service_quota->slot_use_count;
  24449. + } else {
  24450. + slot_use_count = 0;
  24451. + }
  24452. +
  24453. + spin_unlock(&quota_spinlock);
  24454. +
  24455. + if (slot_use_count)
  24456. + vchiq_log_trace(vchiq_core_log_level,
  24457. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  24458. + state->id, service->localport,
  24459. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  24460. + slot_use_count, header);
  24461. +
  24462. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  24463. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  24464. + } else {
  24465. + vchiq_log_info(vchiq_core_log_level,
  24466. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  24467. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24468. + (unsigned int)header, size,
  24469. + VCHIQ_MSG_SRCPORT(msgid),
  24470. + VCHIQ_MSG_DSTPORT(msgid));
  24471. + if (size != 0) {
  24472. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  24473. + memcpy(header->data, elements[0].data,
  24474. + elements[0].size);
  24475. + }
  24476. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  24477. + }
  24478. +
  24479. + header->msgid = msgid;
  24480. + header->size = size;
  24481. +
  24482. + {
  24483. + int svc_fourcc;
  24484. +
  24485. + svc_fourcc = service
  24486. + ? service->base.fourcc
  24487. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  24488. +
  24489. + vchiq_log_info(vchiq_core_msg_log_level,
  24490. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  24491. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24492. + VCHIQ_MSG_TYPE(msgid),
  24493. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  24494. + VCHIQ_MSG_SRCPORT(msgid),
  24495. + VCHIQ_MSG_DSTPORT(msgid),
  24496. + size);
  24497. + }
  24498. +
  24499. + /* Make sure the new header is visible to the peer. */
  24500. + wmb();
  24501. +
  24502. + /* Make the new tx_pos visible to the peer. */
  24503. + local->tx_pos = state->local_tx_pos;
  24504. + wmb();
  24505. +
  24506. + if (service && (type == VCHIQ_MSG_CLOSE))
  24507. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  24508. +
  24509. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  24510. + mutex_unlock(&state->slot_mutex);
  24511. +
  24512. + remote_event_signal(&state->remote->trigger);
  24513. +
  24514. + return VCHIQ_SUCCESS;
  24515. +}
  24516. +
  24517. +/* Called by the slot handler and application threads */
  24518. +static VCHIQ_STATUS_T
  24519. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  24520. + int msgid, const VCHIQ_ELEMENT_T *elements,
  24521. + int count, int size, int is_blocking)
  24522. +{
  24523. + VCHIQ_SHARED_STATE_T *local;
  24524. + VCHIQ_HEADER_T *header;
  24525. +
  24526. + local = state->local;
  24527. +
  24528. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  24529. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  24530. + return VCHIQ_RETRY;
  24531. +
  24532. + remote_event_wait(&local->sync_release);
  24533. +
  24534. + rmb();
  24535. +
  24536. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  24537. + local->slot_sync);
  24538. +
  24539. + {
  24540. + int oldmsgid = header->msgid;
  24541. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  24542. + vchiq_log_error(vchiq_core_log_level,
  24543. + "%d: qms - msgid %x, not PADDING",
  24544. + state->id, oldmsgid);
  24545. + }
  24546. +
  24547. + if (service) {
  24548. + int i, pos;
  24549. +
  24550. + vchiq_log_info(vchiq_sync_log_level,
  24551. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  24552. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24553. + (unsigned int)header, size,
  24554. + VCHIQ_MSG_SRCPORT(msgid),
  24555. + VCHIQ_MSG_DSTPORT(msgid));
  24556. +
  24557. + for (i = 0, pos = 0; i < (unsigned int)count;
  24558. + pos += elements[i++].size)
  24559. + if (elements[i].size) {
  24560. + if (vchiq_copy_from_user
  24561. + (header->data + pos, elements[i].data,
  24562. + (size_t) elements[i].size) !=
  24563. + VCHIQ_SUCCESS) {
  24564. + mutex_unlock(&state->sync_mutex);
  24565. + VCHIQ_SERVICE_STATS_INC(service,
  24566. + error_count);
  24567. + return VCHIQ_ERROR;
  24568. + }
  24569. + if (i == 0) {
  24570. + if (vchiq_sync_log_level >=
  24571. + VCHIQ_LOG_TRACE)
  24572. + vchiq_log_dump_mem("Sent Sync",
  24573. + 0, header->data + pos,
  24574. + min(64u,
  24575. + elements[0].size));
  24576. + }
  24577. + }
  24578. +
  24579. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  24580. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  24581. + } else {
  24582. + vchiq_log_info(vchiq_sync_log_level,
  24583. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  24584. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24585. + (unsigned int)header, size,
  24586. + VCHIQ_MSG_SRCPORT(msgid),
  24587. + VCHIQ_MSG_DSTPORT(msgid));
  24588. + if (size != 0) {
  24589. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  24590. + memcpy(header->data, elements[0].data,
  24591. + elements[0].size);
  24592. + }
  24593. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  24594. + }
  24595. +
  24596. + header->size = size;
  24597. + header->msgid = msgid;
  24598. +
  24599. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  24600. + int svc_fourcc;
  24601. +
  24602. + svc_fourcc = service
  24603. + ? service->base.fourcc
  24604. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  24605. +
  24606. + vchiq_log_trace(vchiq_sync_log_level,
  24607. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  24608. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24609. + VCHIQ_MSG_TYPE(msgid),
  24610. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  24611. + VCHIQ_MSG_SRCPORT(msgid),
  24612. + VCHIQ_MSG_DSTPORT(msgid),
  24613. + size);
  24614. + }
  24615. +
  24616. + /* Make sure the new header is visible to the peer. */
  24617. + wmb();
  24618. +
  24619. + remote_event_signal(&state->remote->sync_trigger);
  24620. +
  24621. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  24622. + mutex_unlock(&state->sync_mutex);
  24623. +
  24624. + return VCHIQ_SUCCESS;
  24625. +}
  24626. +
  24627. +static inline void
  24628. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  24629. +{
  24630. + slot->use_count++;
  24631. +}
  24632. +
  24633. +static void
  24634. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  24635. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  24636. +{
  24637. + int release_count;
  24638. +
  24639. + mutex_lock(&state->recycle_mutex);
  24640. +
  24641. + if (header) {
  24642. + int msgid = header->msgid;
  24643. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  24644. + (service && service->closing)) {
  24645. + mutex_unlock(&state->recycle_mutex);
  24646. + return;
  24647. + }
  24648. +
  24649. + /* Rewrite the message header to prevent a double
  24650. + ** release */
  24651. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  24652. + }
  24653. +
  24654. + release_count = slot_info->release_count;
  24655. + slot_info->release_count = ++release_count;
  24656. +
  24657. + if (release_count == slot_info->use_count) {
  24658. + int slot_queue_recycle;
  24659. + /* Add to the freed queue */
  24660. +
  24661. + /* A read barrier is necessary here to prevent speculative
  24662. + ** fetches of remote->slot_queue_recycle from overtaking the
  24663. + ** mutex. */
  24664. + rmb();
  24665. +
  24666. + slot_queue_recycle = state->remote->slot_queue_recycle;
  24667. + state->remote->slot_queue[slot_queue_recycle &
  24668. + VCHIQ_SLOT_QUEUE_MASK] =
  24669. + SLOT_INDEX_FROM_INFO(state, slot_info);
  24670. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  24671. + vchiq_log_info(vchiq_core_log_level,
  24672. + "%d: release_slot %d - recycle->%x",
  24673. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  24674. + state->remote->slot_queue_recycle);
  24675. +
  24676. + /* A write barrier is necessary, but remote_event_signal
  24677. + ** contains one. */
  24678. + remote_event_signal(&state->remote->recycle);
  24679. + }
  24680. +
  24681. + mutex_unlock(&state->recycle_mutex);
  24682. +}
  24683. +
  24684. +/* Called by the slot handler - don't hold the bulk mutex */
  24685. +static VCHIQ_STATUS_T
  24686. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  24687. + int retry_poll)
  24688. +{
  24689. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  24690. +
  24691. + vchiq_log_trace(vchiq_core_log_level,
  24692. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  24693. + service->state->id, service->localport,
  24694. + (queue == &service->bulk_tx) ? 't' : 'r',
  24695. + queue->process, queue->remote_notify, queue->remove);
  24696. +
  24697. + if (service->state->is_master) {
  24698. + while (queue->remote_notify != queue->process) {
  24699. + VCHIQ_BULK_T *bulk =
  24700. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  24701. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  24702. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  24703. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  24704. + service->remoteport);
  24705. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  24706. + /* Only reply to non-dummy bulk requests */
  24707. + if (bulk->remote_data) {
  24708. + status = queue_message(service->state, NULL,
  24709. + msgid, &element, 1, 4, 0);
  24710. + if (status != VCHIQ_SUCCESS)
  24711. + break;
  24712. + }
  24713. + queue->remote_notify++;
  24714. + }
  24715. + } else {
  24716. + queue->remote_notify = queue->process;
  24717. + }
  24718. +
  24719. + if (status == VCHIQ_SUCCESS) {
  24720. + while (queue->remove != queue->remote_notify) {
  24721. + VCHIQ_BULK_T *bulk =
  24722. + &queue->bulks[BULK_INDEX(queue->remove)];
  24723. +
  24724. + /* Only generate callbacks for non-dummy bulk
  24725. + ** requests, and non-terminated services */
  24726. + if (bulk->data && service->instance) {
  24727. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  24728. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  24729. + VCHIQ_SERVICE_STATS_INC(service,
  24730. + bulk_tx_count);
  24731. + VCHIQ_SERVICE_STATS_ADD(service,
  24732. + bulk_tx_bytes,
  24733. + bulk->actual);
  24734. + } else {
  24735. + VCHIQ_SERVICE_STATS_INC(service,
  24736. + bulk_rx_count);
  24737. + VCHIQ_SERVICE_STATS_ADD(service,
  24738. + bulk_rx_bytes,
  24739. + bulk->actual);
  24740. + }
  24741. + } else {
  24742. + VCHIQ_SERVICE_STATS_INC(service,
  24743. + bulk_aborted_count);
  24744. + }
  24745. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  24746. + struct bulk_waiter *waiter;
  24747. + spin_lock(&bulk_waiter_spinlock);
  24748. + waiter = bulk->userdata;
  24749. + if (waiter) {
  24750. + waiter->actual = bulk->actual;
  24751. + up(&waiter->event);
  24752. + }
  24753. + spin_unlock(&bulk_waiter_spinlock);
  24754. + } else if (bulk->mode ==
  24755. + VCHIQ_BULK_MODE_CALLBACK) {
  24756. + VCHIQ_REASON_T reason = (bulk->dir ==
  24757. + VCHIQ_BULK_TRANSMIT) ?
  24758. + ((bulk->actual ==
  24759. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  24760. + VCHIQ_BULK_TRANSMIT_ABORTED :
  24761. + VCHIQ_BULK_TRANSMIT_DONE) :
  24762. + ((bulk->actual ==
  24763. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  24764. + VCHIQ_BULK_RECEIVE_ABORTED :
  24765. + VCHIQ_BULK_RECEIVE_DONE);
  24766. + status = make_service_callback(service,
  24767. + reason, NULL, bulk->userdata);
  24768. + if (status == VCHIQ_RETRY)
  24769. + break;
  24770. + }
  24771. + }
  24772. +
  24773. + queue->remove++;
  24774. + up(&service->bulk_remove_event);
  24775. + }
  24776. + if (!retry_poll)
  24777. + status = VCHIQ_SUCCESS;
  24778. + }
  24779. +
  24780. + if (status == VCHIQ_RETRY)
  24781. + request_poll(service->state, service,
  24782. + (queue == &service->bulk_tx) ?
  24783. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  24784. +
  24785. + return status;
  24786. +}
  24787. +
  24788. +/* Called by the slot handler thread */
  24789. +static void
  24790. +poll_services(VCHIQ_STATE_T *state)
  24791. +{
  24792. + int group, i;
  24793. +
  24794. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  24795. + uint32_t flags;
  24796. + flags = atomic_xchg(&state->poll_services[group], 0);
  24797. + for (i = 0; flags; i++) {
  24798. + if (flags & (1 << i)) {
  24799. + VCHIQ_SERVICE_T *service =
  24800. + find_service_by_port(state,
  24801. + (group<<5) + i);
  24802. + uint32_t service_flags;
  24803. + flags &= ~(1 << i);
  24804. + if (!service)
  24805. + continue;
  24806. + service_flags =
  24807. + atomic_xchg(&service->poll_flags, 0);
  24808. + if (service_flags &
  24809. + (1 << VCHIQ_POLL_REMOVE)) {
  24810. + vchiq_log_info(vchiq_core_log_level,
  24811. + "%d: ps - remove %d<->%d",
  24812. + state->id, service->localport,
  24813. + service->remoteport);
  24814. +
  24815. + /* Make it look like a client, because
  24816. + it must be removed and not left in
  24817. + the LISTENING state. */
  24818. + service->public_fourcc =
  24819. + VCHIQ_FOURCC_INVALID;
  24820. +
  24821. + if (vchiq_close_service_internal(
  24822. + service, 0/*!close_recvd*/) !=
  24823. + VCHIQ_SUCCESS)
  24824. + request_poll(state, service,
  24825. + VCHIQ_POLL_REMOVE);
  24826. + } else if (service_flags &
  24827. + (1 << VCHIQ_POLL_TERMINATE)) {
  24828. + vchiq_log_info(vchiq_core_log_level,
  24829. + "%d: ps - terminate %d<->%d",
  24830. + state->id, service->localport,
  24831. + service->remoteport);
  24832. + if (vchiq_close_service_internal(
  24833. + service, 0/*!close_recvd*/) !=
  24834. + VCHIQ_SUCCESS)
  24835. + request_poll(state, service,
  24836. + VCHIQ_POLL_TERMINATE);
  24837. + }
  24838. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  24839. + notify_bulks(service,
  24840. + &service->bulk_tx,
  24841. + 1/*retry_poll*/);
  24842. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  24843. + notify_bulks(service,
  24844. + &service->bulk_rx,
  24845. + 1/*retry_poll*/);
  24846. + unlock_service(service);
  24847. + }
  24848. + }
  24849. + }
  24850. +}
  24851. +
  24852. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  24853. +static int
  24854. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  24855. +{
  24856. + VCHIQ_STATE_T *state = service->state;
  24857. + int resolved = 0;
  24858. + int rc;
  24859. +
  24860. + while ((queue->process != queue->local_insert) &&
  24861. + (queue->process != queue->remote_insert)) {
  24862. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  24863. +
  24864. + vchiq_log_trace(vchiq_core_log_level,
  24865. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  24866. + state->id, service->localport,
  24867. + (queue == &service->bulk_tx) ? 't' : 'r',
  24868. + queue->local_insert, queue->remote_insert,
  24869. + queue->process);
  24870. +
  24871. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  24872. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  24873. +
  24874. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  24875. + if (rc != 0)
  24876. + break;
  24877. +
  24878. + vchiq_transfer_bulk(bulk);
  24879. + mutex_unlock(&state->bulk_transfer_mutex);
  24880. +
  24881. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  24882. + const char *header = (queue == &service->bulk_tx) ?
  24883. + "Send Bulk to" : "Recv Bulk from";
  24884. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  24885. + vchiq_log_info(vchiq_core_msg_log_level,
  24886. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  24887. + header,
  24888. + VCHIQ_FOURCC_AS_4CHARS(
  24889. + service->base.fourcc),
  24890. + service->remoteport,
  24891. + bulk->size,
  24892. + (unsigned int)bulk->data,
  24893. + (unsigned int)bulk->remote_data);
  24894. + else
  24895. + vchiq_log_info(vchiq_core_msg_log_level,
  24896. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  24897. + " rx len:%d %x<->%x",
  24898. + header,
  24899. + VCHIQ_FOURCC_AS_4CHARS(
  24900. + service->base.fourcc),
  24901. + service->remoteport,
  24902. + bulk->size,
  24903. + bulk->remote_size,
  24904. + (unsigned int)bulk->data,
  24905. + (unsigned int)bulk->remote_data);
  24906. + }
  24907. +
  24908. + vchiq_complete_bulk(bulk);
  24909. + queue->process++;
  24910. + resolved++;
  24911. + }
  24912. + return resolved;
  24913. +}
  24914. +
  24915. +/* Called with the bulk_mutex held */
  24916. +static void
  24917. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  24918. +{
  24919. + int is_tx = (queue == &service->bulk_tx);
  24920. + vchiq_log_trace(vchiq_core_log_level,
  24921. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  24922. + service->state->id, service->localport, is_tx ? 't' : 'r',
  24923. + queue->local_insert, queue->remote_insert, queue->process);
  24924. +
  24925. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  24926. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  24927. +
  24928. + while ((queue->process != queue->local_insert) ||
  24929. + (queue->process != queue->remote_insert)) {
  24930. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  24931. +
  24932. + if (queue->process == queue->remote_insert) {
  24933. + /* fabricate a matching dummy bulk */
  24934. + bulk->remote_data = NULL;
  24935. + bulk->remote_size = 0;
  24936. + queue->remote_insert++;
  24937. + }
  24938. +
  24939. + if (queue->process != queue->local_insert) {
  24940. + vchiq_complete_bulk(bulk);
  24941. +
  24942. + vchiq_log_info(vchiq_core_msg_log_level,
  24943. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  24944. + "rx len:%d",
  24945. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  24946. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  24947. + service->remoteport,
  24948. + bulk->size,
  24949. + bulk->remote_size);
  24950. + } else {
  24951. + /* fabricate a matching dummy bulk */
  24952. + bulk->data = NULL;
  24953. + bulk->size = 0;
  24954. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  24955. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  24956. + VCHIQ_BULK_RECEIVE;
  24957. + queue->local_insert++;
  24958. + }
  24959. +
  24960. + queue->process++;
  24961. + }
  24962. +}
  24963. +
  24964. +/* Called from the slot handler thread */
  24965. +static void
  24966. +pause_bulks(VCHIQ_STATE_T *state)
  24967. +{
  24968. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  24969. + WARN_ON_ONCE(1);
  24970. + atomic_set(&pause_bulks_count, 1);
  24971. + return;
  24972. + }
  24973. +
  24974. + /* Block bulk transfers from all services */
  24975. + mutex_lock(&state->bulk_transfer_mutex);
  24976. +}
  24977. +
  24978. +/* Called from the slot handler thread */
  24979. +static void
  24980. +resume_bulks(VCHIQ_STATE_T *state)
  24981. +{
  24982. + int i;
  24983. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  24984. + WARN_ON_ONCE(1);
  24985. + atomic_set(&pause_bulks_count, 0);
  24986. + return;
  24987. + }
  24988. +
  24989. + /* Allow bulk transfers from all services */
  24990. + mutex_unlock(&state->bulk_transfer_mutex);
  24991. +
  24992. + if (state->deferred_bulks == 0)
  24993. + return;
  24994. +
  24995. + /* Deal with any bulks which had to be deferred due to being in
  24996. + * paused state. Don't try to match up to number of deferred bulks
  24997. + * in case we've had something come and close the service in the
  24998. + * interim - just process all bulk queues for all services */
  24999. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  25000. + __func__, state->deferred_bulks);
  25001. +
  25002. + for (i = 0; i < state->unused_service; i++) {
  25003. + VCHIQ_SERVICE_T *service = state->services[i];
  25004. + int resolved_rx = 0;
  25005. + int resolved_tx = 0;
  25006. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  25007. + continue;
  25008. +
  25009. + mutex_lock(&service->bulk_mutex);
  25010. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  25011. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  25012. + mutex_unlock(&service->bulk_mutex);
  25013. + if (resolved_rx)
  25014. + notify_bulks(service, &service->bulk_rx, 1);
  25015. + if (resolved_tx)
  25016. + notify_bulks(service, &service->bulk_tx, 1);
  25017. + }
  25018. + state->deferred_bulks = 0;
  25019. +}
  25020. +
  25021. +static int
  25022. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  25023. +{
  25024. + VCHIQ_SERVICE_T *service = NULL;
  25025. + int msgid, size;
  25026. + int type;
  25027. + unsigned int localport, remoteport;
  25028. +
  25029. + msgid = header->msgid;
  25030. + size = header->size;
  25031. + type = VCHIQ_MSG_TYPE(msgid);
  25032. + localport = VCHIQ_MSG_DSTPORT(msgid);
  25033. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  25034. + if (size >= sizeof(struct vchiq_open_payload)) {
  25035. + const struct vchiq_open_payload *payload =
  25036. + (struct vchiq_open_payload *)header->data;
  25037. + unsigned int fourcc;
  25038. +
  25039. + fourcc = payload->fourcc;
  25040. + vchiq_log_info(vchiq_core_log_level,
  25041. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  25042. + state->id, (unsigned int)header,
  25043. + localport,
  25044. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  25045. +
  25046. + service = get_listening_service(state, fourcc);
  25047. +
  25048. + if (service) {
  25049. + /* A matching service exists */
  25050. + short version = payload->version;
  25051. + short version_min = payload->version_min;
  25052. + if ((service->version < version_min) ||
  25053. + (version < service->version_min)) {
  25054. + /* Version mismatch */
  25055. + vchiq_loud_error_header();
  25056. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  25057. + "version mismatch - local (%d, min %d)"
  25058. + " vs. remote (%d, min %d)",
  25059. + state->id, service->localport,
  25060. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  25061. + service->version, service->version_min,
  25062. + version, version_min);
  25063. + vchiq_loud_error_footer();
  25064. + unlock_service(service);
  25065. + service = NULL;
  25066. + goto fail_open;
  25067. + }
  25068. + service->peer_version = version;
  25069. +
  25070. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  25071. + struct vchiq_openack_payload ack_payload = {
  25072. + service->version
  25073. + };
  25074. + VCHIQ_ELEMENT_T body = {
  25075. + &ack_payload,
  25076. + sizeof(ack_payload)
  25077. + };
  25078. +
  25079. + /* Acknowledge the OPEN */
  25080. + if (service->sync) {
  25081. + if (queue_message_sync(state, NULL,
  25082. + VCHIQ_MAKE_MSG(
  25083. + VCHIQ_MSG_OPENACK,
  25084. + service->localport,
  25085. + remoteport),
  25086. + &body, 1, sizeof(ack_payload),
  25087. + 0) == VCHIQ_RETRY)
  25088. + goto bail_not_ready;
  25089. + } else {
  25090. + if (queue_message(state, NULL,
  25091. + VCHIQ_MAKE_MSG(
  25092. + VCHIQ_MSG_OPENACK,
  25093. + service->localport,
  25094. + remoteport),
  25095. + &body, 1, sizeof(ack_payload),
  25096. + 0) == VCHIQ_RETRY)
  25097. + goto bail_not_ready;
  25098. + }
  25099. +
  25100. + /* The service is now open */
  25101. + vchiq_set_service_state(service,
  25102. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  25103. + : VCHIQ_SRVSTATE_OPEN);
  25104. + }
  25105. +
  25106. + service->remoteport = remoteport;
  25107. + service->client_id = ((int *)header->data)[1];
  25108. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  25109. + NULL, NULL) == VCHIQ_RETRY) {
  25110. + /* Bail out if not ready */
  25111. + service->remoteport = VCHIQ_PORT_FREE;
  25112. + goto bail_not_ready;
  25113. + }
  25114. +
  25115. + /* Success - the message has been dealt with */
  25116. + unlock_service(service);
  25117. + return 1;
  25118. + }
  25119. + }
  25120. +
  25121. +fail_open:
  25122. + /* No available service, or an invalid request - send a CLOSE */
  25123. + if (queue_message(state, NULL,
  25124. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  25125. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  25126. + goto bail_not_ready;
  25127. +
  25128. + return 1;
  25129. +
  25130. +bail_not_ready:
  25131. + if (service)
  25132. + unlock_service(service);
  25133. +
  25134. + return 0;
  25135. +}
  25136. +
  25137. +/* Called by the slot handler thread */
  25138. +static void
  25139. +parse_rx_slots(VCHIQ_STATE_T *state)
  25140. +{
  25141. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  25142. + VCHIQ_SERVICE_T *service = NULL;
  25143. + int tx_pos;
  25144. + DEBUG_INITIALISE(state->local)
  25145. +
  25146. + tx_pos = remote->tx_pos;
  25147. +
  25148. + while (state->rx_pos != tx_pos) {
  25149. + VCHIQ_HEADER_T *header;
  25150. + int msgid, size;
  25151. + int type;
  25152. + unsigned int localport, remoteport;
  25153. +
  25154. + DEBUG_TRACE(PARSE_LINE);
  25155. + if (!state->rx_data) {
  25156. + int rx_index;
  25157. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  25158. + rx_index = remote->slot_queue[
  25159. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  25160. + VCHIQ_SLOT_QUEUE_MASK];
  25161. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  25162. + rx_index);
  25163. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  25164. +
  25165. + /* Initialise use_count to one, and increment
  25166. + ** release_count at the end of the slot to avoid
  25167. + ** releasing the slot prematurely. */
  25168. + state->rx_info->use_count = 1;
  25169. + state->rx_info->release_count = 0;
  25170. + }
  25171. +
  25172. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  25173. + (state->rx_pos & VCHIQ_SLOT_MASK));
  25174. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  25175. + msgid = header->msgid;
  25176. + DEBUG_VALUE(PARSE_MSGID, msgid);
  25177. + size = header->size;
  25178. + type = VCHIQ_MSG_TYPE(msgid);
  25179. + localport = VCHIQ_MSG_DSTPORT(msgid);
  25180. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  25181. +
  25182. + if (type != VCHIQ_MSG_DATA)
  25183. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  25184. +
  25185. + switch (type) {
  25186. + case VCHIQ_MSG_OPENACK:
  25187. + case VCHIQ_MSG_CLOSE:
  25188. + case VCHIQ_MSG_DATA:
  25189. + case VCHIQ_MSG_BULK_RX:
  25190. + case VCHIQ_MSG_BULK_TX:
  25191. + case VCHIQ_MSG_BULK_RX_DONE:
  25192. + case VCHIQ_MSG_BULK_TX_DONE:
  25193. + service = find_service_by_port(state, localport);
  25194. + if ((!service || service->remoteport != remoteport) &&
  25195. + (localport == 0) &&
  25196. + (type == VCHIQ_MSG_CLOSE)) {
  25197. + /* This could be a CLOSE from a client which
  25198. + hadn't yet received the OPENACK - look for
  25199. + the connected service */
  25200. + if (service)
  25201. + unlock_service(service);
  25202. + service = get_connected_service(state,
  25203. + remoteport);
  25204. + if (service)
  25205. + vchiq_log_warning(vchiq_core_log_level,
  25206. + "%d: prs %s@%x (%d->%d) - "
  25207. + "found connected service %d",
  25208. + state->id, msg_type_str(type),
  25209. + (unsigned int)header,
  25210. + remoteport, localport,
  25211. + service->localport);
  25212. + }
  25213. +
  25214. + if (!service) {
  25215. + vchiq_log_error(vchiq_core_log_level,
  25216. + "%d: prs %s@%x (%d->%d) - "
  25217. + "invalid/closed service %d",
  25218. + state->id, msg_type_str(type),
  25219. + (unsigned int)header,
  25220. + remoteport, localport, localport);
  25221. + goto skip_message;
  25222. + }
  25223. + break;
  25224. + default:
  25225. + break;
  25226. + }
  25227. +
  25228. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  25229. + int svc_fourcc;
  25230. +
  25231. + svc_fourcc = service
  25232. + ? service->base.fourcc
  25233. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  25234. + vchiq_log_info(vchiq_core_msg_log_level,
  25235. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  25236. + "len:%d",
  25237. + msg_type_str(type), type,
  25238. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  25239. + remoteport, localport, size);
  25240. + if (size > 0)
  25241. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  25242. + min(64, size));
  25243. + }
  25244. +
  25245. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  25246. + > VCHIQ_SLOT_SIZE) {
  25247. + vchiq_log_error(vchiq_core_log_level,
  25248. + "header %x (msgid %x) - size %x too big for "
  25249. + "slot",
  25250. + (unsigned int)header, (unsigned int)msgid,
  25251. + (unsigned int)size);
  25252. + WARN(1, "oversized for slot\n");
  25253. + }
  25254. +
  25255. + switch (type) {
  25256. + case VCHIQ_MSG_OPEN:
  25257. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  25258. + if (!parse_open(state, header))
  25259. + goto bail_not_ready;
  25260. + break;
  25261. + case VCHIQ_MSG_OPENACK:
  25262. + if (size >= sizeof(struct vchiq_openack_payload)) {
  25263. + const struct vchiq_openack_payload *payload =
  25264. + (struct vchiq_openack_payload *)
  25265. + header->data;
  25266. + service->peer_version = payload->version;
  25267. + }
  25268. + vchiq_log_info(vchiq_core_log_level,
  25269. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  25270. + state->id, (unsigned int)header, size,
  25271. + remoteport, localport, service->peer_version);
  25272. + if (service->srvstate ==
  25273. + VCHIQ_SRVSTATE_OPENING) {
  25274. + service->remoteport = remoteport;
  25275. + vchiq_set_service_state(service,
  25276. + VCHIQ_SRVSTATE_OPEN);
  25277. + up(&service->remove_event);
  25278. + } else
  25279. + vchiq_log_error(vchiq_core_log_level,
  25280. + "OPENACK received in state %s",
  25281. + srvstate_names[service->srvstate]);
  25282. + break;
  25283. + case VCHIQ_MSG_CLOSE:
  25284. + WARN_ON(size != 0); /* There should be no data */
  25285. +
  25286. + vchiq_log_info(vchiq_core_log_level,
  25287. + "%d: prs CLOSE@%x (%d->%d)",
  25288. + state->id, (unsigned int)header,
  25289. + remoteport, localport);
  25290. +
  25291. + mark_service_closing_internal(service, 1);
  25292. +
  25293. + if (vchiq_close_service_internal(service,
  25294. + 1/*close_recvd*/) == VCHIQ_RETRY)
  25295. + goto bail_not_ready;
  25296. +
  25297. + vchiq_log_info(vchiq_core_log_level,
  25298. + "Close Service %c%c%c%c s:%u d:%d",
  25299. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  25300. + service->localport,
  25301. + service->remoteport);
  25302. + break;
  25303. + case VCHIQ_MSG_DATA:
  25304. + vchiq_log_trace(vchiq_core_log_level,
  25305. + "%d: prs DATA@%x,%x (%d->%d)",
  25306. + state->id, (unsigned int)header, size,
  25307. + remoteport, localport);
  25308. +
  25309. + if ((service->remoteport == remoteport)
  25310. + && (service->srvstate ==
  25311. + VCHIQ_SRVSTATE_OPEN)) {
  25312. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  25313. + claim_slot(state->rx_info);
  25314. + DEBUG_TRACE(PARSE_LINE);
  25315. + if (make_service_callback(service,
  25316. + VCHIQ_MESSAGE_AVAILABLE, header,
  25317. + NULL) == VCHIQ_RETRY) {
  25318. + DEBUG_TRACE(PARSE_LINE);
  25319. + goto bail_not_ready;
  25320. + }
  25321. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  25322. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  25323. + size);
  25324. + } else {
  25325. + VCHIQ_STATS_INC(state, error_count);
  25326. + }
  25327. + break;
  25328. + case VCHIQ_MSG_CONNECT:
  25329. + vchiq_log_info(vchiq_core_log_level,
  25330. + "%d: prs CONNECT@%x",
  25331. + state->id, (unsigned int)header);
  25332. + up(&state->connect);
  25333. + break;
  25334. + case VCHIQ_MSG_BULK_RX:
  25335. + case VCHIQ_MSG_BULK_TX: {
  25336. + VCHIQ_BULK_QUEUE_T *queue;
  25337. + WARN_ON(!state->is_master);
  25338. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  25339. + &service->bulk_tx : &service->bulk_rx;
  25340. + if ((service->remoteport == remoteport)
  25341. + && (service->srvstate ==
  25342. + VCHIQ_SRVSTATE_OPEN)) {
  25343. + VCHIQ_BULK_T *bulk;
  25344. + int resolved = 0;
  25345. +
  25346. + DEBUG_TRACE(PARSE_LINE);
  25347. + if (mutex_lock_interruptible(
  25348. + &service->bulk_mutex) != 0) {
  25349. + DEBUG_TRACE(PARSE_LINE);
  25350. + goto bail_not_ready;
  25351. + }
  25352. +
  25353. + WARN_ON(!(queue->remote_insert < queue->remove +
  25354. + VCHIQ_NUM_SERVICE_BULKS));
  25355. + bulk = &queue->bulks[
  25356. + BULK_INDEX(queue->remote_insert)];
  25357. + bulk->remote_data =
  25358. + (void *)((int *)header->data)[0];
  25359. + bulk->remote_size = ((int *)header->data)[1];
  25360. + wmb();
  25361. +
  25362. + vchiq_log_info(vchiq_core_log_level,
  25363. + "%d: prs %s@%x (%d->%d) %x@%x",
  25364. + state->id, msg_type_str(type),
  25365. + (unsigned int)header,
  25366. + remoteport, localport,
  25367. + bulk->remote_size,
  25368. + (unsigned int)bulk->remote_data);
  25369. +
  25370. + queue->remote_insert++;
  25371. +
  25372. + if (atomic_read(&pause_bulks_count)) {
  25373. + state->deferred_bulks++;
  25374. + vchiq_log_info(vchiq_core_log_level,
  25375. + "%s: deferring bulk (%d)",
  25376. + __func__,
  25377. + state->deferred_bulks);
  25378. + if (state->conn_state !=
  25379. + VCHIQ_CONNSTATE_PAUSE_SENT)
  25380. + vchiq_log_error(
  25381. + vchiq_core_log_level,
  25382. + "%s: bulks paused in "
  25383. + "unexpected state %s",
  25384. + __func__,
  25385. + conn_state_names[
  25386. + state->conn_state]);
  25387. + } else if (state->conn_state ==
  25388. + VCHIQ_CONNSTATE_CONNECTED) {
  25389. + DEBUG_TRACE(PARSE_LINE);
  25390. + resolved = resolve_bulks(service,
  25391. + queue);
  25392. + }
  25393. +
  25394. + mutex_unlock(&service->bulk_mutex);
  25395. + if (resolved)
  25396. + notify_bulks(service, queue,
  25397. + 1/*retry_poll*/);
  25398. + }
  25399. + } break;
  25400. + case VCHIQ_MSG_BULK_RX_DONE:
  25401. + case VCHIQ_MSG_BULK_TX_DONE:
  25402. + WARN_ON(state->is_master);
  25403. + if ((service->remoteport == remoteport)
  25404. + && (service->srvstate !=
  25405. + VCHIQ_SRVSTATE_FREE)) {
  25406. + VCHIQ_BULK_QUEUE_T *queue;
  25407. + VCHIQ_BULK_T *bulk;
  25408. +
  25409. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  25410. + &service->bulk_rx : &service->bulk_tx;
  25411. +
  25412. + DEBUG_TRACE(PARSE_LINE);
  25413. + if (mutex_lock_interruptible(
  25414. + &service->bulk_mutex) != 0) {
  25415. + DEBUG_TRACE(PARSE_LINE);
  25416. + goto bail_not_ready;
  25417. + }
  25418. + if ((int)(queue->remote_insert -
  25419. + queue->local_insert) >= 0) {
  25420. + vchiq_log_error(vchiq_core_log_level,
  25421. + "%d: prs %s@%x (%d->%d) "
  25422. + "unexpected (ri=%d,li=%d)",
  25423. + state->id, msg_type_str(type),
  25424. + (unsigned int)header,
  25425. + remoteport, localport,
  25426. + queue->remote_insert,
  25427. + queue->local_insert);
  25428. + mutex_unlock(&service->bulk_mutex);
  25429. + break;
  25430. + }
  25431. +
  25432. + BUG_ON(queue->process == queue->local_insert);
  25433. + BUG_ON(queue->process != queue->remote_insert);
  25434. +
  25435. + bulk = &queue->bulks[
  25436. + BULK_INDEX(queue->remote_insert)];
  25437. + bulk->actual = *(int *)header->data;
  25438. + queue->remote_insert++;
  25439. +
  25440. + vchiq_log_info(vchiq_core_log_level,
  25441. + "%d: prs %s@%x (%d->%d) %x@%x",
  25442. + state->id, msg_type_str(type),
  25443. + (unsigned int)header,
  25444. + remoteport, localport,
  25445. + bulk->actual, (unsigned int)bulk->data);
  25446. +
  25447. + vchiq_log_trace(vchiq_core_log_level,
  25448. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  25449. + state->id, localport,
  25450. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  25451. + 'r' : 't',
  25452. + queue->local_insert,
  25453. + queue->remote_insert, queue->process);
  25454. +
  25455. + DEBUG_TRACE(PARSE_LINE);
  25456. + WARN_ON(queue->process == queue->local_insert);
  25457. + vchiq_complete_bulk(bulk);
  25458. + queue->process++;
  25459. + mutex_unlock(&service->bulk_mutex);
  25460. + DEBUG_TRACE(PARSE_LINE);
  25461. + notify_bulks(service, queue, 1/*retry_poll*/);
  25462. + DEBUG_TRACE(PARSE_LINE);
  25463. + }
  25464. + break;
  25465. + case VCHIQ_MSG_PADDING:
  25466. + vchiq_log_trace(vchiq_core_log_level,
  25467. + "%d: prs PADDING@%x,%x",
  25468. + state->id, (unsigned int)header, size);
  25469. + break;
  25470. + case VCHIQ_MSG_PAUSE:
  25471. + /* If initiated, signal the application thread */
  25472. + vchiq_log_trace(vchiq_core_log_level,
  25473. + "%d: prs PAUSE@%x,%x",
  25474. + state->id, (unsigned int)header, size);
  25475. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  25476. + vchiq_log_error(vchiq_core_log_level,
  25477. + "%d: PAUSE received in state PAUSED",
  25478. + state->id);
  25479. + break;
  25480. + }
  25481. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  25482. + /* Send a PAUSE in response */
  25483. + if (queue_message(state, NULL,
  25484. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  25485. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  25486. + goto bail_not_ready;
  25487. + if (state->is_master)
  25488. + pause_bulks(state);
  25489. + }
  25490. + /* At this point slot_mutex is held */
  25491. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  25492. + vchiq_platform_paused(state);
  25493. + break;
  25494. + case VCHIQ_MSG_RESUME:
  25495. + vchiq_log_trace(vchiq_core_log_level,
  25496. + "%d: prs RESUME@%x,%x",
  25497. + state->id, (unsigned int)header, size);
  25498. + /* Release the slot mutex */
  25499. + mutex_unlock(&state->slot_mutex);
  25500. + if (state->is_master)
  25501. + resume_bulks(state);
  25502. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  25503. + vchiq_platform_resumed(state);
  25504. + break;
  25505. +
  25506. + case VCHIQ_MSG_REMOTE_USE:
  25507. + vchiq_on_remote_use(state);
  25508. + break;
  25509. + case VCHIQ_MSG_REMOTE_RELEASE:
  25510. + vchiq_on_remote_release(state);
  25511. + break;
  25512. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  25513. + vchiq_on_remote_use_active(state);
  25514. + break;
  25515. +
  25516. + default:
  25517. + vchiq_log_error(vchiq_core_log_level,
  25518. + "%d: prs invalid msgid %x@%x,%x",
  25519. + state->id, msgid, (unsigned int)header, size);
  25520. + WARN(1, "invalid message\n");
  25521. + break;
  25522. + }
  25523. +
  25524. +skip_message:
  25525. + if (service) {
  25526. + unlock_service(service);
  25527. + service = NULL;
  25528. + }
  25529. +
  25530. + state->rx_pos += calc_stride(size);
  25531. +
  25532. + DEBUG_TRACE(PARSE_LINE);
  25533. + /* Perform some housekeeping when the end of the slot is
  25534. + ** reached. */
  25535. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  25536. + /* Remove the extra reference count. */
  25537. + release_slot(state, state->rx_info, NULL, NULL);
  25538. + state->rx_data = NULL;
  25539. + }
  25540. + }
  25541. +
  25542. +bail_not_ready:
  25543. + if (service)
  25544. + unlock_service(service);
  25545. +}
  25546. +
  25547. +/* Called by the slot handler thread */
  25548. +static int
  25549. +slot_handler_func(void *v)
  25550. +{
  25551. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25552. + VCHIQ_SHARED_STATE_T *local = state->local;
  25553. + DEBUG_INITIALISE(local)
  25554. +
  25555. + while (1) {
  25556. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  25557. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  25558. + remote_event_wait(&local->trigger);
  25559. +
  25560. + rmb();
  25561. +
  25562. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  25563. + if (state->poll_needed) {
  25564. + /* Check if we need to suspend - may change our
  25565. + * conn_state */
  25566. + vchiq_platform_check_suspend(state);
  25567. +
  25568. + state->poll_needed = 0;
  25569. +
  25570. + /* Handle service polling and other rare conditions here
  25571. + ** out of the mainline code */
  25572. + switch (state->conn_state) {
  25573. + case VCHIQ_CONNSTATE_CONNECTED:
  25574. + /* Poll the services as requested */
  25575. + poll_services(state);
  25576. + break;
  25577. +
  25578. + case VCHIQ_CONNSTATE_PAUSING:
  25579. + if (state->is_master)
  25580. + pause_bulks(state);
  25581. + if (queue_message(state, NULL,
  25582. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  25583. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  25584. + vchiq_set_conn_state(state,
  25585. + VCHIQ_CONNSTATE_PAUSE_SENT);
  25586. + } else {
  25587. + if (state->is_master)
  25588. + resume_bulks(state);
  25589. + /* Retry later */
  25590. + state->poll_needed = 1;
  25591. + }
  25592. + break;
  25593. +
  25594. + case VCHIQ_CONNSTATE_PAUSED:
  25595. + vchiq_platform_resume(state);
  25596. + break;
  25597. +
  25598. + case VCHIQ_CONNSTATE_RESUMING:
  25599. + if (queue_message(state, NULL,
  25600. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  25601. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  25602. + if (state->is_master)
  25603. + resume_bulks(state);
  25604. + vchiq_set_conn_state(state,
  25605. + VCHIQ_CONNSTATE_CONNECTED);
  25606. + vchiq_platform_resumed(state);
  25607. + } else {
  25608. + /* This should really be impossible,
  25609. + ** since the PAUSE should have flushed
  25610. + ** through outstanding messages. */
  25611. + vchiq_log_error(vchiq_core_log_level,
  25612. + "Failed to send RESUME "
  25613. + "message");
  25614. + BUG();
  25615. + }
  25616. + break;
  25617. +
  25618. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  25619. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  25620. + vchiq_platform_handle_timeout(state);
  25621. + break;
  25622. + default:
  25623. + break;
  25624. + }
  25625. +
  25626. +
  25627. + }
  25628. +
  25629. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  25630. + parse_rx_slots(state);
  25631. + }
  25632. + return 0;
  25633. +}
  25634. +
  25635. +
  25636. +/* Called by the recycle thread */
  25637. +static int
  25638. +recycle_func(void *v)
  25639. +{
  25640. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25641. + VCHIQ_SHARED_STATE_T *local = state->local;
  25642. +
  25643. + while (1) {
  25644. + remote_event_wait(&local->recycle);
  25645. +
  25646. + process_free_queue(state);
  25647. + }
  25648. + return 0;
  25649. +}
  25650. +
  25651. +
  25652. +/* Called by the sync thread */
  25653. +static int
  25654. +sync_func(void *v)
  25655. +{
  25656. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25657. + VCHIQ_SHARED_STATE_T *local = state->local;
  25658. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  25659. + state->remote->slot_sync);
  25660. +
  25661. + while (1) {
  25662. + VCHIQ_SERVICE_T *service;
  25663. + int msgid, size;
  25664. + int type;
  25665. + unsigned int localport, remoteport;
  25666. +
  25667. + remote_event_wait(&local->sync_trigger);
  25668. +
  25669. + rmb();
  25670. +
  25671. + msgid = header->msgid;
  25672. + size = header->size;
  25673. + type = VCHIQ_MSG_TYPE(msgid);
  25674. + localport = VCHIQ_MSG_DSTPORT(msgid);
  25675. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  25676. +
  25677. + service = find_service_by_port(state, localport);
  25678. +
  25679. + if (!service) {
  25680. + vchiq_log_error(vchiq_sync_log_level,
  25681. + "%d: sf %s@%x (%d->%d) - "
  25682. + "invalid/closed service %d",
  25683. + state->id, msg_type_str(type),
  25684. + (unsigned int)header,
  25685. + remoteport, localport, localport);
  25686. + release_message_sync(state, header);
  25687. + continue;
  25688. + }
  25689. +
  25690. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  25691. + int svc_fourcc;
  25692. +
  25693. + svc_fourcc = service
  25694. + ? service->base.fourcc
  25695. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  25696. + vchiq_log_trace(vchiq_sync_log_level,
  25697. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  25698. + msg_type_str(type),
  25699. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  25700. + remoteport, localport, size);
  25701. + if (size > 0)
  25702. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  25703. + min(64, size));
  25704. + }
  25705. +
  25706. + switch (type) {
  25707. + case VCHIQ_MSG_OPENACK:
  25708. + if (size >= sizeof(struct vchiq_openack_payload)) {
  25709. + const struct vchiq_openack_payload *payload =
  25710. + (struct vchiq_openack_payload *)
  25711. + header->data;
  25712. + service->peer_version = payload->version;
  25713. + }
  25714. + vchiq_log_info(vchiq_sync_log_level,
  25715. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  25716. + state->id, (unsigned int)header, size,
  25717. + remoteport, localport, service->peer_version);
  25718. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  25719. + service->remoteport = remoteport;
  25720. + vchiq_set_service_state(service,
  25721. + VCHIQ_SRVSTATE_OPENSYNC);
  25722. + up(&service->remove_event);
  25723. + }
  25724. + release_message_sync(state, header);
  25725. + break;
  25726. +
  25727. + case VCHIQ_MSG_DATA:
  25728. + vchiq_log_trace(vchiq_sync_log_level,
  25729. + "%d: sf DATA@%x,%x (%d->%d)",
  25730. + state->id, (unsigned int)header, size,
  25731. + remoteport, localport);
  25732. +
  25733. + if ((service->remoteport == remoteport) &&
  25734. + (service->srvstate ==
  25735. + VCHIQ_SRVSTATE_OPENSYNC)) {
  25736. + if (make_service_callback(service,
  25737. + VCHIQ_MESSAGE_AVAILABLE, header,
  25738. + NULL) == VCHIQ_RETRY)
  25739. + vchiq_log_error(vchiq_sync_log_level,
  25740. + "synchronous callback to "
  25741. + "service %d returns "
  25742. + "VCHIQ_RETRY",
  25743. + localport);
  25744. + }
  25745. + break;
  25746. +
  25747. + default:
  25748. + vchiq_log_error(vchiq_sync_log_level,
  25749. + "%d: sf unexpected msgid %x@%x,%x",
  25750. + state->id, msgid, (unsigned int)header, size);
  25751. + release_message_sync(state, header);
  25752. + break;
  25753. + }
  25754. +
  25755. + unlock_service(service);
  25756. + }
  25757. +
  25758. + return 0;
  25759. +}
  25760. +
  25761. +
  25762. +static void
  25763. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  25764. +{
  25765. + queue->local_insert = 0;
  25766. + queue->remote_insert = 0;
  25767. + queue->process = 0;
  25768. + queue->remote_notify = 0;
  25769. + queue->remove = 0;
  25770. +}
  25771. +
  25772. +
  25773. +inline const char *
  25774. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  25775. +{
  25776. + return conn_state_names[conn_state];
  25777. +}
  25778. +
  25779. +
  25780. +VCHIQ_SLOT_ZERO_T *
  25781. +vchiq_init_slots(void *mem_base, int mem_size)
  25782. +{
  25783. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  25784. + VCHIQ_SLOT_ZERO_T *slot_zero =
  25785. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  25786. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  25787. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  25788. +
  25789. + /* Ensure there is enough memory to run an absolutely minimum system */
  25790. + num_slots -= first_data_slot;
  25791. +
  25792. + if (num_slots < 4) {
  25793. + vchiq_log_error(vchiq_core_log_level,
  25794. + "vchiq_init_slots - insufficient memory %x bytes",
  25795. + mem_size);
  25796. + return NULL;
  25797. + }
  25798. +
  25799. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  25800. +
  25801. + slot_zero->magic = VCHIQ_MAGIC;
  25802. + slot_zero->version = VCHIQ_VERSION;
  25803. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  25804. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  25805. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  25806. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  25807. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  25808. +
  25809. + slot_zero->master.slot_sync = first_data_slot;
  25810. + slot_zero->master.slot_first = first_data_slot + 1;
  25811. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  25812. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  25813. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  25814. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  25815. +
  25816. + return slot_zero;
  25817. +}
  25818. +
  25819. +VCHIQ_STATUS_T
  25820. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  25821. + int is_master)
  25822. +{
  25823. + VCHIQ_SHARED_STATE_T *local;
  25824. + VCHIQ_SHARED_STATE_T *remote;
  25825. + VCHIQ_STATUS_T status;
  25826. + char threadname[10];
  25827. + static int id;
  25828. + int i;
  25829. +
  25830. + vchiq_log_warning(vchiq_core_log_level,
  25831. + "%s: slot_zero = 0x%08lx, is_master = %d",
  25832. + __func__, (unsigned long)slot_zero, is_master);
  25833. +
  25834. + /* Check the input configuration */
  25835. +
  25836. + if (slot_zero->magic != VCHIQ_MAGIC) {
  25837. + vchiq_loud_error_header();
  25838. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  25839. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  25840. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  25841. + vchiq_loud_error_footer();
  25842. + return VCHIQ_ERROR;
  25843. + }
  25844. +
  25845. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  25846. + vchiq_loud_error_header();
  25847. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  25848. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  25849. + "(minimum %d)",
  25850. + (unsigned int)slot_zero, slot_zero->version,
  25851. + VCHIQ_VERSION_MIN);
  25852. + vchiq_loud_error("Restart with a newer VideoCore image.");
  25853. + vchiq_loud_error_footer();
  25854. + return VCHIQ_ERROR;
  25855. + }
  25856. +
  25857. + if (VCHIQ_VERSION < slot_zero->version_min) {
  25858. + vchiq_loud_error_header();
  25859. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  25860. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  25861. + "minimum %d)",
  25862. + (unsigned int)slot_zero, VCHIQ_VERSION,
  25863. + slot_zero->version_min);
  25864. + vchiq_loud_error("Restart with a newer kernel.");
  25865. + vchiq_loud_error_footer();
  25866. + return VCHIQ_ERROR;
  25867. + }
  25868. +
  25869. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  25870. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  25871. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  25872. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  25873. + vchiq_loud_error_header();
  25874. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  25875. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  25876. + "(expected %x)",
  25877. + (unsigned int)slot_zero,
  25878. + slot_zero->slot_zero_size,
  25879. + sizeof(VCHIQ_SLOT_ZERO_T));
  25880. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  25881. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  25882. + "(expected %d",
  25883. + (unsigned int)slot_zero, slot_zero->slot_size,
  25884. + VCHIQ_SLOT_SIZE);
  25885. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  25886. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  25887. + "(expected %d)",
  25888. + (unsigned int)slot_zero, slot_zero->max_slots,
  25889. + VCHIQ_MAX_SLOTS);
  25890. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  25891. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  25892. + "(expected %d)",
  25893. + (unsigned int)slot_zero,
  25894. + slot_zero->max_slots_per_side,
  25895. + VCHIQ_MAX_SLOTS_PER_SIDE);
  25896. + vchiq_loud_error_footer();
  25897. + return VCHIQ_ERROR;
  25898. + }
  25899. +
  25900. + if (is_master) {
  25901. + local = &slot_zero->master;
  25902. + remote = &slot_zero->slave;
  25903. + } else {
  25904. + local = &slot_zero->slave;
  25905. + remote = &slot_zero->master;
  25906. + }
  25907. +
  25908. + if (local->initialised) {
  25909. + vchiq_loud_error_header();
  25910. + if (remote->initialised)
  25911. + vchiq_loud_error("local state has already been "
  25912. + "initialised");
  25913. + else
  25914. + vchiq_loud_error("master/slave mismatch - two %ss",
  25915. + is_master ? "master" : "slave");
  25916. + vchiq_loud_error_footer();
  25917. + return VCHIQ_ERROR;
  25918. + }
  25919. +
  25920. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  25921. +
  25922. + state->id = id++;
  25923. + state->is_master = is_master;
  25924. +
  25925. + /*
  25926. + initialize shared state pointers
  25927. + */
  25928. +
  25929. + state->local = local;
  25930. + state->remote = remote;
  25931. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  25932. +
  25933. + /*
  25934. + initialize events and mutexes
  25935. + */
  25936. +
  25937. + sema_init(&state->connect, 0);
  25938. + mutex_init(&state->mutex);
  25939. + sema_init(&state->trigger_event, 0);
  25940. + sema_init(&state->recycle_event, 0);
  25941. + sema_init(&state->sync_trigger_event, 0);
  25942. + sema_init(&state->sync_release_event, 0);
  25943. +
  25944. + mutex_init(&state->slot_mutex);
  25945. + mutex_init(&state->recycle_mutex);
  25946. + mutex_init(&state->sync_mutex);
  25947. + mutex_init(&state->bulk_transfer_mutex);
  25948. +
  25949. + sema_init(&state->slot_available_event, 0);
  25950. + sema_init(&state->slot_remove_event, 0);
  25951. + sema_init(&state->data_quota_event, 0);
  25952. +
  25953. + state->slot_queue_available = 0;
  25954. +
  25955. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  25956. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  25957. + &state->service_quotas[i];
  25958. + sema_init(&service_quota->quota_event, 0);
  25959. + }
  25960. +
  25961. + for (i = local->slot_first; i <= local->slot_last; i++) {
  25962. + local->slot_queue[state->slot_queue_available++] = i;
  25963. + up(&state->slot_available_event);
  25964. + }
  25965. +
  25966. + state->default_slot_quota = state->slot_queue_available/2;
  25967. + state->default_message_quota =
  25968. + min((unsigned short)(state->default_slot_quota * 256),
  25969. + (unsigned short)~0);
  25970. +
  25971. + state->previous_data_index = -1;
  25972. + state->data_use_count = 0;
  25973. + state->data_quota = state->slot_queue_available - 1;
  25974. +
  25975. + local->trigger.event = &state->trigger_event;
  25976. + remote_event_create(&local->trigger);
  25977. + local->tx_pos = 0;
  25978. +
  25979. + local->recycle.event = &state->recycle_event;
  25980. + remote_event_create(&local->recycle);
  25981. + local->slot_queue_recycle = state->slot_queue_available;
  25982. +
  25983. + local->sync_trigger.event = &state->sync_trigger_event;
  25984. + remote_event_create(&local->sync_trigger);
  25985. +
  25986. + local->sync_release.event = &state->sync_release_event;
  25987. + remote_event_create(&local->sync_release);
  25988. +
  25989. + /* At start-of-day, the slot is empty and available */
  25990. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  25991. + = VCHIQ_MSGID_PADDING;
  25992. + remote_event_signal_local(&local->sync_release);
  25993. +
  25994. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  25995. +
  25996. + status = vchiq_platform_init_state(state);
  25997. +
  25998. + /*
  25999. + bring up slot handler thread
  26000. + */
  26001. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  26002. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  26003. + (void *)state,
  26004. + threadname);
  26005. +
  26006. + if (state->slot_handler_thread == NULL) {
  26007. + vchiq_loud_error_header();
  26008. + vchiq_loud_error("couldn't create thread %s", threadname);
  26009. + vchiq_loud_error_footer();
  26010. + return VCHIQ_ERROR;
  26011. + }
  26012. + set_user_nice(state->slot_handler_thread, -19);
  26013. + wake_up_process(state->slot_handler_thread);
  26014. +
  26015. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  26016. + state->recycle_thread = kthread_create(&recycle_func,
  26017. + (void *)state,
  26018. + threadname);
  26019. + if (state->recycle_thread == NULL) {
  26020. + vchiq_loud_error_header();
  26021. + vchiq_loud_error("couldn't create thread %s", threadname);
  26022. + vchiq_loud_error_footer();
  26023. + return VCHIQ_ERROR;
  26024. + }
  26025. + set_user_nice(state->recycle_thread, -19);
  26026. + wake_up_process(state->recycle_thread);
  26027. +
  26028. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  26029. + state->sync_thread = kthread_create(&sync_func,
  26030. + (void *)state,
  26031. + threadname);
  26032. + if (state->sync_thread == NULL) {
  26033. + vchiq_loud_error_header();
  26034. + vchiq_loud_error("couldn't create thread %s", threadname);
  26035. + vchiq_loud_error_footer();
  26036. + return VCHIQ_ERROR;
  26037. + }
  26038. + set_user_nice(state->sync_thread, -20);
  26039. + wake_up_process(state->sync_thread);
  26040. +
  26041. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  26042. + vchiq_states[state->id] = state;
  26043. +
  26044. + /* Indicate readiness to the other side */
  26045. + local->initialised = 1;
  26046. +
  26047. + return status;
  26048. +}
  26049. +
  26050. +/* Called from application thread when a client or server service is created. */
  26051. +VCHIQ_SERVICE_T *
  26052. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  26053. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  26054. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  26055. +{
  26056. + VCHIQ_SERVICE_T *service;
  26057. +
  26058. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  26059. + if (service) {
  26060. + service->base.fourcc = params->fourcc;
  26061. + service->base.callback = params->callback;
  26062. + service->base.userdata = params->userdata;
  26063. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  26064. + service->ref_count = 1;
  26065. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  26066. + service->userdata_term = userdata_term;
  26067. + service->localport = VCHIQ_PORT_FREE;
  26068. + service->remoteport = VCHIQ_PORT_FREE;
  26069. +
  26070. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  26071. + VCHIQ_FOURCC_INVALID : params->fourcc;
  26072. + service->client_id = 0;
  26073. + service->auto_close = 1;
  26074. + service->sync = 0;
  26075. + service->closing = 0;
  26076. + atomic_set(&service->poll_flags, 0);
  26077. + service->version = params->version;
  26078. + service->version_min = params->version_min;
  26079. + service->state = state;
  26080. + service->instance = instance;
  26081. + service->service_use_count = 0;
  26082. + init_bulk_queue(&service->bulk_tx);
  26083. + init_bulk_queue(&service->bulk_rx);
  26084. + sema_init(&service->remove_event, 0);
  26085. + sema_init(&service->bulk_remove_event, 0);
  26086. + mutex_init(&service->bulk_mutex);
  26087. + memset(&service->stats, 0, sizeof(service->stats));
  26088. + } else {
  26089. + vchiq_log_error(vchiq_core_log_level,
  26090. + "Out of memory");
  26091. + }
  26092. +
  26093. + if (service) {
  26094. + VCHIQ_SERVICE_T **pservice = NULL;
  26095. + int i;
  26096. +
  26097. + /* Although it is perfectly possible to use service_spinlock
  26098. + ** to protect the creation of services, it is overkill as it
  26099. + ** disables interrupts while the array is searched.
  26100. + ** The only danger is of another thread trying to create a
  26101. + ** service - service deletion is safe.
  26102. + ** Therefore it is preferable to use state->mutex which,
  26103. + ** although slower to claim, doesn't block interrupts while
  26104. + ** it is held.
  26105. + */
  26106. +
  26107. + mutex_lock(&state->mutex);
  26108. +
  26109. + /* Prepare to use a previously unused service */
  26110. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  26111. + pservice = &state->services[state->unused_service];
  26112. +
  26113. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  26114. + for (i = 0; i < state->unused_service; i++) {
  26115. + VCHIQ_SERVICE_T *srv = state->services[i];
  26116. + if (!srv) {
  26117. + pservice = &state->services[i];
  26118. + break;
  26119. + }
  26120. + }
  26121. + } else {
  26122. + for (i = (state->unused_service - 1); i >= 0; i--) {
  26123. + VCHIQ_SERVICE_T *srv = state->services[i];
  26124. + if (!srv)
  26125. + pservice = &state->services[i];
  26126. + else if ((srv->public_fourcc == params->fourcc)
  26127. + && ((srv->instance != instance) ||
  26128. + (srv->base.callback !=
  26129. + params->callback))) {
  26130. + /* There is another server using this
  26131. + ** fourcc which doesn't match. */
  26132. + pservice = NULL;
  26133. + break;
  26134. + }
  26135. + }
  26136. + }
  26137. +
  26138. + if (pservice) {
  26139. + service->localport = (pservice - state->services);
  26140. + if (!handle_seq)
  26141. + handle_seq = VCHIQ_MAX_STATES *
  26142. + VCHIQ_MAX_SERVICES;
  26143. + service->handle = handle_seq |
  26144. + (state->id * VCHIQ_MAX_SERVICES) |
  26145. + service->localport;
  26146. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  26147. + *pservice = service;
  26148. + if (pservice == &state->services[state->unused_service])
  26149. + state->unused_service++;
  26150. + }
  26151. +
  26152. + mutex_unlock(&state->mutex);
  26153. +
  26154. + if (!pservice) {
  26155. + kfree(service);
  26156. + service = NULL;
  26157. + }
  26158. + }
  26159. +
  26160. + if (service) {
  26161. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  26162. + &state->service_quotas[service->localport];
  26163. + service_quota->slot_quota = state->default_slot_quota;
  26164. + service_quota->message_quota = state->default_message_quota;
  26165. + if (service_quota->slot_use_count == 0)
  26166. + service_quota->previous_tx_index =
  26167. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  26168. + - 1;
  26169. +
  26170. + /* Bring this service online */
  26171. + vchiq_set_service_state(service, srvstate);
  26172. +
  26173. + vchiq_log_info(vchiq_core_msg_log_level,
  26174. + "%s Service %c%c%c%c SrcPort:%d",
  26175. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  26176. + ? "Open" : "Add",
  26177. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  26178. + service->localport);
  26179. + }
  26180. +
  26181. + /* Don't unlock the service - leave it with a ref_count of 1. */
  26182. +
  26183. + return service;
  26184. +}
  26185. +
  26186. +VCHIQ_STATUS_T
  26187. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  26188. +{
  26189. + struct vchiq_open_payload payload = {
  26190. + service->base.fourcc,
  26191. + client_id,
  26192. + service->version,
  26193. + service->version_min
  26194. + };
  26195. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  26196. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26197. +
  26198. + service->client_id = client_id;
  26199. + vchiq_use_service_internal(service);
  26200. + status = queue_message(service->state, NULL,
  26201. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  26202. + &body, 1, sizeof(payload), 1);
  26203. + if (status == VCHIQ_SUCCESS) {
  26204. + if (down_interruptible(&service->remove_event) != 0) {
  26205. + status = VCHIQ_RETRY;
  26206. + vchiq_release_service_internal(service);
  26207. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  26208. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  26209. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  26210. + vchiq_log_error(vchiq_core_log_level,
  26211. + "%d: osi - srvstate = %s (ref %d)",
  26212. + service->state->id,
  26213. + srvstate_names[service->srvstate],
  26214. + service->ref_count);
  26215. + status = VCHIQ_ERROR;
  26216. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  26217. + vchiq_release_service_internal(service);
  26218. + }
  26219. + }
  26220. + return status;
  26221. +}
  26222. +
  26223. +static void
  26224. +release_service_messages(VCHIQ_SERVICE_T *service)
  26225. +{
  26226. + VCHIQ_STATE_T *state = service->state;
  26227. + int slot_last = state->remote->slot_last;
  26228. + int i;
  26229. +
  26230. + /* Release any claimed messages */
  26231. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  26232. + VCHIQ_SLOT_INFO_T *slot_info =
  26233. + SLOT_INFO_FROM_INDEX(state, i);
  26234. + if (slot_info->release_count != slot_info->use_count) {
  26235. + char *data =
  26236. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  26237. + unsigned int pos, end;
  26238. +
  26239. + end = VCHIQ_SLOT_SIZE;
  26240. + if (data == state->rx_data)
  26241. + /* This buffer is still being read from - stop
  26242. + ** at the current read position */
  26243. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  26244. +
  26245. + pos = 0;
  26246. +
  26247. + while (pos < end) {
  26248. + VCHIQ_HEADER_T *header =
  26249. + (VCHIQ_HEADER_T *)(data + pos);
  26250. + int msgid = header->msgid;
  26251. + int port = VCHIQ_MSG_DSTPORT(msgid);
  26252. + if ((port == service->localport) &&
  26253. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  26254. + vchiq_log_info(vchiq_core_log_level,
  26255. + " fsi - hdr %x",
  26256. + (unsigned int)header);
  26257. + release_slot(state, slot_info, header,
  26258. + NULL);
  26259. + }
  26260. + pos += calc_stride(header->size);
  26261. + if (pos > VCHIQ_SLOT_SIZE) {
  26262. + vchiq_log_error(vchiq_core_log_level,
  26263. + "fsi - pos %x: header %x, "
  26264. + "msgid %x, header->msgid %x, "
  26265. + "header->size %x",
  26266. + pos, (unsigned int)header,
  26267. + msgid, header->msgid,
  26268. + header->size);
  26269. + WARN(1, "invalid slot position\n");
  26270. + }
  26271. + }
  26272. + }
  26273. + }
  26274. +}
  26275. +
  26276. +static int
  26277. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  26278. +{
  26279. + VCHIQ_STATUS_T status;
  26280. +
  26281. + /* Abort any outstanding bulk transfers */
  26282. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  26283. + return 0;
  26284. + abort_outstanding_bulks(service, &service->bulk_tx);
  26285. + abort_outstanding_bulks(service, &service->bulk_rx);
  26286. + mutex_unlock(&service->bulk_mutex);
  26287. +
  26288. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  26289. + if (status == VCHIQ_SUCCESS)
  26290. + status = notify_bulks(service, &service->bulk_rx,
  26291. + 0/*!retry_poll*/);
  26292. + return (status == VCHIQ_SUCCESS);
  26293. +}
  26294. +
  26295. +static VCHIQ_STATUS_T
  26296. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  26297. +{
  26298. + VCHIQ_STATUS_T status;
  26299. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  26300. + int newstate;
  26301. +
  26302. + switch (service->srvstate) {
  26303. + case VCHIQ_SRVSTATE_OPEN:
  26304. + case VCHIQ_SRVSTATE_CLOSESENT:
  26305. + case VCHIQ_SRVSTATE_CLOSERECVD:
  26306. + if (is_server) {
  26307. + if (service->auto_close) {
  26308. + service->client_id = 0;
  26309. + service->remoteport = VCHIQ_PORT_FREE;
  26310. + newstate = VCHIQ_SRVSTATE_LISTENING;
  26311. + } else
  26312. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  26313. + } else
  26314. + newstate = VCHIQ_SRVSTATE_CLOSED;
  26315. + vchiq_set_service_state(service, newstate);
  26316. + break;
  26317. + case VCHIQ_SRVSTATE_LISTENING:
  26318. + break;
  26319. + default:
  26320. + vchiq_log_error(vchiq_core_log_level,
  26321. + "close_service_complete(%x) called in state %s",
  26322. + service->handle, srvstate_names[service->srvstate]);
  26323. + WARN(1, "close_service_complete in unexpected state\n");
  26324. + return VCHIQ_ERROR;
  26325. + }
  26326. +
  26327. + status = make_service_callback(service,
  26328. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  26329. +
  26330. + if (status != VCHIQ_RETRY) {
  26331. + int uc = service->service_use_count;
  26332. + int i;
  26333. + /* Complete the close process */
  26334. + for (i = 0; i < uc; i++)
  26335. + /* cater for cases where close is forced and the
  26336. + ** client may not close all it's handles */
  26337. + vchiq_release_service_internal(service);
  26338. +
  26339. + service->client_id = 0;
  26340. + service->remoteport = VCHIQ_PORT_FREE;
  26341. +
  26342. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  26343. + vchiq_free_service_internal(service);
  26344. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  26345. + if (is_server)
  26346. + service->closing = 0;
  26347. +
  26348. + up(&service->remove_event);
  26349. + }
  26350. + } else
  26351. + vchiq_set_service_state(service, failstate);
  26352. +
  26353. + return status;
  26354. +}
  26355. +
  26356. +/* Called by the slot handler */
  26357. +VCHIQ_STATUS_T
  26358. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  26359. +{
  26360. + VCHIQ_STATE_T *state = service->state;
  26361. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26362. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  26363. +
  26364. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  26365. + service->state->id, service->localport, close_recvd,
  26366. + srvstate_names[service->srvstate]);
  26367. +
  26368. + switch (service->srvstate) {
  26369. + case VCHIQ_SRVSTATE_CLOSED:
  26370. + case VCHIQ_SRVSTATE_HIDDEN:
  26371. + case VCHIQ_SRVSTATE_LISTENING:
  26372. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  26373. + if (close_recvd)
  26374. + vchiq_log_error(vchiq_core_log_level,
  26375. + "vchiq_close_service_internal(1) called "
  26376. + "in state %s",
  26377. + srvstate_names[service->srvstate]);
  26378. + else if (is_server) {
  26379. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  26380. + status = VCHIQ_ERROR;
  26381. + } else {
  26382. + service->client_id = 0;
  26383. + service->remoteport = VCHIQ_PORT_FREE;
  26384. + if (service->srvstate ==
  26385. + VCHIQ_SRVSTATE_CLOSEWAIT)
  26386. + vchiq_set_service_state(service,
  26387. + VCHIQ_SRVSTATE_LISTENING);
  26388. + }
  26389. + up(&service->remove_event);
  26390. + } else
  26391. + vchiq_free_service_internal(service);
  26392. + break;
  26393. + case VCHIQ_SRVSTATE_OPENING:
  26394. + if (close_recvd) {
  26395. + /* The open was rejected - tell the user */
  26396. + vchiq_set_service_state(service,
  26397. + VCHIQ_SRVSTATE_CLOSEWAIT);
  26398. + up(&service->remove_event);
  26399. + } else {
  26400. + /* Shutdown mid-open - let the other side know */
  26401. + status = queue_message(state, service,
  26402. + VCHIQ_MAKE_MSG
  26403. + (VCHIQ_MSG_CLOSE,
  26404. + service->localport,
  26405. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  26406. + NULL, 0, 0, 0);
  26407. + }
  26408. + break;
  26409. +
  26410. + case VCHIQ_SRVSTATE_OPENSYNC:
  26411. + mutex_lock(&state->sync_mutex);
  26412. + /* Drop through */
  26413. +
  26414. + case VCHIQ_SRVSTATE_OPEN:
  26415. + if (state->is_master || close_recvd) {
  26416. + if (!do_abort_bulks(service))
  26417. + status = VCHIQ_RETRY;
  26418. + }
  26419. +
  26420. + release_service_messages(service);
  26421. +
  26422. + if (status == VCHIQ_SUCCESS)
  26423. + status = queue_message(state, service,
  26424. + VCHIQ_MAKE_MSG
  26425. + (VCHIQ_MSG_CLOSE,
  26426. + service->localport,
  26427. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  26428. + NULL, 0, 0, 0);
  26429. +
  26430. + if (status == VCHIQ_SUCCESS) {
  26431. + if (!close_recvd)
  26432. + break;
  26433. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  26434. + mutex_unlock(&state->sync_mutex);
  26435. + break;
  26436. + } else
  26437. + break;
  26438. +
  26439. + status = close_service_complete(service,
  26440. + VCHIQ_SRVSTATE_CLOSERECVD);
  26441. + break;
  26442. +
  26443. + case VCHIQ_SRVSTATE_CLOSESENT:
  26444. + if (!close_recvd)
  26445. + /* This happens when a process is killed mid-close */
  26446. + break;
  26447. +
  26448. + if (!state->is_master) {
  26449. + if (!do_abort_bulks(service)) {
  26450. + status = VCHIQ_RETRY;
  26451. + break;
  26452. + }
  26453. + }
  26454. +
  26455. + if (status == VCHIQ_SUCCESS)
  26456. + status = close_service_complete(service,
  26457. + VCHIQ_SRVSTATE_CLOSERECVD);
  26458. + break;
  26459. +
  26460. + case VCHIQ_SRVSTATE_CLOSERECVD:
  26461. + if (!close_recvd && is_server)
  26462. + /* Force into LISTENING mode */
  26463. + vchiq_set_service_state(service,
  26464. + VCHIQ_SRVSTATE_LISTENING);
  26465. + status = close_service_complete(service,
  26466. + VCHIQ_SRVSTATE_CLOSERECVD);
  26467. + break;
  26468. +
  26469. + default:
  26470. + vchiq_log_error(vchiq_core_log_level,
  26471. + "vchiq_close_service_internal(%d) called in state %s",
  26472. + close_recvd, srvstate_names[service->srvstate]);
  26473. + break;
  26474. + }
  26475. +
  26476. + return status;
  26477. +}
  26478. +
  26479. +/* Called from the application process upon process death */
  26480. +void
  26481. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  26482. +{
  26483. + VCHIQ_STATE_T *state = service->state;
  26484. +
  26485. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  26486. + state->id, service->localport, service->remoteport);
  26487. +
  26488. + mark_service_closing(service);
  26489. +
  26490. + /* Mark the service for removal by the slot handler */
  26491. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  26492. +}
  26493. +
  26494. +/* Called from the slot handler */
  26495. +void
  26496. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  26497. +{
  26498. + VCHIQ_STATE_T *state = service->state;
  26499. +
  26500. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  26501. + state->id, service->localport);
  26502. +
  26503. + switch (service->srvstate) {
  26504. + case VCHIQ_SRVSTATE_OPENING:
  26505. + case VCHIQ_SRVSTATE_CLOSED:
  26506. + case VCHIQ_SRVSTATE_HIDDEN:
  26507. + case VCHIQ_SRVSTATE_LISTENING:
  26508. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  26509. + break;
  26510. + default:
  26511. + vchiq_log_error(vchiq_core_log_level,
  26512. + "%d: fsi - (%d) in state %s",
  26513. + state->id, service->localport,
  26514. + srvstate_names[service->srvstate]);
  26515. + return;
  26516. + }
  26517. +
  26518. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  26519. +
  26520. + up(&service->remove_event);
  26521. +
  26522. + /* Release the initial lock */
  26523. + unlock_service(service);
  26524. +}
  26525. +
  26526. +VCHIQ_STATUS_T
  26527. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  26528. +{
  26529. + VCHIQ_SERVICE_T *service;
  26530. + int i;
  26531. +
  26532. + /* Find all services registered to this client and enable them. */
  26533. + i = 0;
  26534. + while ((service = next_service_by_instance(state, instance,
  26535. + &i)) != NULL) {
  26536. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  26537. + vchiq_set_service_state(service,
  26538. + VCHIQ_SRVSTATE_LISTENING);
  26539. + unlock_service(service);
  26540. + }
  26541. +
  26542. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  26543. + if (queue_message(state, NULL,
  26544. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  26545. + 0, 1) == VCHIQ_RETRY)
  26546. + return VCHIQ_RETRY;
  26547. +
  26548. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  26549. + }
  26550. +
  26551. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  26552. + if (down_interruptible(&state->connect) != 0)
  26553. + return VCHIQ_RETRY;
  26554. +
  26555. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  26556. + up(&state->connect);
  26557. + }
  26558. +
  26559. + return VCHIQ_SUCCESS;
  26560. +}
  26561. +
  26562. +VCHIQ_STATUS_T
  26563. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  26564. +{
  26565. + VCHIQ_SERVICE_T *service;
  26566. + int i;
  26567. +
  26568. + /* Find all services registered to this client and enable them. */
  26569. + i = 0;
  26570. + while ((service = next_service_by_instance(state, instance,
  26571. + &i)) != NULL) {
  26572. + (void)vchiq_remove_service(service->handle);
  26573. + unlock_service(service);
  26574. + }
  26575. +
  26576. + return VCHIQ_SUCCESS;
  26577. +}
  26578. +
  26579. +VCHIQ_STATUS_T
  26580. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  26581. +{
  26582. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26583. +
  26584. + switch (state->conn_state) {
  26585. + case VCHIQ_CONNSTATE_CONNECTED:
  26586. + /* Request a pause */
  26587. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  26588. + request_poll(state, NULL, 0);
  26589. + break;
  26590. + default:
  26591. + vchiq_log_error(vchiq_core_log_level,
  26592. + "vchiq_pause_internal in state %s\n",
  26593. + conn_state_names[state->conn_state]);
  26594. + status = VCHIQ_ERROR;
  26595. + VCHIQ_STATS_INC(state, error_count);
  26596. + break;
  26597. + }
  26598. +
  26599. + return status;
  26600. +}
  26601. +
  26602. +VCHIQ_STATUS_T
  26603. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  26604. +{
  26605. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26606. +
  26607. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  26608. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  26609. + request_poll(state, NULL, 0);
  26610. + } else {
  26611. + status = VCHIQ_ERROR;
  26612. + VCHIQ_STATS_INC(state, error_count);
  26613. + }
  26614. +
  26615. + return status;
  26616. +}
  26617. +
  26618. +VCHIQ_STATUS_T
  26619. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  26620. +{
  26621. + /* Unregister the service */
  26622. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26623. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26624. +
  26625. + if (!service)
  26626. + return VCHIQ_ERROR;
  26627. +
  26628. + vchiq_log_info(vchiq_core_log_level,
  26629. + "%d: close_service:%d",
  26630. + service->state->id, service->localport);
  26631. +
  26632. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26633. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  26634. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  26635. + unlock_service(service);
  26636. + return VCHIQ_ERROR;
  26637. + }
  26638. +
  26639. + mark_service_closing(service);
  26640. +
  26641. + if (current == service->state->slot_handler_thread) {
  26642. + status = vchiq_close_service_internal(service,
  26643. + 0/*!close_recvd*/);
  26644. + BUG_ON(status == VCHIQ_RETRY);
  26645. + } else {
  26646. + /* Mark the service for termination by the slot handler */
  26647. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  26648. + }
  26649. +
  26650. + while (1) {
  26651. + if (down_interruptible(&service->remove_event) != 0) {
  26652. + status = VCHIQ_RETRY;
  26653. + break;
  26654. + }
  26655. +
  26656. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26657. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  26658. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  26659. + break;
  26660. +
  26661. + vchiq_log_warning(vchiq_core_log_level,
  26662. + "%d: close_service:%d - waiting in state %s",
  26663. + service->state->id, service->localport,
  26664. + srvstate_names[service->srvstate]);
  26665. + }
  26666. +
  26667. + if ((status == VCHIQ_SUCCESS) &&
  26668. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  26669. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  26670. + status = VCHIQ_ERROR;
  26671. +
  26672. + unlock_service(service);
  26673. +
  26674. + return status;
  26675. +}
  26676. +
  26677. +VCHIQ_STATUS_T
  26678. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  26679. +{
  26680. + /* Unregister the service */
  26681. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26682. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26683. +
  26684. + if (!service)
  26685. + return VCHIQ_ERROR;
  26686. +
  26687. + vchiq_log_info(vchiq_core_log_level,
  26688. + "%d: remove_service:%d",
  26689. + service->state->id, service->localport);
  26690. +
  26691. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  26692. + unlock_service(service);
  26693. + return VCHIQ_ERROR;
  26694. + }
  26695. +
  26696. + mark_service_closing(service);
  26697. +
  26698. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  26699. + (current == service->state->slot_handler_thread)) {
  26700. + /* Make it look like a client, because it must be removed and
  26701. + not left in the LISTENING state. */
  26702. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  26703. +
  26704. + status = vchiq_close_service_internal(service,
  26705. + 0/*!close_recvd*/);
  26706. + BUG_ON(status == VCHIQ_RETRY);
  26707. + } else {
  26708. + /* Mark the service for removal by the slot handler */
  26709. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  26710. + }
  26711. + while (1) {
  26712. + if (down_interruptible(&service->remove_event) != 0) {
  26713. + status = VCHIQ_RETRY;
  26714. + break;
  26715. + }
  26716. +
  26717. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26718. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  26719. + break;
  26720. +
  26721. + vchiq_log_warning(vchiq_core_log_level,
  26722. + "%d: remove_service:%d - waiting in state %s",
  26723. + service->state->id, service->localport,
  26724. + srvstate_names[service->srvstate]);
  26725. + }
  26726. +
  26727. + if ((status == VCHIQ_SUCCESS) &&
  26728. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  26729. + status = VCHIQ_ERROR;
  26730. +
  26731. + unlock_service(service);
  26732. +
  26733. + return status;
  26734. +}
  26735. +
  26736. +
  26737. +/* This function may be called by kernel threads or user threads.
  26738. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  26739. + * received and the call should be retried after being returned to user
  26740. + * context.
  26741. + * When called in blocking mode, the userdata field points to a bulk_waiter
  26742. + * structure.
  26743. + */
  26744. +VCHIQ_STATUS_T
  26745. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  26746. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  26747. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  26748. +{
  26749. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26750. + VCHIQ_BULK_QUEUE_T *queue;
  26751. + VCHIQ_BULK_T *bulk;
  26752. + VCHIQ_STATE_T *state;
  26753. + struct bulk_waiter *bulk_waiter = NULL;
  26754. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  26755. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  26756. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  26757. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26758. +
  26759. + if (!service ||
  26760. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  26761. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  26762. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  26763. + goto error_exit;
  26764. +
  26765. + switch (mode) {
  26766. + case VCHIQ_BULK_MODE_NOCALLBACK:
  26767. + case VCHIQ_BULK_MODE_CALLBACK:
  26768. + break;
  26769. + case VCHIQ_BULK_MODE_BLOCKING:
  26770. + bulk_waiter = (struct bulk_waiter *)userdata;
  26771. + sema_init(&bulk_waiter->event, 0);
  26772. + bulk_waiter->actual = 0;
  26773. + bulk_waiter->bulk = NULL;
  26774. + break;
  26775. + case VCHIQ_BULK_MODE_WAITING:
  26776. + bulk_waiter = (struct bulk_waiter *)userdata;
  26777. + bulk = bulk_waiter->bulk;
  26778. + goto waiting;
  26779. + default:
  26780. + goto error_exit;
  26781. + }
  26782. +
  26783. + state = service->state;
  26784. +
  26785. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  26786. + &service->bulk_tx : &service->bulk_rx;
  26787. +
  26788. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  26789. + status = VCHIQ_RETRY;
  26790. + goto error_exit;
  26791. + }
  26792. +
  26793. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  26794. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  26795. + do {
  26796. + mutex_unlock(&service->bulk_mutex);
  26797. + if (down_interruptible(&service->bulk_remove_event)
  26798. + != 0) {
  26799. + status = VCHIQ_RETRY;
  26800. + goto error_exit;
  26801. + }
  26802. + if (mutex_lock_interruptible(&service->bulk_mutex)
  26803. + != 0) {
  26804. + status = VCHIQ_RETRY;
  26805. + goto error_exit;
  26806. + }
  26807. + } while (queue->local_insert == queue->remove +
  26808. + VCHIQ_NUM_SERVICE_BULKS);
  26809. + }
  26810. +
  26811. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  26812. +
  26813. + bulk->mode = mode;
  26814. + bulk->dir = dir;
  26815. + bulk->userdata = userdata;
  26816. + bulk->size = size;
  26817. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  26818. +
  26819. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  26820. + VCHIQ_SUCCESS)
  26821. + goto unlock_error_exit;
  26822. +
  26823. + wmb();
  26824. +
  26825. + vchiq_log_info(vchiq_core_log_level,
  26826. + "%d: bt (%d->%d) %cx %x@%x %x",
  26827. + state->id,
  26828. + service->localport, service->remoteport, dir_char,
  26829. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  26830. +
  26831. + if (state->is_master) {
  26832. + queue->local_insert++;
  26833. + if (resolve_bulks(service, queue))
  26834. + request_poll(state, service,
  26835. + (dir == VCHIQ_BULK_TRANSMIT) ?
  26836. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  26837. + } else {
  26838. + int payload[2] = { (int)bulk->data, bulk->size };
  26839. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  26840. +
  26841. + status = queue_message(state, NULL,
  26842. + VCHIQ_MAKE_MSG(dir_msgtype,
  26843. + service->localport, service->remoteport),
  26844. + &element, 1, sizeof(payload), 1);
  26845. + if (status != VCHIQ_SUCCESS) {
  26846. + vchiq_complete_bulk(bulk);
  26847. + goto unlock_error_exit;
  26848. + }
  26849. + queue->local_insert++;
  26850. + }
  26851. +
  26852. + mutex_unlock(&service->bulk_mutex);
  26853. +
  26854. + vchiq_log_trace(vchiq_core_log_level,
  26855. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  26856. + state->id,
  26857. + service->localport, dir_char,
  26858. + queue->local_insert, queue->remote_insert, queue->process);
  26859. +
  26860. +waiting:
  26861. + unlock_service(service);
  26862. +
  26863. + status = VCHIQ_SUCCESS;
  26864. +
  26865. + if (bulk_waiter) {
  26866. + bulk_waiter->bulk = bulk;
  26867. + if (down_interruptible(&bulk_waiter->event) != 0)
  26868. + status = VCHIQ_RETRY;
  26869. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  26870. + status = VCHIQ_ERROR;
  26871. + }
  26872. +
  26873. + return status;
  26874. +
  26875. +unlock_error_exit:
  26876. + mutex_unlock(&service->bulk_mutex);
  26877. +
  26878. +error_exit:
  26879. + if (service)
  26880. + unlock_service(service);
  26881. + return status;
  26882. +}
  26883. +
  26884. +VCHIQ_STATUS_T
  26885. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  26886. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  26887. +{
  26888. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26889. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26890. +
  26891. + unsigned int size = 0;
  26892. + unsigned int i;
  26893. +
  26894. + if (!service ||
  26895. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  26896. + goto error_exit;
  26897. +
  26898. + for (i = 0; i < (unsigned int)count; i++) {
  26899. + if (elements[i].size) {
  26900. + if (elements[i].data == NULL) {
  26901. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  26902. + goto error_exit;
  26903. + }
  26904. + size += elements[i].size;
  26905. + }
  26906. + }
  26907. +
  26908. + if (size > VCHIQ_MAX_MSG_SIZE) {
  26909. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  26910. + goto error_exit;
  26911. + }
  26912. +
  26913. + switch (service->srvstate) {
  26914. + case VCHIQ_SRVSTATE_OPEN:
  26915. + status = queue_message(service->state, service,
  26916. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  26917. + service->localport,
  26918. + service->remoteport),
  26919. + elements, count, size, 1);
  26920. + break;
  26921. + case VCHIQ_SRVSTATE_OPENSYNC:
  26922. + status = queue_message_sync(service->state, service,
  26923. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  26924. + service->localport,
  26925. + service->remoteport),
  26926. + elements, count, size, 1);
  26927. + break;
  26928. + default:
  26929. + status = VCHIQ_ERROR;
  26930. + break;
  26931. + }
  26932. +
  26933. +error_exit:
  26934. + if (service)
  26935. + unlock_service(service);
  26936. +
  26937. + return status;
  26938. +}
  26939. +
  26940. +void
  26941. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  26942. +{
  26943. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26944. + VCHIQ_SHARED_STATE_T *remote;
  26945. + VCHIQ_STATE_T *state;
  26946. + int slot_index;
  26947. +
  26948. + if (!service)
  26949. + return;
  26950. +
  26951. + state = service->state;
  26952. + remote = state->remote;
  26953. +
  26954. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  26955. +
  26956. + if ((slot_index >= remote->slot_first) &&
  26957. + (slot_index <= remote->slot_last)) {
  26958. + int msgid = header->msgid;
  26959. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  26960. + VCHIQ_SLOT_INFO_T *slot_info =
  26961. + SLOT_INFO_FROM_INDEX(state, slot_index);
  26962. +
  26963. + release_slot(state, slot_info, header, service);
  26964. + }
  26965. + } else if (slot_index == remote->slot_sync)
  26966. + release_message_sync(state, header);
  26967. +
  26968. + unlock_service(service);
  26969. +}
  26970. +
  26971. +static void
  26972. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  26973. +{
  26974. + header->msgid = VCHIQ_MSGID_PADDING;
  26975. + wmb();
  26976. + remote_event_signal(&state->remote->sync_release);
  26977. +}
  26978. +
  26979. +VCHIQ_STATUS_T
  26980. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  26981. +{
  26982. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26983. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26984. +
  26985. + if (!service ||
  26986. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  26987. + !peer_version)
  26988. + goto exit;
  26989. + *peer_version = service->peer_version;
  26990. + status = VCHIQ_SUCCESS;
  26991. +
  26992. +exit:
  26993. + if (service)
  26994. + unlock_service(service);
  26995. + return status;
  26996. +}
  26997. +
  26998. +VCHIQ_STATUS_T
  26999. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  27000. + int config_size, VCHIQ_CONFIG_T *pconfig)
  27001. +{
  27002. + VCHIQ_CONFIG_T config;
  27003. +
  27004. + (void)instance;
  27005. +
  27006. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  27007. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  27008. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  27009. + config.max_services = VCHIQ_MAX_SERVICES;
  27010. + config.version = VCHIQ_VERSION;
  27011. + config.version_min = VCHIQ_VERSION_MIN;
  27012. +
  27013. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  27014. + return VCHIQ_ERROR;
  27015. +
  27016. + memcpy(pconfig, &config,
  27017. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  27018. +
  27019. + return VCHIQ_SUCCESS;
  27020. +}
  27021. +
  27022. +VCHIQ_STATUS_T
  27023. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  27024. + VCHIQ_SERVICE_OPTION_T option, int value)
  27025. +{
  27026. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27027. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  27028. +
  27029. + if (service) {
  27030. + switch (option) {
  27031. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  27032. + service->auto_close = value;
  27033. + status = VCHIQ_SUCCESS;
  27034. + break;
  27035. +
  27036. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  27037. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  27038. + &service->state->service_quotas[
  27039. + service->localport];
  27040. + if (value == 0)
  27041. + value = service->state->default_slot_quota;
  27042. + if ((value >= service_quota->slot_use_count) &&
  27043. + (value < (unsigned short)~0)) {
  27044. + service_quota->slot_quota = value;
  27045. + if ((value >= service_quota->slot_use_count) &&
  27046. + (service_quota->message_quota >=
  27047. + service_quota->message_use_count)) {
  27048. + /* Signal the service that it may have
  27049. + ** dropped below its quota */
  27050. + up(&service_quota->quota_event);
  27051. + }
  27052. + status = VCHIQ_SUCCESS;
  27053. + }
  27054. + } break;
  27055. +
  27056. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  27057. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  27058. + &service->state->service_quotas[
  27059. + service->localport];
  27060. + if (value == 0)
  27061. + value = service->state->default_message_quota;
  27062. + if ((value >= service_quota->message_use_count) &&
  27063. + (value < (unsigned short)~0)) {
  27064. + service_quota->message_quota = value;
  27065. + if ((value >=
  27066. + service_quota->message_use_count) &&
  27067. + (service_quota->slot_quota >=
  27068. + service_quota->slot_use_count))
  27069. + /* Signal the service that it may have
  27070. + ** dropped below its quota */
  27071. + up(&service_quota->quota_event);
  27072. + status = VCHIQ_SUCCESS;
  27073. + }
  27074. + } break;
  27075. +
  27076. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  27077. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  27078. + (service->srvstate ==
  27079. + VCHIQ_SRVSTATE_LISTENING)) {
  27080. + service->sync = value;
  27081. + status = VCHIQ_SUCCESS;
  27082. + }
  27083. + break;
  27084. +
  27085. + default:
  27086. + break;
  27087. + }
  27088. + unlock_service(service);
  27089. + }
  27090. +
  27091. + return status;
  27092. +}
  27093. +
  27094. +void
  27095. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  27096. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  27097. +{
  27098. + static const char *const debug_names[] = {
  27099. + "<entries>",
  27100. + "SLOT_HANDLER_COUNT",
  27101. + "SLOT_HANDLER_LINE",
  27102. + "PARSE_LINE",
  27103. + "PARSE_HEADER",
  27104. + "PARSE_MSGID",
  27105. + "AWAIT_COMPLETION_LINE",
  27106. + "DEQUEUE_MESSAGE_LINE",
  27107. + "SERVICE_CALLBACK_LINE",
  27108. + "MSG_QUEUE_FULL_COUNT",
  27109. + "COMPLETION_QUEUE_FULL_COUNT"
  27110. + };
  27111. + int i;
  27112. +
  27113. + char buf[80];
  27114. + int len;
  27115. + len = snprintf(buf, sizeof(buf),
  27116. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  27117. + label, shared->slot_first, shared->slot_last,
  27118. + shared->tx_pos, shared->slot_queue_recycle);
  27119. + vchiq_dump(dump_context, buf, len + 1);
  27120. +
  27121. + len = snprintf(buf, sizeof(buf),
  27122. + " Slots claimed:");
  27123. + vchiq_dump(dump_context, buf, len + 1);
  27124. +
  27125. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  27126. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  27127. + if (slot_info.use_count != slot_info.release_count) {
  27128. + len = snprintf(buf, sizeof(buf),
  27129. + " %d: %d/%d", i, slot_info.use_count,
  27130. + slot_info.release_count);
  27131. + vchiq_dump(dump_context, buf, len + 1);
  27132. + }
  27133. + }
  27134. +
  27135. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  27136. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  27137. + debug_names[i], shared->debug[i], shared->debug[i]);
  27138. + vchiq_dump(dump_context, buf, len + 1);
  27139. + }
  27140. +}
  27141. +
  27142. +void
  27143. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  27144. +{
  27145. + char buf[80];
  27146. + int len;
  27147. + int i;
  27148. +
  27149. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  27150. + conn_state_names[state->conn_state]);
  27151. + vchiq_dump(dump_context, buf, len + 1);
  27152. +
  27153. + len = snprintf(buf, sizeof(buf),
  27154. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  27155. + state->local->tx_pos,
  27156. + (uint32_t)state->tx_data +
  27157. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  27158. + state->rx_pos,
  27159. + (uint32_t)state->rx_data +
  27160. + (state->rx_pos & VCHIQ_SLOT_MASK));
  27161. + vchiq_dump(dump_context, buf, len + 1);
  27162. +
  27163. + len = snprintf(buf, sizeof(buf),
  27164. + " Version: %d (min %d)",
  27165. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  27166. + vchiq_dump(dump_context, buf, len + 1);
  27167. +
  27168. + if (VCHIQ_ENABLE_STATS) {
  27169. + len = snprintf(buf, sizeof(buf),
  27170. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  27171. + "error_count=%d",
  27172. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  27173. + state->stats.error_count);
  27174. + vchiq_dump(dump_context, buf, len + 1);
  27175. + }
  27176. +
  27177. + len = snprintf(buf, sizeof(buf),
  27178. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  27179. + "(%d data)",
  27180. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  27181. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  27182. + state->data_quota - state->data_use_count,
  27183. + state->local->slot_queue_recycle - state->slot_queue_available,
  27184. + state->stats.slot_stalls, state->stats.data_stalls);
  27185. + vchiq_dump(dump_context, buf, len + 1);
  27186. +
  27187. + vchiq_dump_platform_state(dump_context);
  27188. +
  27189. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  27190. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  27191. +
  27192. + vchiq_dump_platform_instances(dump_context);
  27193. +
  27194. + for (i = 0; i < state->unused_service; i++) {
  27195. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  27196. +
  27197. + if (service) {
  27198. + vchiq_dump_service_state(dump_context, service);
  27199. + unlock_service(service);
  27200. + }
  27201. + }
  27202. +}
  27203. +
  27204. +void
  27205. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  27206. +{
  27207. + char buf[80];
  27208. + int len;
  27209. +
  27210. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  27211. + service->localport, srvstate_names[service->srvstate],
  27212. + service->ref_count - 1); /*Don't include the lock just taken*/
  27213. +
  27214. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  27215. + char remoteport[30];
  27216. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  27217. + &service->state->service_quotas[service->localport];
  27218. + int fourcc = service->base.fourcc;
  27219. + int tx_pending, rx_pending;
  27220. + if (service->remoteport != VCHIQ_PORT_FREE) {
  27221. + int len2 = snprintf(remoteport, sizeof(remoteport),
  27222. + "%d", service->remoteport);
  27223. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  27224. + snprintf(remoteport + len2,
  27225. + sizeof(remoteport) - len2,
  27226. + " (client %x)", service->client_id);
  27227. + } else
  27228. + strcpy(remoteport, "n/a");
  27229. +
  27230. + len += snprintf(buf + len, sizeof(buf) - len,
  27231. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  27232. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  27233. + remoteport,
  27234. + service_quota->message_use_count,
  27235. + service_quota->message_quota,
  27236. + service_quota->slot_use_count,
  27237. + service_quota->slot_quota);
  27238. +
  27239. + vchiq_dump(dump_context, buf, len + 1);
  27240. +
  27241. + tx_pending = service->bulk_tx.local_insert -
  27242. + service->bulk_tx.remote_insert;
  27243. +
  27244. + rx_pending = service->bulk_rx.local_insert -
  27245. + service->bulk_rx.remote_insert;
  27246. +
  27247. + len = snprintf(buf, sizeof(buf),
  27248. + " Bulk: tx_pending=%d (size %d),"
  27249. + " rx_pending=%d (size %d)",
  27250. + tx_pending,
  27251. + tx_pending ? service->bulk_tx.bulks[
  27252. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  27253. + rx_pending,
  27254. + rx_pending ? service->bulk_rx.bulks[
  27255. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  27256. +
  27257. + if (VCHIQ_ENABLE_STATS) {
  27258. + vchiq_dump(dump_context, buf, len + 1);
  27259. +
  27260. + len = snprintf(buf, sizeof(buf),
  27261. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  27262. + "rx_count=%d, rx_bytes=%llu",
  27263. + service->stats.ctrl_tx_count,
  27264. + service->stats.ctrl_tx_bytes,
  27265. + service->stats.ctrl_rx_count,
  27266. + service->stats.ctrl_rx_bytes);
  27267. + vchiq_dump(dump_context, buf, len + 1);
  27268. +
  27269. + len = snprintf(buf, sizeof(buf),
  27270. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  27271. + "rx_count=%d, rx_bytes=%llu",
  27272. + service->stats.bulk_tx_count,
  27273. + service->stats.bulk_tx_bytes,
  27274. + service->stats.bulk_rx_count,
  27275. + service->stats.bulk_rx_bytes);
  27276. + vchiq_dump(dump_context, buf, len + 1);
  27277. +
  27278. + len = snprintf(buf, sizeof(buf),
  27279. + " %d quota stalls, %d slot stalls, "
  27280. + "%d bulk stalls, %d aborted, %d errors",
  27281. + service->stats.quota_stalls,
  27282. + service->stats.slot_stalls,
  27283. + service->stats.bulk_stalls,
  27284. + service->stats.bulk_aborted_count,
  27285. + service->stats.error_count);
  27286. + }
  27287. + }
  27288. +
  27289. + vchiq_dump(dump_context, buf, len + 1);
  27290. +
  27291. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  27292. + vchiq_dump_platform_service_state(dump_context, service);
  27293. +}
  27294. +
  27295. +
  27296. +void
  27297. +vchiq_loud_error_header(void)
  27298. +{
  27299. + vchiq_log_error(vchiq_core_log_level,
  27300. + "============================================================"
  27301. + "================");
  27302. + vchiq_log_error(vchiq_core_log_level,
  27303. + "============================================================"
  27304. + "================");
  27305. + vchiq_log_error(vchiq_core_log_level, "=====");
  27306. +}
  27307. +
  27308. +void
  27309. +vchiq_loud_error_footer(void)
  27310. +{
  27311. + vchiq_log_error(vchiq_core_log_level, "=====");
  27312. + vchiq_log_error(vchiq_core_log_level,
  27313. + "============================================================"
  27314. + "================");
  27315. + vchiq_log_error(vchiq_core_log_level,
  27316. + "============================================================"
  27317. + "================");
  27318. +}
  27319. +
  27320. +
  27321. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  27322. +{
  27323. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  27324. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  27325. + status = queue_message(state, NULL,
  27326. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  27327. + NULL, 0, 0, 0);
  27328. + return status;
  27329. +}
  27330. +
  27331. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  27332. +{
  27333. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  27334. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  27335. + status = queue_message(state, NULL,
  27336. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  27337. + NULL, 0, 0, 0);
  27338. + return status;
  27339. +}
  27340. +
  27341. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  27342. +{
  27343. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  27344. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  27345. + status = queue_message(state, NULL,
  27346. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  27347. + NULL, 0, 0, 0);
  27348. + return status;
  27349. +}
  27350. +
  27351. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  27352. + size_t numBytes)
  27353. +{
  27354. + const uint8_t *mem = (const uint8_t *)voidMem;
  27355. + size_t offset;
  27356. + char lineBuf[100];
  27357. + char *s;
  27358. +
  27359. + while (numBytes > 0) {
  27360. + s = lineBuf;
  27361. +
  27362. + for (offset = 0; offset < 16; offset++) {
  27363. + if (offset < numBytes)
  27364. + s += snprintf(s, 4, "%02x ", mem[offset]);
  27365. + else
  27366. + s += snprintf(s, 4, " ");
  27367. + }
  27368. +
  27369. + for (offset = 0; offset < 16; offset++) {
  27370. + if (offset < numBytes) {
  27371. + uint8_t ch = mem[offset];
  27372. +
  27373. + if ((ch < ' ') || (ch > '~'))
  27374. + ch = '.';
  27375. + *s++ = (char)ch;
  27376. + }
  27377. + }
  27378. + *s++ = '\0';
  27379. +
  27380. + if ((label != NULL) && (*label != '\0'))
  27381. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  27382. + "%s: %08x: %s", label, addr, lineBuf);
  27383. + else
  27384. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  27385. + "%08x: %s", addr, lineBuf);
  27386. +
  27387. + addr += 16;
  27388. + mem += 16;
  27389. + if (numBytes > 16)
  27390. + numBytes -= 16;
  27391. + else
  27392. + numBytes = 0;
  27393. + }
  27394. +}
  27395. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  27396. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 1970-01-01 01:00:00.000000000 +0100
  27397. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 2014-03-11 17:32:37.000000000 +0100
  27398. @@ -0,0 +1,706 @@
  27399. +/**
  27400. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27401. + *
  27402. + * Redistribution and use in source and binary forms, with or without
  27403. + * modification, are permitted provided that the following conditions
  27404. + * are met:
  27405. + * 1. Redistributions of source code must retain the above copyright
  27406. + * notice, this list of conditions, and the following disclaimer,
  27407. + * without modification.
  27408. + * 2. Redistributions in binary form must reproduce the above copyright
  27409. + * notice, this list of conditions and the following disclaimer in the
  27410. + * documentation and/or other materials provided with the distribution.
  27411. + * 3. The names of the above-listed copyright holders may not be used
  27412. + * to endorse or promote products derived from this software without
  27413. + * specific prior written permission.
  27414. + *
  27415. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27416. + * GNU General Public License ("GPL") version 2, as published by the Free
  27417. + * Software Foundation.
  27418. + *
  27419. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27420. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27421. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27422. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27423. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27424. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27425. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27426. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27427. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27428. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27429. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27430. + */
  27431. +
  27432. +#ifndef VCHIQ_CORE_H
  27433. +#define VCHIQ_CORE_H
  27434. +
  27435. +#include <linux/mutex.h>
  27436. +#include <linux/semaphore.h>
  27437. +#include <linux/kthread.h>
  27438. +
  27439. +#include "vchiq_cfg.h"
  27440. +
  27441. +#include "vchiq.h"
  27442. +
  27443. +/* Run time control of log level, based on KERN_XXX level. */
  27444. +#define VCHIQ_LOG_DEFAULT 4
  27445. +#define VCHIQ_LOG_ERROR 3
  27446. +#define VCHIQ_LOG_WARNING 4
  27447. +#define VCHIQ_LOG_INFO 6
  27448. +#define VCHIQ_LOG_TRACE 7
  27449. +
  27450. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  27451. +
  27452. +#ifndef vchiq_log_error
  27453. +#define vchiq_log_error(cat, fmt, ...) \
  27454. + do { if (cat >= VCHIQ_LOG_ERROR) \
  27455. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27456. +#endif
  27457. +#ifndef vchiq_log_warning
  27458. +#define vchiq_log_warning(cat, fmt, ...) \
  27459. + do { if (cat >= VCHIQ_LOG_WARNING) \
  27460. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27461. +#endif
  27462. +#ifndef vchiq_log_info
  27463. +#define vchiq_log_info(cat, fmt, ...) \
  27464. + do { if (cat >= VCHIQ_LOG_INFO) \
  27465. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27466. +#endif
  27467. +#ifndef vchiq_log_trace
  27468. +#define vchiq_log_trace(cat, fmt, ...) \
  27469. + do { if (cat >= VCHIQ_LOG_TRACE) \
  27470. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27471. +#endif
  27472. +
  27473. +#define vchiq_loud_error(...) \
  27474. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  27475. +
  27476. +#ifndef vchiq_static_assert
  27477. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  27478. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  27479. +#endif
  27480. +
  27481. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  27482. +
  27483. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  27484. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  27485. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  27486. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  27487. +
  27488. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  27489. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  27490. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  27491. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  27492. +
  27493. +#define VCHIQ_MSG_PADDING 0 /* - */
  27494. +#define VCHIQ_MSG_CONNECT 1 /* - */
  27495. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  27496. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  27497. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  27498. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  27499. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  27500. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  27501. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  27502. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  27503. +#define VCHIQ_MSG_PAUSE 10 /* - */
  27504. +#define VCHIQ_MSG_RESUME 11 /* - */
  27505. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  27506. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  27507. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  27508. +
  27509. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  27510. +#define VCHIQ_PORT_FREE 0x1000
  27511. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  27512. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  27513. + ((type<<24) | (srcport<<12) | (dstport<<0))
  27514. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  27515. +#define VCHIQ_MSG_SRCPORT(msgid) \
  27516. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  27517. +#define VCHIQ_MSG_DSTPORT(msgid) \
  27518. + ((unsigned short)msgid & 0xfff)
  27519. +
  27520. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  27521. + ((fourcc) >> 24) & 0xff, \
  27522. + ((fourcc) >> 16) & 0xff, \
  27523. + ((fourcc) >> 8) & 0xff, \
  27524. + (fourcc) & 0xff
  27525. +
  27526. +/* Ensure the fields are wide enough */
  27527. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  27528. + == 0);
  27529. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  27530. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  27531. + (unsigned int)VCHIQ_PORT_FREE);
  27532. +
  27533. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  27534. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  27535. +
  27536. +#define VCHIQ_FOURCC_INVALID 0x00000000
  27537. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  27538. +
  27539. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  27540. +
  27541. +typedef uint32_t BITSET_T;
  27542. +
  27543. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  27544. +
  27545. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  27546. +#define BITSET_WORD(b) (b >> 5)
  27547. +#define BITSET_BIT(b) (1 << (b & 31))
  27548. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  27549. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  27550. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  27551. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  27552. +
  27553. +#if VCHIQ_ENABLE_STATS
  27554. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  27555. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  27556. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  27557. + (service->stats. stat += addend)
  27558. +#else
  27559. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  27560. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  27561. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  27562. +#endif
  27563. +
  27564. +enum {
  27565. + DEBUG_ENTRIES,
  27566. +#if VCHIQ_ENABLE_DEBUG
  27567. + DEBUG_SLOT_HANDLER_COUNT,
  27568. + DEBUG_SLOT_HANDLER_LINE,
  27569. + DEBUG_PARSE_LINE,
  27570. + DEBUG_PARSE_HEADER,
  27571. + DEBUG_PARSE_MSGID,
  27572. + DEBUG_AWAIT_COMPLETION_LINE,
  27573. + DEBUG_DEQUEUE_MESSAGE_LINE,
  27574. + DEBUG_SERVICE_CALLBACK_LINE,
  27575. + DEBUG_MSG_QUEUE_FULL_COUNT,
  27576. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  27577. +#endif
  27578. + DEBUG_MAX
  27579. +};
  27580. +
  27581. +#if VCHIQ_ENABLE_DEBUG
  27582. +
  27583. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  27584. +#define DEBUG_TRACE(d) \
  27585. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  27586. +#define DEBUG_VALUE(d, v) \
  27587. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  27588. +#define DEBUG_COUNT(d) \
  27589. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  27590. +
  27591. +#else /* VCHIQ_ENABLE_DEBUG */
  27592. +
  27593. +#define DEBUG_INITIALISE(local)
  27594. +#define DEBUG_TRACE(d)
  27595. +#define DEBUG_VALUE(d, v)
  27596. +#define DEBUG_COUNT(d)
  27597. +
  27598. +#endif /* VCHIQ_ENABLE_DEBUG */
  27599. +
  27600. +typedef enum {
  27601. + VCHIQ_CONNSTATE_DISCONNECTED,
  27602. + VCHIQ_CONNSTATE_CONNECTING,
  27603. + VCHIQ_CONNSTATE_CONNECTED,
  27604. + VCHIQ_CONNSTATE_PAUSING,
  27605. + VCHIQ_CONNSTATE_PAUSE_SENT,
  27606. + VCHIQ_CONNSTATE_PAUSED,
  27607. + VCHIQ_CONNSTATE_RESUMING,
  27608. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  27609. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  27610. +} VCHIQ_CONNSTATE_T;
  27611. +
  27612. +enum {
  27613. + VCHIQ_SRVSTATE_FREE,
  27614. + VCHIQ_SRVSTATE_HIDDEN,
  27615. + VCHIQ_SRVSTATE_LISTENING,
  27616. + VCHIQ_SRVSTATE_OPENING,
  27617. + VCHIQ_SRVSTATE_OPEN,
  27618. + VCHIQ_SRVSTATE_OPENSYNC,
  27619. + VCHIQ_SRVSTATE_CLOSESENT,
  27620. + VCHIQ_SRVSTATE_CLOSERECVD,
  27621. + VCHIQ_SRVSTATE_CLOSEWAIT,
  27622. + VCHIQ_SRVSTATE_CLOSED
  27623. +};
  27624. +
  27625. +enum {
  27626. + VCHIQ_POLL_TERMINATE,
  27627. + VCHIQ_POLL_REMOVE,
  27628. + VCHIQ_POLL_TXNOTIFY,
  27629. + VCHIQ_POLL_RXNOTIFY,
  27630. + VCHIQ_POLL_COUNT
  27631. +};
  27632. +
  27633. +typedef enum {
  27634. + VCHIQ_BULK_TRANSMIT,
  27635. + VCHIQ_BULK_RECEIVE
  27636. +} VCHIQ_BULK_DIR_T;
  27637. +
  27638. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  27639. +
  27640. +typedef struct vchiq_bulk_struct {
  27641. + short mode;
  27642. + short dir;
  27643. + void *userdata;
  27644. + VCHI_MEM_HANDLE_T handle;
  27645. + void *data;
  27646. + int size;
  27647. + void *remote_data;
  27648. + int remote_size;
  27649. + int actual;
  27650. +} VCHIQ_BULK_T;
  27651. +
  27652. +typedef struct vchiq_bulk_queue_struct {
  27653. + int local_insert; /* Where to insert the next local bulk */
  27654. + int remote_insert; /* Where to insert the next remote bulk (master) */
  27655. + int process; /* Bulk to transfer next */
  27656. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  27657. + int remove; /* Bulk to notify the local client of, and remove,
  27658. + ** next */
  27659. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  27660. +} VCHIQ_BULK_QUEUE_T;
  27661. +
  27662. +typedef struct remote_event_struct {
  27663. + int armed;
  27664. + int fired;
  27665. + struct semaphore *event;
  27666. +} REMOTE_EVENT_T;
  27667. +
  27668. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  27669. +
  27670. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  27671. +
  27672. +typedef struct vchiq_slot_struct {
  27673. + char data[VCHIQ_SLOT_SIZE];
  27674. +} VCHIQ_SLOT_T;
  27675. +
  27676. +typedef struct vchiq_slot_info_struct {
  27677. + /* Use two counters rather than one to avoid the need for a mutex. */
  27678. + short use_count;
  27679. + short release_count;
  27680. +} VCHIQ_SLOT_INFO_T;
  27681. +
  27682. +typedef struct vchiq_service_struct {
  27683. + VCHIQ_SERVICE_BASE_T base;
  27684. + VCHIQ_SERVICE_HANDLE_T handle;
  27685. + unsigned int ref_count;
  27686. + int srvstate;
  27687. + VCHIQ_USERDATA_TERM_T userdata_term;
  27688. + unsigned int localport;
  27689. + unsigned int remoteport;
  27690. + int public_fourcc;
  27691. + int client_id;
  27692. + char auto_close;
  27693. + char sync;
  27694. + char closing;
  27695. + atomic_t poll_flags;
  27696. + short version;
  27697. + short version_min;
  27698. + short peer_version;
  27699. +
  27700. + VCHIQ_STATE_T *state;
  27701. + VCHIQ_INSTANCE_T instance;
  27702. +
  27703. + int service_use_count;
  27704. +
  27705. + VCHIQ_BULK_QUEUE_T bulk_tx;
  27706. + VCHIQ_BULK_QUEUE_T bulk_rx;
  27707. +
  27708. + struct semaphore remove_event;
  27709. + struct semaphore bulk_remove_event;
  27710. + struct mutex bulk_mutex;
  27711. +
  27712. + struct service_stats_struct {
  27713. + int quota_stalls;
  27714. + int slot_stalls;
  27715. + int bulk_stalls;
  27716. + int error_count;
  27717. + int ctrl_tx_count;
  27718. + int ctrl_rx_count;
  27719. + int bulk_tx_count;
  27720. + int bulk_rx_count;
  27721. + int bulk_aborted_count;
  27722. + uint64_t ctrl_tx_bytes;
  27723. + uint64_t ctrl_rx_bytes;
  27724. + uint64_t bulk_tx_bytes;
  27725. + uint64_t bulk_rx_bytes;
  27726. + } stats;
  27727. +} VCHIQ_SERVICE_T;
  27728. +
  27729. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  27730. + statically allocated, since for accounting reasons a service's slot
  27731. + usage is carried over between users of the same port number.
  27732. + */
  27733. +typedef struct vchiq_service_quota_struct {
  27734. + unsigned short slot_quota;
  27735. + unsigned short slot_use_count;
  27736. + unsigned short message_quota;
  27737. + unsigned short message_use_count;
  27738. + struct semaphore quota_event;
  27739. + int previous_tx_index;
  27740. +} VCHIQ_SERVICE_QUOTA_T;
  27741. +
  27742. +typedef struct vchiq_shared_state_struct {
  27743. +
  27744. + /* A non-zero value here indicates that the content is valid. */
  27745. + int initialised;
  27746. +
  27747. + /* The first and last (inclusive) slots allocated to the owner. */
  27748. + int slot_first;
  27749. + int slot_last;
  27750. +
  27751. + /* The slot allocated to synchronous messages from the owner. */
  27752. + int slot_sync;
  27753. +
  27754. + /* Signalling this event indicates that owner's slot handler thread
  27755. + ** should run. */
  27756. + REMOTE_EVENT_T trigger;
  27757. +
  27758. + /* Indicates the byte position within the stream where the next message
  27759. + ** will be written. The least significant bits are an index into the
  27760. + ** slot. The next bits are the index of the slot in slot_queue. */
  27761. + int tx_pos;
  27762. +
  27763. + /* This event should be signalled when a slot is recycled. */
  27764. + REMOTE_EVENT_T recycle;
  27765. +
  27766. + /* The slot_queue index where the next recycled slot will be written. */
  27767. + int slot_queue_recycle;
  27768. +
  27769. + /* This event should be signalled when a synchronous message is sent. */
  27770. + REMOTE_EVENT_T sync_trigger;
  27771. +
  27772. + /* This event should be signalled when a synchronous message has been
  27773. + ** released. */
  27774. + REMOTE_EVENT_T sync_release;
  27775. +
  27776. + /* A circular buffer of slot indexes. */
  27777. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  27778. +
  27779. + /* Debugging state */
  27780. + int debug[DEBUG_MAX];
  27781. +} VCHIQ_SHARED_STATE_T;
  27782. +
  27783. +typedef struct vchiq_slot_zero_struct {
  27784. + int magic;
  27785. + short version;
  27786. + short version_min;
  27787. + int slot_zero_size;
  27788. + int slot_size;
  27789. + int max_slots;
  27790. + int max_slots_per_side;
  27791. + int platform_data[2];
  27792. + VCHIQ_SHARED_STATE_T master;
  27793. + VCHIQ_SHARED_STATE_T slave;
  27794. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  27795. +} VCHIQ_SLOT_ZERO_T;
  27796. +
  27797. +struct vchiq_state_struct {
  27798. + int id;
  27799. + int initialised;
  27800. + VCHIQ_CONNSTATE_T conn_state;
  27801. + int is_master;
  27802. +
  27803. + VCHIQ_SHARED_STATE_T *local;
  27804. + VCHIQ_SHARED_STATE_T *remote;
  27805. + VCHIQ_SLOT_T *slot_data;
  27806. +
  27807. + unsigned short default_slot_quota;
  27808. + unsigned short default_message_quota;
  27809. +
  27810. + /* Event indicating connect message received */
  27811. + struct semaphore connect;
  27812. +
  27813. + /* Mutex protecting services */
  27814. + struct mutex mutex;
  27815. + VCHIQ_INSTANCE_T *instance;
  27816. +
  27817. + /* Processes incoming messages */
  27818. + struct task_struct *slot_handler_thread;
  27819. +
  27820. + /* Processes recycled slots */
  27821. + struct task_struct *recycle_thread;
  27822. +
  27823. + /* Processes synchronous messages */
  27824. + struct task_struct *sync_thread;
  27825. +
  27826. + /* Local implementation of the trigger remote event */
  27827. + struct semaphore trigger_event;
  27828. +
  27829. + /* Local implementation of the recycle remote event */
  27830. + struct semaphore recycle_event;
  27831. +
  27832. + /* Local implementation of the sync trigger remote event */
  27833. + struct semaphore sync_trigger_event;
  27834. +
  27835. + /* Local implementation of the sync release remote event */
  27836. + struct semaphore sync_release_event;
  27837. +
  27838. + char *tx_data;
  27839. + char *rx_data;
  27840. + VCHIQ_SLOT_INFO_T *rx_info;
  27841. +
  27842. + struct mutex slot_mutex;
  27843. +
  27844. + struct mutex recycle_mutex;
  27845. +
  27846. + struct mutex sync_mutex;
  27847. +
  27848. + struct mutex bulk_transfer_mutex;
  27849. +
  27850. + /* Indicates the byte position within the stream from where the next
  27851. + ** message will be read. The least significant bits are an index into
  27852. + ** the slot.The next bits are the index of the slot in
  27853. + ** remote->slot_queue. */
  27854. + int rx_pos;
  27855. +
  27856. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  27857. + from remote->tx_pos. */
  27858. + int local_tx_pos;
  27859. +
  27860. + /* The slot_queue index of the slot to become available next. */
  27861. + int slot_queue_available;
  27862. +
  27863. + /* A flag to indicate if any poll has been requested */
  27864. + int poll_needed;
  27865. +
  27866. + /* Ths index of the previous slot used for data messages. */
  27867. + int previous_data_index;
  27868. +
  27869. + /* The number of slots occupied by data messages. */
  27870. + unsigned short data_use_count;
  27871. +
  27872. + /* The maximum number of slots to be occupied by data messages. */
  27873. + unsigned short data_quota;
  27874. +
  27875. + /* An array of bit sets indicating which services must be polled. */
  27876. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  27877. +
  27878. + /* The number of the first unused service */
  27879. + int unused_service;
  27880. +
  27881. + /* Signalled when a free slot becomes available. */
  27882. + struct semaphore slot_available_event;
  27883. +
  27884. + struct semaphore slot_remove_event;
  27885. +
  27886. + /* Signalled when a free data slot becomes available. */
  27887. + struct semaphore data_quota_event;
  27888. +
  27889. + /* Incremented when there are bulk transfers which cannot be processed
  27890. + * whilst paused and must be processed on resume */
  27891. + int deferred_bulks;
  27892. +
  27893. + struct state_stats_struct {
  27894. + int slot_stalls;
  27895. + int data_stalls;
  27896. + int ctrl_tx_count;
  27897. + int ctrl_rx_count;
  27898. + int error_count;
  27899. + } stats;
  27900. +
  27901. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  27902. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  27903. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  27904. +
  27905. + VCHIQ_PLATFORM_STATE_T platform_state;
  27906. +};
  27907. +
  27908. +struct bulk_waiter {
  27909. + VCHIQ_BULK_T *bulk;
  27910. + struct semaphore event;
  27911. + int actual;
  27912. +};
  27913. +
  27914. +extern spinlock_t bulk_waiter_spinlock;
  27915. +
  27916. +extern int vchiq_core_log_level;
  27917. +extern int vchiq_core_msg_log_level;
  27918. +extern int vchiq_sync_log_level;
  27919. +
  27920. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  27921. +
  27922. +extern const char *
  27923. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  27924. +
  27925. +extern VCHIQ_SLOT_ZERO_T *
  27926. +vchiq_init_slots(void *mem_base, int mem_size);
  27927. +
  27928. +extern VCHIQ_STATUS_T
  27929. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  27930. + int is_master);
  27931. +
  27932. +extern VCHIQ_STATUS_T
  27933. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  27934. +
  27935. +extern VCHIQ_SERVICE_T *
  27936. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  27937. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  27938. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  27939. +
  27940. +extern VCHIQ_STATUS_T
  27941. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  27942. +
  27943. +extern VCHIQ_STATUS_T
  27944. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  27945. +
  27946. +extern void
  27947. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  27948. +
  27949. +extern void
  27950. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  27951. +
  27952. +extern VCHIQ_STATUS_T
  27953. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  27954. +
  27955. +extern VCHIQ_STATUS_T
  27956. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  27957. +
  27958. +extern VCHIQ_STATUS_T
  27959. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  27960. +
  27961. +extern void
  27962. +remote_event_pollall(VCHIQ_STATE_T *state);
  27963. +
  27964. +extern VCHIQ_STATUS_T
  27965. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  27966. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  27967. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  27968. +
  27969. +extern void
  27970. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  27971. +
  27972. +extern void
  27973. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  27974. +
  27975. +extern void
  27976. +vchiq_loud_error_header(void);
  27977. +
  27978. +extern void
  27979. +vchiq_loud_error_footer(void);
  27980. +
  27981. +extern void
  27982. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  27983. +
  27984. +static inline VCHIQ_SERVICE_T *
  27985. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  27986. +{
  27987. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  27988. + (VCHIQ_MAX_STATES - 1)];
  27989. + if (!state)
  27990. + return NULL;
  27991. +
  27992. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  27993. +}
  27994. +
  27995. +extern VCHIQ_SERVICE_T *
  27996. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  27997. +
  27998. +extern VCHIQ_SERVICE_T *
  27999. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  28000. +
  28001. +extern VCHIQ_SERVICE_T *
  28002. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  28003. + VCHIQ_SERVICE_HANDLE_T handle);
  28004. +
  28005. +extern VCHIQ_SERVICE_T *
  28006. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  28007. + int *pidx);
  28008. +
  28009. +extern void
  28010. +lock_service(VCHIQ_SERVICE_T *service);
  28011. +
  28012. +extern void
  28013. +unlock_service(VCHIQ_SERVICE_T *service);
  28014. +
  28015. +/* The following functions are called from vchiq_core, and external
  28016. +** implementations must be provided. */
  28017. +
  28018. +extern VCHIQ_STATUS_T
  28019. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  28020. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  28021. +
  28022. +extern void
  28023. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  28024. +
  28025. +extern void
  28026. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  28027. +
  28028. +extern VCHIQ_STATUS_T
  28029. +vchiq_copy_from_user(void *dst, const void *src, int size);
  28030. +
  28031. +extern void
  28032. +remote_event_signal(REMOTE_EVENT_T *event);
  28033. +
  28034. +void
  28035. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  28036. +
  28037. +extern void
  28038. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  28039. +
  28040. +extern VCHIQ_STATUS_T
  28041. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  28042. +
  28043. +extern void
  28044. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  28045. +
  28046. +extern void
  28047. +vchiq_dump(void *dump_context, const char *str, int len);
  28048. +
  28049. +extern void
  28050. +vchiq_dump_platform_state(void *dump_context);
  28051. +
  28052. +extern void
  28053. +vchiq_dump_platform_instances(void *dump_context);
  28054. +
  28055. +extern void
  28056. +vchiq_dump_platform_service_state(void *dump_context,
  28057. + VCHIQ_SERVICE_T *service);
  28058. +
  28059. +extern VCHIQ_STATUS_T
  28060. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  28061. +
  28062. +extern VCHIQ_STATUS_T
  28063. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  28064. +
  28065. +extern void
  28066. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  28067. +
  28068. +extern void
  28069. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  28070. +
  28071. +extern VCHIQ_STATUS_T
  28072. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  28073. +
  28074. +extern VCHIQ_STATUS_T
  28075. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  28076. +
  28077. +extern void
  28078. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  28079. +
  28080. +extern VCHIQ_STATUS_T
  28081. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  28082. +
  28083. +extern VCHIQ_STATUS_T
  28084. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  28085. +
  28086. +extern VCHIQ_STATUS_T
  28087. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  28088. +
  28089. +extern void
  28090. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  28091. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  28092. +
  28093. +extern void
  28094. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  28095. +
  28096. +extern void
  28097. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  28098. +
  28099. +
  28100. +extern void
  28101. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  28102. + size_t numBytes);
  28103. +
  28104. +#endif
  28105. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  28106. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 1970-01-01 01:00:00.000000000 +0100
  28107. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 2014-03-11 17:51:16.000000000 +0100
  28108. @@ -0,0 +1,87 @@
  28109. +#!/usr/bin/perl -w
  28110. +
  28111. +use strict;
  28112. +
  28113. +#
  28114. +# Generate a version from available information
  28115. +#
  28116. +
  28117. +my $prefix = shift @ARGV;
  28118. +my $root = shift @ARGV;
  28119. +
  28120. +
  28121. +if ( not defined $root ) {
  28122. + die "usage: $0 prefix root-dir\n";
  28123. +}
  28124. +
  28125. +if ( ! -d $root ) {
  28126. + die "root directory $root not found\n";
  28127. +}
  28128. +
  28129. +my $version = "unknown";
  28130. +my $tainted = "";
  28131. +
  28132. +if ( -d "$root/.git" ) {
  28133. + # attempt to work out git version. only do so
  28134. + # on a linux build host, as cygwin builds are
  28135. + # already slow enough
  28136. +
  28137. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  28138. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  28139. + $version = "no git version";
  28140. + }
  28141. + else {
  28142. + $version = <F>;
  28143. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  28144. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  28145. + }
  28146. +
  28147. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  28148. + $tainted = <G>;
  28149. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  28150. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  28151. + if (length $tainted) {
  28152. + $version = join ' ', $version, "(tainted)";
  28153. + }
  28154. + else {
  28155. + $version = join ' ', $version, "(clean)";
  28156. + }
  28157. + }
  28158. + }
  28159. +}
  28160. +
  28161. +my $hostname = `hostname`;
  28162. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  28163. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  28164. +
  28165. +
  28166. +print STDERR "Version $version\n";
  28167. +print <<EOF;
  28168. +#include "${prefix}_build_info.h"
  28169. +#include <linux/broadcom/vc_debug_sym.h>
  28170. +
  28171. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  28172. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  28173. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  28174. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  28175. +
  28176. +const char *vchiq_get_build_hostname( void )
  28177. +{
  28178. + return vchiq_build_hostname;
  28179. +}
  28180. +
  28181. +const char *vchiq_get_build_version( void )
  28182. +{
  28183. + return vchiq_build_version;
  28184. +}
  28185. +
  28186. +const char *vchiq_get_build_date( void )
  28187. +{
  28188. + return vchiq_build_date;
  28189. +}
  28190. +
  28191. +const char *vchiq_get_build_time( void )
  28192. +{
  28193. + return vchiq_build_time;
  28194. +}
  28195. +EOF
  28196. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  28197. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 1970-01-01 01:00:00.000000000 +0100
  28198. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 2014-03-11 17:51:16.000000000 +0100
  28199. @@ -0,0 +1,40 @@
  28200. +/**
  28201. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28202. + *
  28203. + * Redistribution and use in source and binary forms, with or without
  28204. + * modification, are permitted provided that the following conditions
  28205. + * are met:
  28206. + * 1. Redistributions of source code must retain the above copyright
  28207. + * notice, this list of conditions, and the following disclaimer,
  28208. + * without modification.
  28209. + * 2. Redistributions in binary form must reproduce the above copyright
  28210. + * notice, this list of conditions and the following disclaimer in the
  28211. + * documentation and/or other materials provided with the distribution.
  28212. + * 3. The names of the above-listed copyright holders may not be used
  28213. + * to endorse or promote products derived from this software without
  28214. + * specific prior written permission.
  28215. + *
  28216. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28217. + * GNU General Public License ("GPL") version 2, as published by the Free
  28218. + * Software Foundation.
  28219. + *
  28220. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28221. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28222. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28223. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28224. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28225. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28226. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28227. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28228. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28229. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28230. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28231. + */
  28232. +
  28233. +#ifndef VCHIQ_VCHIQ_H
  28234. +#define VCHIQ_VCHIQ_H
  28235. +
  28236. +#include "vchiq_if.h"
  28237. +#include "vchiq_util.h"
  28238. +
  28239. +#endif
  28240. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  28241. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 1970-01-01 01:00:00.000000000 +0100
  28242. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 2014-03-11 17:32:37.000000000 +0100
  28243. @@ -0,0 +1,188 @@
  28244. +/**
  28245. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28246. + *
  28247. + * Redistribution and use in source and binary forms, with or without
  28248. + * modification, are permitted provided that the following conditions
  28249. + * are met:
  28250. + * 1. Redistributions of source code must retain the above copyright
  28251. + * notice, this list of conditions, and the following disclaimer,
  28252. + * without modification.
  28253. + * 2. Redistributions in binary form must reproduce the above copyright
  28254. + * notice, this list of conditions and the following disclaimer in the
  28255. + * documentation and/or other materials provided with the distribution.
  28256. + * 3. The names of the above-listed copyright holders may not be used
  28257. + * to endorse or promote products derived from this software without
  28258. + * specific prior written permission.
  28259. + *
  28260. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28261. + * GNU General Public License ("GPL") version 2, as published by the Free
  28262. + * Software Foundation.
  28263. + *
  28264. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28265. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28266. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28267. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28268. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28269. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28270. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28271. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28272. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28273. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28274. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28275. + */
  28276. +
  28277. +#ifndef VCHIQ_IF_H
  28278. +#define VCHIQ_IF_H
  28279. +
  28280. +#include "interface/vchi/vchi_mh.h"
  28281. +
  28282. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  28283. +
  28284. +#define VCHIQ_SLOT_SIZE 4096
  28285. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  28286. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  28287. +
  28288. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  28289. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  28290. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  28291. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  28292. +
  28293. +typedef enum {
  28294. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  28295. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  28296. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  28297. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  28298. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  28299. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  28300. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  28301. +} VCHIQ_REASON_T;
  28302. +
  28303. +typedef enum {
  28304. + VCHIQ_ERROR = -1,
  28305. + VCHIQ_SUCCESS = 0,
  28306. + VCHIQ_RETRY = 1
  28307. +} VCHIQ_STATUS_T;
  28308. +
  28309. +typedef enum {
  28310. + VCHIQ_BULK_MODE_CALLBACK,
  28311. + VCHIQ_BULK_MODE_BLOCKING,
  28312. + VCHIQ_BULK_MODE_NOCALLBACK,
  28313. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  28314. +} VCHIQ_BULK_MODE_T;
  28315. +
  28316. +typedef enum {
  28317. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  28318. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  28319. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  28320. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS
  28321. +} VCHIQ_SERVICE_OPTION_T;
  28322. +
  28323. +typedef struct vchiq_header_struct {
  28324. + /* The message identifier - opaque to applications. */
  28325. + int msgid;
  28326. +
  28327. + /* Size of message data. */
  28328. + unsigned int size;
  28329. +
  28330. + char data[0]; /* message */
  28331. +} VCHIQ_HEADER_T;
  28332. +
  28333. +typedef struct {
  28334. + const void *data;
  28335. + unsigned int size;
  28336. +} VCHIQ_ELEMENT_T;
  28337. +
  28338. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  28339. +
  28340. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  28341. + VCHIQ_SERVICE_HANDLE_T, void *);
  28342. +
  28343. +typedef struct vchiq_service_base_struct {
  28344. + int fourcc;
  28345. + VCHIQ_CALLBACK_T callback;
  28346. + void *userdata;
  28347. +} VCHIQ_SERVICE_BASE_T;
  28348. +
  28349. +typedef struct vchiq_service_params_struct {
  28350. + int fourcc;
  28351. + VCHIQ_CALLBACK_T callback;
  28352. + void *userdata;
  28353. + short version; /* Increment for non-trivial changes */
  28354. + short version_min; /* Update for incompatible changes */
  28355. +} VCHIQ_SERVICE_PARAMS_T;
  28356. +
  28357. +typedef struct vchiq_config_struct {
  28358. + unsigned int max_msg_size;
  28359. + unsigned int bulk_threshold; /* The message size above which it
  28360. + is better to use a bulk transfer
  28361. + (<= max_msg_size) */
  28362. + unsigned int max_outstanding_bulks;
  28363. + unsigned int max_services;
  28364. + short version; /* The version of VCHIQ */
  28365. + short version_min; /* The minimum compatible version of VCHIQ */
  28366. +} VCHIQ_CONFIG_T;
  28367. +
  28368. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  28369. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  28370. +
  28371. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  28372. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  28373. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  28374. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  28375. + const VCHIQ_SERVICE_PARAMS_T *params,
  28376. + VCHIQ_SERVICE_HANDLE_T *pservice);
  28377. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  28378. + const VCHIQ_SERVICE_PARAMS_T *params,
  28379. + VCHIQ_SERVICE_HANDLE_T *pservice);
  28380. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  28381. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  28382. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  28383. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  28384. + VCHIQ_SERVICE_HANDLE_T service);
  28385. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  28386. +
  28387. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  28388. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  28389. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  28390. + VCHIQ_HEADER_T *header);
  28391. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  28392. + const void *data, unsigned int size, void *userdata);
  28393. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  28394. + void *data, unsigned int size, void *userdata);
  28395. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  28396. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  28397. + const void *offset, unsigned int size, void *userdata);
  28398. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  28399. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  28400. + void *offset, unsigned int size, void *userdata);
  28401. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  28402. + const void *data, unsigned int size, void *userdata,
  28403. + VCHIQ_BULK_MODE_T mode);
  28404. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  28405. + void *data, unsigned int size, void *userdata,
  28406. + VCHIQ_BULK_MODE_T mode);
  28407. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  28408. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  28409. + void *userdata, VCHIQ_BULK_MODE_T mode);
  28410. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  28411. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  28412. + void *userdata, VCHIQ_BULK_MODE_T mode);
  28413. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  28414. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  28415. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  28416. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  28417. + int config_size, VCHIQ_CONFIG_T *pconfig);
  28418. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  28419. + VCHIQ_SERVICE_OPTION_T option, int value);
  28420. +
  28421. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  28422. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  28423. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  28424. +
  28425. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  28426. + void *ptr, size_t num_bytes);
  28427. +
  28428. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  28429. + short *peer_version);
  28430. +
  28431. +#endif /* VCHIQ_IF_H */
  28432. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  28433. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 1970-01-01 01:00:00.000000000 +0100
  28434. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 2014-03-11 17:32:37.000000000 +0100
  28435. @@ -0,0 +1,129 @@
  28436. +/**
  28437. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28438. + *
  28439. + * Redistribution and use in source and binary forms, with or without
  28440. + * modification, are permitted provided that the following conditions
  28441. + * are met:
  28442. + * 1. Redistributions of source code must retain the above copyright
  28443. + * notice, this list of conditions, and the following disclaimer,
  28444. + * without modification.
  28445. + * 2. Redistributions in binary form must reproduce the above copyright
  28446. + * notice, this list of conditions and the following disclaimer in the
  28447. + * documentation and/or other materials provided with the distribution.
  28448. + * 3. The names of the above-listed copyright holders may not be used
  28449. + * to endorse or promote products derived from this software without
  28450. + * specific prior written permission.
  28451. + *
  28452. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28453. + * GNU General Public License ("GPL") version 2, as published by the Free
  28454. + * Software Foundation.
  28455. + *
  28456. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28457. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28458. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28459. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28460. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28461. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28462. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28463. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28464. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28465. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28466. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28467. + */
  28468. +
  28469. +#ifndef VCHIQ_IOCTLS_H
  28470. +#define VCHIQ_IOCTLS_H
  28471. +
  28472. +#include <linux/ioctl.h>
  28473. +#include "vchiq_if.h"
  28474. +
  28475. +#define VCHIQ_IOC_MAGIC 0xc4
  28476. +#define VCHIQ_INVALID_HANDLE (~0)
  28477. +
  28478. +typedef struct {
  28479. + VCHIQ_SERVICE_PARAMS_T params;
  28480. + int is_open;
  28481. + int is_vchi;
  28482. + unsigned int handle; /* OUT */
  28483. +} VCHIQ_CREATE_SERVICE_T;
  28484. +
  28485. +typedef struct {
  28486. + unsigned int handle;
  28487. + unsigned int count;
  28488. + const VCHIQ_ELEMENT_T *elements;
  28489. +} VCHIQ_QUEUE_MESSAGE_T;
  28490. +
  28491. +typedef struct {
  28492. + unsigned int handle;
  28493. + void *data;
  28494. + unsigned int size;
  28495. + void *userdata;
  28496. + VCHIQ_BULK_MODE_T mode;
  28497. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  28498. +
  28499. +typedef struct {
  28500. + VCHIQ_REASON_T reason;
  28501. + VCHIQ_HEADER_T *header;
  28502. + void *service_userdata;
  28503. + void *bulk_userdata;
  28504. +} VCHIQ_COMPLETION_DATA_T;
  28505. +
  28506. +typedef struct {
  28507. + unsigned int count;
  28508. + VCHIQ_COMPLETION_DATA_T *buf;
  28509. + unsigned int msgbufsize;
  28510. + unsigned int msgbufcount; /* IN/OUT */
  28511. + void **msgbufs;
  28512. +} VCHIQ_AWAIT_COMPLETION_T;
  28513. +
  28514. +typedef struct {
  28515. + unsigned int handle;
  28516. + int blocking;
  28517. + unsigned int bufsize;
  28518. + void *buf;
  28519. +} VCHIQ_DEQUEUE_MESSAGE_T;
  28520. +
  28521. +typedef struct {
  28522. + unsigned int config_size;
  28523. + VCHIQ_CONFIG_T *pconfig;
  28524. +} VCHIQ_GET_CONFIG_T;
  28525. +
  28526. +typedef struct {
  28527. + unsigned int handle;
  28528. + VCHIQ_SERVICE_OPTION_T option;
  28529. + int value;
  28530. +} VCHIQ_SET_SERVICE_OPTION_T;
  28531. +
  28532. +typedef struct {
  28533. + void *virt_addr;
  28534. + size_t num_bytes;
  28535. +} VCHIQ_DUMP_MEM_T;
  28536. +
  28537. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  28538. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  28539. +#define VCHIQ_IOC_CREATE_SERVICE \
  28540. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  28541. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  28542. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  28543. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  28544. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  28545. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  28546. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  28547. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  28548. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  28549. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  28550. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  28551. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  28552. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  28553. +#define VCHIQ_IOC_GET_CONFIG \
  28554. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  28555. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  28556. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  28557. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  28558. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  28559. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  28560. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  28561. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  28562. +#define VCHIQ_IOC_MAX 15
  28563. +
  28564. +#endif
  28565. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  28566. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 1970-01-01 01:00:00.000000000 +0100
  28567. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 2014-03-11 17:32:37.000000000 +0100
  28568. @@ -0,0 +1,456 @@
  28569. +/**
  28570. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28571. + *
  28572. + * Redistribution and use in source and binary forms, with or without
  28573. + * modification, are permitted provided that the following conditions
  28574. + * are met:
  28575. + * 1. Redistributions of source code must retain the above copyright
  28576. + * notice, this list of conditions, and the following disclaimer,
  28577. + * without modification.
  28578. + * 2. Redistributions in binary form must reproduce the above copyright
  28579. + * notice, this list of conditions and the following disclaimer in the
  28580. + * documentation and/or other materials provided with the distribution.
  28581. + * 3. The names of the above-listed copyright holders may not be used
  28582. + * to endorse or promote products derived from this software without
  28583. + * specific prior written permission.
  28584. + *
  28585. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28586. + * GNU General Public License ("GPL") version 2, as published by the Free
  28587. + * Software Foundation.
  28588. + *
  28589. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28590. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28591. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28592. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28593. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28594. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28595. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28596. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28597. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28598. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28599. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28600. + */
  28601. +
  28602. +/* ---- Include Files ---------------------------------------------------- */
  28603. +
  28604. +#include <linux/kernel.h>
  28605. +#include <linux/module.h>
  28606. +#include <linux/mutex.h>
  28607. +
  28608. +#include "vchiq_core.h"
  28609. +#include "vchiq_arm.h"
  28610. +
  28611. +/* ---- Public Variables ------------------------------------------------- */
  28612. +
  28613. +/* ---- Private Constants and Types -------------------------------------- */
  28614. +
  28615. +struct bulk_waiter_node {
  28616. + struct bulk_waiter bulk_waiter;
  28617. + int pid;
  28618. + struct list_head list;
  28619. +};
  28620. +
  28621. +struct vchiq_instance_struct {
  28622. + VCHIQ_STATE_T *state;
  28623. +
  28624. + int connected;
  28625. +
  28626. + struct list_head bulk_waiter_list;
  28627. + struct mutex bulk_waiter_list_mutex;
  28628. +};
  28629. +
  28630. +static VCHIQ_STATUS_T
  28631. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28632. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  28633. +
  28634. +/****************************************************************************
  28635. +*
  28636. +* vchiq_initialise
  28637. +*
  28638. +***************************************************************************/
  28639. +#define VCHIQ_INIT_RETRIES 10
  28640. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  28641. +{
  28642. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28643. + VCHIQ_STATE_T *state;
  28644. + VCHIQ_INSTANCE_T instance = NULL;
  28645. + int i;
  28646. +
  28647. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  28648. +
  28649. + /* VideoCore may not be ready due to boot up timing.
  28650. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  28651. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  28652. + state = vchiq_get_state();
  28653. + if (state)
  28654. + break;
  28655. + udelay(500);
  28656. + }
  28657. + if (i==VCHIQ_INIT_RETRIES) {
  28658. + vchiq_log_error(vchiq_core_log_level,
  28659. + "%s: videocore not initialized\n", __func__);
  28660. + goto failed;
  28661. + } else if (i>0) {
  28662. + vchiq_log_warning(vchiq_core_log_level,
  28663. + "%s: videocore initialized after %d retries\n", __func__, i);
  28664. + }
  28665. +
  28666. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  28667. + if (!instance) {
  28668. + vchiq_log_error(vchiq_core_log_level,
  28669. + "%s: error allocating vchiq instance\n", __func__);
  28670. + goto failed;
  28671. + }
  28672. +
  28673. + instance->connected = 0;
  28674. + instance->state = state;
  28675. + mutex_init(&instance->bulk_waiter_list_mutex);
  28676. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  28677. +
  28678. + *instanceOut = instance;
  28679. +
  28680. + status = VCHIQ_SUCCESS;
  28681. +
  28682. +failed:
  28683. + vchiq_log_trace(vchiq_core_log_level,
  28684. + "%s(%p): returning %d", __func__, instance, status);
  28685. +
  28686. + return status;
  28687. +}
  28688. +EXPORT_SYMBOL(vchiq_initialise);
  28689. +
  28690. +/****************************************************************************
  28691. +*
  28692. +* vchiq_shutdown
  28693. +*
  28694. +***************************************************************************/
  28695. +
  28696. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  28697. +{
  28698. + VCHIQ_STATUS_T status;
  28699. + VCHIQ_STATE_T *state = instance->state;
  28700. +
  28701. + vchiq_log_trace(vchiq_core_log_level,
  28702. + "%s(%p) called", __func__, instance);
  28703. +
  28704. + if (mutex_lock_interruptible(&state->mutex) != 0)
  28705. + return VCHIQ_RETRY;
  28706. +
  28707. + /* Remove all services */
  28708. + status = vchiq_shutdown_internal(state, instance);
  28709. +
  28710. + mutex_unlock(&state->mutex);
  28711. +
  28712. + vchiq_log_trace(vchiq_core_log_level,
  28713. + "%s(%p): returning %d", __func__, instance, status);
  28714. +
  28715. + if (status == VCHIQ_SUCCESS) {
  28716. + struct list_head *pos, *next;
  28717. + list_for_each_safe(pos, next,
  28718. + &instance->bulk_waiter_list) {
  28719. + struct bulk_waiter_node *waiter;
  28720. + waiter = list_entry(pos,
  28721. + struct bulk_waiter_node,
  28722. + list);
  28723. + list_del(pos);
  28724. + vchiq_log_info(vchiq_arm_log_level,
  28725. + "bulk_waiter - cleaned up %x "
  28726. + "for pid %d",
  28727. + (unsigned int)waiter, waiter->pid);
  28728. + kfree(waiter);
  28729. + }
  28730. + kfree(instance);
  28731. + }
  28732. +
  28733. + return status;
  28734. +}
  28735. +EXPORT_SYMBOL(vchiq_shutdown);
  28736. +
  28737. +/****************************************************************************
  28738. +*
  28739. +* vchiq_is_connected
  28740. +*
  28741. +***************************************************************************/
  28742. +
  28743. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  28744. +{
  28745. + return instance->connected;
  28746. +}
  28747. +
  28748. +/****************************************************************************
  28749. +*
  28750. +* vchiq_connect
  28751. +*
  28752. +***************************************************************************/
  28753. +
  28754. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  28755. +{
  28756. + VCHIQ_STATUS_T status;
  28757. + VCHIQ_STATE_T *state = instance->state;
  28758. +
  28759. + vchiq_log_trace(vchiq_core_log_level,
  28760. + "%s(%p) called", __func__, instance);
  28761. +
  28762. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  28763. + vchiq_log_trace(vchiq_core_log_level,
  28764. + "%s: call to mutex_lock failed", __func__);
  28765. + status = VCHIQ_RETRY;
  28766. + goto failed;
  28767. + }
  28768. + status = vchiq_connect_internal(state, instance);
  28769. +
  28770. + if (status == VCHIQ_SUCCESS)
  28771. + instance->connected = 1;
  28772. +
  28773. + mutex_unlock(&state->mutex);
  28774. +
  28775. +failed:
  28776. + vchiq_log_trace(vchiq_core_log_level,
  28777. + "%s(%p): returning %d", __func__, instance, status);
  28778. +
  28779. + return status;
  28780. +}
  28781. +EXPORT_SYMBOL(vchiq_connect);
  28782. +
  28783. +/****************************************************************************
  28784. +*
  28785. +* vchiq_add_service
  28786. +*
  28787. +***************************************************************************/
  28788. +
  28789. +VCHIQ_STATUS_T vchiq_add_service(
  28790. + VCHIQ_INSTANCE_T instance,
  28791. + const VCHIQ_SERVICE_PARAMS_T *params,
  28792. + VCHIQ_SERVICE_HANDLE_T *phandle)
  28793. +{
  28794. + VCHIQ_STATUS_T status;
  28795. + VCHIQ_STATE_T *state = instance->state;
  28796. + VCHIQ_SERVICE_T *service = NULL;
  28797. + int srvstate;
  28798. +
  28799. + vchiq_log_trace(vchiq_core_log_level,
  28800. + "%s(%p) called", __func__, instance);
  28801. +
  28802. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  28803. +
  28804. + srvstate = vchiq_is_connected(instance)
  28805. + ? VCHIQ_SRVSTATE_LISTENING
  28806. + : VCHIQ_SRVSTATE_HIDDEN;
  28807. +
  28808. + service = vchiq_add_service_internal(
  28809. + state,
  28810. + params,
  28811. + srvstate,
  28812. + instance,
  28813. + NULL);
  28814. +
  28815. + if (service) {
  28816. + *phandle = service->handle;
  28817. + status = VCHIQ_SUCCESS;
  28818. + } else
  28819. + status = VCHIQ_ERROR;
  28820. +
  28821. + vchiq_log_trace(vchiq_core_log_level,
  28822. + "%s(%p): returning %d", __func__, instance, status);
  28823. +
  28824. + return status;
  28825. +}
  28826. +EXPORT_SYMBOL(vchiq_add_service);
  28827. +
  28828. +/****************************************************************************
  28829. +*
  28830. +* vchiq_open_service
  28831. +*
  28832. +***************************************************************************/
  28833. +
  28834. +VCHIQ_STATUS_T vchiq_open_service(
  28835. + VCHIQ_INSTANCE_T instance,
  28836. + const VCHIQ_SERVICE_PARAMS_T *params,
  28837. + VCHIQ_SERVICE_HANDLE_T *phandle)
  28838. +{
  28839. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28840. + VCHIQ_STATE_T *state = instance->state;
  28841. + VCHIQ_SERVICE_T *service = NULL;
  28842. +
  28843. + vchiq_log_trace(vchiq_core_log_level,
  28844. + "%s(%p) called", __func__, instance);
  28845. +
  28846. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  28847. +
  28848. + if (!vchiq_is_connected(instance))
  28849. + goto failed;
  28850. +
  28851. + service = vchiq_add_service_internal(state,
  28852. + params,
  28853. + VCHIQ_SRVSTATE_OPENING,
  28854. + instance,
  28855. + NULL);
  28856. +
  28857. + if (service) {
  28858. + status = vchiq_open_service_internal(service, current->pid);
  28859. + if (status == VCHIQ_SUCCESS)
  28860. + *phandle = service->handle;
  28861. + else
  28862. + vchiq_remove_service(service->handle);
  28863. + }
  28864. +
  28865. +failed:
  28866. + vchiq_log_trace(vchiq_core_log_level,
  28867. + "%s(%p): returning %d", __func__, instance, status);
  28868. +
  28869. + return status;
  28870. +}
  28871. +EXPORT_SYMBOL(vchiq_open_service);
  28872. +
  28873. +VCHIQ_STATUS_T
  28874. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  28875. + const void *data, unsigned int size, void *userdata)
  28876. +{
  28877. + return vchiq_bulk_transfer(handle,
  28878. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  28879. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  28880. +}
  28881. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  28882. +
  28883. +VCHIQ_STATUS_T
  28884. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28885. + unsigned int size, void *userdata)
  28886. +{
  28887. + return vchiq_bulk_transfer(handle,
  28888. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  28889. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  28890. +}
  28891. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  28892. +
  28893. +VCHIQ_STATUS_T
  28894. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  28895. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  28896. +{
  28897. + VCHIQ_STATUS_T status;
  28898. +
  28899. + switch (mode) {
  28900. + case VCHIQ_BULK_MODE_NOCALLBACK:
  28901. + case VCHIQ_BULK_MODE_CALLBACK:
  28902. + status = vchiq_bulk_transfer(handle,
  28903. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  28904. + mode, VCHIQ_BULK_TRANSMIT);
  28905. + break;
  28906. + case VCHIQ_BULK_MODE_BLOCKING:
  28907. + status = vchiq_blocking_bulk_transfer(handle,
  28908. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  28909. + break;
  28910. + default:
  28911. + return VCHIQ_ERROR;
  28912. + }
  28913. +
  28914. + return status;
  28915. +}
  28916. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  28917. +
  28918. +VCHIQ_STATUS_T
  28919. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28920. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  28921. +{
  28922. + VCHIQ_STATUS_T status;
  28923. +
  28924. + switch (mode) {
  28925. + case VCHIQ_BULK_MODE_NOCALLBACK:
  28926. + case VCHIQ_BULK_MODE_CALLBACK:
  28927. + status = vchiq_bulk_transfer(handle,
  28928. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  28929. + mode, VCHIQ_BULK_RECEIVE);
  28930. + break;
  28931. + case VCHIQ_BULK_MODE_BLOCKING:
  28932. + status = vchiq_blocking_bulk_transfer(handle,
  28933. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  28934. + break;
  28935. + default:
  28936. + return VCHIQ_ERROR;
  28937. + }
  28938. +
  28939. + return status;
  28940. +}
  28941. +EXPORT_SYMBOL(vchiq_bulk_receive);
  28942. +
  28943. +static VCHIQ_STATUS_T
  28944. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28945. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  28946. +{
  28947. + VCHIQ_INSTANCE_T instance;
  28948. + VCHIQ_SERVICE_T *service;
  28949. + VCHIQ_STATUS_T status;
  28950. + struct bulk_waiter_node *waiter = NULL;
  28951. + struct list_head *pos;
  28952. +
  28953. + service = find_service_by_handle(handle);
  28954. + if (!service)
  28955. + return VCHIQ_ERROR;
  28956. +
  28957. + instance = service->instance;
  28958. +
  28959. + unlock_service(service);
  28960. +
  28961. + mutex_lock(&instance->bulk_waiter_list_mutex);
  28962. + list_for_each(pos, &instance->bulk_waiter_list) {
  28963. + if (list_entry(pos, struct bulk_waiter_node,
  28964. + list)->pid == current->pid) {
  28965. + waiter = list_entry(pos,
  28966. + struct bulk_waiter_node,
  28967. + list);
  28968. + list_del(pos);
  28969. + break;
  28970. + }
  28971. + }
  28972. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  28973. +
  28974. + if (waiter) {
  28975. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  28976. + if (bulk) {
  28977. + /* This thread has an outstanding bulk transfer. */
  28978. + if ((bulk->data != data) ||
  28979. + (bulk->size != size)) {
  28980. + /* This is not a retry of the previous one.
  28981. + ** Cancel the signal when the transfer
  28982. + ** completes. */
  28983. + spin_lock(&bulk_waiter_spinlock);
  28984. + bulk->userdata = NULL;
  28985. + spin_unlock(&bulk_waiter_spinlock);
  28986. + }
  28987. + }
  28988. + }
  28989. +
  28990. + if (!waiter) {
  28991. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  28992. + if (!waiter) {
  28993. + vchiq_log_error(vchiq_core_log_level,
  28994. + "%s - out of memory", __func__);
  28995. + return VCHIQ_ERROR;
  28996. + }
  28997. + }
  28998. +
  28999. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  29000. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  29001. + dir);
  29002. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  29003. + !waiter->bulk_waiter.bulk) {
  29004. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  29005. + if (bulk) {
  29006. + /* Cancel the signal when the transfer
  29007. + ** completes. */
  29008. + spin_lock(&bulk_waiter_spinlock);
  29009. + bulk->userdata = NULL;
  29010. + spin_unlock(&bulk_waiter_spinlock);
  29011. + }
  29012. + kfree(waiter);
  29013. + } else {
  29014. + waiter->pid = current->pid;
  29015. + mutex_lock(&instance->bulk_waiter_list_mutex);
  29016. + list_add(&waiter->list, &instance->bulk_waiter_list);
  29017. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  29018. + vchiq_log_info(vchiq_arm_log_level,
  29019. + "saved bulk_waiter %x for pid %d",
  29020. + (unsigned int)waiter, current->pid);
  29021. + }
  29022. +
  29023. + return status;
  29024. +}
  29025. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  29026. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 1970-01-01 01:00:00.000000000 +0100
  29027. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 2014-03-11 17:32:37.000000000 +0100
  29028. @@ -0,0 +1,71 @@
  29029. +/**
  29030. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29031. + *
  29032. + * Redistribution and use in source and binary forms, with or without
  29033. + * modification, are permitted provided that the following conditions
  29034. + * are met:
  29035. + * 1. Redistributions of source code must retain the above copyright
  29036. + * notice, this list of conditions, and the following disclaimer,
  29037. + * without modification.
  29038. + * 2. Redistributions in binary form must reproduce the above copyright
  29039. + * notice, this list of conditions and the following disclaimer in the
  29040. + * documentation and/or other materials provided with the distribution.
  29041. + * 3. The names of the above-listed copyright holders may not be used
  29042. + * to endorse or promote products derived from this software without
  29043. + * specific prior written permission.
  29044. + *
  29045. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29046. + * GNU General Public License ("GPL") version 2, as published by the Free
  29047. + * Software Foundation.
  29048. + *
  29049. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29050. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29051. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29052. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29053. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29054. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29055. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29056. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29057. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29058. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29059. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29060. + */
  29061. +
  29062. +#ifndef VCHIQ_MEMDRV_H
  29063. +#define VCHIQ_MEMDRV_H
  29064. +
  29065. +/* ---- Include Files ----------------------------------------------------- */
  29066. +
  29067. +#include <linux/kernel.h>
  29068. +#include "vchiq_if.h"
  29069. +
  29070. +/* ---- Constants and Types ---------------------------------------------- */
  29071. +
  29072. +typedef struct {
  29073. + void *armSharedMemVirt;
  29074. + dma_addr_t armSharedMemPhys;
  29075. + size_t armSharedMemSize;
  29076. +
  29077. + void *vcSharedMemVirt;
  29078. + dma_addr_t vcSharedMemPhys;
  29079. + size_t vcSharedMemSize;
  29080. +} VCHIQ_SHARED_MEM_INFO_T;
  29081. +
  29082. +/* ---- Variable Externs ------------------------------------------------- */
  29083. +
  29084. +/* ---- Function Prototypes ---------------------------------------------- */
  29085. +
  29086. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  29087. +
  29088. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  29089. +
  29090. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  29091. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  29092. +
  29093. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  29094. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  29095. +
  29096. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  29097. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  29098. +
  29099. +#endif
  29100. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  29101. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 1970-01-01 01:00:00.000000000 +0100
  29102. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 2014-03-11 17:32:37.000000000 +0100
  29103. @@ -0,0 +1,58 @@
  29104. +/**
  29105. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29106. + *
  29107. + * Redistribution and use in source and binary forms, with or without
  29108. + * modification, are permitted provided that the following conditions
  29109. + * are met:
  29110. + * 1. Redistributions of source code must retain the above copyright
  29111. + * notice, this list of conditions, and the following disclaimer,
  29112. + * without modification.
  29113. + * 2. Redistributions in binary form must reproduce the above copyright
  29114. + * notice, this list of conditions and the following disclaimer in the
  29115. + * documentation and/or other materials provided with the distribution.
  29116. + * 3. The names of the above-listed copyright holders may not be used
  29117. + * to endorse or promote products derived from this software without
  29118. + * specific prior written permission.
  29119. + *
  29120. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29121. + * GNU General Public License ("GPL") version 2, as published by the Free
  29122. + * Software Foundation.
  29123. + *
  29124. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29125. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29126. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29127. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29128. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29129. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29130. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29131. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29132. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29133. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29134. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29135. + */
  29136. +
  29137. +#ifndef VCHIQ_PAGELIST_H
  29138. +#define VCHIQ_PAGELIST_H
  29139. +
  29140. +#ifndef PAGE_SIZE
  29141. +#define PAGE_SIZE 4096
  29142. +#endif
  29143. +#define CACHE_LINE_SIZE 32
  29144. +#define PAGELIST_WRITE 0
  29145. +#define PAGELIST_READ 1
  29146. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  29147. +
  29148. +typedef struct pagelist_struct {
  29149. + unsigned long length;
  29150. + unsigned short type;
  29151. + unsigned short offset;
  29152. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  29153. + pages at consecutive addresses. */
  29154. +} PAGELIST_T;
  29155. +
  29156. +typedef struct fragments_struct {
  29157. + char headbuf[CACHE_LINE_SIZE];
  29158. + char tailbuf[CACHE_LINE_SIZE];
  29159. +} FRAGMENTS_T;
  29160. +
  29161. +#endif /* VCHIQ_PAGELIST_H */
  29162. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c
  29163. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 1970-01-01 01:00:00.000000000 +0100
  29164. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 2014-03-11 17:51:16.000000000 +0100
  29165. @@ -0,0 +1,253 @@
  29166. +/**
  29167. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29168. + *
  29169. + * Redistribution and use in source and binary forms, with or without
  29170. + * modification, are permitted provided that the following conditions
  29171. + * are met:
  29172. + * 1. Redistributions of source code must retain the above copyright
  29173. + * notice, this list of conditions, and the following disclaimer,
  29174. + * without modification.
  29175. + * 2. Redistributions in binary form must reproduce the above copyright
  29176. + * notice, this list of conditions and the following disclaimer in the
  29177. + * documentation and/or other materials provided with the distribution.
  29178. + * 3. The names of the above-listed copyright holders may not be used
  29179. + * to endorse or promote products derived from this software without
  29180. + * specific prior written permission.
  29181. + *
  29182. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29183. + * GNU General Public License ("GPL") version 2, as published by the Free
  29184. + * Software Foundation.
  29185. + *
  29186. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29187. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29188. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29189. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29190. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29191. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29192. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29193. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29194. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29195. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29196. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29197. + */
  29198. +
  29199. +
  29200. +#include <linux/proc_fs.h>
  29201. +#include "vchiq_core.h"
  29202. +#include "vchiq_arm.h"
  29203. +
  29204. +#if 1
  29205. +
  29206. +int vchiq_proc_init(void)
  29207. +{
  29208. + return 0;
  29209. +}
  29210. +
  29211. +void vchiq_proc_deinit(void)
  29212. +{
  29213. +}
  29214. +
  29215. +#else
  29216. +
  29217. +struct vchiq_proc_info {
  29218. + /* Global 'vc' proc entry used by all instances */
  29219. + struct proc_dir_entry *vc_cfg_dir;
  29220. +
  29221. + /* one entry per client process */
  29222. + struct proc_dir_entry *clients;
  29223. +
  29224. + /* log categories */
  29225. + struct proc_dir_entry *log_categories;
  29226. +};
  29227. +
  29228. +static struct vchiq_proc_info proc_info;
  29229. +
  29230. +struct proc_dir_entry *vchiq_proc_top(void)
  29231. +{
  29232. + BUG_ON(proc_info.vc_cfg_dir == NULL);
  29233. + return proc_info.vc_cfg_dir;
  29234. +}
  29235. +
  29236. +/****************************************************************************
  29237. +*
  29238. +* log category entries
  29239. +*
  29240. +***************************************************************************/
  29241. +#define PROC_WRITE_BUF_SIZE 256
  29242. +
  29243. +#define VCHIQ_LOG_ERROR_STR "error"
  29244. +#define VCHIQ_LOG_WARNING_STR "warning"
  29245. +#define VCHIQ_LOG_INFO_STR "info"
  29246. +#define VCHIQ_LOG_TRACE_STR "trace"
  29247. +
  29248. +static int log_cfg_read(char *buffer,
  29249. + char **start,
  29250. + off_t off,
  29251. + int count,
  29252. + int *eof,
  29253. + void *data)
  29254. +{
  29255. + int len = 0;
  29256. + char *log_value = NULL;
  29257. +
  29258. + switch (*((int *)data)) {
  29259. + case VCHIQ_LOG_ERROR:
  29260. + log_value = VCHIQ_LOG_ERROR_STR;
  29261. + break;
  29262. + case VCHIQ_LOG_WARNING:
  29263. + log_value = VCHIQ_LOG_WARNING_STR;
  29264. + break;
  29265. + case VCHIQ_LOG_INFO:
  29266. + log_value = VCHIQ_LOG_INFO_STR;
  29267. + break;
  29268. + case VCHIQ_LOG_TRACE:
  29269. + log_value = VCHIQ_LOG_TRACE_STR;
  29270. + break;
  29271. + default:
  29272. + break;
  29273. + }
  29274. +
  29275. + len += sprintf(buffer + len,
  29276. + "%s\n",
  29277. + log_value ? log_value : "(null)");
  29278. +
  29279. + return len;
  29280. +}
  29281. +
  29282. +
  29283. +static int log_cfg_write(struct file *file,
  29284. + const char __user *buffer,
  29285. + unsigned long count,
  29286. + void *data)
  29287. +{
  29288. + int *log_module = data;
  29289. + char kbuf[PROC_WRITE_BUF_SIZE + 1];
  29290. +
  29291. + (void)file;
  29292. +
  29293. + memset(kbuf, 0, PROC_WRITE_BUF_SIZE + 1);
  29294. + if (count >= PROC_WRITE_BUF_SIZE)
  29295. + count = PROC_WRITE_BUF_SIZE;
  29296. +
  29297. + if (copy_from_user(kbuf,
  29298. + buffer,
  29299. + count) != 0)
  29300. + return -EFAULT;
  29301. + kbuf[count - 1] = 0;
  29302. +
  29303. + if (strncmp("error", kbuf, strlen("error")) == 0)
  29304. + *log_module = VCHIQ_LOG_ERROR;
  29305. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  29306. + *log_module = VCHIQ_LOG_WARNING;
  29307. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  29308. + *log_module = VCHIQ_LOG_INFO;
  29309. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  29310. + *log_module = VCHIQ_LOG_TRACE;
  29311. + else
  29312. + *log_module = VCHIQ_LOG_DEFAULT;
  29313. +
  29314. + return count;
  29315. +}
  29316. +
  29317. +/* Log category proc entries */
  29318. +struct vchiq_proc_log_entry {
  29319. + const char *name;
  29320. + int *plevel;
  29321. + struct proc_dir_entry *dir;
  29322. +};
  29323. +
  29324. +static struct vchiq_proc_log_entry vchiq_proc_log_entries[] = {
  29325. + { "core", &vchiq_core_log_level },
  29326. + { "msg", &vchiq_core_msg_log_level },
  29327. + { "sync", &vchiq_sync_log_level },
  29328. + { "susp", &vchiq_susp_log_level },
  29329. + { "arm", &vchiq_arm_log_level },
  29330. +};
  29331. +static int n_log_entries =
  29332. + sizeof(vchiq_proc_log_entries)/sizeof(vchiq_proc_log_entries[0]);
  29333. +
  29334. +/* create an entry under /proc/vc/log for each log category */
  29335. +static int vchiq_proc_create_log_entries(struct proc_dir_entry *top)
  29336. +{
  29337. + struct proc_dir_entry *dir;
  29338. + size_t i;
  29339. + int ret = 0;
  29340. + dir = proc_mkdir("log", proc_info.vc_cfg_dir);
  29341. + if (!dir)
  29342. + return -ENOMEM;
  29343. + proc_info.log_categories = dir;
  29344. +
  29345. + for (i = 0; i < n_log_entries; i++) {
  29346. + dir = create_proc_entry(vchiq_proc_log_entries[i].name,
  29347. + 0644,
  29348. + proc_info.log_categories);
  29349. + if (!dir) {
  29350. + ret = -ENOMEM;
  29351. + break;
  29352. + }
  29353. +
  29354. + dir->read_proc = &log_cfg_read;
  29355. + dir->write_proc = &log_cfg_write;
  29356. + dir->data = (void *)vchiq_proc_log_entries[i].plevel;
  29357. +
  29358. + vchiq_proc_log_entries[i].dir = dir;
  29359. + }
  29360. + return ret;
  29361. +}
  29362. +
  29363. +
  29364. +int vchiq_proc_init(void)
  29365. +{
  29366. + BUG_ON(proc_info.vc_cfg_dir != NULL);
  29367. +
  29368. + proc_info.vc_cfg_dir = proc_mkdir("vc", NULL);
  29369. + if (proc_info.vc_cfg_dir == NULL)
  29370. + goto fail;
  29371. +
  29372. + proc_info.clients = proc_mkdir("clients",
  29373. + proc_info.vc_cfg_dir);
  29374. + if (!proc_info.clients)
  29375. + goto fail;
  29376. +
  29377. + if (vchiq_proc_create_log_entries(proc_info.vc_cfg_dir) != 0)
  29378. + goto fail;
  29379. +
  29380. + return 0;
  29381. +
  29382. +fail:
  29383. + vchiq_proc_deinit();
  29384. + vchiq_log_error(vchiq_arm_log_level,
  29385. + "%s: failed to create proc directory",
  29386. + __func__);
  29387. +
  29388. + return -ENOMEM;
  29389. +}
  29390. +
  29391. +/* remove all the proc entries */
  29392. +void vchiq_proc_deinit(void)
  29393. +{
  29394. + /* log category entries */
  29395. + if (proc_info.log_categories) {
  29396. + size_t i;
  29397. + for (i = 0; i < n_log_entries; i++)
  29398. + if (vchiq_proc_log_entries[i].dir)
  29399. + remove_proc_entry(
  29400. + vchiq_proc_log_entries[i].name,
  29401. + proc_info.log_categories);
  29402. +
  29403. + remove_proc_entry(proc_info.log_categories->name,
  29404. + proc_info.vc_cfg_dir);
  29405. + }
  29406. + if (proc_info.clients)
  29407. + remove_proc_entry(proc_info.clients->name,
  29408. + proc_info.vc_cfg_dir);
  29409. + if (proc_info.vc_cfg_dir)
  29410. + remove_proc_entry(proc_info.vc_cfg_dir->name, NULL);
  29411. +}
  29412. +
  29413. +struct proc_dir_entry *vchiq_clients_top(void)
  29414. +{
  29415. + return proc_info.clients;
  29416. +}
  29417. +
  29418. +#endif
  29419. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  29420. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 1970-01-01 01:00:00.000000000 +0100
  29421. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 2014-03-11 17:32:37.000000000 +0100
  29422. @@ -0,0 +1,828 @@
  29423. +/**
  29424. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29425. + *
  29426. + * Redistribution and use in source and binary forms, with or without
  29427. + * modification, are permitted provided that the following conditions
  29428. + * are met:
  29429. + * 1. Redistributions of source code must retain the above copyright
  29430. + * notice, this list of conditions, and the following disclaimer,
  29431. + * without modification.
  29432. + * 2. Redistributions in binary form must reproduce the above copyright
  29433. + * notice, this list of conditions and the following disclaimer in the
  29434. + * documentation and/or other materials provided with the distribution.
  29435. + * 3. The names of the above-listed copyright holders may not be used
  29436. + * to endorse or promote products derived from this software without
  29437. + * specific prior written permission.
  29438. + *
  29439. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29440. + * GNU General Public License ("GPL") version 2, as published by the Free
  29441. + * Software Foundation.
  29442. + *
  29443. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29444. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29445. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29446. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29447. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29448. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29449. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29450. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29451. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29452. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29453. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29454. + */
  29455. +#include <linux/module.h>
  29456. +#include <linux/types.h>
  29457. +
  29458. +#include "interface/vchi/vchi.h"
  29459. +#include "vchiq.h"
  29460. +#include "vchiq_core.h"
  29461. +
  29462. +#include "vchiq_util.h"
  29463. +
  29464. +#include <stddef.h>
  29465. +
  29466. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  29467. +
  29468. +typedef struct {
  29469. + VCHIQ_SERVICE_HANDLE_T handle;
  29470. +
  29471. + VCHIU_QUEUE_T queue;
  29472. +
  29473. + VCHI_CALLBACK_T callback;
  29474. + void *callback_param;
  29475. +} SHIM_SERVICE_T;
  29476. +
  29477. +/* ----------------------------------------------------------------------
  29478. + * return pointer to the mphi message driver function table
  29479. + * -------------------------------------------------------------------- */
  29480. +const VCHI_MESSAGE_DRIVER_T *
  29481. +vchi_mphi_message_driver_func_table(void)
  29482. +{
  29483. + return NULL;
  29484. +}
  29485. +
  29486. +/* ----------------------------------------------------------------------
  29487. + * return a pointer to the 'single' connection driver fops
  29488. + * -------------------------------------------------------------------- */
  29489. +const VCHI_CONNECTION_API_T *
  29490. +single_get_func_table(void)
  29491. +{
  29492. + return NULL;
  29493. +}
  29494. +
  29495. +VCHI_CONNECTION_T *vchi_create_connection(
  29496. + const VCHI_CONNECTION_API_T *function_table,
  29497. + const VCHI_MESSAGE_DRIVER_T *low_level)
  29498. +{
  29499. + (void)function_table;
  29500. + (void)low_level;
  29501. + return NULL;
  29502. +}
  29503. +
  29504. +/***********************************************************
  29505. + * Name: vchi_msg_peek
  29506. + *
  29507. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  29508. + * void **data,
  29509. + * uint32_t *msg_size,
  29510. +
  29511. +
  29512. + * VCHI_FLAGS_T flags
  29513. + *
  29514. + * Description: Routine to return a pointer to the current message (to allow in
  29515. + * place processing). The message can be removed using
  29516. + * vchi_msg_remove when you're finished
  29517. + *
  29518. + * Returns: int32_t - success == 0
  29519. + *
  29520. + ***********************************************************/
  29521. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  29522. + void **data,
  29523. + uint32_t *msg_size,
  29524. + VCHI_FLAGS_T flags)
  29525. +{
  29526. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29527. + VCHIQ_HEADER_T *header;
  29528. +
  29529. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  29530. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  29531. +
  29532. + if (flags == VCHI_FLAGS_NONE)
  29533. + if (vchiu_queue_is_empty(&service->queue))
  29534. + return -1;
  29535. +
  29536. + header = vchiu_queue_peek(&service->queue);
  29537. +
  29538. + *data = header->data;
  29539. + *msg_size = header->size;
  29540. +
  29541. + return 0;
  29542. +}
  29543. +EXPORT_SYMBOL(vchi_msg_peek);
  29544. +
  29545. +/***********************************************************
  29546. + * Name: vchi_msg_remove
  29547. + *
  29548. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  29549. + *
  29550. + * Description: Routine to remove a message (after it has been read with
  29551. + * vchi_msg_peek)
  29552. + *
  29553. + * Returns: int32_t - success == 0
  29554. + *
  29555. + ***********************************************************/
  29556. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  29557. +{
  29558. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29559. + VCHIQ_HEADER_T *header;
  29560. +
  29561. + header = vchiu_queue_pop(&service->queue);
  29562. +
  29563. + vchiq_release_message(service->handle, header);
  29564. +
  29565. + return 0;
  29566. +}
  29567. +EXPORT_SYMBOL(vchi_msg_remove);
  29568. +
  29569. +/***********************************************************
  29570. + * Name: vchi_msg_queue
  29571. + *
  29572. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29573. + * const void *data,
  29574. + * uint32_t data_size,
  29575. + * VCHI_FLAGS_T flags,
  29576. + * void *msg_handle,
  29577. + *
  29578. + * Description: Thin wrapper to queue a message onto a connection
  29579. + *
  29580. + * Returns: int32_t - success == 0
  29581. + *
  29582. + ***********************************************************/
  29583. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  29584. + const void *data,
  29585. + uint32_t data_size,
  29586. + VCHI_FLAGS_T flags,
  29587. + void *msg_handle)
  29588. +{
  29589. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29590. + VCHIQ_ELEMENT_T element = {data, data_size};
  29591. + VCHIQ_STATUS_T status;
  29592. +
  29593. + (void)msg_handle;
  29594. +
  29595. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  29596. +
  29597. + status = vchiq_queue_message(service->handle, &element, 1);
  29598. +
  29599. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  29600. + ** implement a retry mechanism since this function is supposed
  29601. + ** to block until queued
  29602. + */
  29603. + while (status == VCHIQ_RETRY) {
  29604. + msleep(1);
  29605. + status = vchiq_queue_message(service->handle, &element, 1);
  29606. + }
  29607. +
  29608. + return vchiq_status_to_vchi(status);
  29609. +}
  29610. +EXPORT_SYMBOL(vchi_msg_queue);
  29611. +
  29612. +/***********************************************************
  29613. + * Name: vchi_bulk_queue_receive
  29614. + *
  29615. + * Arguments: VCHI_BULK_HANDLE_T handle,
  29616. + * void *data_dst,
  29617. + * const uint32_t data_size,
  29618. + * VCHI_FLAGS_T flags
  29619. + * void *bulk_handle
  29620. + *
  29621. + * Description: Routine to setup a rcv buffer
  29622. + *
  29623. + * Returns: int32_t - success == 0
  29624. + *
  29625. + ***********************************************************/
  29626. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  29627. + void *data_dst,
  29628. + uint32_t data_size,
  29629. + VCHI_FLAGS_T flags,
  29630. + void *bulk_handle)
  29631. +{
  29632. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29633. + VCHIQ_BULK_MODE_T mode;
  29634. + VCHIQ_STATUS_T status;
  29635. +
  29636. + switch ((int)flags) {
  29637. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  29638. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29639. + WARN_ON(!service->callback);
  29640. + mode = VCHIQ_BULK_MODE_CALLBACK;
  29641. + break;
  29642. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  29643. + mode = VCHIQ_BULK_MODE_BLOCKING;
  29644. + break;
  29645. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29646. + case VCHI_FLAGS_NONE:
  29647. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  29648. + break;
  29649. + default:
  29650. + WARN(1, "unsupported message\n");
  29651. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  29652. + }
  29653. +
  29654. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  29655. + bulk_handle, mode);
  29656. +
  29657. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  29658. + ** implement a retry mechanism since this function is supposed
  29659. + ** to block until queued
  29660. + */
  29661. + while (status == VCHIQ_RETRY) {
  29662. + msleep(1);
  29663. + status = vchiq_bulk_receive(service->handle, data_dst,
  29664. + data_size, bulk_handle, mode);
  29665. + }
  29666. +
  29667. + return vchiq_status_to_vchi(status);
  29668. +}
  29669. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  29670. +
  29671. +/***********************************************************
  29672. + * Name: vchi_bulk_queue_transmit
  29673. + *
  29674. + * Arguments: VCHI_BULK_HANDLE_T handle,
  29675. + * const void *data_src,
  29676. + * uint32_t data_size,
  29677. + * VCHI_FLAGS_T flags,
  29678. + * void *bulk_handle
  29679. + *
  29680. + * Description: Routine to transmit some data
  29681. + *
  29682. + * Returns: int32_t - success == 0
  29683. + *
  29684. + ***********************************************************/
  29685. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  29686. + const void *data_src,
  29687. + uint32_t data_size,
  29688. + VCHI_FLAGS_T flags,
  29689. + void *bulk_handle)
  29690. +{
  29691. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29692. + VCHIQ_BULK_MODE_T mode;
  29693. + VCHIQ_STATUS_T status;
  29694. +
  29695. + switch ((int)flags) {
  29696. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  29697. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29698. + WARN_ON(!service->callback);
  29699. + mode = VCHIQ_BULK_MODE_CALLBACK;
  29700. + break;
  29701. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  29702. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  29703. + mode = VCHIQ_BULK_MODE_BLOCKING;
  29704. + break;
  29705. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29706. + case VCHI_FLAGS_NONE:
  29707. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  29708. + break;
  29709. + default:
  29710. + WARN(1, "unsupported message\n");
  29711. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  29712. + }
  29713. +
  29714. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  29715. + bulk_handle, mode);
  29716. +
  29717. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  29718. + ** implement a retry mechanism since this function is supposed
  29719. + ** to block until queued
  29720. + */
  29721. + while (status == VCHIQ_RETRY) {
  29722. + msleep(1);
  29723. + status = vchiq_bulk_transmit(service->handle, data_src,
  29724. + data_size, bulk_handle, mode);
  29725. + }
  29726. +
  29727. + return vchiq_status_to_vchi(status);
  29728. +}
  29729. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  29730. +
  29731. +/***********************************************************
  29732. + * Name: vchi_msg_dequeue
  29733. + *
  29734. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29735. + * void *data,
  29736. + * uint32_t max_data_size_to_read,
  29737. + * uint32_t *actual_msg_size
  29738. + * VCHI_FLAGS_T flags
  29739. + *
  29740. + * Description: Routine to dequeue a message into the supplied buffer
  29741. + *
  29742. + * Returns: int32_t - success == 0
  29743. + *
  29744. + ***********************************************************/
  29745. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  29746. + void *data,
  29747. + uint32_t max_data_size_to_read,
  29748. + uint32_t *actual_msg_size,
  29749. + VCHI_FLAGS_T flags)
  29750. +{
  29751. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29752. + VCHIQ_HEADER_T *header;
  29753. +
  29754. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  29755. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  29756. +
  29757. + if (flags == VCHI_FLAGS_NONE)
  29758. + if (vchiu_queue_is_empty(&service->queue))
  29759. + return -1;
  29760. +
  29761. + header = vchiu_queue_pop(&service->queue);
  29762. +
  29763. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  29764. + header->size : max_data_size_to_read);
  29765. +
  29766. + *actual_msg_size = header->size;
  29767. +
  29768. + vchiq_release_message(service->handle, header);
  29769. +
  29770. + return 0;
  29771. +}
  29772. +EXPORT_SYMBOL(vchi_msg_dequeue);
  29773. +
  29774. +/***********************************************************
  29775. + * Name: vchi_msg_queuev
  29776. + *
  29777. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29778. + * VCHI_MSG_VECTOR_T *vector,
  29779. + * uint32_t count,
  29780. + * VCHI_FLAGS_T flags,
  29781. + * void *msg_handle
  29782. + *
  29783. + * Description: Thin wrapper to queue a message onto a connection
  29784. + *
  29785. + * Returns: int32_t - success == 0
  29786. + *
  29787. + ***********************************************************/
  29788. +
  29789. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  29790. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  29791. + offsetof(VCHIQ_ELEMENT_T, data));
  29792. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  29793. + offsetof(VCHIQ_ELEMENT_T, size));
  29794. +
  29795. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  29796. + VCHI_MSG_VECTOR_T *vector,
  29797. + uint32_t count,
  29798. + VCHI_FLAGS_T flags,
  29799. + void *msg_handle)
  29800. +{
  29801. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29802. +
  29803. + (void)msg_handle;
  29804. +
  29805. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  29806. +
  29807. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  29808. + (const VCHIQ_ELEMENT_T *)vector, count));
  29809. +}
  29810. +EXPORT_SYMBOL(vchi_msg_queuev);
  29811. +
  29812. +/***********************************************************
  29813. + * Name: vchi_held_msg_release
  29814. + *
  29815. + * Arguments: VCHI_HELD_MSG_T *message
  29816. + *
  29817. + * Description: Routine to release a held message (after it has been read with
  29818. + * vchi_msg_hold)
  29819. + *
  29820. + * Returns: int32_t - success == 0
  29821. + *
  29822. + ***********************************************************/
  29823. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  29824. +{
  29825. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  29826. + (VCHIQ_HEADER_T *)message->message);
  29827. +
  29828. + return 0;
  29829. +}
  29830. +EXPORT_SYMBOL(vchi_held_msg_release);
  29831. +
  29832. +/***********************************************************
  29833. + * Name: vchi_msg_hold
  29834. + *
  29835. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29836. + * void **data,
  29837. + * uint32_t *msg_size,
  29838. + * VCHI_FLAGS_T flags,
  29839. + * VCHI_HELD_MSG_T *message_handle
  29840. + *
  29841. + * Description: Routine to return a pointer to the current message (to allow
  29842. + * in place processing). The message is dequeued - don't forget
  29843. + * to release the message using vchi_held_msg_release when you're
  29844. + * finished.
  29845. + *
  29846. + * Returns: int32_t - success == 0
  29847. + *
  29848. + ***********************************************************/
  29849. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  29850. + void **data,
  29851. + uint32_t *msg_size,
  29852. + VCHI_FLAGS_T flags,
  29853. + VCHI_HELD_MSG_T *message_handle)
  29854. +{
  29855. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29856. + VCHIQ_HEADER_T *header;
  29857. +
  29858. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  29859. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  29860. +
  29861. + if (flags == VCHI_FLAGS_NONE)
  29862. + if (vchiu_queue_is_empty(&service->queue))
  29863. + return -1;
  29864. +
  29865. + header = vchiu_queue_pop(&service->queue);
  29866. +
  29867. + *data = header->data;
  29868. + *msg_size = header->size;
  29869. +
  29870. + message_handle->service =
  29871. + (struct opaque_vchi_service_t *)service->handle;
  29872. + message_handle->message = header;
  29873. +
  29874. + return 0;
  29875. +}
  29876. +EXPORT_SYMBOL(vchi_msg_hold);
  29877. +
  29878. +/***********************************************************
  29879. + * Name: vchi_initialise
  29880. + *
  29881. + * Arguments: VCHI_INSTANCE_T *instance_handle
  29882. + * VCHI_CONNECTION_T **connections
  29883. + * const uint32_t num_connections
  29884. + *
  29885. + * Description: Initialises the hardware but does not transmit anything
  29886. + * When run as a Host App this will be called twice hence the need
  29887. + * to malloc the state information
  29888. + *
  29889. + * Returns: 0 if successful, failure otherwise
  29890. + *
  29891. + ***********************************************************/
  29892. +
  29893. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  29894. +{
  29895. + VCHIQ_INSTANCE_T instance;
  29896. + VCHIQ_STATUS_T status;
  29897. +
  29898. + status = vchiq_initialise(&instance);
  29899. +
  29900. + *instance_handle = (VCHI_INSTANCE_T)instance;
  29901. +
  29902. + return vchiq_status_to_vchi(status);
  29903. +}
  29904. +EXPORT_SYMBOL(vchi_initialise);
  29905. +
  29906. +/***********************************************************
  29907. + * Name: vchi_connect
  29908. + *
  29909. + * Arguments: VCHI_CONNECTION_T **connections
  29910. + * const uint32_t num_connections
  29911. + * VCHI_INSTANCE_T instance_handle)
  29912. + *
  29913. + * Description: Starts the command service on each connection,
  29914. + * causing INIT messages to be pinged back and forth
  29915. + *
  29916. + * Returns: 0 if successful, failure otherwise
  29917. + *
  29918. + ***********************************************************/
  29919. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  29920. + const uint32_t num_connections,
  29921. + VCHI_INSTANCE_T instance_handle)
  29922. +{
  29923. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  29924. +
  29925. + (void)connections;
  29926. + (void)num_connections;
  29927. +
  29928. + return vchiq_connect(instance);
  29929. +}
  29930. +EXPORT_SYMBOL(vchi_connect);
  29931. +
  29932. +
  29933. +/***********************************************************
  29934. + * Name: vchi_disconnect
  29935. + *
  29936. + * Arguments: VCHI_INSTANCE_T instance_handle
  29937. + *
  29938. + * Description: Stops the command service on each connection,
  29939. + * causing DE-INIT messages to be pinged back and forth
  29940. + *
  29941. + * Returns: 0 if successful, failure otherwise
  29942. + *
  29943. + ***********************************************************/
  29944. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  29945. +{
  29946. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  29947. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  29948. +}
  29949. +EXPORT_SYMBOL(vchi_disconnect);
  29950. +
  29951. +
  29952. +/***********************************************************
  29953. + * Name: vchi_service_open
  29954. + * Name: vchi_service_create
  29955. + *
  29956. + * Arguments: VCHI_INSTANCE_T *instance_handle
  29957. + * SERVICE_CREATION_T *setup,
  29958. + * VCHI_SERVICE_HANDLE_T *handle
  29959. + *
  29960. + * Description: Routine to open a service
  29961. + *
  29962. + * Returns: int32_t - success == 0
  29963. + *
  29964. + ***********************************************************/
  29965. +
  29966. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  29967. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  29968. +{
  29969. + SHIM_SERVICE_T *service =
  29970. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  29971. +
  29972. + if (!service->callback)
  29973. + goto release;
  29974. +
  29975. + switch (reason) {
  29976. + case VCHIQ_MESSAGE_AVAILABLE:
  29977. + vchiu_queue_push(&service->queue, header);
  29978. +
  29979. + service->callback(service->callback_param,
  29980. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  29981. +
  29982. + goto done;
  29983. + break;
  29984. +
  29985. + case VCHIQ_BULK_TRANSMIT_DONE:
  29986. + service->callback(service->callback_param,
  29987. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  29988. + break;
  29989. +
  29990. + case VCHIQ_BULK_RECEIVE_DONE:
  29991. + service->callback(service->callback_param,
  29992. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  29993. + break;
  29994. +
  29995. + case VCHIQ_SERVICE_CLOSED:
  29996. + service->callback(service->callback_param,
  29997. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  29998. + break;
  29999. +
  30000. + case VCHIQ_SERVICE_OPENED:
  30001. + /* No equivalent VCHI reason */
  30002. + break;
  30003. +
  30004. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  30005. + service->callback(service->callback_param,
  30006. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  30007. + bulk_user);
  30008. + break;
  30009. +
  30010. + case VCHIQ_BULK_RECEIVE_ABORTED:
  30011. + service->callback(service->callback_param,
  30012. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  30013. + bulk_user);
  30014. + break;
  30015. +
  30016. + default:
  30017. + WARN(1, "not supported\n");
  30018. + break;
  30019. + }
  30020. +
  30021. +release:
  30022. + vchiq_release_message(service->handle, header);
  30023. +done:
  30024. + return VCHIQ_SUCCESS;
  30025. +}
  30026. +
  30027. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  30028. + SERVICE_CREATION_T *setup)
  30029. +{
  30030. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  30031. +
  30032. + (void)instance;
  30033. +
  30034. + if (service) {
  30035. + if (vchiu_queue_init(&service->queue, 64)) {
  30036. + service->callback = setup->callback;
  30037. + service->callback_param = setup->callback_param;
  30038. + } else {
  30039. + kfree(service);
  30040. + service = NULL;
  30041. + }
  30042. + }
  30043. +
  30044. + return service;
  30045. +}
  30046. +
  30047. +static void service_free(SHIM_SERVICE_T *service)
  30048. +{
  30049. + if (service) {
  30050. + vchiu_queue_delete(&service->queue);
  30051. + kfree(service);
  30052. + }
  30053. +}
  30054. +
  30055. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  30056. + SERVICE_CREATION_T *setup,
  30057. + VCHI_SERVICE_HANDLE_T *handle)
  30058. +{
  30059. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  30060. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  30061. + if (service) {
  30062. + VCHIQ_SERVICE_PARAMS_T params;
  30063. + VCHIQ_STATUS_T status;
  30064. +
  30065. + memset(&params, 0, sizeof(params));
  30066. + params.fourcc = setup->service_id;
  30067. + params.callback = shim_callback;
  30068. + params.userdata = service;
  30069. + params.version = setup->version.version;
  30070. + params.version_min = setup->version.version_min;
  30071. +
  30072. + status = vchiq_open_service(instance, &params,
  30073. + &service->handle);
  30074. + if (status != VCHIQ_SUCCESS) {
  30075. + service_free(service);
  30076. + service = NULL;
  30077. + }
  30078. + }
  30079. +
  30080. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  30081. +
  30082. + return (service != NULL) ? 0 : -1;
  30083. +}
  30084. +EXPORT_SYMBOL(vchi_service_open);
  30085. +
  30086. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  30087. + SERVICE_CREATION_T *setup,
  30088. + VCHI_SERVICE_HANDLE_T *handle)
  30089. +{
  30090. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  30091. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  30092. + if (service) {
  30093. + VCHIQ_SERVICE_PARAMS_T params;
  30094. + VCHIQ_STATUS_T status;
  30095. +
  30096. + memset(&params, 0, sizeof(params));
  30097. + params.fourcc = setup->service_id;
  30098. + params.callback = shim_callback;
  30099. + params.userdata = service;
  30100. + params.version = setup->version.version;
  30101. + params.version_min = setup->version.version_min;
  30102. + status = vchiq_add_service(instance, &params, &service->handle);
  30103. +
  30104. + if (status != VCHIQ_SUCCESS) {
  30105. + service_free(service);
  30106. + service = NULL;
  30107. + }
  30108. + }
  30109. +
  30110. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  30111. +
  30112. + return (service != NULL) ? 0 : -1;
  30113. +}
  30114. +EXPORT_SYMBOL(vchi_service_create);
  30115. +
  30116. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  30117. +{
  30118. + int32_t ret = -1;
  30119. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30120. + if (service) {
  30121. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  30122. + if (status == VCHIQ_SUCCESS) {
  30123. + service_free(service);
  30124. + service = NULL;
  30125. + }
  30126. +
  30127. + ret = vchiq_status_to_vchi(status);
  30128. + }
  30129. + return ret;
  30130. +}
  30131. +EXPORT_SYMBOL(vchi_service_close);
  30132. +
  30133. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  30134. +{
  30135. + int32_t ret = -1;
  30136. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30137. + if (service) {
  30138. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  30139. + if (status == VCHIQ_SUCCESS) {
  30140. + service_free(service);
  30141. + service = NULL;
  30142. + }
  30143. +
  30144. + ret = vchiq_status_to_vchi(status);
  30145. + }
  30146. + return ret;
  30147. +}
  30148. +EXPORT_SYMBOL(vchi_service_destroy);
  30149. +
  30150. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  30151. +{
  30152. + int32_t ret = -1;
  30153. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30154. + if(service)
  30155. + {
  30156. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  30157. + ret = vchiq_status_to_vchi( status );
  30158. + }
  30159. + return ret;
  30160. +}
  30161. +EXPORT_SYMBOL(vchi_get_peer_version);
  30162. +
  30163. +/* ----------------------------------------------------------------------
  30164. + * read a uint32_t from buffer.
  30165. + * network format is defined to be little endian
  30166. + * -------------------------------------------------------------------- */
  30167. +uint32_t
  30168. +vchi_readbuf_uint32(const void *_ptr)
  30169. +{
  30170. + const unsigned char *ptr = _ptr;
  30171. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  30172. +}
  30173. +
  30174. +/* ----------------------------------------------------------------------
  30175. + * write a uint32_t to buffer.
  30176. + * network format is defined to be little endian
  30177. + * -------------------------------------------------------------------- */
  30178. +void
  30179. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  30180. +{
  30181. + unsigned char *ptr = _ptr;
  30182. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  30183. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  30184. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  30185. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  30186. +}
  30187. +
  30188. +/* ----------------------------------------------------------------------
  30189. + * read a uint16_t from buffer.
  30190. + * network format is defined to be little endian
  30191. + * -------------------------------------------------------------------- */
  30192. +uint16_t
  30193. +vchi_readbuf_uint16(const void *_ptr)
  30194. +{
  30195. + const unsigned char *ptr = _ptr;
  30196. + return ptr[0] | (ptr[1] << 8);
  30197. +}
  30198. +
  30199. +/* ----------------------------------------------------------------------
  30200. + * write a uint16_t into the buffer.
  30201. + * network format is defined to be little endian
  30202. + * -------------------------------------------------------------------- */
  30203. +void
  30204. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  30205. +{
  30206. + unsigned char *ptr = _ptr;
  30207. + ptr[0] = (value >> 0) & 0xFF;
  30208. + ptr[1] = (value >> 8) & 0xFF;
  30209. +}
  30210. +
  30211. +/***********************************************************
  30212. + * Name: vchi_service_use
  30213. + *
  30214. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  30215. + *
  30216. + * Description: Routine to increment refcount on a service
  30217. + *
  30218. + * Returns: void
  30219. + *
  30220. + ***********************************************************/
  30221. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  30222. +{
  30223. + int32_t ret = -1;
  30224. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30225. + if (service)
  30226. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  30227. + return ret;
  30228. +}
  30229. +EXPORT_SYMBOL(vchi_service_use);
  30230. +
  30231. +/***********************************************************
  30232. + * Name: vchi_service_release
  30233. + *
  30234. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  30235. + *
  30236. + * Description: Routine to decrement refcount on a service
  30237. + *
  30238. + * Returns: void
  30239. + *
  30240. + ***********************************************************/
  30241. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  30242. +{
  30243. + int32_t ret = -1;
  30244. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30245. + if (service)
  30246. + ret = vchiq_status_to_vchi(
  30247. + vchiq_release_service(service->handle));
  30248. + return ret;
  30249. +}
  30250. +EXPORT_SYMBOL(vchi_service_release);
  30251. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  30252. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 1970-01-01 01:00:00.000000000 +0100
  30253. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 2014-03-11 17:32:37.000000000 +0100
  30254. @@ -0,0 +1,151 @@
  30255. +/**
  30256. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  30257. + *
  30258. + * Redistribution and use in source and binary forms, with or without
  30259. + * modification, are permitted provided that the following conditions
  30260. + * are met:
  30261. + * 1. Redistributions of source code must retain the above copyright
  30262. + * notice, this list of conditions, and the following disclaimer,
  30263. + * without modification.
  30264. + * 2. Redistributions in binary form must reproduce the above copyright
  30265. + * notice, this list of conditions and the following disclaimer in the
  30266. + * documentation and/or other materials provided with the distribution.
  30267. + * 3. The names of the above-listed copyright holders may not be used
  30268. + * to endorse or promote products derived from this software without
  30269. + * specific prior written permission.
  30270. + *
  30271. + * ALTERNATIVELY, this software may be distributed under the terms of the
  30272. + * GNU General Public License ("GPL") version 2, as published by the Free
  30273. + * Software Foundation.
  30274. + *
  30275. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30276. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30277. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30278. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30279. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30280. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30281. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30282. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30283. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30284. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30285. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30286. + */
  30287. +
  30288. +#include "vchiq_util.h"
  30289. +
  30290. +static inline int is_pow2(int i)
  30291. +{
  30292. + return i && !(i & (i - 1));
  30293. +}
  30294. +
  30295. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  30296. +{
  30297. + WARN_ON(!is_pow2(size));
  30298. +
  30299. + queue->size = size;
  30300. + queue->read = 0;
  30301. + queue->write = 0;
  30302. +
  30303. + sema_init(&queue->pop, 0);
  30304. + sema_init(&queue->push, 0);
  30305. +
  30306. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  30307. + if (queue->storage == NULL) {
  30308. + vchiu_queue_delete(queue);
  30309. + return 0;
  30310. + }
  30311. + return 1;
  30312. +}
  30313. +
  30314. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  30315. +{
  30316. + if (queue->storage != NULL)
  30317. + kfree(queue->storage);
  30318. +}
  30319. +
  30320. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  30321. +{
  30322. + return queue->read == queue->write;
  30323. +}
  30324. +
  30325. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  30326. +{
  30327. + return queue->write == queue->read + queue->size;
  30328. +}
  30329. +
  30330. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  30331. +{
  30332. + while (queue->write == queue->read + queue->size) {
  30333. + if (down_interruptible(&queue->pop) != 0) {
  30334. + flush_signals(current);
  30335. + }
  30336. + }
  30337. +
  30338. + /*
  30339. + * Write to queue->storage must be visible after read from
  30340. + * queue->read
  30341. + */
  30342. + smp_mb();
  30343. +
  30344. + queue->storage[queue->write & (queue->size - 1)] = header;
  30345. +
  30346. + /*
  30347. + * Write to queue->storage must be visible before write to
  30348. + * queue->write
  30349. + */
  30350. + smp_wmb();
  30351. +
  30352. + queue->write++;
  30353. +
  30354. + up(&queue->push);
  30355. +}
  30356. +
  30357. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  30358. +{
  30359. + while (queue->write == queue->read) {
  30360. + if (down_interruptible(&queue->push) != 0) {
  30361. + flush_signals(current);
  30362. + }
  30363. + }
  30364. +
  30365. + up(&queue->push); // We haven't removed anything from the queue.
  30366. +
  30367. + /*
  30368. + * Read from queue->storage must be visible after read from
  30369. + * queue->write
  30370. + */
  30371. + smp_rmb();
  30372. +
  30373. + return queue->storage[queue->read & (queue->size - 1)];
  30374. +}
  30375. +
  30376. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  30377. +{
  30378. + VCHIQ_HEADER_T *header;
  30379. +
  30380. + while (queue->write == queue->read) {
  30381. + if (down_interruptible(&queue->push) != 0) {
  30382. + flush_signals(current);
  30383. + }
  30384. + }
  30385. +
  30386. + /*
  30387. + * Read from queue->storage must be visible after read from
  30388. + * queue->write
  30389. + */
  30390. + smp_rmb();
  30391. +
  30392. + header = queue->storage[queue->read & (queue->size - 1)];
  30393. +
  30394. + /*
  30395. + * Read from queue->storage must be visible before write to
  30396. + * queue->read
  30397. + */
  30398. + smp_mb();
  30399. +
  30400. + queue->read++;
  30401. +
  30402. + up(&queue->pop);
  30403. +
  30404. + return header;
  30405. +}
  30406. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  30407. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 1970-01-01 01:00:00.000000000 +0100
  30408. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 2014-03-11 17:51:16.000000000 +0100
  30409. @@ -0,0 +1,81 @@
  30410. +/**
  30411. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  30412. + *
  30413. + * Redistribution and use in source and binary forms, with or without
  30414. + * modification, are permitted provided that the following conditions
  30415. + * are met:
  30416. + * 1. Redistributions of source code must retain the above copyright
  30417. + * notice, this list of conditions, and the following disclaimer,
  30418. + * without modification.
  30419. + * 2. Redistributions in binary form must reproduce the above copyright
  30420. + * notice, this list of conditions and the following disclaimer in the
  30421. + * documentation and/or other materials provided with the distribution.
  30422. + * 3. The names of the above-listed copyright holders may not be used
  30423. + * to endorse or promote products derived from this software without
  30424. + * specific prior written permission.
  30425. + *
  30426. + * ALTERNATIVELY, this software may be distributed under the terms of the
  30427. + * GNU General Public License ("GPL") version 2, as published by the Free
  30428. + * Software Foundation.
  30429. + *
  30430. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30431. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30432. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30433. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30434. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30435. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30436. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30437. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30438. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30439. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30440. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30441. + */
  30442. +
  30443. +#ifndef VCHIQ_UTIL_H
  30444. +#define VCHIQ_UTIL_H
  30445. +
  30446. +#include <linux/types.h>
  30447. +#include <linux/semaphore.h>
  30448. +#include <linux/mutex.h>
  30449. +#include <linux/bitops.h>
  30450. +#include <linux/kthread.h>
  30451. +#include <linux/wait.h>
  30452. +#include <linux/vmalloc.h>
  30453. +#include <linux/jiffies.h>
  30454. +#include <linux/delay.h>
  30455. +#include <linux/string.h>
  30456. +#include <linux/types.h>
  30457. +#include <linux/interrupt.h>
  30458. +#include <linux/random.h>
  30459. +#include <linux/sched.h>
  30460. +#include <linux/ctype.h>
  30461. +#include <linux/uaccess.h>
  30462. +#include <linux/time.h> /* for time_t */
  30463. +#include <linux/slab.h>
  30464. +#include <linux/vmalloc.h>
  30465. +
  30466. +#include "vchiq_if.h"
  30467. +
  30468. +typedef struct {
  30469. + int size;
  30470. + int read;
  30471. + int write;
  30472. +
  30473. + struct semaphore pop;
  30474. + struct semaphore push;
  30475. +
  30476. + VCHIQ_HEADER_T **storage;
  30477. +} VCHIU_QUEUE_T;
  30478. +
  30479. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  30480. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  30481. +
  30482. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  30483. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  30484. +
  30485. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  30486. +
  30487. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  30488. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  30489. +
  30490. +#endif
  30491. diff -Nur linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  30492. --- linux-3.12.13/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 1970-01-01 01:00:00.000000000 +0100
  30493. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 2014-03-11 17:32:37.000000000 +0100
  30494. @@ -0,0 +1,59 @@
  30495. +/**
  30496. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  30497. + *
  30498. + * Redistribution and use in source and binary forms, with or without
  30499. + * modification, are permitted provided that the following conditions
  30500. + * are met:
  30501. + * 1. Redistributions of source code must retain the above copyright
  30502. + * notice, this list of conditions, and the following disclaimer,
  30503. + * without modification.
  30504. + * 2. Redistributions in binary form must reproduce the above copyright
  30505. + * notice, this list of conditions and the following disclaimer in the
  30506. + * documentation and/or other materials provided with the distribution.
  30507. + * 3. The names of the above-listed copyright holders may not be used
  30508. + * to endorse or promote products derived from this software without
  30509. + * specific prior written permission.
  30510. + *
  30511. + * ALTERNATIVELY, this software may be distributed under the terms of the
  30512. + * GNU General Public License ("GPL") version 2, as published by the Free
  30513. + * Software Foundation.
  30514. + *
  30515. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30516. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30517. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30518. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30519. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30520. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30521. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30522. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30523. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30524. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30525. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30526. + */
  30527. +#include "vchiq_build_info.h"
  30528. +#include <linux/broadcom/vc_debug_sym.h>
  30529. +
  30530. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  30531. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  30532. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  30533. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  30534. +
  30535. +const char *vchiq_get_build_hostname( void )
  30536. +{
  30537. + return vchiq_build_hostname;
  30538. +}
  30539. +
  30540. +const char *vchiq_get_build_version( void )
  30541. +{
  30542. + return vchiq_build_version;
  30543. +}
  30544. +
  30545. +const char *vchiq_get_build_date( void )
  30546. +{
  30547. + return vchiq_build_date;
  30548. +}
  30549. +
  30550. +const char *vchiq_get_build_time( void )
  30551. +{
  30552. + return vchiq_build_time;
  30553. +}
  30554. diff -Nur linux-3.12.13/drivers/misc/vc04_services/Kconfig linux-raspberry-pi/drivers/misc/vc04_services/Kconfig
  30555. --- linux-3.12.13/drivers/misc/vc04_services/Kconfig 1970-01-01 01:00:00.000000000 +0100
  30556. +++ linux-raspberry-pi/drivers/misc/vc04_services/Kconfig 2014-03-11 17:51:16.000000000 +0100
  30557. @@ -0,0 +1,9 @@
  30558. +config BCM2708_VCHIQ
  30559. + tristate "Videocore VCHIQ"
  30560. + depends on MACH_BCM2708
  30561. + default y
  30562. + help
  30563. + Kernel to VideoCore communication interface for the
  30564. + BCM2708 family of products.
  30565. + Defaults to Y when the Broadcom Videocore services
  30566. + are included in the build, N otherwise.
  30567. diff -Nur linux-3.12.13/drivers/misc/vc04_services/Makefile linux-raspberry-pi/drivers/misc/vc04_services/Makefile
  30568. --- linux-3.12.13/drivers/misc/vc04_services/Makefile 1970-01-01 01:00:00.000000000 +0100
  30569. +++ linux-raspberry-pi/drivers/misc/vc04_services/Makefile 2014-03-11 17:51:16.000000000 +0100
  30570. @@ -0,0 +1,17 @@
  30571. +ifeq ($(CONFIG_MACH_BCM2708),y)
  30572. +
  30573. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  30574. +
  30575. +vchiq-objs := \
  30576. + interface/vchiq_arm/vchiq_core.o \
  30577. + interface/vchiq_arm/vchiq_arm.o \
  30578. + interface/vchiq_arm/vchiq_kern_lib.o \
  30579. + interface/vchiq_arm/vchiq_2835_arm.o \
  30580. + interface/vchiq_arm/vchiq_proc.o \
  30581. + interface/vchiq_arm/vchiq_shim.o \
  30582. + interface/vchiq_arm/vchiq_util.o \
  30583. + interface/vchiq_arm/vchiq_connected.o \
  30584. +
  30585. +EXTRA_CFLAGS += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  30586. +
  30587. +endif
  30588. diff -Nur linux-3.12.13/drivers/mmc/card/block.c linux-raspberry-pi/drivers/mmc/card/block.c
  30589. --- linux-3.12.13/drivers/mmc/card/block.c 2014-02-22 22:32:50.000000000 +0100
  30590. +++ linux-raspberry-pi/drivers/mmc/card/block.c 2014-03-11 17:51:16.000000000 +0100
  30591. @@ -1361,7 +1361,7 @@
  30592. brq->data.blocks = 1;
  30593. }
  30594. - if (brq->data.blocks > 1 || do_rel_wr) {
  30595. + if (brq->data.blocks > 1 || do_rel_wr || card->host->caps2 & MMC_CAP2_FORCE_MULTIBLOCK) {
  30596. /* SPI multiblock writes terminate using a special
  30597. * token, not a STOP_TRANSMISSION request.
  30598. */
  30599. diff -Nur linux-3.12.13/drivers/mmc/core/sd.c linux-raspberry-pi/drivers/mmc/core/sd.c
  30600. --- linux-3.12.13/drivers/mmc/core/sd.c 2014-02-22 22:32:50.000000000 +0100
  30601. +++ linux-raspberry-pi/drivers/mmc/core/sd.c 2014-03-11 17:51:16.000000000 +0100
  30602. @@ -14,6 +14,8 @@
  30603. #include <linux/sizes.h>
  30604. #include <linux/slab.h>
  30605. #include <linux/stat.h>
  30606. +#include <linux/jiffies.h>
  30607. +#include <linux/nmi.h>
  30608. #include <linux/mmc/host.h>
  30609. #include <linux/mmc/card.h>
  30610. @@ -66,6 +68,15 @@
  30611. __res & __mask; \
  30612. })
  30613. +// timeout for tries
  30614. +static const unsigned long retry_timeout_ms= 10*1000;
  30615. +
  30616. +// try at least 10 times, even if timeout is reached
  30617. +static const int retry_min_tries= 10;
  30618. +
  30619. +// delay between tries
  30620. +static const unsigned long retry_delay_ms= 10;
  30621. +
  30622. /*
  30623. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  30624. */
  30625. @@ -218,12 +229,63 @@
  30626. }
  30627. /*
  30628. - * Fetch and process SD Status register.
  30629. + * Fetch and process SD Configuration Register.
  30630. + */
  30631. +static int mmc_read_scr(struct mmc_card *card)
  30632. +{
  30633. + unsigned long timeout_at;
  30634. + int err, tries;
  30635. +
  30636. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  30637. + tries= 0;
  30638. +
  30639. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  30640. + {
  30641. + unsigned long delay_at;
  30642. + tries++;
  30643. +
  30644. + err = mmc_app_send_scr(card, card->raw_scr);
  30645. + if( !err )
  30646. + break; // success!!!
  30647. +
  30648. + touch_nmi_watchdog(); // we are still alive!
  30649. +
  30650. + // delay
  30651. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  30652. + while( time_before( jiffies, delay_at ) )
  30653. + {
  30654. + mdelay( 1 );
  30655. + touch_nmi_watchdog(); // we are still alive!
  30656. + }
  30657. + }
  30658. +
  30659. + if( err)
  30660. + {
  30661. + 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 );
  30662. + return err;
  30663. + }
  30664. +
  30665. + if( tries > 1 )
  30666. + {
  30667. + pr_info("%s: could read SD Configuration register (SCR) at the %dth attempt\n", mmc_hostname(card->host), tries );
  30668. + }
  30669. +
  30670. + err = mmc_decode_scr(card);
  30671. + if (err)
  30672. + return err;
  30673. +
  30674. + return err;
  30675. +}
  30676. +
  30677. +/*
  30678. + * Fetch and process SD Status Register.
  30679. */
  30680. static int mmc_read_ssr(struct mmc_card *card)
  30681. {
  30682. + unsigned long timeout_at;
  30683. unsigned int au, es, et, eo;
  30684. int err, i;
  30685. + int tries;
  30686. u32 *ssr;
  30687. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  30688. @@ -236,14 +298,40 @@
  30689. if (!ssr)
  30690. return -ENOMEM;
  30691. - err = mmc_app_sd_status(card, ssr);
  30692. - if (err) {
  30693. - pr_warning("%s: problem reading SD Status "
  30694. - "register.\n", mmc_hostname(card->host));
  30695. - err = 0;
  30696. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  30697. + tries= 0;
  30698. +
  30699. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  30700. + {
  30701. + unsigned long delay_at;
  30702. + tries++;
  30703. +
  30704. + err= mmc_app_sd_status(card, ssr);
  30705. + if( !err )
  30706. + break; // sucess!!!
  30707. +
  30708. + touch_nmi_watchdog(); // we are still alive!
  30709. +
  30710. + // delay
  30711. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  30712. + while( time_before( jiffies, delay_at ) )
  30713. + {
  30714. + mdelay( 1 );
  30715. + touch_nmi_watchdog(); // we are still alive!
  30716. + }
  30717. + }
  30718. +
  30719. + if( err)
  30720. + {
  30721. + 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 );
  30722. goto out;
  30723. }
  30724. + if( tries > 1 )
  30725. + {
  30726. + pr_info("%s: read SD Status register (SSR) after %d attempts\n", mmc_hostname(card->host), tries );
  30727. + }
  30728. +
  30729. for (i = 0; i < 16; i++)
  30730. ssr[i] = be32_to_cpu(ssr[i]);
  30731. @@ -823,14 +911,10 @@
  30732. if (!reinit) {
  30733. /*
  30734. - * Fetch SCR from card.
  30735. + * Fetch and decode SD Configuration register.
  30736. */
  30737. - err = mmc_app_send_scr(card, card->raw_scr);
  30738. - if (err)
  30739. - return err;
  30740. -
  30741. - err = mmc_decode_scr(card);
  30742. - if (err)
  30743. + err = mmc_read_scr(card);
  30744. + if( err )
  30745. return err;
  30746. /*
  30747. diff -Nur linux-3.12.13/drivers/mmc/host/Kconfig linux-raspberry-pi/drivers/mmc/host/Kconfig
  30748. --- linux-3.12.13/drivers/mmc/host/Kconfig 2014-02-22 22:32:50.000000000 +0100
  30749. +++ linux-raspberry-pi/drivers/mmc/host/Kconfig 2014-03-11 17:51:16.000000000 +0100
  30750. @@ -260,6 +260,27 @@
  30751. If you have a controller with this interface, say Y or M here.
  30752. +config MMC_SDHCI_BCM2708
  30753. + tristate "SDHCI support on BCM2708"
  30754. + depends on MMC_SDHCI && MACH_BCM2708
  30755. + select MMC_SDHCI_IO_ACCESSORS
  30756. + help
  30757. + This selects the Secure Digital Host Controller Interface (SDHCI)
  30758. + often referrered to as the eMMC block.
  30759. +
  30760. + If you have a controller with this interface, say Y or M here.
  30761. +
  30762. + If unsure, say N.
  30763. +
  30764. +config MMC_SDHCI_BCM2708_DMA
  30765. + bool "DMA support on BCM2708 Arasan controller"
  30766. + depends on MMC_SDHCI_BCM2708
  30767. + help
  30768. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  30769. + based chips.
  30770. +
  30771. + If unsure, say N.
  30772. +
  30773. config MMC_SDHCI_BCM2835
  30774. tristate "SDHCI platform support for the BCM2835 SD/MMC Controller"
  30775. depends on ARCH_BCM2835
  30776. diff -Nur linux-3.12.13/drivers/mmc/host/Makefile linux-raspberry-pi/drivers/mmc/host/Makefile
  30777. --- linux-3.12.13/drivers/mmc/host/Makefile 2014-02-22 22:32:50.000000000 +0100
  30778. +++ linux-raspberry-pi/drivers/mmc/host/Makefile 2014-03-11 17:51:16.000000000 +0100
  30779. @@ -15,6 +15,7 @@
  30780. obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
  30781. obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
  30782. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  30783. +obj-$(CONFIG_MMC_SDHCI_BCM2708) += sdhci-bcm2708.o
  30784. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  30785. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  30786. obj-$(CONFIG_MMC_OMAP) += omap.o
  30787. diff -Nur linux-3.12.13/drivers/mmc/host/sdhci-bcm2708.c linux-raspberry-pi/drivers/mmc/host/sdhci-bcm2708.c
  30788. --- linux-3.12.13/drivers/mmc/host/sdhci-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  30789. +++ linux-raspberry-pi/drivers/mmc/host/sdhci-bcm2708.c 2014-03-11 17:51:21.000000000 +0100
  30790. @@ -0,0 +1,1410 @@
  30791. +/*
  30792. + * sdhci-bcm2708.c Support for SDHCI device on BCM2708
  30793. + * Copyright (c) 2010 Broadcom
  30794. + *
  30795. + * This program is free software; you can redistribute it and/or modify
  30796. + * it under the terms of the GNU General Public License version 2 as
  30797. + * published by the Free Software Foundation.
  30798. + *
  30799. + * This program is distributed in the hope that it will be useful,
  30800. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30801. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30802. + * GNU General Public License for more details.
  30803. + *
  30804. + * You should have received a copy of the GNU General Public License
  30805. + * along with this program; if not, write to the Free Software
  30806. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30807. + */
  30808. +
  30809. +/* Supports:
  30810. + * SDHCI platform device - Arasan SD controller in BCM2708
  30811. + *
  30812. + * Inspired by sdhci-pci.c, by Pierre Ossman
  30813. + */
  30814. +
  30815. +#include <linux/delay.h>
  30816. +#include <linux/highmem.h>
  30817. +#include <linux/platform_device.h>
  30818. +#include <linux/module.h>
  30819. +#include <linux/mmc/mmc.h>
  30820. +#include <linux/mmc/host.h>
  30821. +#include <linux/mmc/sd.h>
  30822. +
  30823. +#include <linux/io.h>
  30824. +#include <linux/dma-mapping.h>
  30825. +#include <mach/dma.h>
  30826. +
  30827. +#include "sdhci.h"
  30828. +
  30829. +/*****************************************************************************\
  30830. + * *
  30831. + * Configuration *
  30832. + * *
  30833. +\*****************************************************************************/
  30834. +
  30835. +#define DRIVER_NAME "bcm2708_sdhci"
  30836. +
  30837. +/* for the time being insist on DMA mode - PIO seems not to work */
  30838. +#ifndef CONFIG_MMC_SDHCI_BCM2708_DMA
  30839. +#warning Non-DMA (PIO) version of this driver currently unavailable
  30840. +#endif
  30841. +#undef CONFIG_MMC_SDHCI_BCM2708_DMA
  30842. +#define CONFIG_MMC_SDHCI_BCM2708_DMA y
  30843. +
  30844. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  30845. +/* #define CHECK_DMA_USE */
  30846. +#endif
  30847. +//#define LOG_REGISTERS
  30848. +
  30849. +#define USE_SCHED_TIME
  30850. +#define USE_SPACED_WRITES_2CLK 1 /* space consecutive register writes */
  30851. +#define USE_SOFTWARE_TIMEOUTS 1 /* not hardware timeouts */
  30852. +#define SOFTWARE_ERASE_TIMEOUT_SEC 30
  30853. +
  30854. +#define SDHCI_BCM_DMA_CHAN 4 /* this default is normally overriden */
  30855. +#define SDHCI_BCM_DMA_WAITS 0 /* delays slowing DMA transfers: 0-31 */
  30856. +/* We are worried that SD card DMA use may be blocking the AXI bus for others */
  30857. +
  30858. +/*! TODO: obtain these from the physical address */
  30859. +#define DMA_SDHCI_BASE 0x7e300000 /* EMMC register block on Videocore */
  30860. +#define DMA_SDHCI_BUFFER (DMA_SDHCI_BASE + SDHCI_BUFFER)
  30861. +
  30862. +#define BCM2708_SDHCI_SLEEP_TIMEOUT 1000 /* msecs */
  30863. +
  30864. +/* Mhz clock that the EMMC core is running at. Should match the platform clockman settings */
  30865. +#define BCM2708_EMMC_CLOCK_FREQ 50000000
  30866. +
  30867. +#define REG_EXRDFIFO_EN 0x80
  30868. +#define REG_EXRDFIFO_CFG 0x84
  30869. +
  30870. +int cycle_delay=2;
  30871. +
  30872. +/*****************************************************************************\
  30873. + * *
  30874. + * Debug *
  30875. + * *
  30876. +\*****************************************************************************/
  30877. +
  30878. +
  30879. +
  30880. +#define DBG(f, x...) \
  30881. + pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  30882. +// printk(KERN_INFO DRIVER_NAME " [%s()]: " f, __func__,## x)//GRAYG
  30883. +
  30884. +
  30885. +/*****************************************************************************\
  30886. + * *
  30887. + * High Precision Time *
  30888. + * *
  30889. +\*****************************************************************************/
  30890. +
  30891. +#ifdef USE_SCHED_TIME
  30892. +
  30893. +#include <mach/frc.h>
  30894. +
  30895. +typedef unsigned long hptime_t;
  30896. +
  30897. +#define FMT_HPT "lu"
  30898. +
  30899. +static inline hptime_t hptime(void)
  30900. +{
  30901. + return frc_clock_ticks32();
  30902. +}
  30903. +
  30904. +#define HPTIME_CLK_NS 1000ul
  30905. +
  30906. +#else
  30907. +
  30908. +typedef unsigned long hptime_t;
  30909. +
  30910. +#define FMT_HPT "lu"
  30911. +
  30912. +static inline hptime_t hptime(void)
  30913. +{
  30914. + return jiffies;
  30915. +}
  30916. +
  30917. +#define HPTIME_CLK_NS (1000000000ul/HZ)
  30918. +
  30919. +#endif
  30920. +
  30921. +static inline unsigned long int since_ns(hptime_t t)
  30922. +{
  30923. + return (unsigned long)((hptime() - t) * HPTIME_CLK_NS);
  30924. +}
  30925. +
  30926. +static bool allow_highspeed = 1;
  30927. +static int emmc_clock_freq = BCM2708_EMMC_CLOCK_FREQ;
  30928. +static bool sync_after_dma = 1;
  30929. +static bool missing_status = 1;
  30930. +static bool spurious_crc_acmd51 = 0;
  30931. +bool enable_llm = 1;
  30932. +bool extra_messages = 0;
  30933. +
  30934. +#if 0
  30935. +static void hptime_test(void)
  30936. +{
  30937. + hptime_t now;
  30938. + hptime_t later;
  30939. +
  30940. + now = hptime();
  30941. + msleep(10);
  30942. + later = hptime();
  30943. +
  30944. + printk(KERN_INFO DRIVER_NAME": 10ms = %"FMT_HPT" clks "
  30945. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  30946. + later-now, now, later,
  30947. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  30948. +
  30949. + now = hptime();
  30950. + msleep(1000);
  30951. + later = hptime();
  30952. +
  30953. + printk(KERN_INFO DRIVER_NAME": 1s = %"FMT_HPT" clks "
  30954. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  30955. + later-now, now, later,
  30956. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  30957. +}
  30958. +#endif
  30959. +
  30960. +/*****************************************************************************\
  30961. + * *
  30962. + * SDHCI core callbacks *
  30963. + * *
  30964. +\*****************************************************************************/
  30965. +
  30966. +
  30967. +#ifdef CHECK_DMA_USE
  30968. +/*#define CHECK_DMA_REG_USE*/
  30969. +#endif
  30970. +
  30971. +#ifdef CHECK_DMA_REG_USE
  30972. +/* we don't expect anything to be using these registers during a
  30973. + DMA (except the IRQ status) - so check */
  30974. +static void check_dma_reg_use(struct sdhci_host *host, int reg);
  30975. +#else
  30976. +#define check_dma_reg_use(host, reg)
  30977. +#endif
  30978. +
  30979. +
  30980. +static inline u32 sdhci_bcm2708_raw_readl(struct sdhci_host *host, int reg)
  30981. +{
  30982. + return readl(host->ioaddr + reg);
  30983. +}
  30984. +
  30985. +u32 sdhci_bcm2708_readl(struct sdhci_host *host, int reg)
  30986. +{
  30987. + u32 l = sdhci_bcm2708_raw_readl(host, reg);
  30988. +
  30989. +#ifdef LOG_REGISTERS
  30990. + printk(KERN_ERR "%s: readl from 0x%02x, value 0x%08x\n",
  30991. + mmc_hostname(host->mmc), reg, l);
  30992. +#endif
  30993. + check_dma_reg_use(host, reg);
  30994. +
  30995. + return l;
  30996. +}
  30997. +
  30998. +u16 sdhci_bcm2708_readw(struct sdhci_host *host, int reg)
  30999. +{
  31000. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  31001. + u32 w = l >> (reg << 3 & 0x18) & 0xffff;
  31002. +
  31003. +#ifdef LOG_REGISTERS
  31004. + printk(KERN_ERR "%s: readw from 0x%02x, value 0x%04x\n",
  31005. + mmc_hostname(host->mmc), reg, w);
  31006. +#endif
  31007. + check_dma_reg_use(host, reg);
  31008. +
  31009. + return (u16)w;
  31010. +}
  31011. +
  31012. +u8 sdhci_bcm2708_readb(struct sdhci_host *host, int reg)
  31013. +{
  31014. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  31015. + u32 b = l >> (reg << 3 & 0x18) & 0xff;
  31016. +
  31017. +#ifdef LOG_REGISTERS
  31018. + printk(KERN_ERR "%s: readb from 0x%02x, value 0x%02x\n",
  31019. + mmc_hostname(host->mmc), reg, b);
  31020. +#endif
  31021. + check_dma_reg_use(host, reg);
  31022. +
  31023. + return (u8)b;
  31024. +}
  31025. +
  31026. +
  31027. +static void sdhci_bcm2708_raw_writel(struct sdhci_host *host, u32 val, int reg)
  31028. +{
  31029. + u32 ier;
  31030. +
  31031. +#if USE_SPACED_WRITES_2CLK
  31032. + static bool timeout_disabled = false;
  31033. + unsigned int ns_2clk = 0;
  31034. +
  31035. + /* The Arasan has a bugette whereby it may lose the content of
  31036. + * successive writes to registers that are within two SD-card clock
  31037. + * cycles of each other (a clock domain crossing problem).
  31038. + * It seems, however, that the data register does not have this problem.
  31039. + * (Which is just as well - otherwise we'd have to nobble the DMA engine
  31040. + * too)
  31041. + */
  31042. + if (reg != SDHCI_BUFFER && host->clock != 0) {
  31043. + /* host->clock is the clock freq in Hz */
  31044. + static hptime_t last_write_hpt;
  31045. + hptime_t now = hptime();
  31046. + ns_2clk = cycle_delay*1000000/(host->clock/1000);
  31047. +
  31048. + if (now == last_write_hpt || now == last_write_hpt+1) {
  31049. + /* we can't guarantee any significant time has
  31050. + * passed - we'll have to wait anyway ! */
  31051. + ndelay(ns_2clk);
  31052. + } else
  31053. + {
  31054. + /* we must have waited at least this many ns: */
  31055. + unsigned int ns_wait = HPTIME_CLK_NS *
  31056. + (last_write_hpt - now - 1);
  31057. + if (ns_wait < ns_2clk)
  31058. + ndelay(ns_2clk - ns_wait);
  31059. + }
  31060. + last_write_hpt = now;
  31061. + }
  31062. +#if USE_SOFTWARE_TIMEOUTS
  31063. + /* The Arasan is clocked for timeouts using the SD clock which is too
  31064. + * fast for ERASE commands and causes issues. So we disable timeouts
  31065. + * for ERASE */
  31066. + if (host->cmd != NULL && host->cmd->opcode == MMC_ERASE &&
  31067. + reg == (SDHCI_COMMAND & ~3)) {
  31068. + mod_timer(&host->timer,
  31069. + jiffies + SOFTWARE_ERASE_TIMEOUT_SEC * HZ);
  31070. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31071. + ier &= ~SDHCI_INT_DATA_TIMEOUT;
  31072. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31073. + timeout_disabled = true;
  31074. + ndelay(ns_2clk);
  31075. + } else if (timeout_disabled) {
  31076. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31077. + ier |= SDHCI_INT_DATA_TIMEOUT;
  31078. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31079. + timeout_disabled = false;
  31080. + ndelay(ns_2clk);
  31081. + }
  31082. +#endif
  31083. + writel(val, host->ioaddr + reg);
  31084. +#else
  31085. + void __iomem * regaddr = host->ioaddr + reg;
  31086. +
  31087. + writel(val, regaddr);
  31088. +
  31089. + if (reg != SDHCI_BUFFER && reg != SDHCI_INT_STATUS && host->clock != 0)
  31090. + {
  31091. + int timeout = 100000;
  31092. + while (val != readl(regaddr) && --timeout > 0)
  31093. + continue;
  31094. +
  31095. + if (timeout <= 0)
  31096. + printk(KERN_ERR "%s: writing 0x%X to reg 0x%X "
  31097. + "always gives 0x%X\n",
  31098. + mmc_hostname(host->mmc),
  31099. + val, reg, readl(regaddr));
  31100. + BUG_ON(timeout <= 0);
  31101. + }
  31102. +#endif
  31103. +}
  31104. +
  31105. +
  31106. +void sdhci_bcm2708_writel(struct sdhci_host *host, u32 val, int reg)
  31107. +{
  31108. +#ifdef LOG_REGISTERS
  31109. + printk(KERN_ERR "%s: writel to 0x%02x, value 0x%08x\n",
  31110. + mmc_hostname(host->mmc), reg, val);
  31111. +#endif
  31112. + check_dma_reg_use(host, reg);
  31113. +
  31114. + sdhci_bcm2708_raw_writel(host, val, reg);
  31115. +}
  31116. +
  31117. +void sdhci_bcm2708_writew(struct sdhci_host *host, u16 val, int reg)
  31118. +{
  31119. + static u32 shadow = 0;
  31120. +
  31121. + u32 p = reg == SDHCI_COMMAND ? shadow :
  31122. + sdhci_bcm2708_raw_readl(host, reg & ~3);
  31123. + u32 s = reg << 3 & 0x18;
  31124. + u32 l = val << s;
  31125. + u32 m = 0xffff << s;
  31126. +
  31127. +#ifdef LOG_REGISTERS
  31128. + printk(KERN_ERR "%s: writew to 0x%02x, value 0x%04x\n",
  31129. + mmc_hostname(host->mmc), reg, val);
  31130. +#endif
  31131. +
  31132. + if (reg == SDHCI_TRANSFER_MODE)
  31133. + shadow = (p & ~m) | l;
  31134. + else {
  31135. + check_dma_reg_use(host, reg);
  31136. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  31137. + }
  31138. +}
  31139. +
  31140. +void sdhci_bcm2708_writeb(struct sdhci_host *host, u8 val, int reg)
  31141. +{
  31142. + u32 p = sdhci_bcm2708_raw_readl(host, reg & ~3);
  31143. + u32 s = reg << 3 & 0x18;
  31144. + u32 l = val << s;
  31145. + u32 m = 0xff << s;
  31146. +
  31147. +#ifdef LOG_REGISTERS
  31148. + printk(KERN_ERR "%s: writeb to 0x%02x, value 0x%02x\n",
  31149. + mmc_hostname(host->mmc), reg, val);
  31150. +#endif
  31151. +
  31152. + check_dma_reg_use(host, reg);
  31153. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  31154. +}
  31155. +
  31156. +static unsigned int sdhci_bcm2708_get_max_clock(struct sdhci_host *host)
  31157. +{
  31158. + return emmc_clock_freq;
  31159. +}
  31160. +
  31161. +/*****************************************************************************\
  31162. + * *
  31163. + * DMA Operation *
  31164. + * *
  31165. +\*****************************************************************************/
  31166. +
  31167. +struct sdhci_bcm2708_priv {
  31168. + int dma_chan;
  31169. + int dma_irq;
  31170. + void __iomem *dma_chan_base;
  31171. + struct bcm2708_dma_cb *cb_base; /* DMA control blocks */
  31172. + dma_addr_t cb_handle;
  31173. + /* tracking scatter gather progress */
  31174. + unsigned sg_ix; /* scatter gather list index */
  31175. + unsigned sg_done; /* bytes in current sg_ix done */
  31176. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31177. + unsigned char dma_wanted; /* DMA transfer requested */
  31178. + unsigned char dma_waits; /* wait states in DMAs */
  31179. +#ifdef CHECK_DMA_USE
  31180. + unsigned char dmas_pending; /* no of unfinished DMAs */
  31181. + hptime_t when_started;
  31182. + hptime_t when_reset;
  31183. + hptime_t when_stopped;
  31184. +#endif
  31185. +#endif
  31186. + /* signalling the end of a transfer */
  31187. + void (*complete)(struct sdhci_host *);
  31188. +};
  31189. +
  31190. +#define SDHCI_HOST_PRIV(host) \
  31191. + (struct sdhci_bcm2708_priv *)((struct sdhci_host *)(host)+1)
  31192. +
  31193. +
  31194. +
  31195. +#ifdef CHECK_DMA_REG_USE
  31196. +static void check_dma_reg_use(struct sdhci_host *host, int reg)
  31197. +{
  31198. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31199. + if (host_priv->dma_wanted && reg != SDHCI_INT_STATUS) {
  31200. + printk(KERN_INFO"%s: accessing register 0x%x during DMA\n",
  31201. + mmc_hostname(host->mmc), reg);
  31202. + }
  31203. +}
  31204. +#endif
  31205. +
  31206. +
  31207. +
  31208. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31209. +
  31210. +static void sdhci_clear_set_irqgen(struct sdhci_host *host, u32 clear, u32 set)
  31211. +{
  31212. + u32 ier;
  31213. +
  31214. + ier = sdhci_bcm2708_raw_readl(host, SDHCI_SIGNAL_ENABLE);
  31215. + ier &= ~clear;
  31216. + ier |= set;
  31217. + /* change which requests generate IRQs - makes no difference to
  31218. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  31219. + sdhci_bcm2708_raw_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  31220. +}
  31221. +
  31222. +static void sdhci_signal_irqs(struct sdhci_host *host, u32 irqs)
  31223. +{
  31224. + sdhci_clear_set_irqgen(host, 0, irqs);
  31225. +}
  31226. +
  31227. +static void sdhci_unsignal_irqs(struct sdhci_host *host, u32 irqs)
  31228. +{
  31229. + sdhci_clear_set_irqgen(host, irqs, 0);
  31230. +}
  31231. +
  31232. +
  31233. +
  31234. +static void schci_bcm2708_cb_read(struct sdhci_bcm2708_priv *host,
  31235. + int ix,
  31236. + dma_addr_t dma_addr, unsigned len,
  31237. + int /*bool*/ is_last)
  31238. +{
  31239. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  31240. + unsigned char dmawaits = host->dma_waits;
  31241. +
  31242. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  31243. + BCM2708_DMA_WAITS(dmawaits) |
  31244. + BCM2708_DMA_S_DREQ |
  31245. + BCM2708_DMA_D_WIDTH |
  31246. + BCM2708_DMA_D_INC;
  31247. + cb->src = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  31248. + cb->dst = dma_addr;
  31249. + cb->length = len;
  31250. + cb->stride = 0;
  31251. +
  31252. + if (is_last) {
  31253. + cb->info |= BCM2708_DMA_INT_EN |
  31254. + BCM2708_DMA_WAIT_RESP;
  31255. + cb->next = 0;
  31256. + } else
  31257. + cb->next = host->cb_handle +
  31258. + (ix+1)*sizeof(struct bcm2708_dma_cb);
  31259. +
  31260. + cb->pad[0] = 0;
  31261. + cb->pad[1] = 0;
  31262. +}
  31263. +
  31264. +static void schci_bcm2708_cb_write(struct sdhci_bcm2708_priv *host,
  31265. + int ix,
  31266. + dma_addr_t dma_addr, unsigned len,
  31267. + int /*bool*/ is_last)
  31268. +{
  31269. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  31270. + unsigned char dmawaits = host->dma_waits;
  31271. +
  31272. + /* We can make arbitrarily large writes as long as we specify DREQ to
  31273. + pace the delivery of bytes to the Arasan hardware */
  31274. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  31275. + BCM2708_DMA_WAITS(dmawaits) |
  31276. + BCM2708_DMA_D_DREQ |
  31277. + BCM2708_DMA_S_WIDTH |
  31278. + BCM2708_DMA_S_INC;
  31279. + cb->src = dma_addr;
  31280. + cb->dst = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  31281. + cb->length = len;
  31282. + cb->stride = 0;
  31283. +
  31284. + if (is_last) {
  31285. + cb->info |= BCM2708_DMA_INT_EN |
  31286. + BCM2708_DMA_WAIT_RESP;
  31287. + cb->next = 0;
  31288. + } else
  31289. + cb->next = host->cb_handle +
  31290. + (ix+1)*sizeof(struct bcm2708_dma_cb);
  31291. +
  31292. + cb->pad[0] = 0;
  31293. + cb->pad[1] = 0;
  31294. +}
  31295. +
  31296. +
  31297. +static void schci_bcm2708_dma_go(struct sdhci_host *host)
  31298. +{
  31299. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31300. + void __iomem *dma_chan_base = host_priv->dma_chan_base;
  31301. +
  31302. + BUG_ON(host_priv->dma_wanted);
  31303. +#ifdef CHECK_DMA_USE
  31304. + if (host_priv->dma_wanted)
  31305. + printk(KERN_ERR "%s: DMA already in progress - "
  31306. + "now %"FMT_HPT", last started %lu "
  31307. + "reset %lu stopped %lu\n",
  31308. + mmc_hostname(host->mmc),
  31309. + hptime(), since_ns(host_priv->when_started),
  31310. + since_ns(host_priv->when_reset),
  31311. + since_ns(host_priv->when_stopped));
  31312. + else if (host_priv->dmas_pending > 0)
  31313. + printk(KERN_INFO "%s: note - new DMA when %d reset DMAs "
  31314. + "already in progress - "
  31315. + "now %"FMT_HPT", started %lu reset %lu stopped %lu\n",
  31316. + mmc_hostname(host->mmc),
  31317. + host_priv->dmas_pending,
  31318. + hptime(), since_ns(host_priv->when_started),
  31319. + since_ns(host_priv->when_reset),
  31320. + since_ns(host_priv->when_stopped));
  31321. + host_priv->dmas_pending += 1;
  31322. + host_priv->when_started = hptime();
  31323. +#endif
  31324. + host_priv->dma_wanted = 1;
  31325. + DBG("PDMA go - base %p handle %08X\n", dma_chan_base,
  31326. + host_priv->cb_handle);
  31327. + bcm_dma_start(dma_chan_base, host_priv->cb_handle);
  31328. +}
  31329. +
  31330. +
  31331. +static void
  31332. +sdhci_platdma_read(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  31333. +{
  31334. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31335. +
  31336. + DBG("PDMA to read %d bytes\n", len);
  31337. + host_priv->sg_done += len;
  31338. + schci_bcm2708_cb_read(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  31339. + schci_bcm2708_dma_go(host);
  31340. +}
  31341. +
  31342. +
  31343. +static void
  31344. +sdhci_platdma_write(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  31345. +{
  31346. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31347. +
  31348. + DBG("PDMA to write %d bytes\n", len);
  31349. + //BUG_ON(0 != (len & 0x1ff));
  31350. +
  31351. + host_priv->sg_done += len;
  31352. + schci_bcm2708_cb_write(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  31353. + schci_bcm2708_dma_go(host);
  31354. +}
  31355. +
  31356. +/*! space is avaiable to receive into or data is available to write
  31357. + Platform DMA exported function
  31358. +*/
  31359. +void
  31360. +sdhci_bcm2708_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  31361. + void(*completion_callback)(struct sdhci_host *host))
  31362. +{
  31363. + struct mmc_data *data = host->data;
  31364. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31365. + int sg_ix;
  31366. + size_t bytes;
  31367. + dma_addr_t addr;
  31368. +
  31369. + BUG_ON(NULL == data);
  31370. + BUG_ON(0 == data->blksz);
  31371. +
  31372. + host_priv->complete = completion_callback;
  31373. +
  31374. + sg_ix = host_priv->sg_ix;
  31375. + BUG_ON(sg_ix >= data->sg_len);
  31376. +
  31377. + /* we can DMA blocks larger than blksz - it may hang the DMA
  31378. + channel but we are its only user */
  31379. + bytes = sg_dma_len(&data->sg[sg_ix]) - host_priv->sg_done;
  31380. + addr = sg_dma_address(&data->sg[sg_ix]) + host_priv->sg_done;
  31381. +
  31382. + if (bytes > 0) {
  31383. + /* We're going to poll for read/write available state until
  31384. + we finish this DMA
  31385. + */
  31386. +
  31387. + if (data->flags & MMC_DATA_READ) {
  31388. + if (*ref_intmask & SDHCI_INT_DATA_AVAIL) {
  31389. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31390. + SDHCI_INT_SPACE_AVAIL);
  31391. + sdhci_platdma_read(host, addr, bytes);
  31392. + }
  31393. + } else {
  31394. + if (*ref_intmask & SDHCI_INT_SPACE_AVAIL) {
  31395. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31396. + SDHCI_INT_SPACE_AVAIL);
  31397. + sdhci_platdma_write(host, addr, bytes);
  31398. + }
  31399. + }
  31400. + }
  31401. + /* else:
  31402. + we have run out of bytes that need transferring (e.g. we may be in
  31403. + the middle of the last DMA transfer), or
  31404. + it is also possible that we've been called when another IRQ is
  31405. + signalled, even though we've turned off signalling of our own IRQ */
  31406. +
  31407. + *ref_intmask &= ~SDHCI_INT_DATA_END;
  31408. + /* don't let the main sdhci driver act on this .. we'll deal with it
  31409. + when we respond to the DMA - if one is currently in progress */
  31410. +}
  31411. +
  31412. +/* is it possible to DMA the given mmc_data structure?
  31413. + Platform DMA exported function
  31414. +*/
  31415. +int /*bool*/
  31416. +sdhci_bcm2708_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  31417. +{
  31418. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31419. + int ok = bcm_sg_suitable_for_dma(data->sg, data->sg_len);
  31420. +
  31421. + if (!ok)
  31422. + DBG("Reverting to PIO - bad cache alignment\n");
  31423. +
  31424. + else {
  31425. + host_priv->sg_ix = 0; /* first SG index */
  31426. + host_priv->sg_done = 0; /* no bytes done */
  31427. + }
  31428. +
  31429. + return ok;
  31430. +}
  31431. +
  31432. +#include <mach/arm_control.h> //GRAYG
  31433. +/*! the current SD transacton has been abandonned
  31434. + We need to tidy up if we were in the middle of a DMA
  31435. + Platform DMA exported function
  31436. +*/
  31437. +void
  31438. +sdhci_bcm2708_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  31439. +{
  31440. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31441. +// unsigned long flags;
  31442. +
  31443. + BUG_ON(NULL == host);
  31444. +
  31445. +// spin_lock_irqsave(&host->lock, flags);
  31446. +
  31447. + if (host_priv->dma_wanted) {
  31448. + if (NULL == data) {
  31449. + printk(KERN_ERR "%s: ongoing DMA reset - no data!\n",
  31450. + mmc_hostname(host->mmc));
  31451. + BUG_ON(NULL == data);
  31452. + } else {
  31453. + struct scatterlist *sg;
  31454. + int sg_len;
  31455. + int sg_todo;
  31456. + int rc;
  31457. + unsigned long cs;
  31458. +
  31459. + sg = data->sg;
  31460. + sg_len = data->sg_len;
  31461. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  31462. +
  31463. + cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  31464. +
  31465. + if (!(BCM2708_DMA_ACTIVE & cs))
  31466. + {
  31467. + if (extra_messages)
  31468. + printk(KERN_INFO "%s: missed completion of "
  31469. + "cmd %d DMA (%d/%d [%d]/[%d]) - "
  31470. + "ignoring it\n",
  31471. + mmc_hostname(host->mmc),
  31472. + host->last_cmdop,
  31473. + host_priv->sg_done, sg_todo,
  31474. + host_priv->sg_ix+1, sg_len);
  31475. + }
  31476. + else
  31477. + printk(KERN_INFO "%s: resetting ongoing cmd %d"
  31478. + "DMA before %d/%d [%d]/[%d] complete\n",
  31479. + mmc_hostname(host->mmc),
  31480. + host->last_cmdop,
  31481. + host_priv->sg_done, sg_todo,
  31482. + host_priv->sg_ix+1, sg_len);
  31483. +#ifdef CHECK_DMA_USE
  31484. + printk(KERN_INFO "%s: now %"FMT_HPT" started %lu "
  31485. + "last reset %lu last stopped %lu\n",
  31486. + mmc_hostname(host->mmc),
  31487. + hptime(), since_ns(host_priv->when_started),
  31488. + since_ns(host_priv->when_reset),
  31489. + since_ns(host_priv->when_stopped));
  31490. + { unsigned long info, debug;
  31491. + void __iomem *base;
  31492. + unsigned long pend0, pend1, pend2;
  31493. +
  31494. + base = host_priv->dma_chan_base;
  31495. + cs = readl(base + BCM2708_DMA_CS);
  31496. + info = readl(base + BCM2708_DMA_INFO);
  31497. + debug = readl(base + BCM2708_DMA_DEBUG);
  31498. + printk(KERN_INFO "%s: DMA%d CS=%08lX TI=%08lX "
  31499. + "DEBUG=%08lX\n",
  31500. + mmc_hostname(host->mmc),
  31501. + host_priv->dma_chan,
  31502. + cs, info, debug);
  31503. + pend0 = readl(__io_address(ARM_IRQ_PEND0));
  31504. + pend1 = readl(__io_address(ARM_IRQ_PEND1));
  31505. + pend2 = readl(__io_address(ARM_IRQ_PEND2));
  31506. +
  31507. + printk(KERN_INFO "%s: PEND0=%08lX "
  31508. + "PEND1=%08lX PEND2=%08lX\n",
  31509. + mmc_hostname(host->mmc),
  31510. + pend0, pend1, pend2);
  31511. +
  31512. + //gintsts = readl(__io_address(GINTSTS));
  31513. + //gintmsk = readl(__io_address(GINTMSK));
  31514. + //printk(KERN_INFO "%s: USB GINTSTS=%08lX"
  31515. + // "GINTMSK=%08lX\n",
  31516. + // mmc_hostname(host->mmc), gintsts, gintmsk);
  31517. + }
  31518. +#endif
  31519. + rc = bcm_dma_abort(host_priv->dma_chan_base);
  31520. + BUG_ON(rc != 0);
  31521. + }
  31522. + host_priv->dma_wanted = 0;
  31523. +#ifdef CHECK_DMA_USE
  31524. + host_priv->when_reset = hptime();
  31525. +#endif
  31526. + }
  31527. +
  31528. +// spin_unlock_irqrestore(&host->lock, flags);
  31529. +}
  31530. +
  31531. +
  31532. +static void sdhci_bcm2708_dma_complete_irq(struct sdhci_host *host,
  31533. + u32 dma_cs)
  31534. +{
  31535. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31536. + struct mmc_data *data;
  31537. + struct scatterlist *sg;
  31538. + int sg_len;
  31539. + int sg_ix;
  31540. + int sg_todo;
  31541. +// unsigned long flags;
  31542. +
  31543. + BUG_ON(NULL == host);
  31544. +
  31545. +// spin_lock_irqsave(&host->lock, flags);
  31546. + data = host->data;
  31547. +
  31548. +#ifdef CHECK_DMA_USE
  31549. + if (host_priv->dmas_pending <= 0)
  31550. + DBG("on completion no DMA in progress - "
  31551. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  31552. + hptime(), since_ns(host_priv->when_started),
  31553. + since_ns(host_priv->when_reset),
  31554. + since_ns(host_priv->when_stopped));
  31555. + else if (host_priv->dmas_pending > 1)
  31556. + DBG("still %d DMA in progress after completion - "
  31557. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  31558. + host_priv->dmas_pending - 1,
  31559. + hptime(), since_ns(host_priv->when_started),
  31560. + since_ns(host_priv->when_reset),
  31561. + since_ns(host_priv->when_stopped));
  31562. + BUG_ON(host_priv->dmas_pending <= 0);
  31563. + host_priv->dmas_pending -= 1;
  31564. + host_priv->when_stopped = hptime();
  31565. +#endif
  31566. + host_priv->dma_wanted = 0;
  31567. +
  31568. + if (NULL == data) {
  31569. + DBG("PDMA unused completion - status 0x%X\n", dma_cs);
  31570. +// spin_unlock_irqrestore(&host->lock, flags);
  31571. + return;
  31572. + }
  31573. + sg = data->sg;
  31574. + sg_len = data->sg_len;
  31575. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  31576. +
  31577. + DBG("PDMA complete %d/%d [%d]/[%d]..\n",
  31578. + host_priv->sg_done, sg_todo,
  31579. + host_priv->sg_ix+1, sg_len);
  31580. +
  31581. + BUG_ON(host_priv->sg_done > sg_todo);
  31582. +
  31583. + if (host_priv->sg_done >= sg_todo) {
  31584. + host_priv->sg_ix++;
  31585. + host_priv->sg_done = 0;
  31586. + }
  31587. +
  31588. + sg_ix = host_priv->sg_ix;
  31589. + if (sg_ix < sg_len) {
  31590. + u32 irq_mask;
  31591. + /* Set off next DMA if we've got the capacity */
  31592. +
  31593. + if (data->flags & MMC_DATA_READ)
  31594. + irq_mask = SDHCI_INT_DATA_AVAIL;
  31595. + else
  31596. + irq_mask = SDHCI_INT_SPACE_AVAIL;
  31597. +
  31598. + /* We have to use the interrupt status register on the BCM2708
  31599. + rather than the SDHCI_PRESENT_STATE register because latency
  31600. + in the glue logic means that the information retrieved from
  31601. + the latter is not always up-to-date w.r.t the DMA engine -
  31602. + it may not indicate that a read or a write is ready yet */
  31603. + if (sdhci_bcm2708_raw_readl(host, SDHCI_INT_STATUS) &
  31604. + irq_mask) {
  31605. + size_t bytes = sg_dma_len(&sg[sg_ix]) -
  31606. + host_priv->sg_done;
  31607. + dma_addr_t addr = sg_dma_address(&data->sg[sg_ix]) +
  31608. + host_priv->sg_done;
  31609. +
  31610. + /* acknowledge interrupt */
  31611. + sdhci_bcm2708_raw_writel(host, irq_mask,
  31612. + SDHCI_INT_STATUS);
  31613. +
  31614. + BUG_ON(0 == bytes);
  31615. +
  31616. + if (data->flags & MMC_DATA_READ)
  31617. + sdhci_platdma_read(host, addr, bytes);
  31618. + else
  31619. + sdhci_platdma_write(host, addr, bytes);
  31620. + } else {
  31621. + DBG("PDMA - wait avail\n");
  31622. + /* may generate an IRQ if already present */
  31623. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31624. + SDHCI_INT_SPACE_AVAIL);
  31625. + }
  31626. + } else {
  31627. + if (sync_after_dma) {
  31628. + /* On the Arasan controller the stop command (which will be
  31629. + scheduled after this completes) does not seem to work
  31630. + properly if we allow it to be issued when we are
  31631. + transferring data to/from the SD card.
  31632. + We get CRC and DEND errors unless we wait for
  31633. + the SD controller to finish reading/writing to the card. */
  31634. + u32 state_mask;
  31635. + int timeout=3*1000*1000;
  31636. +
  31637. + DBG("PDMA over - sync card\n");
  31638. + if (data->flags & MMC_DATA_READ)
  31639. + state_mask = SDHCI_DOING_READ;
  31640. + else
  31641. + state_mask = SDHCI_DOING_WRITE;
  31642. +
  31643. + while (0 != (sdhci_bcm2708_raw_readl(host, SDHCI_PRESENT_STATE)
  31644. + & state_mask) && --timeout > 0)
  31645. + {
  31646. + udelay(1);
  31647. + continue;
  31648. + }
  31649. + if (timeout <= 0)
  31650. + printk(KERN_ERR"%s: final %s to SD card still "
  31651. + "running\n",
  31652. + mmc_hostname(host->mmc),
  31653. + data->flags & MMC_DATA_READ? "read": "write");
  31654. + }
  31655. + if (host_priv->complete) {
  31656. + (*host_priv->complete)(host);
  31657. + DBG("PDMA %s complete\n",
  31658. + data->flags & MMC_DATA_READ?"read":"write");
  31659. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31660. + SDHCI_INT_SPACE_AVAIL);
  31661. + }
  31662. + }
  31663. +// spin_unlock_irqrestore(&host->lock, flags);
  31664. +}
  31665. +
  31666. +static irqreturn_t sdhci_bcm2708_dma_irq(int irq, void *dev_id)
  31667. +{
  31668. + irqreturn_t result = IRQ_NONE;
  31669. + struct sdhci_host *host = dev_id;
  31670. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31671. + u32 dma_cs; /* control and status register */
  31672. +
  31673. + BUG_ON(NULL == dev_id);
  31674. + BUG_ON(NULL == host_priv->dma_chan_base);
  31675. +
  31676. + sdhci_spin_lock(host);
  31677. +
  31678. + dma_cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  31679. +
  31680. + if (dma_cs & BCM2708_DMA_ERR) {
  31681. + unsigned long debug;
  31682. + debug = readl(host_priv->dma_chan_base +
  31683. + BCM2708_DMA_DEBUG);
  31684. + printk(KERN_ERR "%s: DMA error - CS %lX DEBUG %lX\n",
  31685. + mmc_hostname(host->mmc), (unsigned long)dma_cs,
  31686. + (unsigned long)debug);
  31687. + /* reset error */
  31688. + writel(debug, host_priv->dma_chan_base +
  31689. + BCM2708_DMA_DEBUG);
  31690. + }
  31691. + if (dma_cs & BCM2708_DMA_INT) {
  31692. + /* acknowledge interrupt */
  31693. + writel(BCM2708_DMA_INT,
  31694. + host_priv->dma_chan_base + BCM2708_DMA_CS);
  31695. +
  31696. + dsb(); /* ARM data synchronization (push) operation */
  31697. +
  31698. + if (!host_priv->dma_wanted) {
  31699. + /* ignore this interrupt - it was reset */
  31700. + if (extra_messages)
  31701. + printk(KERN_INFO "%s: DMA IRQ %X ignored - "
  31702. + "results were reset\n",
  31703. + mmc_hostname(host->mmc), dma_cs);
  31704. +#ifdef CHECK_DMA_USE
  31705. + printk(KERN_INFO "%s: now %"FMT_HPT
  31706. + " started %lu reset %lu stopped %lu\n",
  31707. + mmc_hostname(host->mmc), hptime(),
  31708. + since_ns(host_priv->when_started),
  31709. + since_ns(host_priv->when_reset),
  31710. + since_ns(host_priv->when_stopped));
  31711. + host_priv->dmas_pending--;
  31712. +#endif
  31713. + } else
  31714. + sdhci_bcm2708_dma_complete_irq(host, dma_cs);
  31715. +
  31716. + result = IRQ_HANDLED;
  31717. + }
  31718. + sdhci_spin_unlock(host);
  31719. +
  31720. + return result;
  31721. +}
  31722. +#endif /* CONFIG_MMC_SDHCI_BCM2708_DMA */
  31723. +
  31724. +
  31725. +/***************************************************************************** \
  31726. + * *
  31727. + * Device Attributes *
  31728. + * *
  31729. +\*****************************************************************************/
  31730. +
  31731. +
  31732. +/**
  31733. + * Show the DMA-using status
  31734. + */
  31735. +static ssize_t attr_dma_show(struct device *_dev,
  31736. + struct device_attribute *attr, char *buf)
  31737. +{
  31738. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31739. +
  31740. + if (host) {
  31741. + int use_dma = (host->flags & SDHCI_USE_PLATDMA? 1:0);
  31742. + return sprintf(buf, "%d\n", use_dma);
  31743. + } else
  31744. + return -EINVAL;
  31745. +}
  31746. +
  31747. +/**
  31748. + * Set the DMA-using status
  31749. + */
  31750. +static ssize_t attr_dma_store(struct device *_dev,
  31751. + struct device_attribute *attr,
  31752. + const char *buf, size_t count)
  31753. +{
  31754. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31755. +
  31756. + if (host) {
  31757. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31758. + int on = simple_strtol(buf, NULL, 0);
  31759. + if (on) {
  31760. + host->flags |= SDHCI_USE_PLATDMA;
  31761. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  31762. + printk(KERN_INFO "%s: DMA enabled\n",
  31763. + mmc_hostname(host->mmc));
  31764. + } else {
  31765. + host->flags &= ~(SDHCI_USE_PLATDMA | SDHCI_REQ_USE_DMA);
  31766. + sdhci_bcm2708_writel(host, 0, REG_EXRDFIFO_EN);
  31767. + printk(KERN_INFO "%s: DMA disabled\n",
  31768. + mmc_hostname(host->mmc));
  31769. + }
  31770. +#endif
  31771. + return count;
  31772. + } else
  31773. + return -EINVAL;
  31774. +}
  31775. +
  31776. +static DEVICE_ATTR(use_dma, S_IRUGO | S_IWUGO, attr_dma_show, attr_dma_store);
  31777. +
  31778. +
  31779. +/**
  31780. + * Show the DMA wait states used
  31781. + */
  31782. +static ssize_t attr_dmawait_show(struct device *_dev,
  31783. + struct device_attribute *attr, char *buf)
  31784. +{
  31785. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31786. +
  31787. + if (host) {
  31788. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31789. + int dmawait = host_priv->dma_waits;
  31790. + return sprintf(buf, "%d\n", dmawait);
  31791. + } else
  31792. + return -EINVAL;
  31793. +}
  31794. +
  31795. +/**
  31796. + * Set the DMA wait state used
  31797. + */
  31798. +static ssize_t attr_dmawait_store(struct device *_dev,
  31799. + struct device_attribute *attr,
  31800. + const char *buf, size_t count)
  31801. +{
  31802. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31803. +
  31804. + if (host) {
  31805. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31806. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31807. + int dma_waits = simple_strtol(buf, NULL, 0);
  31808. + if (dma_waits >= 0 && dma_waits < 32)
  31809. + host_priv->dma_waits = dma_waits;
  31810. + else
  31811. + printk(KERN_ERR "%s: illegal dma_waits value - %d",
  31812. + mmc_hostname(host->mmc), dma_waits);
  31813. +#endif
  31814. + return count;
  31815. + } else
  31816. + return -EINVAL;
  31817. +}
  31818. +
  31819. +static DEVICE_ATTR(dma_wait, S_IRUGO | S_IWUGO,
  31820. + attr_dmawait_show, attr_dmawait_store);
  31821. +
  31822. +
  31823. +/**
  31824. + * Show the DMA-using status
  31825. + */
  31826. +static ssize_t attr_status_show(struct device *_dev,
  31827. + struct device_attribute *attr, char *buf)
  31828. +{
  31829. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31830. +
  31831. + if (host) {
  31832. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31833. + return sprintf(buf,
  31834. + "present: yes\n"
  31835. + "power: %s\n"
  31836. + "clock: %u Hz\n"
  31837. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31838. + "dma: %s (%d waits)\n",
  31839. +#else
  31840. + "dma: unconfigured\n",
  31841. +#endif
  31842. + "always on",
  31843. + host->clock
  31844. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31845. + , (host->flags & SDHCI_USE_PLATDMA)? "on": "off"
  31846. + , host_priv->dma_waits
  31847. +#endif
  31848. + );
  31849. + } else
  31850. + return -EINVAL;
  31851. +}
  31852. +
  31853. +static DEVICE_ATTR(status, S_IRUGO, attr_status_show, NULL);
  31854. +
  31855. +/***************************************************************************** \
  31856. + * *
  31857. + * Power Management *
  31858. + * *
  31859. +\*****************************************************************************/
  31860. +
  31861. +
  31862. +#ifdef CONFIG_PM
  31863. +static int sdhci_bcm2708_suspend(struct platform_device *dev, pm_message_t state)
  31864. +{
  31865. + struct sdhci_host *host = (struct sdhci_host *)
  31866. + platform_get_drvdata(dev);
  31867. + int ret = 0;
  31868. +
  31869. + if (host->mmc) {
  31870. + ret = mmc_suspend_host(host->mmc);
  31871. + }
  31872. +
  31873. + return ret;
  31874. +}
  31875. +
  31876. +static int sdhci_bcm2708_resume(struct platform_device *dev)
  31877. +{
  31878. + struct sdhci_host *host = (struct sdhci_host *)
  31879. + platform_get_drvdata(dev);
  31880. + int ret = 0;
  31881. +
  31882. + if (host->mmc) {
  31883. + ret = mmc_resume_host(host->mmc);
  31884. + }
  31885. +
  31886. + return ret;
  31887. +}
  31888. +#endif
  31889. +
  31890. +
  31891. +/*****************************************************************************\
  31892. + * *
  31893. + * Device quirk functions. Implemented as local ops because the flags *
  31894. + * field is out of space with newer kernels. This implementation can be *
  31895. + * back ported to older kernels as well. *
  31896. +\****************************************************************************/
  31897. +static unsigned int sdhci_bcm2708_quirk_extra_ints(struct sdhci_host *host)
  31898. +{
  31899. + return 1;
  31900. +}
  31901. +
  31902. +static unsigned int sdhci_bcm2708_quirk_spurious_crc_acmd51(struct sdhci_host *host)
  31903. +{
  31904. + return 1;
  31905. +}
  31906. +
  31907. +static unsigned int sdhci_bcm2708_missing_status(struct sdhci_host *host)
  31908. +{
  31909. + return 1;
  31910. +}
  31911. +
  31912. +/***************************************************************************** \
  31913. + * *
  31914. + * Device ops *
  31915. + * *
  31916. +\*****************************************************************************/
  31917. +
  31918. +static struct sdhci_ops sdhci_bcm2708_ops = {
  31919. +#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
  31920. + .read_l = sdhci_bcm2708_readl,
  31921. + .read_w = sdhci_bcm2708_readw,
  31922. + .read_b = sdhci_bcm2708_readb,
  31923. + .write_l = sdhci_bcm2708_writel,
  31924. + .write_w = sdhci_bcm2708_writew,
  31925. + .write_b = sdhci_bcm2708_writeb,
  31926. +#else
  31927. +#error The BCM2708 SDHCI driver needs CONFIG_MMC_SDHCI_IO_ACCESSORS to be set
  31928. +#endif
  31929. + .get_max_clock = sdhci_bcm2708_get_max_clock,
  31930. +
  31931. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31932. + // Platform DMA operations
  31933. + .pdma_able = sdhci_bcm2708_platdma_dmaable,
  31934. + .pdma_avail = sdhci_bcm2708_platdma_avail,
  31935. + .pdma_reset = sdhci_bcm2708_platdma_reset,
  31936. +#endif
  31937. + .extra_ints = sdhci_bcm2708_quirk_extra_ints,
  31938. +};
  31939. +
  31940. +/*****************************************************************************\
  31941. + * *
  31942. + * Device probing/removal *
  31943. + * *
  31944. +\*****************************************************************************/
  31945. +
  31946. +static int sdhci_bcm2708_probe(struct platform_device *pdev)
  31947. +{
  31948. + struct sdhci_host *host;
  31949. + struct resource *iomem;
  31950. + struct sdhci_bcm2708_priv *host_priv;
  31951. + int ret;
  31952. +
  31953. + BUG_ON(pdev == NULL);
  31954. +
  31955. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  31956. + if (!iomem) {
  31957. + ret = -ENOMEM;
  31958. + goto err;
  31959. + }
  31960. +
  31961. + if (resource_size(iomem) != 0x100)
  31962. + dev_err(&pdev->dev, "Invalid iomem size. You may "
  31963. + "experience problems.\n");
  31964. +
  31965. + if (pdev->dev.parent)
  31966. + host = sdhci_alloc_host(pdev->dev.parent,
  31967. + sizeof(struct sdhci_bcm2708_priv));
  31968. + else
  31969. + host = sdhci_alloc_host(&pdev->dev,
  31970. + sizeof(struct sdhci_bcm2708_priv));
  31971. +
  31972. + if (IS_ERR(host)) {
  31973. + ret = PTR_ERR(host);
  31974. + goto err;
  31975. + }
  31976. + if (missing_status) {
  31977. + sdhci_bcm2708_ops.missing_status = sdhci_bcm2708_missing_status;
  31978. + }
  31979. +
  31980. + if( spurious_crc_acmd51 ) {
  31981. + sdhci_bcm2708_ops.spurious_crc_acmd51 = sdhci_bcm2708_quirk_spurious_crc_acmd51;
  31982. + }
  31983. +
  31984. +
  31985. + printk("sdhci: %s low-latency mode\n",enable_llm?"Enable":"Disable");
  31986. +
  31987. + host->hw_name = "BCM2708_Arasan";
  31988. + host->ops = &sdhci_bcm2708_ops;
  31989. + host->irq = platform_get_irq(pdev, 0);
  31990. + host->second_irq = 0;
  31991. +
  31992. + host->quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  31993. + SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  31994. + SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  31995. + SDHCI_QUIRK_MISSING_CAPS |
  31996. + SDHCI_QUIRK_NO_HISPD_BIT |
  31997. + (sync_after_dma ? 0:SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12);
  31998. +
  31999. +
  32000. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32001. + host->flags = SDHCI_USE_PLATDMA;
  32002. +#endif
  32003. +
  32004. + if (!request_mem_region(iomem->start, resource_size(iomem),
  32005. + mmc_hostname(host->mmc))) {
  32006. + dev_err(&pdev->dev, "cannot request region\n");
  32007. + ret = -EBUSY;
  32008. + goto err_request;
  32009. + }
  32010. +
  32011. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  32012. + if (!host->ioaddr) {
  32013. + dev_err(&pdev->dev, "failed to remap registers\n");
  32014. + ret = -ENOMEM;
  32015. + goto err_remap;
  32016. + }
  32017. +
  32018. + host_priv = SDHCI_HOST_PRIV(host);
  32019. +
  32020. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32021. + host_priv->dma_wanted = 0;
  32022. +#ifdef CHECK_DMA_USE
  32023. + host_priv->dmas_pending = 0;
  32024. + host_priv->when_started = 0;
  32025. + host_priv->when_reset = 0;
  32026. + host_priv->when_stopped = 0;
  32027. +#endif
  32028. + host_priv->sg_ix = 0;
  32029. + host_priv->sg_done = 0;
  32030. + host_priv->complete = NULL;
  32031. + host_priv->dma_waits = SDHCI_BCM_DMA_WAITS;
  32032. +
  32033. + host_priv->cb_base = dma_alloc_writecombine(&pdev->dev, SZ_4K,
  32034. + &host_priv->cb_handle,
  32035. + GFP_KERNEL);
  32036. + if (!host_priv->cb_base) {
  32037. + dev_err(&pdev->dev, "cannot allocate DMA CBs\n");
  32038. + ret = -ENOMEM;
  32039. + goto err_alloc_cb;
  32040. + }
  32041. +
  32042. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  32043. + &host_priv->dma_chan_base,
  32044. + &host_priv->dma_irq);
  32045. + if (ret < 0) {
  32046. + dev_err(&pdev->dev, "couldn't allocate a DMA channel\n");
  32047. + goto err_add_dma;
  32048. + }
  32049. + host_priv->dma_chan = ret;
  32050. +
  32051. + ret = request_irq(host_priv->dma_irq, sdhci_bcm2708_dma_irq,
  32052. + 0 /*IRQF_SHARED*/, DRIVER_NAME " (dma)", host);
  32053. + if (ret) {
  32054. + dev_err(&pdev->dev, "cannot set DMA IRQ\n");
  32055. + goto err_add_dma_irq;
  32056. + }
  32057. + host->second_irq = host_priv->dma_irq;
  32058. + DBG("DMA CBs %p handle %08X DMA%d %p DMA IRQ %d\n",
  32059. + host_priv->cb_base, (unsigned)host_priv->cb_handle,
  32060. + host_priv->dma_chan, host_priv->dma_chan_base,
  32061. + host_priv->dma_irq);
  32062. +
  32063. + // we support 3.3V
  32064. + host->caps |= SDHCI_CAN_VDD_330;
  32065. + if (allow_highspeed)
  32066. + host->mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  32067. +
  32068. + /* single block writes cause data loss with some SD cards! */
  32069. + host->mmc->caps2 |= MMC_CAP2_FORCE_MULTIBLOCK;
  32070. +#endif
  32071. +
  32072. + ret = sdhci_add_host(host);
  32073. + if (ret)
  32074. + goto err_add_host;
  32075. +
  32076. + platform_set_drvdata(pdev, host);
  32077. + ret = device_create_file(&pdev->dev, &dev_attr_use_dma);
  32078. + ret = device_create_file(&pdev->dev, &dev_attr_dma_wait);
  32079. + ret = device_create_file(&pdev->dev, &dev_attr_status);
  32080. +
  32081. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32082. + /* enable extension fifo for paced DMA transfers */
  32083. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  32084. + sdhci_bcm2708_writel(host, 4, REG_EXRDFIFO_CFG);
  32085. +#endif
  32086. +
  32087. + printk(KERN_INFO "%s: BCM2708 SDHC host at 0x%08llx DMA %d IRQ %d\n",
  32088. + mmc_hostname(host->mmc), (unsigned long long)iomem->start,
  32089. + host_priv->dma_chan, host_priv->dma_irq);
  32090. +
  32091. + return 0;
  32092. +
  32093. +err_add_host:
  32094. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32095. + free_irq(host_priv->dma_irq, host);
  32096. +err_add_dma_irq:
  32097. + bcm_dma_chan_free(host_priv->dma_chan);
  32098. +err_add_dma:
  32099. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  32100. + host_priv->cb_handle);
  32101. +err_alloc_cb:
  32102. +#endif
  32103. + iounmap(host->ioaddr);
  32104. +err_remap:
  32105. + release_mem_region(iomem->start, resource_size(iomem));
  32106. +err_request:
  32107. + sdhci_free_host(host);
  32108. +err:
  32109. + dev_err(&pdev->dev, "probe failed, err %d\n", ret);
  32110. + return ret;
  32111. +}
  32112. +
  32113. +static int sdhci_bcm2708_remove(struct platform_device *pdev)
  32114. +{
  32115. + struct sdhci_host *host = platform_get_drvdata(pdev);
  32116. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  32117. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  32118. + int dead;
  32119. + u32 scratch;
  32120. +
  32121. + dead = 0;
  32122. + scratch = sdhci_bcm2708_readl(host, SDHCI_INT_STATUS);
  32123. + if (scratch == (u32)-1)
  32124. + dead = 1;
  32125. +
  32126. + device_remove_file(&pdev->dev, &dev_attr_status);
  32127. + device_remove_file(&pdev->dev, &dev_attr_dma_wait);
  32128. + device_remove_file(&pdev->dev, &dev_attr_use_dma);
  32129. +
  32130. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32131. + free_irq(host_priv->dma_irq, host);
  32132. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  32133. + host_priv->cb_handle);
  32134. +#endif
  32135. + sdhci_remove_host(host, dead);
  32136. + iounmap(host->ioaddr);
  32137. + release_mem_region(iomem->start, resource_size(iomem));
  32138. + sdhci_free_host(host);
  32139. + platform_set_drvdata(pdev, NULL);
  32140. +
  32141. + return 0;
  32142. +}
  32143. +
  32144. +static struct platform_driver sdhci_bcm2708_driver = {
  32145. + .driver = {
  32146. + .name = DRIVER_NAME,
  32147. + .owner = THIS_MODULE,
  32148. + },
  32149. + .probe = sdhci_bcm2708_probe,
  32150. + .remove = sdhci_bcm2708_remove,
  32151. +
  32152. +#ifdef CONFIG_PM
  32153. + .suspend = sdhci_bcm2708_suspend,
  32154. + .resume = sdhci_bcm2708_resume,
  32155. +#endif
  32156. +
  32157. +};
  32158. +
  32159. +/*****************************************************************************\
  32160. + * *
  32161. + * Driver init/exit *
  32162. + * *
  32163. +\*****************************************************************************/
  32164. +
  32165. +static int __init sdhci_drv_init(void)
  32166. +{
  32167. + return platform_driver_register(&sdhci_bcm2708_driver);
  32168. +}
  32169. +
  32170. +static void __exit sdhci_drv_exit(void)
  32171. +{
  32172. + platform_driver_unregister(&sdhci_bcm2708_driver);
  32173. +}
  32174. +
  32175. +module_init(sdhci_drv_init);
  32176. +module_exit(sdhci_drv_exit);
  32177. +
  32178. +module_param(allow_highspeed, bool, 0444);
  32179. +module_param(emmc_clock_freq, int, 0444);
  32180. +module_param(sync_after_dma, bool, 0444);
  32181. +module_param(missing_status, bool, 0444);
  32182. +module_param(spurious_crc_acmd51, bool, 0444);
  32183. +module_param(enable_llm, bool, 0444);
  32184. +module_param(cycle_delay, int, 0444);
  32185. +module_param(extra_messages, bool, 0444);
  32186. +
  32187. +MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
  32188. +MODULE_AUTHOR("Broadcom <info@broadcom.com>");
  32189. +MODULE_LICENSE("GPL v2");
  32190. +MODULE_ALIAS("platform:"DRIVER_NAME);
  32191. +
  32192. +MODULE_PARM_DESC(allow_highspeed, "Allow high speed transfers modes");
  32193. +MODULE_PARM_DESC(emmc_clock_freq, "Specify the speed of emmc clock");
  32194. +MODULE_PARM_DESC(sync_after_dma, "Block in driver until dma complete");
  32195. +MODULE_PARM_DESC(missing_status, "Use the missing status quirk");
  32196. +MODULE_PARM_DESC(spurious_crc_acmd51, "Use the spurious crc quirk for reading SCR (ACMD51)");
  32197. +MODULE_PARM_DESC(enable_llm, "Enable low-latency mode");
  32198. +MODULE_PARM_DESC(extra_messages, "Enable more sdcard warning messages");
  32199. +
  32200. +
  32201. diff -Nur linux-3.12.13/drivers/mmc/host/sdhci.c linux-raspberry-pi/drivers/mmc/host/sdhci.c
  32202. --- linux-3.12.13/drivers/mmc/host/sdhci.c 2014-02-22 22:32:50.000000000 +0100
  32203. +++ linux-raspberry-pi/drivers/mmc/host/sdhci.c 2014-03-11 17:51:21.000000000 +0100
  32204. @@ -28,6 +28,7 @@
  32205. #include <linux/mmc/mmc.h>
  32206. #include <linux/mmc/host.h>
  32207. #include <linux/mmc/card.h>
  32208. +#include <linux/mmc/sd.h>
  32209. #include <linux/mmc/slot-gpio.h>
  32210. #include "sdhci.h"
  32211. @@ -131,6 +132,99 @@
  32212. * Low level functions *
  32213. * *
  32214. \*****************************************************************************/
  32215. +extern bool enable_llm;
  32216. +static int sdhci_locked=0;
  32217. +void sdhci_spin_lock(struct sdhci_host *host)
  32218. +{
  32219. + spin_lock(&host->lock);
  32220. +#ifdef CONFIG_PREEMPT
  32221. + if(enable_llm)
  32222. + {
  32223. + disable_irq_nosync(host->irq);
  32224. + if(host->second_irq)
  32225. + disable_irq_nosync(host->second_irq);
  32226. + local_irq_enable();
  32227. + }
  32228. +#endif
  32229. +}
  32230. +
  32231. +void sdhci_spin_unlock(struct sdhci_host *host)
  32232. +{
  32233. +#ifdef CONFIG_PREEMPT
  32234. + if(enable_llm)
  32235. + {
  32236. + local_irq_disable();
  32237. + if(host->second_irq)
  32238. + enable_irq(host->second_irq);
  32239. + enable_irq(host->irq);
  32240. + }
  32241. +#endif
  32242. + spin_unlock(&host->lock);
  32243. +}
  32244. +
  32245. +void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags)
  32246. +{
  32247. +#ifdef CONFIG_PREEMPT
  32248. + if(enable_llm)
  32249. + {
  32250. + while(sdhci_locked)
  32251. + {
  32252. + preempt_schedule();
  32253. + }
  32254. + spin_lock_irqsave(&host->lock,*flags);
  32255. + disable_irq(host->irq);
  32256. + if(host->second_irq)
  32257. + disable_irq(host->second_irq);
  32258. + local_irq_enable();
  32259. + }
  32260. + else
  32261. +#endif
  32262. + spin_lock_irqsave(&host->lock,*flags);
  32263. +}
  32264. +
  32265. +void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags)
  32266. +{
  32267. +#ifdef CONFIG_PREEMPT
  32268. + if(enable_llm)
  32269. + {
  32270. + local_irq_disable();
  32271. + if(host->second_irq)
  32272. + enable_irq(host->second_irq);
  32273. + enable_irq(host->irq);
  32274. + }
  32275. +#endif
  32276. + spin_unlock_irqrestore(&host->lock,flags);
  32277. +}
  32278. +
  32279. +static void sdhci_spin_enable_schedule(struct sdhci_host *host)
  32280. +{
  32281. +#ifdef CONFIG_PREEMPT
  32282. + if(enable_llm)
  32283. + {
  32284. + sdhci_locked = 1;
  32285. + preempt_enable();
  32286. + }
  32287. +#endif
  32288. +}
  32289. +
  32290. +static void sdhci_spin_disable_schedule(struct sdhci_host *host)
  32291. +{
  32292. +#ifdef CONFIG_PREEMPT
  32293. + if(enable_llm)
  32294. + {
  32295. + preempt_disable();
  32296. + sdhci_locked = 0;
  32297. + }
  32298. +#endif
  32299. +}
  32300. +
  32301. +
  32302. +#undef spin_lock_irqsave
  32303. +#define spin_lock_irqsave(host_lock, flags) sdhci_spin_lock_irqsave(container_of(host_lock, struct sdhci_host, lock), &flags)
  32304. +#define spin_unlock_irqrestore(host_lock, flags) sdhci_spin_unlock_irqrestore(container_of(host_lock, struct sdhci_host, lock), flags)
  32305. +
  32306. +#define spin_lock(host_lock) sdhci_spin_lock(container_of(host_lock, struct sdhci_host, lock))
  32307. +#define spin_unlock(host_lock) sdhci_spin_unlock(container_of(host_lock, struct sdhci_host, lock))
  32308. static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  32309. {
  32310. @@ -300,7 +394,7 @@
  32311. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  32312. unsigned long flags;
  32313. - spin_lock_irqsave(&host->lock, flags);
  32314. + sdhci_spin_lock_irqsave(host, &flags);
  32315. if (host->runtime_suspended)
  32316. goto out;
  32317. @@ -310,7 +404,7 @@
  32318. else
  32319. sdhci_activate_led(host);
  32320. out:
  32321. - spin_unlock_irqrestore(&host->lock, flags);
  32322. + sdhci_spin_unlock_irqrestore(host, flags);
  32323. }
  32324. #endif
  32325. @@ -327,7 +421,7 @@
  32326. u32 uninitialized_var(scratch);
  32327. u8 *buf;
  32328. - DBG("PIO reading\n");
  32329. + DBG("PIO reading %db\n", host->data->blksz);
  32330. blksize = host->data->blksz;
  32331. chunk = 0;
  32332. @@ -372,7 +466,7 @@
  32333. u32 scratch;
  32334. u8 *buf;
  32335. - DBG("PIO writing\n");
  32336. + DBG("PIO writing %db\n", host->data->blksz);
  32337. blksize = host->data->blksz;
  32338. chunk = 0;
  32339. @@ -411,19 +505,28 @@
  32340. local_irq_restore(flags);
  32341. }
  32342. -static void sdhci_transfer_pio(struct sdhci_host *host)
  32343. +static void sdhci_transfer_pio(struct sdhci_host *host, u32 intstate)
  32344. {
  32345. u32 mask;
  32346. + u32 state = 0;
  32347. + u32 intmask;
  32348. + int available;
  32349. BUG_ON(!host->data);
  32350. if (host->blocks == 0)
  32351. return;
  32352. - if (host->data->flags & MMC_DATA_READ)
  32353. + if (host->data->flags & MMC_DATA_READ) {
  32354. mask = SDHCI_DATA_AVAILABLE;
  32355. - else
  32356. + intmask = SDHCI_INT_DATA_AVAIL;
  32357. + } else {
  32358. mask = SDHCI_SPACE_AVAILABLE;
  32359. + intmask = SDHCI_INT_SPACE_AVAIL;
  32360. + }
  32361. +
  32362. + /* initially we can see whether we can procede using intstate */
  32363. + available = (intstate & intmask);
  32364. /*
  32365. * Some controllers (JMicron JMB38x) mess up the buffer bits
  32366. @@ -434,7 +537,7 @@
  32367. (host->data->blocks == 1))
  32368. mask = ~0;
  32369. - while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  32370. + while (available) {
  32371. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  32372. udelay(100);
  32373. @@ -446,9 +549,12 @@
  32374. host->blocks--;
  32375. if (host->blocks == 0)
  32376. break;
  32377. + state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  32378. + available = state & mask;
  32379. + break;
  32380. }
  32381. - DBG("PIO transfer complete.\n");
  32382. + DBG("PIO transfer complete - %d blocks left.\n", host->blocks);
  32383. }
  32384. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  32385. @@ -721,7 +827,9 @@
  32386. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  32387. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  32388. - if (host->flags & SDHCI_REQ_USE_DMA)
  32389. + /* platform DMA will begin on receipt of PIO irqs */
  32390. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  32391. + !(host->flags & SDHCI_USE_PLATDMA))
  32392. sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
  32393. else
  32394. sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
  32395. @@ -753,44 +861,25 @@
  32396. host->data_early = 0;
  32397. host->data->bytes_xfered = 0;
  32398. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  32399. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA | SDHCI_USE_PLATDMA))
  32400. host->flags |= SDHCI_REQ_USE_DMA;
  32401. /*
  32402. * FIXME: This doesn't account for merging when mapping the
  32403. * scatterlist.
  32404. */
  32405. - if (host->flags & SDHCI_REQ_USE_DMA) {
  32406. - int broken, i;
  32407. - struct scatterlist *sg;
  32408. -
  32409. - broken = 0;
  32410. - if (host->flags & SDHCI_USE_ADMA) {
  32411. - if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  32412. - broken = 1;
  32413. - } else {
  32414. - if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  32415. - broken = 1;
  32416. - }
  32417. -
  32418. - if (unlikely(broken)) {
  32419. - for_each_sg(data->sg, sg, data->sg_len, i) {
  32420. - if (sg->length & 0x3) {
  32421. - DBG("Reverting to PIO because of "
  32422. - "transfer size (%d)\n",
  32423. - sg->length);
  32424. - host->flags &= ~SDHCI_REQ_USE_DMA;
  32425. - break;
  32426. - }
  32427. - }
  32428. - }
  32429. - }
  32430. /*
  32431. * The assumption here being that alignment is the same after
  32432. * translation to device address space.
  32433. */
  32434. - if (host->flags & SDHCI_REQ_USE_DMA) {
  32435. + if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) ==
  32436. + (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) {
  32437. +
  32438. + if (! sdhci_platdma_dmaable(host, data))
  32439. + host->flags &= ~SDHCI_REQ_USE_DMA;
  32440. +
  32441. + } else if (host->flags & SDHCI_REQ_USE_DMA) {
  32442. int broken, i;
  32443. struct scatterlist *sg;
  32444. @@ -849,7 +938,8 @@
  32445. */
  32446. WARN_ON(1);
  32447. host->flags &= ~SDHCI_REQ_USE_DMA;
  32448. - } else {
  32449. + } else
  32450. + if (!(host->flags & SDHCI_USE_PLATDMA)) {
  32451. WARN_ON(sg_cnt != 1);
  32452. sdhci_writel(host, sg_dma_address(data->sg),
  32453. SDHCI_DMA_ADDRESS);
  32454. @@ -865,11 +955,13 @@
  32455. if (host->version >= SDHCI_SPEC_200) {
  32456. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  32457. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  32458. + if (! (host->flags & SDHCI_USE_PLATDMA)) {
  32459. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  32460. (host->flags & SDHCI_USE_ADMA))
  32461. ctrl |= SDHCI_CTRL_ADMA32;
  32462. else
  32463. ctrl |= SDHCI_CTRL_SDMA;
  32464. + }
  32465. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  32466. }
  32467. @@ -921,7 +1013,8 @@
  32468. if (data->flags & MMC_DATA_READ)
  32469. mode |= SDHCI_TRNS_READ;
  32470. - if (host->flags & SDHCI_REQ_USE_DMA)
  32471. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  32472. + !(host->flags & SDHCI_USE_PLATDMA))
  32473. mode |= SDHCI_TRNS_DMA;
  32474. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  32475. @@ -937,13 +1030,16 @@
  32476. host->data = NULL;
  32477. if (host->flags & SDHCI_REQ_USE_DMA) {
  32478. - if (host->flags & SDHCI_USE_ADMA)
  32479. - sdhci_adma_table_post(host, data);
  32480. - else {
  32481. + /* we may have to abandon an ongoing platform DMA */
  32482. + if (host->flags & SDHCI_USE_PLATDMA)
  32483. + sdhci_platdma_reset(host, data);
  32484. +
  32485. + if (host->flags & (SDHCI_USE_PLATDMA | SDHCI_USE_SDMA)) {
  32486. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  32487. data->sg_len, (data->flags & MMC_DATA_READ) ?
  32488. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  32489. - }
  32490. + } else if (host->flags & SDHCI_USE_ADMA)
  32491. + sdhci_adma_table_post(host, data);
  32492. }
  32493. /*
  32494. @@ -996,6 +1092,12 @@
  32495. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  32496. mask |= SDHCI_DATA_INHIBIT;
  32497. + if(host->ops->missing_status && (cmd->opcode == MMC_SEND_STATUS)) {
  32498. + timeout = 5000; // Really obscenely large delay to send the status, due to bug in controller
  32499. + // which might cause the STATUS command to get stuck when a data operation is in flow
  32500. + mask |= SDHCI_DATA_INHIBIT;
  32501. + }
  32502. +
  32503. /* We shouldn't wait for data inihibit for stop commands, even
  32504. though they might use busy signaling */
  32505. if (host->mrq->data && (cmd == host->mrq->data->stop))
  32506. @@ -1011,12 +1113,20 @@
  32507. return;
  32508. }
  32509. timeout--;
  32510. + sdhci_spin_enable_schedule(host);
  32511. mdelay(1);
  32512. + sdhci_spin_disable_schedule(host);
  32513. }
  32514. + DBG("send cmd %d - wait 0x%X irq 0x%x\n", cmd->opcode, mask,
  32515. + sdhci_readl(host, SDHCI_INT_STATUS));
  32516. mod_timer(&host->timer, jiffies + 10 * HZ);
  32517. host->cmd = cmd;
  32518. + if (host->last_cmdop == MMC_APP_CMD)
  32519. + host->last_cmdop = -cmd->opcode;
  32520. + else
  32521. + host->last_cmdop = cmd->opcode;
  32522. sdhci_prepare_data(host, cmd);
  32523. @@ -1232,7 +1342,9 @@
  32524. return;
  32525. }
  32526. timeout--;
  32527. + sdhci_spin_enable_schedule(host);
  32528. mdelay(1);
  32529. + sdhci_spin_disable_schedule(host);
  32530. }
  32531. clk |= SDHCI_CLOCK_CARD_EN;
  32532. @@ -1333,7 +1445,7 @@
  32533. sdhci_runtime_pm_get(host);
  32534. - spin_lock_irqsave(&host->lock, flags);
  32535. + sdhci_spin_lock_irqsave(host, &flags);
  32536. WARN_ON(host->mrq != NULL);
  32537. @@ -1391,9 +1503,9 @@
  32538. mmc->card->type == MMC_TYPE_MMC ?
  32539. MMC_SEND_TUNING_BLOCK_HS200 :
  32540. MMC_SEND_TUNING_BLOCK;
  32541. - spin_unlock_irqrestore(&host->lock, flags);
  32542. + sdhci_spin_unlock_irqrestore(host, flags);
  32543. sdhci_execute_tuning(mmc, tuning_opcode);
  32544. - spin_lock_irqsave(&host->lock, flags);
  32545. + sdhci_spin_lock_irqsave(host, &flags);
  32546. /* Restore original mmc_request structure */
  32547. host->mrq = mrq;
  32548. @@ -1407,7 +1519,7 @@
  32549. }
  32550. mmiowb();
  32551. - spin_unlock_irqrestore(&host->lock, flags);
  32552. + sdhci_spin_unlock_irqrestore(host, flags);
  32553. }
  32554. static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
  32555. @@ -1416,10 +1528,10 @@
  32556. int vdd_bit = -1;
  32557. u8 ctrl;
  32558. - spin_lock_irqsave(&host->lock, flags);
  32559. + sdhci_spin_lock_irqsave(host, &flags);
  32560. if (host->flags & SDHCI_DEVICE_DEAD) {
  32561. - spin_unlock_irqrestore(&host->lock, flags);
  32562. + sdhci_spin_unlock_irqrestore(host, flags);
  32563. if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
  32564. mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
  32565. return;
  32566. @@ -1446,9 +1558,9 @@
  32567. vdd_bit = sdhci_set_power(host, ios->vdd);
  32568. if (host->vmmc && vdd_bit != -1) {
  32569. - spin_unlock_irqrestore(&host->lock, flags);
  32570. + sdhci_spin_unlock_irqrestore(host, flags);
  32571. mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
  32572. - spin_lock_irqsave(&host->lock, flags);
  32573. + sdhci_spin_lock_irqsave(host, &flags);
  32574. }
  32575. if (host->ops->platform_send_init_74_clocks)
  32576. @@ -1585,7 +1697,7 @@
  32577. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  32578. mmiowb();
  32579. - spin_unlock_irqrestore(&host->lock, flags);
  32580. + sdhci_spin_unlock_irqrestore(host, flags);
  32581. }
  32582. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  32583. @@ -1633,7 +1745,7 @@
  32584. unsigned long flags;
  32585. int is_readonly;
  32586. - spin_lock_irqsave(&host->lock, flags);
  32587. + sdhci_spin_lock_irqsave(host, &flags);
  32588. if (host->flags & SDHCI_DEVICE_DEAD)
  32589. is_readonly = 0;
  32590. @@ -1643,7 +1755,7 @@
  32591. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  32592. & SDHCI_WRITE_PROTECT);
  32593. - spin_unlock_irqrestore(&host->lock, flags);
  32594. + sdhci_spin_unlock_irqrestore(host, flags);
  32595. /* This quirk needs to be replaced by a callback-function later */
  32596. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  32597. @@ -1716,9 +1828,9 @@
  32598. struct sdhci_host *host = mmc_priv(mmc);
  32599. unsigned long flags;
  32600. - spin_lock_irqsave(&host->lock, flags);
  32601. + sdhci_spin_lock_irqsave(host, &flags);
  32602. sdhci_enable_sdio_irq_nolock(host, enable);
  32603. - spin_unlock_irqrestore(&host->lock, flags);
  32604. + sdhci_spin_unlock_irqrestore(host, flags);
  32605. }
  32606. static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
  32607. @@ -2066,7 +2178,7 @@
  32608. if (host->ops->card_event)
  32609. host->ops->card_event(host);
  32610. - spin_lock_irqsave(&host->lock, flags);
  32611. + sdhci_spin_lock_irqsave(host, &flags);
  32612. /* Check host->mrq first in case we are runtime suspended */
  32613. if (host->mrq && !sdhci_do_get_cd(host)) {
  32614. @@ -2082,7 +2194,7 @@
  32615. tasklet_schedule(&host->finish_tasklet);
  32616. }
  32617. - spin_unlock_irqrestore(&host->lock, flags);
  32618. + sdhci_spin_unlock_irqrestore(host, flags);
  32619. }
  32620. static const struct mmc_host_ops sdhci_ops = {
  32621. @@ -2121,14 +2233,14 @@
  32622. host = (struct sdhci_host*)param;
  32623. - spin_lock_irqsave(&host->lock, flags);
  32624. + sdhci_spin_lock_irqsave(host, &flags);
  32625. /*
  32626. * If this tasklet gets rescheduled while running, it will
  32627. * be run again afterwards but without any active request.
  32628. */
  32629. if (!host->mrq) {
  32630. - spin_unlock_irqrestore(&host->lock, flags);
  32631. + sdhci_spin_unlock_irqrestore(host, flags);
  32632. return;
  32633. }
  32634. @@ -2166,7 +2278,7 @@
  32635. #endif
  32636. mmiowb();
  32637. - spin_unlock_irqrestore(&host->lock, flags);
  32638. + sdhci_spin_unlock_irqrestore(host, flags);
  32639. mmc_request_done(host->mmc, mrq);
  32640. sdhci_runtime_pm_put(host);
  32641. @@ -2179,11 +2291,11 @@
  32642. host = (struct sdhci_host*)data;
  32643. - spin_lock_irqsave(&host->lock, flags);
  32644. + sdhci_spin_lock_irqsave(host, &flags);
  32645. if (host->mrq) {
  32646. pr_err("%s: Timeout waiting for hardware "
  32647. - "interrupt.\n", mmc_hostname(host->mmc));
  32648. + "interrupt - cmd%d.\n", mmc_hostname(host->mmc), host->last_cmdop);
  32649. sdhci_dumpregs(host);
  32650. if (host->data) {
  32651. @@ -2200,7 +2312,7 @@
  32652. }
  32653. mmiowb();
  32654. - spin_unlock_irqrestore(&host->lock, flags);
  32655. + sdhci_spin_unlock_irqrestore(host, flags);
  32656. }
  32657. static void sdhci_tuning_timer(unsigned long data)
  32658. @@ -2210,11 +2322,11 @@
  32659. host = (struct sdhci_host *)data;
  32660. - spin_lock_irqsave(&host->lock, flags);
  32661. + sdhci_spin_lock_irqsave(host, &flags);
  32662. host->flags |= SDHCI_NEEDS_RETUNING;
  32663. - spin_unlock_irqrestore(&host->lock, flags);
  32664. + sdhci_spin_unlock_irqrestore(host, flags);
  32665. }
  32666. /*****************************************************************************\
  32667. @@ -2228,10 +2340,13 @@
  32668. BUG_ON(intmask == 0);
  32669. if (!host->cmd) {
  32670. + if (!(host->ops->extra_ints)) {
  32671. pr_err("%s: Got command interrupt 0x%08x even "
  32672. "though no command operation was in progress.\n",
  32673. mmc_hostname(host->mmc), (unsigned)intmask);
  32674. sdhci_dumpregs(host);
  32675. + } else
  32676. + DBG("cmd irq 0x%08x cmd complete\n", (unsigned)intmask);
  32677. return;
  32678. }
  32679. @@ -2301,6 +2416,19 @@
  32680. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  32681. #endif
  32682. +static void sdhci_data_end(struct sdhci_host *host)
  32683. +{
  32684. + if (host->cmd) {
  32685. + /*
  32686. + * Data managed to finish before the
  32687. + * command completed. Make sure we do
  32688. + * things in the proper order.
  32689. + */
  32690. + host->data_early = 1;
  32691. + } else
  32692. + sdhci_finish_data(host);
  32693. +}
  32694. +
  32695. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  32696. {
  32697. u32 command;
  32698. @@ -2330,23 +2458,39 @@
  32699. }
  32700. }
  32701. + if (!(host->ops->extra_ints)) {
  32702. pr_err("%s: Got data interrupt 0x%08x even "
  32703. "though no data operation was in progress.\n",
  32704. mmc_hostname(host->mmc), (unsigned)intmask);
  32705. sdhci_dumpregs(host);
  32706. + } else
  32707. + DBG("data irq 0x%08x but no data\n", (unsigned)intmask);
  32708. return;
  32709. }
  32710. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  32711. host->data->error = -ETIMEDOUT;
  32712. - else if (intmask & SDHCI_INT_DATA_END_BIT)
  32713. + else if (intmask & SDHCI_INT_DATA_END_BIT) {
  32714. + DBG("end error in cmd %d\n", host->last_cmdop);
  32715. + if (host->ops->spurious_crc_acmd51 &&
  32716. + host->last_cmdop == -SD_APP_SEND_SCR) {
  32717. + DBG("ignoring spurious data_end_bit error\n");
  32718. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  32719. + } else
  32720. host->data->error = -EILSEQ;
  32721. - else if ((intmask & SDHCI_INT_DATA_CRC) &&
  32722. + } else if ((intmask & SDHCI_INT_DATA_CRC) &&
  32723. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  32724. - != MMC_BUS_TEST_R)
  32725. + != MMC_BUS_TEST_R) {
  32726. + DBG("crc error in cmd %d\n", host->last_cmdop);
  32727. + if (host->ops->spurious_crc_acmd51 &&
  32728. + host->last_cmdop == -SD_APP_SEND_SCR) {
  32729. + DBG("ignoring spurious data_crc_bit error\n");
  32730. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  32731. + } else {
  32732. host->data->error = -EILSEQ;
  32733. - else if (intmask & SDHCI_INT_ADMA_ERROR) {
  32734. + }
  32735. + } else if (intmask & SDHCI_INT_ADMA_ERROR) {
  32736. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  32737. sdhci_show_adma_error(host);
  32738. host->data->error = -EIO;
  32739. @@ -2354,11 +2498,18 @@
  32740. host->ops->adma_workaround(host, intmask);
  32741. }
  32742. - if (host->data->error)
  32743. + if (host->data->error) {
  32744. + DBG("finish request early on error %d\n", host->data->error);
  32745. sdhci_finish_data(host);
  32746. - else {
  32747. - if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  32748. - sdhci_transfer_pio(host);
  32749. + } else {
  32750. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
  32751. + if (host->flags & SDHCI_REQ_USE_DMA) {
  32752. + /* possible only in PLATDMA mode */
  32753. + sdhci_platdma_avail(host, &intmask,
  32754. + &sdhci_data_end);
  32755. + } else
  32756. + sdhci_transfer_pio(host, intmask);
  32757. + }
  32758. /*
  32759. * We currently don't do anything fancy with DMA
  32760. @@ -2387,18 +2538,8 @@
  32761. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  32762. }
  32763. - if (intmask & SDHCI_INT_DATA_END) {
  32764. - if (host->cmd) {
  32765. - /*
  32766. - * Data managed to finish before the
  32767. - * command completed. Make sure we do
  32768. - * things in the proper order.
  32769. - */
  32770. - host->data_early = 1;
  32771. - } else {
  32772. - sdhci_finish_data(host);
  32773. - }
  32774. - }
  32775. + if (intmask & SDHCI_INT_DATA_END)
  32776. + sdhci_data_end(host);
  32777. }
  32778. }
  32779. @@ -2409,10 +2550,10 @@
  32780. u32 intmask, unexpected = 0;
  32781. int cardint = 0, max_loops = 16;
  32782. - spin_lock(&host->lock);
  32783. + sdhci_spin_lock(host);
  32784. if (host->runtime_suspended) {
  32785. - spin_unlock(&host->lock);
  32786. + sdhci_spin_unlock(host);
  32787. pr_warning("%s: got irq while runtime suspended\n",
  32788. mmc_hostname(host->mmc));
  32789. return IRQ_HANDLED;
  32790. @@ -2454,6 +2595,22 @@
  32791. tasklet_schedule(&host->card_tasklet);
  32792. }
  32793. + if (intmask & SDHCI_INT_ERROR_MASK & ~SDHCI_INT_ERROR)
  32794. + DBG("controller reports error 0x%x -"
  32795. + "%s%s%s%s%s%s%s%s%s%s",
  32796. + intmask,
  32797. + intmask & SDHCI_INT_TIMEOUT? " timeout": "",
  32798. + intmask & SDHCI_INT_CRC ? " crc": "",
  32799. + intmask & SDHCI_INT_END_BIT? " endbit": "",
  32800. + intmask & SDHCI_INT_INDEX? " index": "",
  32801. + intmask & SDHCI_INT_DATA_TIMEOUT? " data_timeout": "",
  32802. + intmask & SDHCI_INT_DATA_CRC? " data_crc": "",
  32803. + intmask & SDHCI_INT_DATA_END_BIT? " data_endbit": "",
  32804. + intmask & SDHCI_INT_BUS_POWER? " buspower": "",
  32805. + intmask & SDHCI_INT_ACMD12ERR? " acmd12": "",
  32806. + intmask & SDHCI_INT_ADMA_ERROR? " adma": ""
  32807. + );
  32808. +
  32809. if (intmask & SDHCI_INT_CMD_MASK) {
  32810. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  32811. SDHCI_INT_STATUS);
  32812. @@ -2468,7 +2625,13 @@
  32813. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  32814. - intmask &= ~SDHCI_INT_ERROR;
  32815. + if (intmask & SDHCI_INT_ERROR_MASK) {
  32816. + /* collect any uncovered errors */
  32817. + sdhci_writel(host, intmask & SDHCI_INT_ERROR_MASK,
  32818. + SDHCI_INT_STATUS);
  32819. + }
  32820. +
  32821. + intmask &= ~SDHCI_INT_ERROR_MASK;
  32822. if (intmask & SDHCI_INT_BUS_POWER) {
  32823. pr_err("%s: Card is consuming too much power!\n",
  32824. @@ -2494,7 +2657,7 @@
  32825. if (intmask && --max_loops)
  32826. goto again;
  32827. out:
  32828. - spin_unlock(&host->lock);
  32829. + sdhci_spin_unlock(host);
  32830. if (unexpected) {
  32831. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  32832. @@ -2588,13 +2751,14 @@
  32833. {
  32834. int ret;
  32835. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  32836. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32837. + SDHCI_USE_PLATDMA)) {
  32838. if (host->ops->enable_dma)
  32839. host->ops->enable_dma(host);
  32840. }
  32841. if (!device_may_wakeup(mmc_dev(host->mmc))) {
  32842. - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  32843. + ret = request_irq(host->irq, sdhci_irq, 0 /*IRQF_SHARED*/,
  32844. mmc_hostname(host->mmc), host);
  32845. if (ret)
  32846. return ret;
  32847. @@ -2671,15 +2835,15 @@
  32848. host->flags &= ~SDHCI_NEEDS_RETUNING;
  32849. }
  32850. - spin_lock_irqsave(&host->lock, flags);
  32851. + sdhci_spin_lock_irqsave(host, &flags);
  32852. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  32853. - spin_unlock_irqrestore(&host->lock, flags);
  32854. + sdhci_spin_unlock_irqrestore(host, flags);
  32855. synchronize_irq(host->irq);
  32856. - spin_lock_irqsave(&host->lock, flags);
  32857. + sdhci_spin_lock_irqsave(host, &flags);
  32858. host->runtime_suspended = true;
  32859. - spin_unlock_irqrestore(&host->lock, flags);
  32860. + sdhci_spin_unlock_irqrestore(host, flags);
  32861. return ret;
  32862. }
  32863. @@ -2705,16 +2869,16 @@
  32864. sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
  32865. if ((host_flags & SDHCI_PV_ENABLED) &&
  32866. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  32867. - spin_lock_irqsave(&host->lock, flags);
  32868. + sdhci_spin_lock_irqsave(host, &flags);
  32869. sdhci_enable_preset_value(host, true);
  32870. - spin_unlock_irqrestore(&host->lock, flags);
  32871. + sdhci_spin_unlock_irqrestore(host, flags);
  32872. }
  32873. /* Set the re-tuning expiration flag */
  32874. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  32875. host->flags |= SDHCI_NEEDS_RETUNING;
  32876. - spin_lock_irqsave(&host->lock, flags);
  32877. + sdhci_spin_lock_irqsave(host, &flags);
  32878. host->runtime_suspended = false;
  32879. @@ -2725,7 +2889,7 @@
  32880. /* Enable Card Detection */
  32881. sdhci_enable_card_detection(host);
  32882. - spin_unlock_irqrestore(&host->lock, flags);
  32883. + sdhci_spin_unlock_irqrestore(host, flags);
  32884. return ret;
  32885. }
  32886. @@ -2820,14 +2984,16 @@
  32887. host->flags &= ~SDHCI_USE_ADMA;
  32888. }
  32889. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  32890. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32891. + SDHCI_USE_PLATDMA)) {
  32892. if (host->ops->enable_dma) {
  32893. if (host->ops->enable_dma(host)) {
  32894. pr_warning("%s: No suitable DMA "
  32895. "available. Falling back to PIO.\n",
  32896. mmc_hostname(mmc));
  32897. host->flags &=
  32898. - ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  32899. + ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32900. + SDHCI_USE_PLATDMA);
  32901. }
  32902. }
  32903. }
  32904. @@ -3218,8 +3384,8 @@
  32905. sdhci_init(host, 0);
  32906. - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  32907. - mmc_hostname(mmc), host);
  32908. + ret = request_irq(host->irq, sdhci_irq, 0 /*IRQF_SHARED*/,
  32909. + mmc_hostname(mmc), host);
  32910. if (ret) {
  32911. pr_err("%s: Failed to request IRQ %d: %d\n",
  32912. mmc_hostname(mmc), host->irq, ret);
  32913. @@ -3252,6 +3418,7 @@
  32914. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  32915. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  32916. + (host->flags & SDHCI_USE_PLATDMA) ? "platform's DMA" :
  32917. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  32918. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  32919. @@ -3279,7 +3446,7 @@
  32920. unsigned long flags;
  32921. if (dead) {
  32922. - spin_lock_irqsave(&host->lock, flags);
  32923. + sdhci_spin_lock_irqsave(host, &flags);
  32924. host->flags |= SDHCI_DEVICE_DEAD;
  32925. @@ -3291,7 +3458,7 @@
  32926. tasklet_schedule(&host->finish_tasklet);
  32927. }
  32928. - spin_unlock_irqrestore(&host->lock, flags);
  32929. + sdhci_spin_unlock_irqrestore(host, flags);
  32930. }
  32931. sdhci_disable_card_detection(host);
  32932. diff -Nur linux-3.12.13/drivers/mmc/host/sdhci.h linux-raspberry-pi/drivers/mmc/host/sdhci.h
  32933. --- linux-3.12.13/drivers/mmc/host/sdhci.h 2014-02-22 22:32:50.000000000 +0100
  32934. +++ linux-raspberry-pi/drivers/mmc/host/sdhci.h 2014-03-11 17:51:21.000000000 +0100
  32935. @@ -289,6 +289,18 @@
  32936. void (*platform_reset_enter)(struct sdhci_host *host, u8 mask);
  32937. void (*platform_reset_exit)(struct sdhci_host *host, u8 mask);
  32938. int (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);
  32939. +
  32940. + int (*pdma_able)(struct sdhci_host *host,
  32941. + struct mmc_data *data);
  32942. + void (*pdma_avail)(struct sdhci_host *host,
  32943. + unsigned int *ref_intmask,
  32944. + void(*complete)(struct sdhci_host *));
  32945. + void (*pdma_reset)(struct sdhci_host *host,
  32946. + struct mmc_data *data);
  32947. + unsigned int (*extra_ints)(struct sdhci_host *host);
  32948. + unsigned int (*spurious_crc_acmd51)(struct sdhci_host *host);
  32949. + unsigned int (*missing_status)(struct sdhci_host *host);
  32950. +
  32951. void (*hw_reset)(struct sdhci_host *host);
  32952. void (*platform_suspend)(struct sdhci_host *host);
  32953. void (*platform_resume)(struct sdhci_host *host);
  32954. @@ -400,9 +412,38 @@
  32955. extern void sdhci_enable_irq_wakeups(struct sdhci_host *host);
  32956. #endif
  32957. +static inline int /*bool*/
  32958. +sdhci_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  32959. +{
  32960. + if (host->ops->pdma_able)
  32961. + return host->ops->pdma_able(host, data);
  32962. + else
  32963. + return 1;
  32964. +}
  32965. +static inline void
  32966. +sdhci_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  32967. + void(*completion_callback)(struct sdhci_host *))
  32968. +{
  32969. + if (host->ops->pdma_avail)
  32970. + host->ops->pdma_avail(host, ref_intmask, completion_callback);
  32971. +}
  32972. +
  32973. +static inline void
  32974. +sdhci_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  32975. +{
  32976. + if (host->ops->pdma_reset)
  32977. + host->ops->pdma_reset(host, data);
  32978. +}
  32979. +
  32980. #ifdef CONFIG_PM_RUNTIME
  32981. extern int sdhci_runtime_suspend_host(struct sdhci_host *host);
  32982. extern int sdhci_runtime_resume_host(struct sdhci_host *host);
  32983. #endif
  32984. +extern void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags);
  32985. +extern void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags);
  32986. +extern void sdhci_spin_lock(struct sdhci_host *host);
  32987. +extern void sdhci_spin_unlock(struct sdhci_host *host);
  32988. +
  32989. +
  32990. #endif /* __SDHCI_HW_H */
  32991. diff -Nur linux-3.12.13/drivers/net/usb/smsc95xx.c linux-raspberry-pi/drivers/net/usb/smsc95xx.c
  32992. --- linux-3.12.13/drivers/net/usb/smsc95xx.c 2014-02-22 22:32:50.000000000 +0100
  32993. +++ linux-raspberry-pi/drivers/net/usb/smsc95xx.c 2014-03-11 17:51:24.000000000 +0100
  32994. @@ -61,6 +61,7 @@
  32995. #define SUSPEND_SUSPEND3 (0x08)
  32996. #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
  32997. SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
  32998. +#define MAC_ADDR_LEN (6)
  32999. struct smsc95xx_priv {
  33000. u32 mac_cr;
  33001. @@ -76,6 +77,10 @@
  33002. module_param(turbo_mode, bool, 0644);
  33003. MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
  33004. +static char *macaddr = ":";
  33005. +module_param(macaddr, charp, 0);
  33006. +MODULE_PARM_DESC(macaddr, "MAC address");
  33007. +
  33008. static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
  33009. u32 *data, int in_pm)
  33010. {
  33011. @@ -765,8 +770,59 @@
  33012. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  33013. }
  33014. +/* Check the macaddr module parameter for a MAC address */
  33015. +static int smsc95xx_is_macaddr_param(struct usbnet *dev, u8 *dev_mac)
  33016. +{
  33017. + int i, j, got_num, num;
  33018. + u8 mtbl[MAC_ADDR_LEN];
  33019. +
  33020. + if (macaddr[0] == ':')
  33021. + return 0;
  33022. +
  33023. + i = 0;
  33024. + j = 0;
  33025. + num = 0;
  33026. + got_num = 0;
  33027. + while (j < MAC_ADDR_LEN) {
  33028. + if (macaddr[i] && macaddr[i] != ':') {
  33029. + got_num++;
  33030. + if ('0' <= macaddr[i] && macaddr[i] <= '9')
  33031. + num = num * 16 + macaddr[i] - '0';
  33032. + else if ('A' <= macaddr[i] && macaddr[i] <= 'F')
  33033. + num = num * 16 + 10 + macaddr[i] - 'A';
  33034. + else if ('a' <= macaddr[i] && macaddr[i] <= 'f')
  33035. + num = num * 16 + 10 + macaddr[i] - 'a';
  33036. + else
  33037. + break;
  33038. + i++;
  33039. + } else if (got_num == 2) {
  33040. + mtbl[j++] = (u8) num;
  33041. + num = 0;
  33042. + got_num = 0;
  33043. + i++;
  33044. + } else {
  33045. + break;
  33046. + }
  33047. + }
  33048. +
  33049. + if (j == MAC_ADDR_LEN) {
  33050. + netif_dbg(dev, ifup, dev->net, "Overriding MAC address with: "
  33051. + "%02x:%02x:%02x:%02x:%02x:%02x\n", mtbl[0], mtbl[1], mtbl[2],
  33052. + mtbl[3], mtbl[4], mtbl[5]);
  33053. + for (i = 0; i < MAC_ADDR_LEN; i++)
  33054. + dev_mac[i] = mtbl[i];
  33055. + return 1;
  33056. + } else {
  33057. + return 0;
  33058. + }
  33059. +}
  33060. +
  33061. static void smsc95xx_init_mac_address(struct usbnet *dev)
  33062. {
  33063. + /* Check module parameters */
  33064. + if (smsc95xx_is_macaddr_param(dev, dev->net->dev_addr))
  33065. + return;
  33066. +
  33067. /* try reading mac address from EEPROM */
  33068. if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  33069. dev->net->dev_addr) == 0) {
  33070. diff -Nur linux-3.12.13/drivers/spi/Kconfig linux-raspberry-pi/drivers/spi/Kconfig
  33071. --- linux-3.12.13/drivers/spi/Kconfig 2014-02-22 22:32:50.000000000 +0100
  33072. +++ linux-raspberry-pi/drivers/spi/Kconfig 2014-03-11 17:51:25.000000000 +0100
  33073. @@ -85,6 +85,14 @@
  33074. is for the regular SPI controller. Slave mode operation is not also
  33075. not supported.
  33076. +config SPI_BCM2708
  33077. + tristate "BCM2708 SPI controller driver (SPI0)"
  33078. + depends on MACH_BCM2708
  33079. + help
  33080. + This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
  33081. + driver is not compatible with the "Universal SPI Master" or the SPI slave
  33082. + device.
  33083. +
  33084. config SPI_BFIN5XX
  33085. tristate "SPI controller driver for ADI Blackfin5xx"
  33086. depends on BLACKFIN && !BF60x
  33087. diff -Nur linux-3.12.13/drivers/spi/Makefile linux-raspberry-pi/drivers/spi/Makefile
  33088. --- linux-3.12.13/drivers/spi/Makefile 2014-02-22 22:32:50.000000000 +0100
  33089. +++ linux-raspberry-pi/drivers/spi/Makefile 2014-03-11 17:51:25.000000000 +0100
  33090. @@ -18,6 +18,7 @@
  33091. obj-$(CONFIG_SPI_BCM63XX) += spi-bcm63xx.o
  33092. obj-$(CONFIG_SPI_BFIN5XX) += spi-bfin5xx.o
  33093. obj-$(CONFIG_SPI_BFIN_V3) += spi-bfin-v3.o
  33094. +obj-$(CONFIG_SPI_BCM2708) += spi-bcm2708.o
  33095. obj-$(CONFIG_SPI_BFIN_SPORT) += spi-bfin-sport.o
  33096. obj-$(CONFIG_SPI_BITBANG) += spi-bitbang.o
  33097. obj-$(CONFIG_SPI_BUTTERFLY) += spi-butterfly.o
  33098. diff -Nur linux-3.12.13/drivers/spi/spi-bcm2708.c linux-raspberry-pi/drivers/spi/spi-bcm2708.c
  33099. --- linux-3.12.13/drivers/spi/spi-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  33100. +++ linux-raspberry-pi/drivers/spi/spi-bcm2708.c 2014-03-11 17:33:04.000000000 +0100
  33101. @@ -0,0 +1,626 @@
  33102. +/*
  33103. + * Driver for Broadcom BCM2708 SPI Controllers
  33104. + *
  33105. + * Copyright (C) 2012 Chris Boot
  33106. + *
  33107. + * This driver is inspired by:
  33108. + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  33109. + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  33110. + *
  33111. + * This program is free software; you can redistribute it and/or modify
  33112. + * it under the terms of the GNU General Public License as published by
  33113. + * the Free Software Foundation; either version 2 of the License, or
  33114. + * (at your option) any later version.
  33115. + *
  33116. + * This program is distributed in the hope that it will be useful,
  33117. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33118. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33119. + * GNU General Public License for more details.
  33120. + *
  33121. + * You should have received a copy of the GNU General Public License
  33122. + * along with this program; if not, write to the Free Software
  33123. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  33124. + */
  33125. +
  33126. +#include <linux/kernel.h>
  33127. +#include <linux/module.h>
  33128. +#include <linux/spinlock.h>
  33129. +#include <linux/clk.h>
  33130. +#include <linux/err.h>
  33131. +#include <linux/platform_device.h>
  33132. +#include <linux/io.h>
  33133. +#include <linux/spi/spi.h>
  33134. +#include <linux/interrupt.h>
  33135. +#include <linux/delay.h>
  33136. +#include <linux/log2.h>
  33137. +#include <linux/sched.h>
  33138. +#include <linux/wait.h>
  33139. +
  33140. +/* SPI register offsets */
  33141. +#define SPI_CS 0x00
  33142. +#define SPI_FIFO 0x04
  33143. +#define SPI_CLK 0x08
  33144. +#define SPI_DLEN 0x0c
  33145. +#define SPI_LTOH 0x10
  33146. +#define SPI_DC 0x14
  33147. +
  33148. +/* Bitfields in CS */
  33149. +#define SPI_CS_LEN_LONG 0x02000000
  33150. +#define SPI_CS_DMA_LEN 0x01000000
  33151. +#define SPI_CS_CSPOL2 0x00800000
  33152. +#define SPI_CS_CSPOL1 0x00400000
  33153. +#define SPI_CS_CSPOL0 0x00200000
  33154. +#define SPI_CS_RXF 0x00100000
  33155. +#define SPI_CS_RXR 0x00080000
  33156. +#define SPI_CS_TXD 0x00040000
  33157. +#define SPI_CS_RXD 0x00020000
  33158. +#define SPI_CS_DONE 0x00010000
  33159. +#define SPI_CS_LEN 0x00002000
  33160. +#define SPI_CS_REN 0x00001000
  33161. +#define SPI_CS_ADCS 0x00000800
  33162. +#define SPI_CS_INTR 0x00000400
  33163. +#define SPI_CS_INTD 0x00000200
  33164. +#define SPI_CS_DMAEN 0x00000100
  33165. +#define SPI_CS_TA 0x00000080
  33166. +#define SPI_CS_CSPOL 0x00000040
  33167. +#define SPI_CS_CLEAR_RX 0x00000020
  33168. +#define SPI_CS_CLEAR_TX 0x00000010
  33169. +#define SPI_CS_CPOL 0x00000008
  33170. +#define SPI_CS_CPHA 0x00000004
  33171. +#define SPI_CS_CS_10 0x00000002
  33172. +#define SPI_CS_CS_01 0x00000001
  33173. +
  33174. +#define SPI_TIMEOUT_MS 150
  33175. +
  33176. +#define DRV_NAME "bcm2708_spi"
  33177. +
  33178. +struct bcm2708_spi {
  33179. + spinlock_t lock;
  33180. + void __iomem *base;
  33181. + int irq;
  33182. + struct clk *clk;
  33183. + bool stopping;
  33184. +
  33185. + struct list_head queue;
  33186. + struct workqueue_struct *workq;
  33187. + struct work_struct work;
  33188. + struct completion done;
  33189. +
  33190. + const u8 *tx_buf;
  33191. + u8 *rx_buf;
  33192. + int len;
  33193. +};
  33194. +
  33195. +struct bcm2708_spi_state {
  33196. + u32 cs;
  33197. + u16 cdiv;
  33198. +};
  33199. +
  33200. +/*
  33201. + * This function sets the ALT mode on the SPI pins so that we can use them with
  33202. + * the SPI hardware.
  33203. + *
  33204. + * FIXME: This is a hack. Use pinmux / pinctrl.
  33205. + */
  33206. +static void bcm2708_init_pinmode(void)
  33207. +{
  33208. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  33209. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  33210. +
  33211. + int pin;
  33212. + u32 *gpio = ioremap(0x20200000, SZ_16K);
  33213. +
  33214. + /* SPI is on GPIO 7..11 */
  33215. + for (pin = 7; pin <= 11; pin++) {
  33216. + INP_GPIO(pin); /* set mode to GPIO input first */
  33217. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  33218. + }
  33219. +
  33220. + iounmap(gpio);
  33221. +
  33222. +#undef INP_GPIO
  33223. +#undef SET_GPIO_ALT
  33224. +}
  33225. +
  33226. +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
  33227. +{
  33228. + return readl(bs->base + reg);
  33229. +}
  33230. +
  33231. +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
  33232. +{
  33233. + writel(val, bs->base + reg);
  33234. +}
  33235. +
  33236. +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
  33237. +{
  33238. + u8 byte;
  33239. +
  33240. + while (len--) {
  33241. + byte = bcm2708_rd(bs, SPI_FIFO);
  33242. + if (bs->rx_buf)
  33243. + *bs->rx_buf++ = byte;
  33244. + }
  33245. +}
  33246. +
  33247. +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
  33248. +{
  33249. + u8 byte;
  33250. + u16 val;
  33251. +
  33252. + if (len > bs->len)
  33253. + len = bs->len;
  33254. +
  33255. + if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
  33256. + /* LoSSI mode */
  33257. + if (unlikely(len % 2)) {
  33258. + printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
  33259. + bs->len = 0;
  33260. + return;
  33261. + }
  33262. + while (len) {
  33263. + if (bs->tx_buf) {
  33264. + val = *(const u16 *)bs->tx_buf;
  33265. + bs->tx_buf += 2;
  33266. + } else
  33267. + val = 0;
  33268. + bcm2708_wr(bs, SPI_FIFO, val);
  33269. + bs->len -= 2;
  33270. + len -= 2;
  33271. + }
  33272. + return;
  33273. + }
  33274. +
  33275. + while (len--) {
  33276. + byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  33277. + bcm2708_wr(bs, SPI_FIFO, byte);
  33278. + bs->len--;
  33279. + }
  33280. +}
  33281. +
  33282. +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
  33283. +{
  33284. + struct spi_master *master = dev_id;
  33285. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  33286. + u32 cs;
  33287. +
  33288. + spin_lock(&bs->lock);
  33289. +
  33290. + cs = bcm2708_rd(bs, SPI_CS);
  33291. +
  33292. + if (cs & SPI_CS_DONE) {
  33293. + if (bs->len) { /* first interrupt in a transfer */
  33294. + /* fill the TX fifo with up to 16 bytes */
  33295. + bcm2708_wr_fifo(bs, 16);
  33296. + } else { /* transfer complete */
  33297. + /* disable interrupts */
  33298. + cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
  33299. + bcm2708_wr(bs, SPI_CS, cs);
  33300. +
  33301. + /* drain RX FIFO */
  33302. + while (cs & SPI_CS_RXD) {
  33303. + bcm2708_rd_fifo(bs, 1);
  33304. + cs = bcm2708_rd(bs, SPI_CS);
  33305. + }
  33306. +
  33307. + /* wake up our bh */
  33308. + complete(&bs->done);
  33309. + }
  33310. + } else if (cs & SPI_CS_RXR) {
  33311. + /* read 12 bytes of data */
  33312. + bcm2708_rd_fifo(bs, 12);
  33313. +
  33314. + /* write up to 12 bytes */
  33315. + bcm2708_wr_fifo(bs, 12);
  33316. + }
  33317. +
  33318. + spin_unlock(&bs->lock);
  33319. +
  33320. + return IRQ_HANDLED;
  33321. +}
  33322. +
  33323. +static int bcm2708_setup_state(struct spi_master *master,
  33324. + struct device *dev, struct bcm2708_spi_state *state,
  33325. + u32 hz, u8 csel, u8 mode, u8 bpw)
  33326. +{
  33327. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  33328. + int cdiv;
  33329. + unsigned long bus_hz;
  33330. + u32 cs = 0;
  33331. +
  33332. + bus_hz = clk_get_rate(bs->clk);
  33333. +
  33334. + if (hz >= bus_hz) {
  33335. + cdiv = 2; /* bus_hz / 2 is as fast as we can go */
  33336. + } else if (hz) {
  33337. + cdiv = DIV_ROUND_UP(bus_hz, hz);
  33338. +
  33339. + /* CDIV must be a power of 2, so round up */
  33340. + cdiv = roundup_pow_of_two(cdiv);
  33341. +
  33342. + if (cdiv > 65536) {
  33343. + dev_dbg(dev,
  33344. + "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
  33345. + hz, cdiv, bus_hz / 65536);
  33346. + return -EINVAL;
  33347. + } else if (cdiv == 65536) {
  33348. + cdiv = 0;
  33349. + } else if (cdiv == 1) {
  33350. + cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
  33351. + }
  33352. + } else {
  33353. + cdiv = 0;
  33354. + }
  33355. +
  33356. + switch (bpw) {
  33357. + case 8:
  33358. + break;
  33359. + case 9:
  33360. + /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
  33361. + cs |= SPI_CS_LEN;
  33362. + break;
  33363. + default:
  33364. + dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
  33365. + bpw);
  33366. + return -EINVAL;
  33367. + }
  33368. +
  33369. + if (mode & SPI_CPOL)
  33370. + cs |= SPI_CS_CPOL;
  33371. + if (mode & SPI_CPHA)
  33372. + cs |= SPI_CS_CPHA;
  33373. +
  33374. + if (!(mode & SPI_NO_CS)) {
  33375. + if (mode & SPI_CS_HIGH) {
  33376. + cs |= SPI_CS_CSPOL;
  33377. + cs |= SPI_CS_CSPOL0 << csel;
  33378. + }
  33379. +
  33380. + cs |= csel;
  33381. + } else {
  33382. + cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
  33383. + }
  33384. +
  33385. + if (state) {
  33386. + state->cs = cs;
  33387. + state->cdiv = cdiv;
  33388. + dev_dbg(dev, "setup: want %d Hz; "
  33389. + "bus_hz=%lu / cdiv=%u == %lu Hz; "
  33390. + "mode %u: cs 0x%08X\n",
  33391. + hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
  33392. + }
  33393. +
  33394. + return 0;
  33395. +}
  33396. +
  33397. +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
  33398. + struct spi_message *msg, struct spi_transfer *xfer)
  33399. +{
  33400. + struct spi_device *spi = msg->spi;
  33401. + struct bcm2708_spi_state state, *stp;
  33402. + int ret;
  33403. + u32 cs;
  33404. +
  33405. + if (bs->stopping)
  33406. + return -ESHUTDOWN;
  33407. +
  33408. + if (xfer->bits_per_word || xfer->speed_hz) {
  33409. + ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
  33410. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  33411. + spi->chip_select, spi->mode,
  33412. + xfer->bits_per_word ? xfer->bits_per_word :
  33413. + spi->bits_per_word);
  33414. + if (ret)
  33415. + return ret;
  33416. +
  33417. + stp = &state;
  33418. + } else {
  33419. + stp = spi->controller_state;
  33420. + }
  33421. +
  33422. + INIT_COMPLETION(bs->done);
  33423. + bs->tx_buf = xfer->tx_buf;
  33424. + bs->rx_buf = xfer->rx_buf;
  33425. + bs->len = xfer->len;
  33426. +
  33427. + cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
  33428. +
  33429. + bcm2708_wr(bs, SPI_CLK, stp->cdiv);
  33430. + bcm2708_wr(bs, SPI_CS, cs);
  33431. +
  33432. + ret = wait_for_completion_timeout(&bs->done,
  33433. + msecs_to_jiffies(SPI_TIMEOUT_MS));
  33434. + if (ret == 0) {
  33435. + dev_err(&spi->dev, "transfer timed out\n");
  33436. + return -ETIMEDOUT;
  33437. + }
  33438. +
  33439. + if (xfer->delay_usecs)
  33440. + udelay(xfer->delay_usecs);
  33441. +
  33442. + if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
  33443. + xfer->cs_change) {
  33444. + /* clear TA and interrupt flags */
  33445. + bcm2708_wr(bs, SPI_CS, stp->cs);
  33446. + }
  33447. +
  33448. + msg->actual_length += (xfer->len - bs->len);
  33449. +
  33450. + return 0;
  33451. +}
  33452. +
  33453. +static void bcm2708_work(struct work_struct *work)
  33454. +{
  33455. + struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
  33456. + unsigned long flags;
  33457. + struct spi_message *msg;
  33458. + struct spi_transfer *xfer;
  33459. + int status = 0;
  33460. +
  33461. + spin_lock_irqsave(&bs->lock, flags);
  33462. + while (!list_empty(&bs->queue)) {
  33463. + msg = list_first_entry(&bs->queue, struct spi_message, queue);
  33464. + list_del_init(&msg->queue);
  33465. + spin_unlock_irqrestore(&bs->lock, flags);
  33466. +
  33467. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  33468. + status = bcm2708_process_transfer(bs, msg, xfer);
  33469. + if (status)
  33470. + break;
  33471. + }
  33472. +
  33473. + msg->status = status;
  33474. + msg->complete(msg->context);
  33475. +
  33476. + spin_lock_irqsave(&bs->lock, flags);
  33477. + }
  33478. + spin_unlock_irqrestore(&bs->lock, flags);
  33479. +}
  33480. +
  33481. +static int bcm2708_spi_setup(struct spi_device *spi)
  33482. +{
  33483. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  33484. + struct bcm2708_spi_state *state;
  33485. + int ret;
  33486. +
  33487. + if (bs->stopping)
  33488. + return -ESHUTDOWN;
  33489. +
  33490. + if (!(spi->mode & SPI_NO_CS) &&
  33491. + (spi->chip_select > spi->master->num_chipselect)) {
  33492. + dev_dbg(&spi->dev,
  33493. + "setup: invalid chipselect %u (%u defined)\n",
  33494. + spi->chip_select, spi->master->num_chipselect);
  33495. + return -EINVAL;
  33496. + }
  33497. +
  33498. + state = spi->controller_state;
  33499. + if (!state) {
  33500. + state = kzalloc(sizeof(*state), GFP_KERNEL);
  33501. + if (!state)
  33502. + return -ENOMEM;
  33503. +
  33504. + spi->controller_state = state;
  33505. + }
  33506. +
  33507. + ret = bcm2708_setup_state(spi->master, &spi->dev, state,
  33508. + spi->max_speed_hz, spi->chip_select, spi->mode,
  33509. + spi->bits_per_word);
  33510. + if (ret < 0) {
  33511. + kfree(state);
  33512. + spi->controller_state = NULL;
  33513. + return ret;
  33514. + }
  33515. +
  33516. + dev_dbg(&spi->dev,
  33517. + "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
  33518. + spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
  33519. + spi->mode, state->cs, state->cdiv);
  33520. +
  33521. + return 0;
  33522. +}
  33523. +
  33524. +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  33525. +{
  33526. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  33527. + struct spi_transfer *xfer;
  33528. + int ret;
  33529. + unsigned long flags;
  33530. +
  33531. + if (unlikely(list_empty(&msg->transfers)))
  33532. + return -EINVAL;
  33533. +
  33534. + if (bs->stopping)
  33535. + return -ESHUTDOWN;
  33536. +
  33537. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  33538. + if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  33539. + dev_dbg(&spi->dev, "missing rx or tx buf\n");
  33540. + return -EINVAL;
  33541. + }
  33542. +
  33543. + if (!xfer->bits_per_word || xfer->speed_hz)
  33544. + continue;
  33545. +
  33546. + ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
  33547. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  33548. + spi->chip_select, spi->mode,
  33549. + xfer->bits_per_word ? xfer->bits_per_word :
  33550. + spi->bits_per_word);
  33551. + if (ret)
  33552. + return ret;
  33553. + }
  33554. +
  33555. + msg->status = -EINPROGRESS;
  33556. + msg->actual_length = 0;
  33557. +
  33558. + spin_lock_irqsave(&bs->lock, flags);
  33559. + list_add_tail(&msg->queue, &bs->queue);
  33560. + queue_work(bs->workq, &bs->work);
  33561. + spin_unlock_irqrestore(&bs->lock, flags);
  33562. +
  33563. + return 0;
  33564. +}
  33565. +
  33566. +static void bcm2708_spi_cleanup(struct spi_device *spi)
  33567. +{
  33568. + if (spi->controller_state) {
  33569. + kfree(spi->controller_state);
  33570. + spi->controller_state = NULL;
  33571. + }
  33572. +}
  33573. +
  33574. +static int bcm2708_spi_probe(struct platform_device *pdev)
  33575. +{
  33576. + struct resource *regs;
  33577. + int irq, err = -ENOMEM;
  33578. + struct clk *clk;
  33579. + struct spi_master *master;
  33580. + struct bcm2708_spi *bs;
  33581. +
  33582. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  33583. + if (!regs) {
  33584. + dev_err(&pdev->dev, "could not get IO memory\n");
  33585. + return -ENXIO;
  33586. + }
  33587. +
  33588. + irq = platform_get_irq(pdev, 0);
  33589. + if (irq < 0) {
  33590. + dev_err(&pdev->dev, "could not get IRQ\n");
  33591. + return irq;
  33592. + }
  33593. +
  33594. + clk = clk_get(&pdev->dev, NULL);
  33595. + if (IS_ERR(clk)) {
  33596. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  33597. + return PTR_ERR(clk);
  33598. + }
  33599. +
  33600. + bcm2708_init_pinmode();
  33601. +
  33602. + master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  33603. + if (!master) {
  33604. + dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  33605. + goto out_clk_put;
  33606. + }
  33607. +
  33608. + /* the spi->mode bits understood by this driver: */
  33609. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
  33610. +
  33611. + master->bus_num = pdev->id;
  33612. + master->num_chipselect = 3;
  33613. + master->setup = bcm2708_spi_setup;
  33614. + master->transfer = bcm2708_spi_transfer;
  33615. + master->cleanup = bcm2708_spi_cleanup;
  33616. + platform_set_drvdata(pdev, master);
  33617. +
  33618. + bs = spi_master_get_devdata(master);
  33619. +
  33620. + spin_lock_init(&bs->lock);
  33621. + INIT_LIST_HEAD(&bs->queue);
  33622. + init_completion(&bs->done);
  33623. + INIT_WORK(&bs->work, bcm2708_work);
  33624. +
  33625. + bs->base = ioremap(regs->start, resource_size(regs));
  33626. + if (!bs->base) {
  33627. + dev_err(&pdev->dev, "could not remap memory\n");
  33628. + goto out_master_put;
  33629. + }
  33630. +
  33631. + bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
  33632. + if (!bs->workq) {
  33633. + dev_err(&pdev->dev, "could not create workqueue\n");
  33634. + goto out_iounmap;
  33635. + }
  33636. +
  33637. + bs->irq = irq;
  33638. + bs->clk = clk;
  33639. + bs->stopping = false;
  33640. +
  33641. + err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
  33642. + master);
  33643. + if (err) {
  33644. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  33645. + goto out_workqueue;
  33646. + }
  33647. +
  33648. + /* initialise the hardware */
  33649. + clk_enable(clk);
  33650. + bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  33651. +
  33652. + err = spi_register_master(master);
  33653. + if (err) {
  33654. + dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  33655. + goto out_free_irq;
  33656. + }
  33657. +
  33658. + dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
  33659. + (unsigned long)regs->start, irq);
  33660. +
  33661. + return 0;
  33662. +
  33663. +out_free_irq:
  33664. + free_irq(bs->irq, master);
  33665. +out_workqueue:
  33666. + destroy_workqueue(bs->workq);
  33667. +out_iounmap:
  33668. + iounmap(bs->base);
  33669. +out_master_put:
  33670. + spi_master_put(master);
  33671. +out_clk_put:
  33672. + clk_put(clk);
  33673. + return err;
  33674. +}
  33675. +
  33676. +static int bcm2708_spi_remove(struct platform_device *pdev)
  33677. +{
  33678. + struct spi_master *master = platform_get_drvdata(pdev);
  33679. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  33680. +
  33681. + /* reset the hardware and block queue progress */
  33682. + spin_lock_irq(&bs->lock);
  33683. + bs->stopping = true;
  33684. + bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  33685. + spin_unlock_irq(&bs->lock);
  33686. +
  33687. + flush_work_sync(&bs->work);
  33688. +
  33689. + clk_disable(bs->clk);
  33690. + clk_put(bs->clk);
  33691. + free_irq(bs->irq, master);
  33692. + iounmap(bs->base);
  33693. +
  33694. + spi_unregister_master(master);
  33695. +
  33696. + return 0;
  33697. +}
  33698. +
  33699. +static struct platform_driver bcm2708_spi_driver = {
  33700. + .driver = {
  33701. + .name = DRV_NAME,
  33702. + .owner = THIS_MODULE,
  33703. + },
  33704. + .probe = bcm2708_spi_probe,
  33705. + .remove = bcm2708_spi_remove,
  33706. +};
  33707. +
  33708. +
  33709. +static int __init bcm2708_spi_init(void)
  33710. +{
  33711. + return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
  33712. +}
  33713. +module_init(bcm2708_spi_init);
  33714. +
  33715. +static void __exit bcm2708_spi_exit(void)
  33716. +{
  33717. + platform_driver_unregister(&bcm2708_spi_driver);
  33718. +}
  33719. +module_exit(bcm2708_spi_exit);
  33720. +
  33721. +
  33722. +//module_platform_driver(bcm2708_spi_driver);
  33723. +
  33724. +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
  33725. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  33726. +MODULE_LICENSE("GPL v2");
  33727. +MODULE_ALIAS("platform:" DRV_NAME);
  33728. diff -Nur linux-3.12.13/drivers/staging/media/lirc/Kconfig linux-raspberry-pi/drivers/staging/media/lirc/Kconfig
  33729. --- linux-3.12.13/drivers/staging/media/lirc/Kconfig 2014-02-22 22:32:50.000000000 +0100
  33730. +++ linux-raspberry-pi/drivers/staging/media/lirc/Kconfig 2014-03-11 17:33:05.000000000 +0100
  33731. @@ -38,6 +38,12 @@
  33732. help
  33733. Driver for Homebrew Parallel Port Receivers
  33734. +config LIRC_RPI
  33735. + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
  33736. + depends on LIRC
  33737. + help
  33738. + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
  33739. +
  33740. config LIRC_SASEM
  33741. tristate "Sasem USB IR Remote"
  33742. depends on LIRC && USB
  33743. diff -Nur linux-3.12.13/drivers/staging/media/lirc/lirc_rpi.c linux-raspberry-pi/drivers/staging/media/lirc/lirc_rpi.c
  33744. --- linux-3.12.13/drivers/staging/media/lirc/lirc_rpi.c 1970-01-01 01:00:00.000000000 +0100
  33745. +++ linux-raspberry-pi/drivers/staging/media/lirc/lirc_rpi.c 2014-03-11 17:33:05.000000000 +0100
  33746. @@ -0,0 +1,693 @@
  33747. +/*
  33748. + * lirc_rpi.c
  33749. + *
  33750. + * lirc_rpi - Device driver that records pulse- and pause-lengths
  33751. + * (space-lengths) (just like the lirc_serial driver does)
  33752. + * between GPIO interrupt events on the Raspberry Pi.
  33753. + * Lots of code has been taken from the lirc_serial module,
  33754. + * so I would like say thanks to the authors.
  33755. + *
  33756. + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
  33757. + * Michael Bishop <cleverca22@gmail.com>
  33758. + * This program is free software; you can redistribute it and/or modify
  33759. + * it under the terms of the GNU General Public License as published by
  33760. + * the Free Software Foundation; either version 2 of the License, or
  33761. + * (at your option) any later version.
  33762. + *
  33763. + * This program is distributed in the hope that it will be useful,
  33764. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33765. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33766. + * GNU General Public License for more details.
  33767. + *
  33768. + * You should have received a copy of the GNU General Public License
  33769. + * along with this program; if not, write to the Free Software
  33770. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33771. + */
  33772. +
  33773. +#include <linux/module.h>
  33774. +#include <linux/errno.h>
  33775. +#include <linux/interrupt.h>
  33776. +#include <linux/sched.h>
  33777. +#include <linux/kernel.h>
  33778. +#include <linux/time.h>
  33779. +#include <linux/string.h>
  33780. +#include <linux/delay.h>
  33781. +#include <linux/platform_device.h>
  33782. +#include <linux/irq.h>
  33783. +#include <linux/spinlock.h>
  33784. +#include <media/lirc.h>
  33785. +#include <media/lirc_dev.h>
  33786. +#include <linux/gpio.h>
  33787. +
  33788. +#define LIRC_DRIVER_NAME "lirc_rpi"
  33789. +#define RBUF_LEN 256
  33790. +#define LIRC_TRANSMITTER_LATENCY 256
  33791. +
  33792. +#ifndef MAX_UDELAY_MS
  33793. +#define MAX_UDELAY_US 5000
  33794. +#else
  33795. +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
  33796. +#endif
  33797. +
  33798. +#define dprintk(fmt, args...) \
  33799. + do { \
  33800. + if (debug) \
  33801. + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
  33802. + fmt, ## args); \
  33803. + } while (0)
  33804. +
  33805. +/* module parameters */
  33806. +
  33807. +/* set the default GPIO input pin */
  33808. +static int gpio_in_pin = 18;
  33809. +/* set the default GPIO output pin */
  33810. +static int gpio_out_pin = 17;
  33811. +/* enable debugging messages */
  33812. +static bool debug;
  33813. +/* -1 = auto, 0 = active high, 1 = active low */
  33814. +static int sense = -1;
  33815. +/* use softcarrier by default */
  33816. +static bool softcarrier = 1;
  33817. +/* 0 = do not invert output, 1 = invert output */
  33818. +static bool invert = 0;
  33819. +
  33820. +struct gpio_chip *gpiochip;
  33821. +struct irq_chip *irqchip;
  33822. +struct irq_data *irqdata;
  33823. +
  33824. +/* forward declarations */
  33825. +static long send_pulse(unsigned long length);
  33826. +static void send_space(long length);
  33827. +static void lirc_rpi_exit(void);
  33828. +
  33829. +int valid_gpio_pins[] = { 0, 1, 4, 8, 7, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23,
  33830. + 24, 25 };
  33831. +
  33832. +static struct platform_device *lirc_rpi_dev;
  33833. +static struct timeval lasttv = { 0, 0 };
  33834. +static struct lirc_buffer rbuf;
  33835. +static spinlock_t lock;
  33836. +
  33837. +/* initialized/set in init_timing_params() */
  33838. +static unsigned int freq = 38000;
  33839. +static unsigned int duty_cycle = 50;
  33840. +static unsigned long period;
  33841. +static unsigned long pulse_width;
  33842. +static unsigned long space_width;
  33843. +
  33844. +static void safe_udelay(unsigned long usecs)
  33845. +{
  33846. + while (usecs > MAX_UDELAY_US) {
  33847. + udelay(MAX_UDELAY_US);
  33848. + usecs -= MAX_UDELAY_US;
  33849. + }
  33850. + udelay(usecs);
  33851. +}
  33852. +
  33853. +static int init_timing_params(unsigned int new_duty_cycle,
  33854. + unsigned int new_freq)
  33855. +{
  33856. + /*
  33857. + * period, pulse/space width are kept with 8 binary places -
  33858. + * IE multiplied by 256.
  33859. + */
  33860. + if (256 * 1000000L / new_freq * new_duty_cycle / 100 <=
  33861. + LIRC_TRANSMITTER_LATENCY)
  33862. + return -EINVAL;
  33863. + if (256 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
  33864. + LIRC_TRANSMITTER_LATENCY)
  33865. + return -EINVAL;
  33866. + duty_cycle = new_duty_cycle;
  33867. + freq = new_freq;
  33868. + period = 256 * 1000000L / freq;
  33869. + pulse_width = period * duty_cycle / 100;
  33870. + space_width = period - pulse_width;
  33871. + dprintk("in init_timing_params, freq=%d pulse=%ld, "
  33872. + "space=%ld\n", freq, pulse_width, space_width);
  33873. + return 0;
  33874. +}
  33875. +
  33876. +static long send_pulse_softcarrier(unsigned long length)
  33877. +{
  33878. + int flag;
  33879. + unsigned long actual, target, d;
  33880. +
  33881. + length <<= 8;
  33882. +
  33883. + actual = 0; target = 0; flag = 0;
  33884. + while (actual < length) {
  33885. + if (flag) {
  33886. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33887. + target += space_width;
  33888. + } else {
  33889. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  33890. + target += pulse_width;
  33891. + }
  33892. + d = (target - actual -
  33893. + LIRC_TRANSMITTER_LATENCY + 128) >> 8;
  33894. + /*
  33895. + * Note - we've checked in ioctl that the pulse/space
  33896. + * widths are big enough so that d is > 0
  33897. + */
  33898. + udelay(d);
  33899. + actual += (d << 8) + LIRC_TRANSMITTER_LATENCY;
  33900. + flag = !flag;
  33901. + }
  33902. + return (actual-length) >> 8;
  33903. +}
  33904. +
  33905. +static long send_pulse(unsigned long length)
  33906. +{
  33907. + if (length <= 0)
  33908. + return 0;
  33909. +
  33910. + if (softcarrier) {
  33911. + return send_pulse_softcarrier(length);
  33912. + } else {
  33913. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  33914. + safe_udelay(length);
  33915. + return 0;
  33916. + }
  33917. +}
  33918. +
  33919. +static void send_space(long length)
  33920. +{
  33921. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33922. + if (length <= 0)
  33923. + return;
  33924. + safe_udelay(length);
  33925. +}
  33926. +
  33927. +static void rbwrite(int l)
  33928. +{
  33929. + if (lirc_buffer_full(&rbuf)) {
  33930. + /* no new signals will be accepted */
  33931. + dprintk("Buffer overrun\n");
  33932. + return;
  33933. + }
  33934. + lirc_buffer_write(&rbuf, (void *)&l);
  33935. +}
  33936. +
  33937. +static void frbwrite(int l)
  33938. +{
  33939. + /* simple noise filter */
  33940. + static int pulse, space;
  33941. + static unsigned int ptr;
  33942. +
  33943. + if (ptr > 0 && (l & PULSE_BIT)) {
  33944. + pulse += l & PULSE_MASK;
  33945. + if (pulse > 250) {
  33946. + rbwrite(space);
  33947. + rbwrite(pulse | PULSE_BIT);
  33948. + ptr = 0;
  33949. + pulse = 0;
  33950. + }
  33951. + return;
  33952. + }
  33953. + if (!(l & PULSE_BIT)) {
  33954. + if (ptr == 0) {
  33955. + if (l > 20000) {
  33956. + space = l;
  33957. + ptr++;
  33958. + return;
  33959. + }
  33960. + } else {
  33961. + if (l > 20000) {
  33962. + space += pulse;
  33963. + if (space > PULSE_MASK)
  33964. + space = PULSE_MASK;
  33965. + space += l;
  33966. + if (space > PULSE_MASK)
  33967. + space = PULSE_MASK;
  33968. + pulse = 0;
  33969. + return;
  33970. + }
  33971. + rbwrite(space);
  33972. + rbwrite(pulse | PULSE_BIT);
  33973. + ptr = 0;
  33974. + pulse = 0;
  33975. + }
  33976. + }
  33977. + rbwrite(l);
  33978. +}
  33979. +
  33980. +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
  33981. +{
  33982. + struct timeval tv;
  33983. + long deltv;
  33984. + int data;
  33985. + int signal;
  33986. +
  33987. + /* use the GPIO signal level */
  33988. + signal = gpiochip->get(gpiochip, gpio_in_pin);
  33989. +
  33990. + /* unmask the irq */
  33991. + irqchip->irq_unmask(irqdata);
  33992. +
  33993. + if (sense != -1) {
  33994. + /* get current time */
  33995. + do_gettimeofday(&tv);
  33996. +
  33997. + /* calc time since last interrupt in microseconds */
  33998. + deltv = tv.tv_sec-lasttv.tv_sec;
  33999. + if (tv.tv_sec < lasttv.tv_sec ||
  34000. + (tv.tv_sec == lasttv.tv_sec &&
  34001. + tv.tv_usec < lasttv.tv_usec)) {
  34002. + printk(KERN_WARNING LIRC_DRIVER_NAME
  34003. + ": AIEEEE: your clock just jumped backwards\n");
  34004. + printk(KERN_WARNING LIRC_DRIVER_NAME
  34005. + ": %d %d %lx %lx %lx %lx\n", signal, sense,
  34006. + tv.tv_sec, lasttv.tv_sec,
  34007. + tv.tv_usec, lasttv.tv_usec);
  34008. + data = PULSE_MASK;
  34009. + } else if (deltv > 15) {
  34010. + data = PULSE_MASK; /* really long time */
  34011. + if (!(signal^sense)) {
  34012. + /* sanity check */
  34013. + printk(KERN_WARNING LIRC_DRIVER_NAME
  34014. + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
  34015. + signal, sense, tv.tv_sec, lasttv.tv_sec,
  34016. + tv.tv_usec, lasttv.tv_usec);
  34017. + /*
  34018. + * detecting pulse while this
  34019. + * MUST be a space!
  34020. + */
  34021. + sense = sense ? 0 : 1;
  34022. + }
  34023. + } else {
  34024. + data = (int) (deltv*1000000 +
  34025. + (tv.tv_usec - lasttv.tv_usec));
  34026. + }
  34027. + frbwrite(signal^sense ? data : (data|PULSE_BIT));
  34028. + lasttv = tv;
  34029. + wake_up_interruptible(&rbuf.wait_poll);
  34030. + }
  34031. +
  34032. + return IRQ_HANDLED;
  34033. +}
  34034. +
  34035. +static int is_right_chip(struct gpio_chip *chip, void *data)
  34036. +{
  34037. + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
  34038. +
  34039. + if (strcmp(data, chip->label) == 0)
  34040. + return 1;
  34041. + return 0;
  34042. +}
  34043. +
  34044. +static int init_port(void)
  34045. +{
  34046. + int i, nlow, nhigh, ret, irq;
  34047. +
  34048. + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
  34049. +
  34050. + if (!gpiochip)
  34051. + return -ENODEV;
  34052. +
  34053. + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
  34054. + printk(KERN_ALERT LIRC_DRIVER_NAME
  34055. + ": cant claim gpio pin %d\n", gpio_out_pin);
  34056. + ret = -ENODEV;
  34057. + goto exit_init_port;
  34058. + }
  34059. +
  34060. + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
  34061. + printk(KERN_ALERT LIRC_DRIVER_NAME
  34062. + ": cant claim gpio pin %d\n", gpio_in_pin);
  34063. + ret = -ENODEV;
  34064. + goto exit_gpio_free_out_pin;
  34065. + }
  34066. +
  34067. + gpiochip->direction_input(gpiochip, gpio_in_pin);
  34068. + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
  34069. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  34070. +
  34071. + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
  34072. + dprintk("to_irq %d\n", irq);
  34073. + irqdata = irq_get_irq_data(irq);
  34074. +
  34075. + if (irqdata && irqdata->chip) {
  34076. + irqchip = irqdata->chip;
  34077. + } else {
  34078. + ret = -ENODEV;
  34079. + goto exit_gpio_free_in_pin;
  34080. + }
  34081. +
  34082. + /* if pin is high, then this must be an active low receiver. */
  34083. + if (sense == -1) {
  34084. + /* wait 1/2 sec for the power supply */
  34085. + msleep(500);
  34086. +
  34087. + /*
  34088. + * probe 9 times every 0.04s, collect "votes" for
  34089. + * active high/low
  34090. + */
  34091. + nlow = 0;
  34092. + nhigh = 0;
  34093. + for (i = 0; i < 9; i++) {
  34094. + if (gpiochip->get(gpiochip, gpio_in_pin))
  34095. + nlow++;
  34096. + else
  34097. + nhigh++;
  34098. + msleep(40);
  34099. + }
  34100. + sense = (nlow >= nhigh ? 1 : 0);
  34101. + printk(KERN_INFO LIRC_DRIVER_NAME
  34102. + ": auto-detected active %s receiver on GPIO pin %d\n",
  34103. + sense ? "low" : "high", gpio_in_pin);
  34104. + } else {
  34105. + printk(KERN_INFO LIRC_DRIVER_NAME
  34106. + ": manually using active %s receiver on GPIO pin %d\n",
  34107. + sense ? "low" : "high", gpio_in_pin);
  34108. + }
  34109. +
  34110. + return 0;
  34111. +
  34112. + exit_gpio_free_in_pin:
  34113. + gpio_free(gpio_in_pin);
  34114. +
  34115. + exit_gpio_free_out_pin:
  34116. + gpio_free(gpio_out_pin);
  34117. +
  34118. + exit_init_port:
  34119. + return ret;
  34120. +}
  34121. +
  34122. +// called when the character device is opened
  34123. +static int set_use_inc(void *data)
  34124. +{
  34125. + int result;
  34126. + unsigned long flags;
  34127. +
  34128. + /* initialize timestamp */
  34129. + do_gettimeofday(&lasttv);
  34130. +
  34131. + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
  34132. + (irq_handler_t) irq_handler, 0,
  34133. + LIRC_DRIVER_NAME, (void*) 0);
  34134. +
  34135. + switch (result) {
  34136. + case -EBUSY:
  34137. + printk(KERN_ERR LIRC_DRIVER_NAME
  34138. + ": IRQ %d is busy\n",
  34139. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  34140. + return -EBUSY;
  34141. + case -EINVAL:
  34142. + printk(KERN_ERR LIRC_DRIVER_NAME
  34143. + ": Bad irq number or handler\n");
  34144. + return -EINVAL;
  34145. + default:
  34146. + dprintk("Interrupt %d obtained\n",
  34147. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  34148. + break;
  34149. + };
  34150. +
  34151. + /* initialize pulse/space widths */
  34152. + init_timing_params(duty_cycle, freq);
  34153. +
  34154. + spin_lock_irqsave(&lock, flags);
  34155. +
  34156. + /* GPIO Pin Falling/Rising Edge Detect Enable */
  34157. + irqchip->irq_set_type(irqdata,
  34158. + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
  34159. +
  34160. + /* unmask the irq */
  34161. + irqchip->irq_unmask(irqdata);
  34162. +
  34163. + spin_unlock_irqrestore(&lock, flags);
  34164. +
  34165. + return 0;
  34166. +}
  34167. +
  34168. +static void set_use_dec(void *data)
  34169. +{
  34170. + unsigned long flags;
  34171. +
  34172. + spin_lock_irqsave(&lock, flags);
  34173. +
  34174. + /* GPIO Pin Falling/Rising Edge Detect Disable */
  34175. + irqchip->irq_set_type(irqdata, 0);
  34176. + irqchip->irq_mask(irqdata);
  34177. +
  34178. + spin_unlock_irqrestore(&lock, flags);
  34179. +
  34180. + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
  34181. +
  34182. + dprintk(KERN_INFO LIRC_DRIVER_NAME
  34183. + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
  34184. +}
  34185. +
  34186. +static ssize_t lirc_write(struct file *file, const char *buf,
  34187. + size_t n, loff_t *ppos)
  34188. +{
  34189. + int i, count;
  34190. + unsigned long flags;
  34191. + long delta = 0;
  34192. + int *wbuf;
  34193. +
  34194. + count = n / sizeof(int);
  34195. + if (n % sizeof(int) || count % 2 == 0)
  34196. + return -EINVAL;
  34197. + wbuf = memdup_user(buf, n);
  34198. + if (IS_ERR(wbuf))
  34199. + return PTR_ERR(wbuf);
  34200. + spin_lock_irqsave(&lock, flags);
  34201. +
  34202. + for (i = 0; i < count; i++) {
  34203. + if (i%2)
  34204. + send_space(wbuf[i] - delta);
  34205. + else
  34206. + delta = send_pulse(wbuf[i]);
  34207. + }
  34208. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  34209. +
  34210. + spin_unlock_irqrestore(&lock, flags);
  34211. + kfree(wbuf);
  34212. + return n;
  34213. +}
  34214. +
  34215. +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  34216. +{
  34217. + int result;
  34218. + __u32 value;
  34219. +
  34220. + switch (cmd) {
  34221. + case LIRC_GET_SEND_MODE:
  34222. + return -ENOIOCTLCMD;
  34223. + break;
  34224. +
  34225. + case LIRC_SET_SEND_MODE:
  34226. + result = get_user(value, (__u32 *) arg);
  34227. + if (result)
  34228. + return result;
  34229. + /* only LIRC_MODE_PULSE supported */
  34230. + if (value != LIRC_MODE_PULSE)
  34231. + return -ENOSYS;
  34232. + break;
  34233. +
  34234. + case LIRC_GET_LENGTH:
  34235. + return -ENOSYS;
  34236. + break;
  34237. +
  34238. + case LIRC_SET_SEND_DUTY_CYCLE:
  34239. + dprintk("SET_SEND_DUTY_CYCLE\n");
  34240. + result = get_user(value, (__u32 *) arg);
  34241. + if (result)
  34242. + return result;
  34243. + if (value <= 0 || value > 100)
  34244. + return -EINVAL;
  34245. + return init_timing_params(value, freq);
  34246. + break;
  34247. +
  34248. + case LIRC_SET_SEND_CARRIER:
  34249. + dprintk("SET_SEND_CARRIER\n");
  34250. + result = get_user(value, (__u32 *) arg);
  34251. + if (result)
  34252. + return result;
  34253. + if (value > 500000 || value < 20000)
  34254. + return -EINVAL;
  34255. + return init_timing_params(duty_cycle, value);
  34256. + break;
  34257. +
  34258. + default:
  34259. + return lirc_dev_fop_ioctl(filep, cmd, arg);
  34260. + }
  34261. + return 0;
  34262. +}
  34263. +
  34264. +static const struct file_operations lirc_fops = {
  34265. + .owner = THIS_MODULE,
  34266. + .write = lirc_write,
  34267. + .unlocked_ioctl = lirc_ioctl,
  34268. + .read = lirc_dev_fop_read,
  34269. + .poll = lirc_dev_fop_poll,
  34270. + .open = lirc_dev_fop_open,
  34271. + .release = lirc_dev_fop_close,
  34272. + .llseek = no_llseek,
  34273. +};
  34274. +
  34275. +static struct lirc_driver driver = {
  34276. + .name = LIRC_DRIVER_NAME,
  34277. + .minor = -1,
  34278. + .code_length = 1,
  34279. + .sample_rate = 0,
  34280. + .data = NULL,
  34281. + .add_to_buf = NULL,
  34282. + .rbuf = &rbuf,
  34283. + .set_use_inc = set_use_inc,
  34284. + .set_use_dec = set_use_dec,
  34285. + .fops = &lirc_fops,
  34286. + .dev = NULL,
  34287. + .owner = THIS_MODULE,
  34288. +};
  34289. +
  34290. +static struct platform_driver lirc_rpi_driver = {
  34291. + .driver = {
  34292. + .name = LIRC_DRIVER_NAME,
  34293. + .owner = THIS_MODULE,
  34294. + },
  34295. +};
  34296. +
  34297. +static int __init lirc_rpi_init(void)
  34298. +{
  34299. + int result;
  34300. +
  34301. + /* Init read buffer. */
  34302. + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
  34303. + if (result < 0)
  34304. + return -ENOMEM;
  34305. +
  34306. + result = platform_driver_register(&lirc_rpi_driver);
  34307. + if (result) {
  34308. + printk(KERN_ERR LIRC_DRIVER_NAME
  34309. + ": lirc register returned %d\n", result);
  34310. + goto exit_buffer_free;
  34311. + }
  34312. +
  34313. + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
  34314. + if (!lirc_rpi_dev) {
  34315. + result = -ENOMEM;
  34316. + goto exit_driver_unregister;
  34317. + }
  34318. +
  34319. + result = platform_device_add(lirc_rpi_dev);
  34320. + if (result)
  34321. + goto exit_device_put;
  34322. +
  34323. + return 0;
  34324. +
  34325. + exit_device_put:
  34326. + platform_device_put(lirc_rpi_dev);
  34327. +
  34328. + exit_driver_unregister:
  34329. + platform_driver_unregister(&lirc_rpi_driver);
  34330. +
  34331. + exit_buffer_free:
  34332. + lirc_buffer_free(&rbuf);
  34333. +
  34334. + return result;
  34335. +}
  34336. +
  34337. +static void lirc_rpi_exit(void)
  34338. +{
  34339. + platform_device_unregister(lirc_rpi_dev);
  34340. + platform_driver_unregister(&lirc_rpi_driver);
  34341. + lirc_buffer_free(&rbuf);
  34342. +}
  34343. +
  34344. +static int __init lirc_rpi_init_module(void)
  34345. +{
  34346. + int result, i;
  34347. +
  34348. + result = lirc_rpi_init();
  34349. + if (result)
  34350. + return result;
  34351. +
  34352. + /* check if the module received valid gpio pin numbers */
  34353. + result = 0;
  34354. + if (gpio_in_pin != gpio_out_pin) {
  34355. + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
  34356. + if (gpio_in_pin == valid_gpio_pins[i] ||
  34357. + gpio_out_pin == valid_gpio_pins[i]) {
  34358. + result++;
  34359. + }
  34360. + }
  34361. + }
  34362. +
  34363. + if (result != 2) {
  34364. + result = -EINVAL;
  34365. + printk(KERN_ERR LIRC_DRIVER_NAME
  34366. + ": invalid GPIO pin(s) specified!\n");
  34367. + goto exit_rpi;
  34368. + }
  34369. +
  34370. + result = init_port();
  34371. + if (result < 0)
  34372. + goto exit_rpi;
  34373. +
  34374. + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
  34375. + LIRC_CAN_SET_SEND_CARRIER |
  34376. + LIRC_CAN_SEND_PULSE |
  34377. + LIRC_CAN_REC_MODE2;
  34378. +
  34379. + driver.dev = &lirc_rpi_dev->dev;
  34380. + driver.minor = lirc_register_driver(&driver);
  34381. +
  34382. + if (driver.minor < 0) {
  34383. + printk(KERN_ERR LIRC_DRIVER_NAME
  34384. + ": device registration failed with %d\n", result);
  34385. + result = -EIO;
  34386. + goto exit_rpi;
  34387. + }
  34388. +
  34389. + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
  34390. +
  34391. + return 0;
  34392. +
  34393. + exit_rpi:
  34394. + lirc_rpi_exit();
  34395. +
  34396. + return result;
  34397. +}
  34398. +
  34399. +static void __exit lirc_rpi_exit_module(void)
  34400. +{
  34401. + gpio_free(gpio_out_pin);
  34402. + gpio_free(gpio_in_pin);
  34403. +
  34404. + lirc_rpi_exit();
  34405. +
  34406. + lirc_unregister_driver(driver.minor);
  34407. + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
  34408. +}
  34409. +
  34410. +module_init(lirc_rpi_init_module);
  34411. +module_exit(lirc_rpi_exit_module);
  34412. +
  34413. +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
  34414. +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
  34415. +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
  34416. +MODULE_LICENSE("GPL");
  34417. +
  34418. +module_param(gpio_out_pin, int, S_IRUGO);
  34419. +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
  34420. + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
  34421. + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
  34422. +
  34423. +module_param(gpio_in_pin, int, S_IRUGO);
  34424. +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
  34425. + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
  34426. + " 17, 18, 21, 22, 23, 24, 25, default 18");
  34427. +
  34428. +module_param(sense, int, S_IRUGO);
  34429. +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
  34430. + " (0 = active high, 1 = active low )");
  34431. +
  34432. +module_param(softcarrier, bool, S_IRUGO);
  34433. +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
  34434. +
  34435. +module_param(invert, bool, S_IRUGO);
  34436. +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
  34437. +
  34438. +module_param(debug, bool, S_IRUGO | S_IWUSR);
  34439. +MODULE_PARM_DESC(debug, "Enable debugging messages");
  34440. diff -Nur linux-3.12.13/drivers/staging/media/lirc/Makefile linux-raspberry-pi/drivers/staging/media/lirc/Makefile
  34441. --- linux-3.12.13/drivers/staging/media/lirc/Makefile 2014-02-22 22:32:50.000000000 +0100
  34442. +++ linux-raspberry-pi/drivers/staging/media/lirc/Makefile 2014-03-11 17:33:05.000000000 +0100
  34443. @@ -7,6 +7,7 @@
  34444. obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
  34445. obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
  34446. obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
  34447. +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
  34448. obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
  34449. obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
  34450. obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
  34451. diff -Nur linux-3.12.13/drivers/thermal/bcm2835-thermal.c linux-raspberry-pi/drivers/thermal/bcm2835-thermal.c
  34452. --- linux-3.12.13/drivers/thermal/bcm2835-thermal.c 1970-01-01 01:00:00.000000000 +0100
  34453. +++ linux-raspberry-pi/drivers/thermal/bcm2835-thermal.c 2014-03-11 17:33:06.000000000 +0100
  34454. @@ -0,0 +1,184 @@
  34455. +/*****************************************************************************
  34456. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  34457. +*
  34458. +* Unless you and Broadcom execute a separate written software license
  34459. +* agreement governing use of this software, this software is licensed to you
  34460. +* under the terms of the GNU General Public License version 2, available at
  34461. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  34462. +*
  34463. +* Notwithstanding the above, under no circumstances may you combine this
  34464. +* software in any way with any other Broadcom software provided under a
  34465. +* license other than the GPL, without Broadcom's express prior written
  34466. +* consent.
  34467. +*****************************************************************************/
  34468. +
  34469. +#include <linux/kernel.h>
  34470. +#include <linux/module.h>
  34471. +#include <linux/init.h>
  34472. +#include <linux/platform_device.h>
  34473. +#include <linux/slab.h>
  34474. +#include <linux/sysfs.h>
  34475. +#include <mach/vcio.h>
  34476. +#include <linux/thermal.h>
  34477. +
  34478. +
  34479. +/* --- DEFINITIONS --- */
  34480. +#define MODULE_NAME "bcm2835_thermal"
  34481. +
  34482. +/*#define THERMAL_DEBUG_ENABLE*/
  34483. +
  34484. +#ifdef THERMAL_DEBUG_ENABLE
  34485. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  34486. +#else
  34487. +#define print_debug(fmt,...)
  34488. +#endif
  34489. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  34490. +
  34491. +#define VC_TAG_GET_TEMP 0x00030006
  34492. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  34493. +
  34494. +typedef enum {
  34495. + TEMP,
  34496. + MAX_TEMP,
  34497. +} temp_type;
  34498. +
  34499. +/* --- STRUCTS --- */
  34500. +/* tag part of the message */
  34501. +struct vc_msg_tag {
  34502. + uint32_t tag_id; /* the tag ID for the temperature */
  34503. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  34504. + uint32_t request_code; /* identifies message as a request (should be 0) */
  34505. + uint32_t id; /* extra ID field (should be 0) */
  34506. + uint32_t val; /* returned value of the temperature */
  34507. +};
  34508. +
  34509. +/* message structure to be sent to videocore */
  34510. +struct vc_msg {
  34511. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  34512. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  34513. + struct vc_msg_tag tag; /* the tag structure above to make */
  34514. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  34515. +};
  34516. +
  34517. +struct bcm2835_thermal_data {
  34518. + struct thermal_zone_device *thermal_dev;
  34519. + struct vc_msg msg;
  34520. +};
  34521. +
  34522. +/* --- GLOBALS --- */
  34523. +static struct bcm2835_thermal_data bcm2835_data;
  34524. +
  34525. +/* Thermal Device Operations */
  34526. +static struct thermal_zone_device_ops ops;
  34527. +
  34528. +/* --- FUNCTIONS --- */
  34529. +
  34530. +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
  34531. +{
  34532. + int result = -1, retry = 3;
  34533. + print_debug("IN");
  34534. +
  34535. + *temp = 0;
  34536. + while (result != 0 && retry-- > 0) {
  34537. + /* wipe all previous message data */
  34538. + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
  34539. +
  34540. + /* prepare message */
  34541. + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
  34542. + bcm2835_data.msg.tag.buffer_size = 8;
  34543. + bcm2835_data.msg.tag.tag_id = tag_id;
  34544. +
  34545. + /* send the message */
  34546. + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
  34547. + 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);
  34548. + if (!(bcm2835_data.msg.request_code & 0x80000000))
  34549. + result = -1;
  34550. + }
  34551. +
  34552. + /* check if it was all ok and return the rate in milli degrees C */
  34553. + if (result == 0)
  34554. + *temp = (uint)bcm2835_data.msg.tag.val;
  34555. + else
  34556. + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
  34557. + print_debug("OUT");
  34558. + return result;
  34559. +}
  34560. +
  34561. +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
  34562. +{
  34563. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
  34564. +}
  34565. +
  34566. +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
  34567. +{
  34568. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
  34569. +}
  34570. +
  34571. +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
  34572. +{
  34573. + *trip_type = THERMAL_TRIP_HOT;
  34574. + return 0;
  34575. +}
  34576. +
  34577. +
  34578. +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
  34579. +{
  34580. + *dev_mode = THERMAL_DEVICE_ENABLED;
  34581. + return 0;
  34582. +}
  34583. +
  34584. +
  34585. +static int bcm2835_thermal_probe(struct platform_device *pdev)
  34586. +{
  34587. + print_debug("IN");
  34588. + print_debug("THERMAL Driver has been probed!");
  34589. +
  34590. + /* check that the device isn't null!*/
  34591. + if(pdev == NULL)
  34592. + {
  34593. + print_debug("Platform device is empty!");
  34594. + return -ENODEV;
  34595. + }
  34596. +
  34597. + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
  34598. + {
  34599. + print_debug("Unable to register the thermal device!");
  34600. + return -EFAULT;
  34601. + }
  34602. + return 0;
  34603. +}
  34604. +
  34605. +
  34606. +static int bcm2835_thermal_remove(struct platform_device *pdev)
  34607. +{
  34608. + print_debug("IN");
  34609. +
  34610. + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
  34611. +
  34612. + print_debug("OUT");
  34613. +
  34614. + return 0;
  34615. +}
  34616. +
  34617. +static struct thermal_zone_device_ops ops = {
  34618. + .get_temp = bcm2835_get_temp,
  34619. + .get_trip_temp = bcm2835_get_max_temp,
  34620. + .get_trip_type = bcm2835_get_trip_type,
  34621. + .get_mode = bcm2835_get_mode,
  34622. +};
  34623. +
  34624. +/* Thermal Driver */
  34625. +static struct platform_driver bcm2835_thermal_driver = {
  34626. + .probe = bcm2835_thermal_probe,
  34627. + .remove = bcm2835_thermal_remove,
  34628. + .driver = {
  34629. + .name = "bcm2835_thermal",
  34630. + .owner = THIS_MODULE,
  34631. + },
  34632. +};
  34633. +
  34634. +MODULE_LICENSE("GPL");
  34635. +MODULE_AUTHOR("Dorian Peake");
  34636. +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  34637. +
  34638. +module_platform_driver(bcm2835_thermal_driver);
  34639. diff -Nur linux-3.12.13/drivers/thermal/Kconfig linux-raspberry-pi/drivers/thermal/Kconfig
  34640. --- linux-3.12.13/drivers/thermal/Kconfig 2014-02-22 22:32:50.000000000 +0100
  34641. +++ linux-raspberry-pi/drivers/thermal/Kconfig 2014-03-11 17:51:27.000000000 +0100
  34642. @@ -181,6 +181,12 @@
  34643. enforce idle time which results in more package C-state residency. The
  34644. user interface is exposed via generic thermal framework.
  34645. +config THERMAL_BCM2835
  34646. + tristate "BCM2835 Thermal Driver"
  34647. + help
  34648. + This will enable temperature monitoring for the Broadcom BCM2835
  34649. + chip. If built as a module, it will be called 'bcm2835-thermal'.
  34650. +
  34651. config X86_PKG_TEMP_THERMAL
  34652. tristate "X86 package temperature thermal driver"
  34653. depends on X86_THERMAL_VECTOR
  34654. diff -Nur linux-3.12.13/drivers/thermal/Makefile linux-raspberry-pi/drivers/thermal/Makefile
  34655. --- linux-3.12.13/drivers/thermal/Makefile 2014-02-22 22:32:50.000000000 +0100
  34656. +++ linux-raspberry-pi/drivers/thermal/Makefile 2014-03-11 17:51:27.000000000 +0100
  34657. @@ -27,5 +27,6 @@
  34658. obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
  34659. obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
  34660. obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
  34661. +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
  34662. obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
  34663. obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
  34664. diff -Nur linux-3.12.13/drivers/tty/serial/amba-pl011.c linux-raspberry-pi/drivers/tty/serial/amba-pl011.c
  34665. --- linux-3.12.13/drivers/tty/serial/amba-pl011.c 2014-02-22 22:32:50.000000000 +0100
  34666. +++ linux-raspberry-pi/drivers/tty/serial/amba-pl011.c 2014-03-11 17:51:27.000000000 +0100
  34667. @@ -84,7 +84,7 @@
  34668. static unsigned int get_fifosize_arm(struct amba_device *dev)
  34669. {
  34670. - return amba_rev(dev) < 3 ? 16 : 32;
  34671. + return 16; //TODO: fix: amba_rev(dev) < 3 ? 16 : 32;
  34672. }
  34673. static struct vendor_data vendor_arm = {
  34674. diff -Nur linux-3.12.13/drivers/usb/core/generic.c linux-raspberry-pi/drivers/usb/core/generic.c
  34675. --- linux-3.12.13/drivers/usb/core/generic.c 2014-02-22 22:32:50.000000000 +0100
  34676. +++ linux-raspberry-pi/drivers/usb/core/generic.c 2014-03-11 17:33:06.000000000 +0100
  34677. @@ -152,6 +152,7 @@
  34678. dev_warn(&udev->dev,
  34679. "no configuration chosen from %d choice%s\n",
  34680. num_configs, plural(num_configs));
  34681. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  34682. }
  34683. return i;
  34684. }
  34685. diff -Nur linux-3.12.13/drivers/usb/core/message.c linux-raspberry-pi/drivers/usb/core/message.c
  34686. --- linux-3.12.13/drivers/usb/core/message.c 2014-02-22 22:32:50.000000000 +0100
  34687. +++ linux-raspberry-pi/drivers/usb/core/message.c 2014-03-11 17:51:27.000000000 +0100
  34688. @@ -1885,6 +1885,85 @@
  34689. if (cp->string == NULL &&
  34690. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  34691. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  34692. +/* Uncomment this define to enable the HS Electrical Test support */
  34693. +#define DWC_HS_ELECT_TST 1
  34694. +#ifdef DWC_HS_ELECT_TST
  34695. + /* Here we implement the HS Electrical Test support. The
  34696. + * tester uses a vendor ID of 0x1A0A to indicate we should
  34697. + * run a special test sequence. The product ID tells us
  34698. + * which sequence to run. We invoke the test sequence by
  34699. + * sending a non-standard SetFeature command to our root
  34700. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  34701. + * recognize the command and perform the desired test
  34702. + * sequence.
  34703. + */
  34704. + if (dev->descriptor.idVendor == 0x1A0A) {
  34705. + /* HSOTG Electrical Test */
  34706. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  34707. +
  34708. + if (dev->bus && dev->bus->root_hub) {
  34709. + struct usb_device *hdev = dev->bus->root_hub;
  34710. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  34711. +
  34712. + switch (dev->descriptor.idProduct) {
  34713. + case 0x0101: /* TEST_SE0_NAK */
  34714. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  34715. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34716. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34717. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  34718. + break;
  34719. +
  34720. + case 0x0102: /* TEST_J */
  34721. + dev_warn(&dev->dev, "TEST_J\n");
  34722. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34723. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34724. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  34725. + break;
  34726. +
  34727. + case 0x0103: /* TEST_K */
  34728. + dev_warn(&dev->dev, "TEST_K\n");
  34729. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34730. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34731. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  34732. + break;
  34733. +
  34734. + case 0x0104: /* TEST_PACKET */
  34735. + dev_warn(&dev->dev, "TEST_PACKET\n");
  34736. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34737. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34738. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  34739. + break;
  34740. +
  34741. + case 0x0105: /* TEST_FORCE_ENABLE */
  34742. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  34743. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34744. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34745. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  34746. + break;
  34747. +
  34748. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  34749. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  34750. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34751. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34752. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  34753. + break;
  34754. +
  34755. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  34756. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  34757. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34758. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34759. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  34760. + break;
  34761. +
  34762. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  34763. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  34764. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34765. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34766. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  34767. + }
  34768. + }
  34769. + }
  34770. +#endif /* DWC_HS_ELECT_TST */
  34771. /* Now that the interfaces are installed, re-enable LPM. */
  34772. usb_unlocked_enable_lpm(dev);
  34773. diff -Nur linux-3.12.13/drivers/usb/core/otg_whitelist.h linux-raspberry-pi/drivers/usb/core/otg_whitelist.h
  34774. --- linux-3.12.13/drivers/usb/core/otg_whitelist.h 2014-02-22 22:32:50.000000000 +0100
  34775. +++ linux-raspberry-pi/drivers/usb/core/otg_whitelist.h 2014-03-11 17:51:27.000000000 +0100
  34776. @@ -19,33 +19,82 @@
  34777. static struct usb_device_id whitelist_table [] = {
  34778. /* hubs are optional in OTG, but very handy ... */
  34779. +#define CERT_WITHOUT_HUBS
  34780. +#if defined(CERT_WITHOUT_HUBS)
  34781. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  34782. +#else
  34783. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  34784. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  34785. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  34786. +#endif
  34787. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  34788. /* FIXME actually, printers are NOT supposed to use device classes;
  34789. * they're supposed to use interface classes...
  34790. */
  34791. -{ USB_DEVICE_INFO(7, 1, 1) },
  34792. -{ USB_DEVICE_INFO(7, 1, 2) },
  34793. -{ USB_DEVICE_INFO(7, 1, 3) },
  34794. +//{ USB_DEVICE_INFO(7, 1, 1) },
  34795. +//{ USB_DEVICE_INFO(7, 1, 2) },
  34796. +//{ USB_DEVICE_INFO(7, 1, 3) },
  34797. #endif
  34798. #ifdef CONFIG_USB_NET_CDCETHER
  34799. /* Linux-USB CDC Ethernet gadget */
  34800. -{ USB_DEVICE(0x0525, 0xa4a1), },
  34801. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  34802. /* Linux-USB CDC Ethernet + RNDIS gadget */
  34803. -{ USB_DEVICE(0x0525, 0xa4a2), },
  34804. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  34805. #endif
  34806. #if defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
  34807. /* gadget zero, for testing */
  34808. -{ USB_DEVICE(0x0525, 0xa4a0), },
  34809. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  34810. #endif
  34811. +/* OPT Tester */
  34812. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  34813. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  34814. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  34815. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  34816. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  34817. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  34818. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  34819. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  34820. +
  34821. +/* Sony cameras */
  34822. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  34823. +
  34824. +/* Memory Devices */
  34825. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  34826. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  34827. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  34828. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  34829. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  34830. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  34831. +
  34832. +/* HP Printers */
  34833. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  34834. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  34835. +
  34836. +/* Speakers */
  34837. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  34838. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  34839. +
  34840. { } /* Terminating entry */
  34841. };
  34842. +static inline void report_errors(struct usb_device *dev)
  34843. +{
  34844. + /* OTG MESSAGE: report errors here, customize to match your product */
  34845. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  34846. + le16_to_cpu(dev->descriptor.idVendor),
  34847. + le16_to_cpu(dev->descriptor.idProduct));
  34848. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  34849. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  34850. + } else {
  34851. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  34852. + }
  34853. +}
  34854. +
  34855. +
  34856. static int is_targeted(struct usb_device *dev)
  34857. {
  34858. struct usb_device_id *id = whitelist_table;
  34859. @@ -55,58 +104,83 @@
  34860. return 1;
  34861. /* HNP test device is _never_ targeted (see OTG spec 6.6.6) */
  34862. - if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1a0a &&
  34863. - le16_to_cpu(dev->descriptor.idProduct) == 0xbadd))
  34864. - return 0;
  34865. + if (dev->descriptor.idVendor == 0x1a0a &&
  34866. + dev->descriptor.idProduct == 0xbadd) {
  34867. + return 0;
  34868. + } else if (!enable_whitelist) {
  34869. + return 1;
  34870. + } else {
  34871. - /* NOTE: can't use usb_match_id() since interface caches
  34872. - * aren't set up yet. this is cut/paste from that code.
  34873. - */
  34874. - for (id = whitelist_table; id->match_flags; id++) {
  34875. - if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  34876. - id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  34877. - continue;
  34878. -
  34879. - if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  34880. - id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  34881. - continue;
  34882. -
  34883. - /* No need to test id->bcdDevice_lo != 0, since 0 is never
  34884. - greater than any unsigned number. */
  34885. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  34886. - (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  34887. - continue;
  34888. -
  34889. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  34890. - (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  34891. - continue;
  34892. -
  34893. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  34894. - (id->bDeviceClass != dev->descriptor.bDeviceClass))
  34895. - continue;
  34896. -
  34897. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  34898. - (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  34899. - continue;
  34900. -
  34901. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  34902. - (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  34903. - continue;
  34904. +#ifdef DEBUG
  34905. + dev_dbg(&dev->dev, "device V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  34906. + dev->descriptor.idVendor,
  34907. + dev->descriptor.idProduct,
  34908. + dev->descriptor.bDeviceClass,
  34909. + dev->descriptor.bDeviceSubClass,
  34910. + dev->descriptor.bDeviceProtocol);
  34911. +#endif
  34912. return 1;
  34913. + /* NOTE: can't use usb_match_id() since interface caches
  34914. + * aren't set up yet. this is cut/paste from that code.
  34915. + */
  34916. + for (id = whitelist_table; id->match_flags; id++) {
  34917. +#ifdef DEBUG
  34918. + dev_dbg(&dev->dev,
  34919. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  34920. + id->idVendor,
  34921. + id->idProduct,
  34922. + id->bDeviceClass,
  34923. + id->bDeviceSubClass,
  34924. + id->bDeviceProtocol);
  34925. +#endif
  34926. +
  34927. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  34928. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  34929. + continue;
  34930. +
  34931. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  34932. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  34933. + continue;
  34934. +
  34935. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  34936. + greater than any unsigned number. */
  34937. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  34938. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  34939. + continue;
  34940. +
  34941. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  34942. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  34943. + continue;
  34944. +
  34945. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  34946. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  34947. + continue;
  34948. +
  34949. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  34950. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  34951. + continue;
  34952. +
  34953. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  34954. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  34955. + continue;
  34956. +
  34957. + return 1;
  34958. + }
  34959. }
  34960. /* add other match criteria here ... */
  34961. -
  34962. - /* OTG MESSAGE: report errors here, customize to match your product */
  34963. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  34964. - le16_to_cpu(dev->descriptor.idVendor),
  34965. - le16_to_cpu(dev->descriptor.idProduct));
  34966. #ifdef CONFIG_USB_OTG_WHITELIST
  34967. + report_errors(dev);
  34968. return 0;
  34969. #else
  34970. - return 1;
  34971. + if (enable_whitelist) {
  34972. + report_errors(dev);
  34973. + return 0;
  34974. + } else {
  34975. + return 1;
  34976. + }
  34977. #endif
  34978. }
  34979. diff -Nur linux-3.12.13/drivers/usb/gadget/file_storage.c linux-raspberry-pi/drivers/usb/gadget/file_storage.c
  34980. --- linux-3.12.13/drivers/usb/gadget/file_storage.c 1970-01-01 01:00:00.000000000 +0100
  34981. +++ linux-raspberry-pi/drivers/usb/gadget/file_storage.c 2014-03-11 17:33:06.000000000 +0100
  34982. @@ -0,0 +1,3676 @@
  34983. +/*
  34984. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  34985. + *
  34986. + * Copyright (C) 2003-2008 Alan Stern
  34987. + * All rights reserved.
  34988. + *
  34989. + * Redistribution and use in source and binary forms, with or without
  34990. + * modification, are permitted provided that the following conditions
  34991. + * are met:
  34992. + * 1. Redistributions of source code must retain the above copyright
  34993. + * notice, this list of conditions, and the following disclaimer,
  34994. + * without modification.
  34995. + * 2. Redistributions in binary form must reproduce the above copyright
  34996. + * notice, this list of conditions and the following disclaimer in the
  34997. + * documentation and/or other materials provided with the distribution.
  34998. + * 3. The names of the above-listed copyright holders may not be used
  34999. + * to endorse or promote products derived from this software without
  35000. + * specific prior written permission.
  35001. + *
  35002. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35003. + * GNU General Public License ("GPL") as published by the Free Software
  35004. + * Foundation, either version 2 of that License or (at your option) any
  35005. + * later version.
  35006. + *
  35007. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35008. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35009. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35010. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35011. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35012. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35013. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35014. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35015. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35016. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35017. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35018. + */
  35019. +
  35020. +
  35021. +/*
  35022. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  35023. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  35024. + * to providing an example of a genuinely useful gadget driver for a USB
  35025. + * device, it also illustrates a technique of double-buffering for increased
  35026. + * throughput. Last but not least, it gives an easy way to probe the
  35027. + * behavior of the Mass Storage drivers in a USB host.
  35028. + *
  35029. + * Backing storage is provided by a regular file or a block device, specified
  35030. + * by the "file" module parameter. Access can be limited to read-only by
  35031. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  35032. + * access is always read-only.) The gadget will indicate that it has
  35033. + * removable media if the optional "removable" module parameter is set.
  35034. + *
  35035. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  35036. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  35037. + * by the optional "transport" module parameter. It also supports the
  35038. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  35039. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  35040. + * the optional "protocol" module parameter. In addition, the default
  35041. + * Vendor ID, Product ID, release number and serial number can be overridden.
  35042. + *
  35043. + * There is support for multiple logical units (LUNs), each of which has
  35044. + * its own backing file. The number of LUNs can be set using the optional
  35045. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  35046. + * files are specified using comma-separated lists for "file" and "ro".
  35047. + * The default number of LUNs is taken from the number of "file" elements;
  35048. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  35049. + * file must be specified for each LUN. If it is set, then an unspecified
  35050. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  35051. + * each LUN would be settable independently as a disk drive or a CD-ROM
  35052. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  35053. + * emulation includes a single data track and no audio tracks; hence there
  35054. + * need be only one backing file per LUN.
  35055. + *
  35056. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  35057. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  35058. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  35059. + * Support is included for both full-speed and high-speed operation.
  35060. + *
  35061. + * Note that the driver is slightly non-portable in that it assumes a
  35062. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  35063. + * interrupt-in endpoints. With most device controllers this isn't an
  35064. + * issue, but there may be some with hardware restrictions that prevent
  35065. + * a buffer from being used by more than one endpoint.
  35066. + *
  35067. + * Module options:
  35068. + *
  35069. + * file=filename[,filename...]
  35070. + * Required if "removable" is not set, names of
  35071. + * the files or block devices used for
  35072. + * backing storage
  35073. + * serial=HHHH... Required serial number (string of hex chars)
  35074. + * ro=b[,b...] Default false, booleans for read-only access
  35075. + * removable Default false, boolean for removable media
  35076. + * luns=N Default N = number of filenames, number of
  35077. + * LUNs to support
  35078. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  35079. + * in SCSI WRITE(10,12) commands
  35080. + * stall Default determined according to the type of
  35081. + * USB device controller (usually true),
  35082. + * boolean to permit the driver to halt
  35083. + * bulk endpoints
  35084. + * cdrom Default false, boolean for whether to emulate
  35085. + * a CD-ROM drive
  35086. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  35087. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  35088. + * ATAPI, QIC, UFI, 8070, or SCSI;
  35089. + * also 1 - 6)
  35090. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  35091. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  35092. + * release=0xRRRR Override the USB release number (bcdDevice)
  35093. + * buflen=N Default N=16384, buffer size used (will be
  35094. + * rounded down to a multiple of
  35095. + * PAGE_CACHE_SIZE)
  35096. + *
  35097. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  35098. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  35099. + * default values are used for everything else.
  35100. + *
  35101. + * The pathnames of the backing files and the ro settings are available in
  35102. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  35103. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  35104. + * these files will simulate ejecting/loading the medium (writing an empty
  35105. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  35106. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  35107. + * is being used.
  35108. + *
  35109. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  35110. + * The driver's SCSI command interface was based on the "Information
  35111. + * technology - Small Computer System Interface - 2" document from
  35112. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  35113. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  35114. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  35115. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  35116. + * document, Revision 1.0, December 14, 1998, available at
  35117. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  35118. + */
  35119. +
  35120. +
  35121. +/*
  35122. + * Driver Design
  35123. + *
  35124. + * The FSG driver is fairly straightforward. There is a main kernel
  35125. + * thread that handles most of the work. Interrupt routines field
  35126. + * callbacks from the controller driver: bulk- and interrupt-request
  35127. + * completion notifications, endpoint-0 events, and disconnect events.
  35128. + * Completion events are passed to the main thread by wakeup calls. Many
  35129. + * ep0 requests are handled at interrupt time, but SetInterface,
  35130. + * SetConfiguration, and device reset requests are forwarded to the
  35131. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  35132. + * should interrupt any ongoing file I/O operations).
  35133. + *
  35134. + * The thread's main routine implements the standard command/data/status
  35135. + * parts of a SCSI interaction. It and its subroutines are full of tests
  35136. + * for pending signals/exceptions -- all this polling is necessary since
  35137. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  35138. + * indication that the driver really wants to be running in userspace.)
  35139. + * An important point is that so long as the thread is alive it keeps an
  35140. + * open reference to the backing file. This will prevent unmounting
  35141. + * the backing file's underlying filesystem and could cause problems
  35142. + * during system shutdown, for example. To prevent such problems, the
  35143. + * thread catches INT, TERM, and KILL signals and converts them into
  35144. + * an EXIT exception.
  35145. + *
  35146. + * In normal operation the main thread is started during the gadget's
  35147. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  35148. + * exit when it receives a signal, and there's no point leaving the
  35149. + * gadget running when the thread is dead. So just before the thread
  35150. + * exits, it deregisters the gadget driver. This makes things a little
  35151. + * tricky: The driver is deregistered at two places, and the exiting
  35152. + * thread can indirectly call fsg_unbind() which in turn can tell the
  35153. + * thread to exit. The first problem is resolved through the use of the
  35154. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  35155. + * The second problem is resolved by having fsg_unbind() check
  35156. + * fsg->state; it won't try to stop the thread if the state is already
  35157. + * FSG_STATE_TERMINATED.
  35158. + *
  35159. + * To provide maximum throughput, the driver uses a circular pipeline of
  35160. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  35161. + * arbitrarily long; in practice the benefits don't justify having more
  35162. + * than 2 stages (i.e., double buffering). But it helps to think of the
  35163. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  35164. + * a bulk-out request pointer (since the buffer can be used for both
  35165. + * output and input -- directions always are given from the host's
  35166. + * point of view) as well as a pointer to the buffer and various state
  35167. + * variables.
  35168. + *
  35169. + * Use of the pipeline follows a simple protocol. There is a variable
  35170. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  35171. + * At any time that buffer head may still be in use from an earlier
  35172. + * request, so each buffer head has a state variable indicating whether
  35173. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  35174. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  35175. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  35176. + * head FULL when the I/O is complete. Then the buffer will be emptied
  35177. + * (again possibly by USB I/O, during which it is marked BUSY) and
  35178. + * finally marked EMPTY again (possibly by a completion routine).
  35179. + *
  35180. + * A module parameter tells the driver to avoid stalling the bulk
  35181. + * endpoints wherever the transport specification allows. This is
  35182. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  35183. + * halt on a bulk endpoint. However, under certain circumstances the
  35184. + * Bulk-only specification requires a stall. In such cases the driver
  35185. + * will halt the endpoint and set a flag indicating that it should clear
  35186. + * the halt in software during the next device reset. Hopefully this
  35187. + * will permit everything to work correctly. Furthermore, although the
  35188. + * specification allows the bulk-out endpoint to halt when the host sends
  35189. + * too much data, implementing this would cause an unavoidable race.
  35190. + * The driver will always use the "no-stall" approach for OUT transfers.
  35191. + *
  35192. + * One subtle point concerns sending status-stage responses for ep0
  35193. + * requests. Some of these requests, such as device reset, can involve
  35194. + * interrupting an ongoing file I/O operation, which might take an
  35195. + * arbitrarily long time. During that delay the host might give up on
  35196. + * the original ep0 request and issue a new one. When that happens the
  35197. + * driver should not notify the host about completion of the original
  35198. + * request, as the host will no longer be waiting for it. So the driver
  35199. + * assigns to each ep0 request a unique tag, and it keeps track of the
  35200. + * tag value of the request associated with a long-running exception
  35201. + * (device-reset, interface-change, or configuration-change). When the
  35202. + * exception handler is finished, the status-stage response is submitted
  35203. + * only if the current ep0 request tag is equal to the exception request
  35204. + * tag. Thus only the most recently received ep0 request will get a
  35205. + * status-stage response.
  35206. + *
  35207. + * Warning: This driver source file is too long. It ought to be split up
  35208. + * into a header file plus about 3 separate .c files, to handle the details
  35209. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  35210. + */
  35211. +
  35212. +
  35213. +/* #define VERBOSE_DEBUG */
  35214. +/* #define DUMP_MSGS */
  35215. +
  35216. +
  35217. +#include <linux/blkdev.h>
  35218. +#include <linux/completion.h>
  35219. +#include <linux/dcache.h>
  35220. +#include <linux/delay.h>
  35221. +#include <linux/device.h>
  35222. +#include <linux/fcntl.h>
  35223. +#include <linux/file.h>
  35224. +#include <linux/fs.h>
  35225. +#include <linux/kref.h>
  35226. +#include <linux/kthread.h>
  35227. +#include <linux/limits.h>
  35228. +#include <linux/module.h>
  35229. +#include <linux/rwsem.h>
  35230. +#include <linux/slab.h>
  35231. +#include <linux/spinlock.h>
  35232. +#include <linux/string.h>
  35233. +#include <linux/freezer.h>
  35234. +#include <linux/utsname.h>
  35235. +
  35236. +#include <linux/usb/ch9.h>
  35237. +#include <linux/usb/gadget.h>
  35238. +
  35239. +#include "gadget_chips.h"
  35240. +
  35241. +
  35242. +
  35243. +/*
  35244. + * Kbuild is not very cooperative with respect to linking separately
  35245. + * compiled library objects into one module. So for now we won't use
  35246. + * separate compilation ... ensuring init/exit sections work to shrink
  35247. + * the runtime footprint, and giving us at least some parts of what
  35248. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  35249. + */
  35250. +#include "usbstring.c"
  35251. +#include "config.c"
  35252. +#include "epautoconf.c"
  35253. +
  35254. +/*-------------------------------------------------------------------------*/
  35255. +
  35256. +#define DRIVER_DESC "File-backed Storage Gadget"
  35257. +#define DRIVER_NAME "g_file_storage"
  35258. +#define DRIVER_VERSION "1 September 2010"
  35259. +
  35260. +static char fsg_string_manufacturer[64];
  35261. +static const char fsg_string_product[] = DRIVER_DESC;
  35262. +static const char fsg_string_config[] = "Self-powered";
  35263. +static const char fsg_string_interface[] = "Mass Storage";
  35264. +
  35265. +
  35266. +#include "storage_common.c"
  35267. +
  35268. +
  35269. +MODULE_DESCRIPTION(DRIVER_DESC);
  35270. +MODULE_AUTHOR("Alan Stern");
  35271. +MODULE_LICENSE("Dual BSD/GPL");
  35272. +
  35273. +/*
  35274. + * This driver assumes self-powered hardware and has no way for users to
  35275. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  35276. + * and endpoint addresses.
  35277. + */
  35278. +
  35279. +
  35280. +/*-------------------------------------------------------------------------*/
  35281. +
  35282. +
  35283. +/* Encapsulate the module parameter settings */
  35284. +
  35285. +static struct {
  35286. + char *file[FSG_MAX_LUNS];
  35287. + char *serial;
  35288. + bool ro[FSG_MAX_LUNS];
  35289. + bool nofua[FSG_MAX_LUNS];
  35290. + unsigned int num_filenames;
  35291. + unsigned int num_ros;
  35292. + unsigned int num_nofuas;
  35293. + unsigned int nluns;
  35294. +
  35295. + bool removable;
  35296. + bool can_stall;
  35297. + bool cdrom;
  35298. +
  35299. + char *transport_parm;
  35300. + char *protocol_parm;
  35301. + unsigned short vendor;
  35302. + unsigned short product;
  35303. + unsigned short release;
  35304. + unsigned int buflen;
  35305. +
  35306. + int transport_type;
  35307. + char *transport_name;
  35308. + int protocol_type;
  35309. + char *protocol_name;
  35310. +
  35311. +} mod_data = { // Default values
  35312. + .transport_parm = "BBB",
  35313. + .protocol_parm = "SCSI",
  35314. + .removable = 0,
  35315. + .can_stall = 1,
  35316. + .cdrom = 0,
  35317. + .vendor = FSG_VENDOR_ID,
  35318. + .product = FSG_PRODUCT_ID,
  35319. + .release = 0xffff, // Use controller chip type
  35320. + .buflen = 16384,
  35321. + };
  35322. +
  35323. +
  35324. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  35325. + S_IRUGO);
  35326. +MODULE_PARM_DESC(file, "names of backing files or devices");
  35327. +
  35328. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  35329. +MODULE_PARM_DESC(serial, "USB serial number");
  35330. +
  35331. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  35332. +MODULE_PARM_DESC(ro, "true to force read-only");
  35333. +
  35334. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  35335. + S_IRUGO);
  35336. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  35337. +
  35338. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  35339. +MODULE_PARM_DESC(luns, "number of LUNs");
  35340. +
  35341. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  35342. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  35343. +
  35344. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  35345. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  35346. +
  35347. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  35348. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  35349. +
  35350. +/* In the non-TEST version, only the module parameters listed above
  35351. + * are available. */
  35352. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35353. +
  35354. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  35355. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  35356. +
  35357. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  35358. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  35359. + "8070, or SCSI)");
  35360. +
  35361. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  35362. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  35363. +
  35364. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  35365. +MODULE_PARM_DESC(product, "USB Product ID");
  35366. +
  35367. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  35368. +MODULE_PARM_DESC(release, "USB release number");
  35369. +
  35370. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  35371. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  35372. +
  35373. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35374. +
  35375. +
  35376. +/*
  35377. + * These definitions will permit the compiler to avoid generating code for
  35378. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  35379. + * can recognize when a test of a constant expression yields a dead code
  35380. + * path.
  35381. + */
  35382. +
  35383. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35384. +
  35385. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  35386. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  35387. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  35388. +
  35389. +#else
  35390. +
  35391. +#define transport_is_bbb() 1
  35392. +#define transport_is_cbi() 0
  35393. +#define protocol_is_scsi() 1
  35394. +
  35395. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35396. +
  35397. +
  35398. +/*-------------------------------------------------------------------------*/
  35399. +
  35400. +
  35401. +struct fsg_dev {
  35402. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  35403. + spinlock_t lock;
  35404. + struct usb_gadget *gadget;
  35405. +
  35406. + /* filesem protects: backing files in use */
  35407. + struct rw_semaphore filesem;
  35408. +
  35409. + /* reference counting: wait until all LUNs are released */
  35410. + struct kref ref;
  35411. +
  35412. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  35413. + struct usb_request *ep0req; // For control responses
  35414. + unsigned int ep0_req_tag;
  35415. + const char *ep0req_name;
  35416. +
  35417. + struct usb_request *intreq; // For interrupt responses
  35418. + int intreq_busy;
  35419. + struct fsg_buffhd *intr_buffhd;
  35420. +
  35421. + unsigned int bulk_out_maxpacket;
  35422. + enum fsg_state state; // For exception handling
  35423. + unsigned int exception_req_tag;
  35424. +
  35425. + u8 config, new_config;
  35426. +
  35427. + unsigned int running : 1;
  35428. + unsigned int bulk_in_enabled : 1;
  35429. + unsigned int bulk_out_enabled : 1;
  35430. + unsigned int intr_in_enabled : 1;
  35431. + unsigned int phase_error : 1;
  35432. + unsigned int short_packet_received : 1;
  35433. + unsigned int bad_lun_okay : 1;
  35434. +
  35435. + unsigned long atomic_bitflags;
  35436. +#define REGISTERED 0
  35437. +#define IGNORE_BULK_OUT 1
  35438. +#define SUSPENDED 2
  35439. +
  35440. + struct usb_ep *bulk_in;
  35441. + struct usb_ep *bulk_out;
  35442. + struct usb_ep *intr_in;
  35443. +
  35444. + struct fsg_buffhd *next_buffhd_to_fill;
  35445. + struct fsg_buffhd *next_buffhd_to_drain;
  35446. +
  35447. + int thread_wakeup_needed;
  35448. + struct completion thread_notifier;
  35449. + struct task_struct *thread_task;
  35450. +
  35451. + int cmnd_size;
  35452. + u8 cmnd[MAX_COMMAND_SIZE];
  35453. + enum data_direction data_dir;
  35454. + u32 data_size;
  35455. + u32 data_size_from_cmnd;
  35456. + u32 tag;
  35457. + unsigned int lun;
  35458. + u32 residue;
  35459. + u32 usb_amount_left;
  35460. +
  35461. + /* The CB protocol offers no way for a host to know when a command
  35462. + * has completed. As a result the next command may arrive early,
  35463. + * and we will still have to handle it. For that reason we need
  35464. + * a buffer to store new commands when using CB (or CBI, which
  35465. + * does not oblige a host to wait for command completion either). */
  35466. + int cbbuf_cmnd_size;
  35467. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  35468. +
  35469. + unsigned int nluns;
  35470. + struct fsg_lun *luns;
  35471. + struct fsg_lun *curlun;
  35472. + /* Must be the last entry */
  35473. + struct fsg_buffhd buffhds[];
  35474. +};
  35475. +
  35476. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  35477. +
  35478. +static int exception_in_progress(struct fsg_dev *fsg)
  35479. +{
  35480. + return (fsg->state > FSG_STATE_IDLE);
  35481. +}
  35482. +
  35483. +/* Make bulk-out requests be divisible by the maxpacket size */
  35484. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  35485. + struct fsg_buffhd *bh, unsigned int length)
  35486. +{
  35487. + unsigned int rem;
  35488. +
  35489. + bh->bulk_out_intended_length = length;
  35490. + rem = length % fsg->bulk_out_maxpacket;
  35491. + if (rem > 0)
  35492. + length += fsg->bulk_out_maxpacket - rem;
  35493. + bh->outreq->length = length;
  35494. +}
  35495. +
  35496. +static struct fsg_dev *the_fsg;
  35497. +static struct usb_gadget_driver fsg_driver;
  35498. +
  35499. +
  35500. +/*-------------------------------------------------------------------------*/
  35501. +
  35502. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  35503. +{
  35504. + const char *name;
  35505. +
  35506. + if (ep == fsg->bulk_in)
  35507. + name = "bulk-in";
  35508. + else if (ep == fsg->bulk_out)
  35509. + name = "bulk-out";
  35510. + else
  35511. + name = ep->name;
  35512. + DBG(fsg, "%s set halt\n", name);
  35513. + return usb_ep_set_halt(ep);
  35514. +}
  35515. +
  35516. +
  35517. +/*-------------------------------------------------------------------------*/
  35518. +
  35519. +/*
  35520. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  35521. + * descriptors are built on demand. Also the (static) config and interface
  35522. + * descriptors are adjusted during fsg_bind().
  35523. + */
  35524. +
  35525. +/* There is only one configuration. */
  35526. +#define CONFIG_VALUE 1
  35527. +
  35528. +static struct usb_device_descriptor
  35529. +device_desc = {
  35530. + .bLength = sizeof device_desc,
  35531. + .bDescriptorType = USB_DT_DEVICE,
  35532. +
  35533. + .bcdUSB = cpu_to_le16(0x0200),
  35534. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  35535. +
  35536. + /* The next three values can be overridden by module parameters */
  35537. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  35538. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  35539. + .bcdDevice = cpu_to_le16(0xffff),
  35540. +
  35541. + .iManufacturer = FSG_STRING_MANUFACTURER,
  35542. + .iProduct = FSG_STRING_PRODUCT,
  35543. + .iSerialNumber = FSG_STRING_SERIAL,
  35544. + .bNumConfigurations = 1,
  35545. +};
  35546. +
  35547. +static struct usb_config_descriptor
  35548. +config_desc = {
  35549. + .bLength = sizeof config_desc,
  35550. + .bDescriptorType = USB_DT_CONFIG,
  35551. +
  35552. + /* wTotalLength computed by usb_gadget_config_buf() */
  35553. + .bNumInterfaces = 1,
  35554. + .bConfigurationValue = CONFIG_VALUE,
  35555. + .iConfiguration = FSG_STRING_CONFIG,
  35556. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  35557. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  35558. +};
  35559. +
  35560. +
  35561. +static struct usb_qualifier_descriptor
  35562. +dev_qualifier = {
  35563. + .bLength = sizeof dev_qualifier,
  35564. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  35565. +
  35566. + .bcdUSB = cpu_to_le16(0x0200),
  35567. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  35568. +
  35569. + .bNumConfigurations = 1,
  35570. +};
  35571. +
  35572. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  35573. +{
  35574. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  35575. + buf += USB_DT_BOS_SIZE;
  35576. +
  35577. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  35578. + buf += USB_DT_USB_EXT_CAP_SIZE;
  35579. +
  35580. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  35581. +
  35582. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  35583. + + USB_DT_USB_EXT_CAP_SIZE;
  35584. +}
  35585. +
  35586. +/*
  35587. + * Config descriptors must agree with the code that sets configurations
  35588. + * and with code managing interfaces and their altsettings. They must
  35589. + * also handle different speeds and other-speed requests.
  35590. + */
  35591. +static int populate_config_buf(struct usb_gadget *gadget,
  35592. + u8 *buf, u8 type, unsigned index)
  35593. +{
  35594. + enum usb_device_speed speed = gadget->speed;
  35595. + int len;
  35596. + const struct usb_descriptor_header **function;
  35597. +
  35598. + if (index > 0)
  35599. + return -EINVAL;
  35600. +
  35601. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  35602. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  35603. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  35604. + ? (const struct usb_descriptor_header **)fsg_hs_function
  35605. + : (const struct usb_descriptor_header **)fsg_fs_function;
  35606. +
  35607. + /* for now, don't advertise srp-only devices */
  35608. + if (!gadget_is_otg(gadget))
  35609. + function++;
  35610. +
  35611. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  35612. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  35613. + return len;
  35614. +}
  35615. +
  35616. +
  35617. +/*-------------------------------------------------------------------------*/
  35618. +
  35619. +/* These routines may be called in process context or in_irq */
  35620. +
  35621. +/* Caller must hold fsg->lock */
  35622. +static void wakeup_thread(struct fsg_dev *fsg)
  35623. +{
  35624. + /* Tell the main thread that something has happened */
  35625. + fsg->thread_wakeup_needed = 1;
  35626. + if (fsg->thread_task)
  35627. + wake_up_process(fsg->thread_task);
  35628. +}
  35629. +
  35630. +
  35631. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  35632. +{
  35633. + unsigned long flags;
  35634. +
  35635. + /* Do nothing if a higher-priority exception is already in progress.
  35636. + * If a lower-or-equal priority exception is in progress, preempt it
  35637. + * and notify the main thread by sending it a signal. */
  35638. + spin_lock_irqsave(&fsg->lock, flags);
  35639. + if (fsg->state <= new_state) {
  35640. + fsg->exception_req_tag = fsg->ep0_req_tag;
  35641. + fsg->state = new_state;
  35642. + if (fsg->thread_task)
  35643. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  35644. + fsg->thread_task);
  35645. + }
  35646. + spin_unlock_irqrestore(&fsg->lock, flags);
  35647. +}
  35648. +
  35649. +
  35650. +/*-------------------------------------------------------------------------*/
  35651. +
  35652. +/* The disconnect callback and ep0 routines. These always run in_irq,
  35653. + * except that ep0_queue() is called in the main thread to acknowledge
  35654. + * completion of various requests: set config, set interface, and
  35655. + * Bulk-only device reset. */
  35656. +
  35657. +static void fsg_disconnect(struct usb_gadget *gadget)
  35658. +{
  35659. + struct fsg_dev *fsg = get_gadget_data(gadget);
  35660. +
  35661. + DBG(fsg, "disconnect or port reset\n");
  35662. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  35663. +}
  35664. +
  35665. +
  35666. +static int ep0_queue(struct fsg_dev *fsg)
  35667. +{
  35668. + int rc;
  35669. +
  35670. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  35671. + if (rc != 0 && rc != -ESHUTDOWN) {
  35672. +
  35673. + /* We can't do much more than wait for a reset */
  35674. + WARNING(fsg, "error in submission: %s --> %d\n",
  35675. + fsg->ep0->name, rc);
  35676. + }
  35677. + return rc;
  35678. +}
  35679. +
  35680. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  35681. +{
  35682. + struct fsg_dev *fsg = ep->driver_data;
  35683. +
  35684. + if (req->actual > 0)
  35685. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  35686. + if (req->status || req->actual != req->length)
  35687. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35688. + req->status, req->actual, req->length);
  35689. + if (req->status == -ECONNRESET) // Request was cancelled
  35690. + usb_ep_fifo_flush(ep);
  35691. +
  35692. + if (req->status == 0 && req->context)
  35693. + ((fsg_routine_t) (req->context))(fsg);
  35694. +}
  35695. +
  35696. +
  35697. +/*-------------------------------------------------------------------------*/
  35698. +
  35699. +/* Bulk and interrupt endpoint completion handlers.
  35700. + * These always run in_irq. */
  35701. +
  35702. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  35703. +{
  35704. + struct fsg_dev *fsg = ep->driver_data;
  35705. + struct fsg_buffhd *bh = req->context;
  35706. +
  35707. + if (req->status || req->actual != req->length)
  35708. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35709. + req->status, req->actual, req->length);
  35710. + if (req->status == -ECONNRESET) // Request was cancelled
  35711. + usb_ep_fifo_flush(ep);
  35712. +
  35713. + /* Hold the lock while we update the request and buffer states */
  35714. + smp_wmb();
  35715. + spin_lock(&fsg->lock);
  35716. + bh->inreq_busy = 0;
  35717. + bh->state = BUF_STATE_EMPTY;
  35718. + wakeup_thread(fsg);
  35719. + spin_unlock(&fsg->lock);
  35720. +}
  35721. +
  35722. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  35723. +{
  35724. + struct fsg_dev *fsg = ep->driver_data;
  35725. + struct fsg_buffhd *bh = req->context;
  35726. +
  35727. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  35728. + if (req->status || req->actual != bh->bulk_out_intended_length)
  35729. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35730. + req->status, req->actual,
  35731. + bh->bulk_out_intended_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->outreq_busy = 0;
  35739. + bh->state = BUF_STATE_FULL;
  35740. + wakeup_thread(fsg);
  35741. + spin_unlock(&fsg->lock);
  35742. +}
  35743. +
  35744. +
  35745. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35746. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  35747. +{
  35748. + struct fsg_dev *fsg = ep->driver_data;
  35749. + struct fsg_buffhd *bh = req->context;
  35750. +
  35751. + if (req->status || req->actual != req->length)
  35752. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35753. + req->status, req->actual, req->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. + fsg->intreq_busy = 0;
  35761. + bh->state = BUF_STATE_EMPTY;
  35762. + wakeup_thread(fsg);
  35763. + spin_unlock(&fsg->lock);
  35764. +}
  35765. +
  35766. +#else
  35767. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  35768. +{}
  35769. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35770. +
  35771. +
  35772. +/*-------------------------------------------------------------------------*/
  35773. +
  35774. +/* Ep0 class-specific handlers. These always run in_irq. */
  35775. +
  35776. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35777. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35778. +{
  35779. + struct usb_request *req = fsg->ep0req;
  35780. + static u8 cbi_reset_cmnd[6] = {
  35781. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  35782. +
  35783. + /* Error in command transfer? */
  35784. + if (req->status || req->length != req->actual ||
  35785. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  35786. +
  35787. + /* Not all controllers allow a protocol stall after
  35788. + * receiving control-out data, but we'll try anyway. */
  35789. + fsg_set_halt(fsg, fsg->ep0);
  35790. + return; // Wait for reset
  35791. + }
  35792. +
  35793. + /* Is it the special reset command? */
  35794. + if (req->actual >= sizeof cbi_reset_cmnd &&
  35795. + memcmp(req->buf, cbi_reset_cmnd,
  35796. + sizeof cbi_reset_cmnd) == 0) {
  35797. +
  35798. + /* Raise an exception to stop the current operation
  35799. + * and reinitialize our state. */
  35800. + DBG(fsg, "cbi reset request\n");
  35801. + raise_exception(fsg, FSG_STATE_RESET);
  35802. + return;
  35803. + }
  35804. +
  35805. + VDBG(fsg, "CB[I] accept device-specific command\n");
  35806. + spin_lock(&fsg->lock);
  35807. +
  35808. + /* Save the command for later */
  35809. + if (fsg->cbbuf_cmnd_size)
  35810. + WARNING(fsg, "CB[I] overwriting previous command\n");
  35811. + fsg->cbbuf_cmnd_size = req->actual;
  35812. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  35813. +
  35814. + wakeup_thread(fsg);
  35815. + spin_unlock(&fsg->lock);
  35816. +}
  35817. +
  35818. +#else
  35819. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35820. +{}
  35821. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35822. +
  35823. +
  35824. +static int class_setup_req(struct fsg_dev *fsg,
  35825. + const struct usb_ctrlrequest *ctrl)
  35826. +{
  35827. + struct usb_request *req = fsg->ep0req;
  35828. + int value = -EOPNOTSUPP;
  35829. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  35830. + u16 w_value = le16_to_cpu(ctrl->wValue);
  35831. + u16 w_length = le16_to_cpu(ctrl->wLength);
  35832. +
  35833. + if (!fsg->config)
  35834. + return value;
  35835. +
  35836. + /* Handle Bulk-only class-specific requests */
  35837. + if (transport_is_bbb()) {
  35838. + switch (ctrl->bRequest) {
  35839. +
  35840. + case US_BULK_RESET_REQUEST:
  35841. + if (ctrl->bRequestType != (USB_DIR_OUT |
  35842. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35843. + break;
  35844. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  35845. + value = -EDOM;
  35846. + break;
  35847. + }
  35848. +
  35849. + /* Raise an exception to stop the current operation
  35850. + * and reinitialize our state. */
  35851. + DBG(fsg, "bulk reset request\n");
  35852. + raise_exception(fsg, FSG_STATE_RESET);
  35853. + value = DELAYED_STATUS;
  35854. + break;
  35855. +
  35856. + case US_BULK_GET_MAX_LUN:
  35857. + if (ctrl->bRequestType != (USB_DIR_IN |
  35858. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35859. + break;
  35860. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  35861. + value = -EDOM;
  35862. + break;
  35863. + }
  35864. + VDBG(fsg, "get max LUN\n");
  35865. + *(u8 *) req->buf = fsg->nluns - 1;
  35866. + value = 1;
  35867. + break;
  35868. + }
  35869. + }
  35870. +
  35871. + /* Handle CBI class-specific requests */
  35872. + else {
  35873. + switch (ctrl->bRequest) {
  35874. +
  35875. + case USB_CBI_ADSC_REQUEST:
  35876. + if (ctrl->bRequestType != (USB_DIR_OUT |
  35877. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35878. + break;
  35879. + if (w_index != 0 || w_value != 0) {
  35880. + value = -EDOM;
  35881. + break;
  35882. + }
  35883. + if (w_length > MAX_COMMAND_SIZE) {
  35884. + value = -EOVERFLOW;
  35885. + break;
  35886. + }
  35887. + value = w_length;
  35888. + fsg->ep0req->context = received_cbi_adsc;
  35889. + break;
  35890. + }
  35891. + }
  35892. +
  35893. + if (value == -EOPNOTSUPP)
  35894. + VDBG(fsg,
  35895. + "unknown class-specific control req "
  35896. + "%02x.%02x v%04x i%04x l%u\n",
  35897. + ctrl->bRequestType, ctrl->bRequest,
  35898. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  35899. + return value;
  35900. +}
  35901. +
  35902. +
  35903. +/*-------------------------------------------------------------------------*/
  35904. +
  35905. +/* Ep0 standard request handlers. These always run in_irq. */
  35906. +
  35907. +static int standard_setup_req(struct fsg_dev *fsg,
  35908. + const struct usb_ctrlrequest *ctrl)
  35909. +{
  35910. + struct usb_request *req = fsg->ep0req;
  35911. + int value = -EOPNOTSUPP;
  35912. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  35913. + u16 w_value = le16_to_cpu(ctrl->wValue);
  35914. +
  35915. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  35916. + * but config change events will also reconfigure hardware. */
  35917. + switch (ctrl->bRequest) {
  35918. +
  35919. + case USB_REQ_GET_DESCRIPTOR:
  35920. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  35921. + USB_RECIP_DEVICE))
  35922. + break;
  35923. + switch (w_value >> 8) {
  35924. +
  35925. + case USB_DT_DEVICE:
  35926. + VDBG(fsg, "get device descriptor\n");
  35927. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  35928. + value = sizeof device_desc;
  35929. + memcpy(req->buf, &device_desc, value);
  35930. + break;
  35931. + case USB_DT_DEVICE_QUALIFIER:
  35932. + VDBG(fsg, "get device qualifier\n");
  35933. + if (!gadget_is_dualspeed(fsg->gadget) ||
  35934. + fsg->gadget->speed == USB_SPEED_SUPER)
  35935. + break;
  35936. + /*
  35937. + * Assume ep0 uses the same maxpacket value for both
  35938. + * speeds
  35939. + */
  35940. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  35941. + value = sizeof dev_qualifier;
  35942. + memcpy(req->buf, &dev_qualifier, value);
  35943. + break;
  35944. +
  35945. + case USB_DT_OTHER_SPEED_CONFIG:
  35946. + VDBG(fsg, "get other-speed config descriptor\n");
  35947. + if (!gadget_is_dualspeed(fsg->gadget) ||
  35948. + fsg->gadget->speed == USB_SPEED_SUPER)
  35949. + break;
  35950. + goto get_config;
  35951. + case USB_DT_CONFIG:
  35952. + VDBG(fsg, "get configuration descriptor\n");
  35953. +get_config:
  35954. + value = populate_config_buf(fsg->gadget,
  35955. + req->buf,
  35956. + w_value >> 8,
  35957. + w_value & 0xff);
  35958. + break;
  35959. +
  35960. + case USB_DT_STRING:
  35961. + VDBG(fsg, "get string descriptor\n");
  35962. +
  35963. + /* wIndex == language code */
  35964. + value = usb_gadget_get_string(&fsg_stringtab,
  35965. + w_value & 0xff, req->buf);
  35966. + break;
  35967. +
  35968. + case USB_DT_BOS:
  35969. + VDBG(fsg, "get bos descriptor\n");
  35970. +
  35971. + if (gadget_is_superspeed(fsg->gadget))
  35972. + value = populate_bos(fsg, req->buf);
  35973. + break;
  35974. + }
  35975. +
  35976. + break;
  35977. +
  35978. + /* One config, two speeds */
  35979. + case USB_REQ_SET_CONFIGURATION:
  35980. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  35981. + USB_RECIP_DEVICE))
  35982. + break;
  35983. + VDBG(fsg, "set configuration\n");
  35984. + if (w_value == CONFIG_VALUE || w_value == 0) {
  35985. + fsg->new_config = w_value;
  35986. +
  35987. + /* Raise an exception to wipe out previous transaction
  35988. + * state (queued bufs, etc) and set the new config. */
  35989. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  35990. + value = DELAYED_STATUS;
  35991. + }
  35992. + break;
  35993. + case USB_REQ_GET_CONFIGURATION:
  35994. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  35995. + USB_RECIP_DEVICE))
  35996. + break;
  35997. + VDBG(fsg, "get configuration\n");
  35998. + *(u8 *) req->buf = fsg->config;
  35999. + value = 1;
  36000. + break;
  36001. +
  36002. + case USB_REQ_SET_INTERFACE:
  36003. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  36004. + USB_RECIP_INTERFACE))
  36005. + break;
  36006. + if (fsg->config && w_index == 0) {
  36007. +
  36008. + /* Raise an exception to wipe out previous transaction
  36009. + * state (queued bufs, etc) and install the new
  36010. + * interface altsetting. */
  36011. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  36012. + value = DELAYED_STATUS;
  36013. + }
  36014. + break;
  36015. + case USB_REQ_GET_INTERFACE:
  36016. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  36017. + USB_RECIP_INTERFACE))
  36018. + break;
  36019. + if (!fsg->config)
  36020. + break;
  36021. + if (w_index != 0) {
  36022. + value = -EDOM;
  36023. + break;
  36024. + }
  36025. + VDBG(fsg, "get interface\n");
  36026. + *(u8 *) req->buf = 0;
  36027. + value = 1;
  36028. + break;
  36029. +
  36030. + default:
  36031. + VDBG(fsg,
  36032. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  36033. + ctrl->bRequestType, ctrl->bRequest,
  36034. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  36035. + }
  36036. +
  36037. + return value;
  36038. +}
  36039. +
  36040. +
  36041. +static int fsg_setup(struct usb_gadget *gadget,
  36042. + const struct usb_ctrlrequest *ctrl)
  36043. +{
  36044. + struct fsg_dev *fsg = get_gadget_data(gadget);
  36045. + int rc;
  36046. + int w_length = le16_to_cpu(ctrl->wLength);
  36047. +
  36048. + ++fsg->ep0_req_tag; // Record arrival of a new request
  36049. + fsg->ep0req->context = NULL;
  36050. + fsg->ep0req->length = 0;
  36051. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  36052. +
  36053. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  36054. + rc = class_setup_req(fsg, ctrl);
  36055. + else
  36056. + rc = standard_setup_req(fsg, ctrl);
  36057. +
  36058. + /* Respond with data/status or defer until later? */
  36059. + if (rc >= 0 && rc != DELAYED_STATUS) {
  36060. + rc = min(rc, w_length);
  36061. + fsg->ep0req->length = rc;
  36062. + fsg->ep0req->zero = rc < w_length;
  36063. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  36064. + "ep0-in" : "ep0-out");
  36065. + rc = ep0_queue(fsg);
  36066. + }
  36067. +
  36068. + /* Device either stalls (rc < 0) or reports success */
  36069. + return rc;
  36070. +}
  36071. +
  36072. +
  36073. +/*-------------------------------------------------------------------------*/
  36074. +
  36075. +/* All the following routines run in process context */
  36076. +
  36077. +
  36078. +/* Use this for bulk or interrupt transfers, not ep0 */
  36079. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  36080. + struct usb_request *req, int *pbusy,
  36081. + enum fsg_buffer_state *state)
  36082. +{
  36083. + int rc;
  36084. +
  36085. + if (ep == fsg->bulk_in)
  36086. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  36087. + else if (ep == fsg->intr_in)
  36088. + dump_msg(fsg, "intr-in", req->buf, req->length);
  36089. +
  36090. + spin_lock_irq(&fsg->lock);
  36091. + *pbusy = 1;
  36092. + *state = BUF_STATE_BUSY;
  36093. + spin_unlock_irq(&fsg->lock);
  36094. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  36095. + if (rc != 0) {
  36096. + *pbusy = 0;
  36097. + *state = BUF_STATE_EMPTY;
  36098. +
  36099. + /* We can't do much more than wait for a reset */
  36100. +
  36101. + /* Note: currently the net2280 driver fails zero-length
  36102. + * submissions if DMA is enabled. */
  36103. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  36104. + req->length == 0))
  36105. + WARNING(fsg, "error in submission: %s --> %d\n",
  36106. + ep->name, rc);
  36107. + }
  36108. +}
  36109. +
  36110. +
  36111. +static int sleep_thread(struct fsg_dev *fsg)
  36112. +{
  36113. + int rc = 0;
  36114. +
  36115. + /* Wait until a signal arrives or we are woken up */
  36116. + for (;;) {
  36117. + try_to_freeze();
  36118. + set_current_state(TASK_INTERRUPTIBLE);
  36119. + if (signal_pending(current)) {
  36120. + rc = -EINTR;
  36121. + break;
  36122. + }
  36123. + if (fsg->thread_wakeup_needed)
  36124. + break;
  36125. + schedule();
  36126. + }
  36127. + __set_current_state(TASK_RUNNING);
  36128. + fsg->thread_wakeup_needed = 0;
  36129. + return rc;
  36130. +}
  36131. +
  36132. +
  36133. +/*-------------------------------------------------------------------------*/
  36134. +
  36135. +static int do_read(struct fsg_dev *fsg)
  36136. +{
  36137. + struct fsg_lun *curlun = fsg->curlun;
  36138. + u32 lba;
  36139. + struct fsg_buffhd *bh;
  36140. + int rc;
  36141. + u32 amount_left;
  36142. + loff_t file_offset, file_offset_tmp;
  36143. + unsigned int amount;
  36144. + ssize_t nread;
  36145. +
  36146. + /* Get the starting Logical Block Address and check that it's
  36147. + * not too big */
  36148. + if (fsg->cmnd[0] == READ_6)
  36149. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  36150. + else {
  36151. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  36152. +
  36153. + /* We allow DPO (Disable Page Out = don't save data in the
  36154. + * cache) and FUA (Force Unit Access = don't read from the
  36155. + * cache), but we don't implement them. */
  36156. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  36157. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36158. + return -EINVAL;
  36159. + }
  36160. + }
  36161. + if (lba >= curlun->num_sectors) {
  36162. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36163. + return -EINVAL;
  36164. + }
  36165. + file_offset = ((loff_t) lba) << curlun->blkbits;
  36166. +
  36167. + /* Carry out the file reads */
  36168. + amount_left = fsg->data_size_from_cmnd;
  36169. + if (unlikely(amount_left == 0))
  36170. + return -EIO; // No default reply
  36171. +
  36172. + for (;;) {
  36173. +
  36174. + /* Figure out how much we need to read:
  36175. + * Try to read the remaining amount.
  36176. + * But don't read more than the buffer size.
  36177. + * And don't try to read past the end of the file.
  36178. + */
  36179. + amount = min((unsigned int) amount_left, mod_data.buflen);
  36180. + amount = min((loff_t) amount,
  36181. + curlun->file_length - file_offset);
  36182. +
  36183. + /* Wait for the next buffer to become available */
  36184. + bh = fsg->next_buffhd_to_fill;
  36185. + while (bh->state != BUF_STATE_EMPTY) {
  36186. + rc = sleep_thread(fsg);
  36187. + if (rc)
  36188. + return rc;
  36189. + }
  36190. +
  36191. + /* If we were asked to read past the end of file,
  36192. + * end with an empty buffer. */
  36193. + if (amount == 0) {
  36194. + curlun->sense_data =
  36195. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36196. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36197. + curlun->info_valid = 1;
  36198. + bh->inreq->length = 0;
  36199. + bh->state = BUF_STATE_FULL;
  36200. + break;
  36201. + }
  36202. +
  36203. + /* Perform the read */
  36204. + file_offset_tmp = file_offset;
  36205. + nread = vfs_read(curlun->filp,
  36206. + (char __user *) bh->buf,
  36207. + amount, &file_offset_tmp);
  36208. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  36209. + (unsigned long long) file_offset,
  36210. + (int) nread);
  36211. + if (signal_pending(current))
  36212. + return -EINTR;
  36213. +
  36214. + if (nread < 0) {
  36215. + LDBG(curlun, "error in file read: %d\n",
  36216. + (int) nread);
  36217. + nread = 0;
  36218. + } else if (nread < amount) {
  36219. + LDBG(curlun, "partial file read: %d/%u\n",
  36220. + (int) nread, amount);
  36221. + nread = round_down(nread, curlun->blksize);
  36222. + }
  36223. + file_offset += nread;
  36224. + amount_left -= nread;
  36225. + fsg->residue -= nread;
  36226. +
  36227. + /* Except at the end of the transfer, nread will be
  36228. + * equal to the buffer size, which is divisible by the
  36229. + * bulk-in maxpacket size.
  36230. + */
  36231. + bh->inreq->length = nread;
  36232. + bh->state = BUF_STATE_FULL;
  36233. +
  36234. + /* If an error occurred, report it and its position */
  36235. + if (nread < amount) {
  36236. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  36237. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36238. + curlun->info_valid = 1;
  36239. + break;
  36240. + }
  36241. +
  36242. + if (amount_left == 0)
  36243. + break; // No more left to read
  36244. +
  36245. + /* Send this buffer and go read some more */
  36246. + bh->inreq->zero = 0;
  36247. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  36248. + &bh->inreq_busy, &bh->state);
  36249. + fsg->next_buffhd_to_fill = bh->next;
  36250. + }
  36251. +
  36252. + return -EIO; // No default reply
  36253. +}
  36254. +
  36255. +
  36256. +/*-------------------------------------------------------------------------*/
  36257. +
  36258. +static int do_write(struct fsg_dev *fsg)
  36259. +{
  36260. + struct fsg_lun *curlun = fsg->curlun;
  36261. + u32 lba;
  36262. + struct fsg_buffhd *bh;
  36263. + int get_some_more;
  36264. + u32 amount_left_to_req, amount_left_to_write;
  36265. + loff_t usb_offset, file_offset, file_offset_tmp;
  36266. + unsigned int amount;
  36267. + ssize_t nwritten;
  36268. + int rc;
  36269. +
  36270. + if (curlun->ro) {
  36271. + curlun->sense_data = SS_WRITE_PROTECTED;
  36272. + return -EINVAL;
  36273. + }
  36274. + spin_lock(&curlun->filp->f_lock);
  36275. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  36276. + spin_unlock(&curlun->filp->f_lock);
  36277. +
  36278. + /* Get the starting Logical Block Address and check that it's
  36279. + * not too big */
  36280. + if (fsg->cmnd[0] == WRITE_6)
  36281. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  36282. + else {
  36283. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  36284. +
  36285. + /* We allow DPO (Disable Page Out = don't save data in the
  36286. + * cache) and FUA (Force Unit Access = write directly to the
  36287. + * medium). We don't implement DPO; we implement FUA by
  36288. + * performing synchronous output. */
  36289. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  36290. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36291. + return -EINVAL;
  36292. + }
  36293. + /* FUA */
  36294. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  36295. + spin_lock(&curlun->filp->f_lock);
  36296. + curlun->filp->f_flags |= O_DSYNC;
  36297. + spin_unlock(&curlun->filp->f_lock);
  36298. + }
  36299. + }
  36300. + if (lba >= curlun->num_sectors) {
  36301. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36302. + return -EINVAL;
  36303. + }
  36304. +
  36305. + /* Carry out the file writes */
  36306. + get_some_more = 1;
  36307. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  36308. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  36309. +
  36310. + while (amount_left_to_write > 0) {
  36311. +
  36312. + /* Queue a request for more data from the host */
  36313. + bh = fsg->next_buffhd_to_fill;
  36314. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  36315. +
  36316. + /* Figure out how much we want to get:
  36317. + * Try to get the remaining amount,
  36318. + * but not more than the buffer size.
  36319. + */
  36320. + amount = min(amount_left_to_req, mod_data.buflen);
  36321. +
  36322. + /* Beyond the end of the backing file? */
  36323. + if (usb_offset >= curlun->file_length) {
  36324. + get_some_more = 0;
  36325. + curlun->sense_data =
  36326. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36327. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  36328. + curlun->info_valid = 1;
  36329. + continue;
  36330. + }
  36331. +
  36332. + /* Get the next buffer */
  36333. + usb_offset += amount;
  36334. + fsg->usb_amount_left -= amount;
  36335. + amount_left_to_req -= amount;
  36336. + if (amount_left_to_req == 0)
  36337. + get_some_more = 0;
  36338. +
  36339. + /* Except at the end of the transfer, amount will be
  36340. + * equal to the buffer size, which is divisible by
  36341. + * the bulk-out maxpacket size.
  36342. + */
  36343. + set_bulk_out_req_length(fsg, bh, amount);
  36344. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  36345. + &bh->outreq_busy, &bh->state);
  36346. + fsg->next_buffhd_to_fill = bh->next;
  36347. + continue;
  36348. + }
  36349. +
  36350. + /* Write the received data to the backing file */
  36351. + bh = fsg->next_buffhd_to_drain;
  36352. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  36353. + break; // We stopped early
  36354. + if (bh->state == BUF_STATE_FULL) {
  36355. + smp_rmb();
  36356. + fsg->next_buffhd_to_drain = bh->next;
  36357. + bh->state = BUF_STATE_EMPTY;
  36358. +
  36359. + /* Did something go wrong with the transfer? */
  36360. + if (bh->outreq->status != 0) {
  36361. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  36362. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36363. + curlun->info_valid = 1;
  36364. + break;
  36365. + }
  36366. +
  36367. + amount = bh->outreq->actual;
  36368. + if (curlun->file_length - file_offset < amount) {
  36369. + LERROR(curlun,
  36370. + "write %u @ %llu beyond end %llu\n",
  36371. + amount, (unsigned long long) file_offset,
  36372. + (unsigned long long) curlun->file_length);
  36373. + amount = curlun->file_length - file_offset;
  36374. + }
  36375. +
  36376. + /* Don't accept excess data. The spec doesn't say
  36377. + * what to do in this case. We'll ignore the error.
  36378. + */
  36379. + amount = min(amount, bh->bulk_out_intended_length);
  36380. +
  36381. + /* Don't write a partial block */
  36382. + amount = round_down(amount, curlun->blksize);
  36383. + if (amount == 0)
  36384. + goto empty_write;
  36385. +
  36386. + /* Perform the write */
  36387. + file_offset_tmp = file_offset;
  36388. + nwritten = vfs_write(curlun->filp,
  36389. + (char __user *) bh->buf,
  36390. + amount, &file_offset_tmp);
  36391. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  36392. + (unsigned long long) file_offset,
  36393. + (int) nwritten);
  36394. + if (signal_pending(current))
  36395. + return -EINTR; // Interrupted!
  36396. +
  36397. + if (nwritten < 0) {
  36398. + LDBG(curlun, "error in file write: %d\n",
  36399. + (int) nwritten);
  36400. + nwritten = 0;
  36401. + } else if (nwritten < amount) {
  36402. + LDBG(curlun, "partial file write: %d/%u\n",
  36403. + (int) nwritten, amount);
  36404. + nwritten = round_down(nwritten, curlun->blksize);
  36405. + }
  36406. + file_offset += nwritten;
  36407. + amount_left_to_write -= nwritten;
  36408. + fsg->residue -= nwritten;
  36409. +
  36410. + /* If an error occurred, report it and its position */
  36411. + if (nwritten < amount) {
  36412. + curlun->sense_data = SS_WRITE_ERROR;
  36413. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36414. + curlun->info_valid = 1;
  36415. + break;
  36416. + }
  36417. +
  36418. + empty_write:
  36419. + /* Did the host decide to stop early? */
  36420. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  36421. + fsg->short_packet_received = 1;
  36422. + break;
  36423. + }
  36424. + continue;
  36425. + }
  36426. +
  36427. + /* Wait for something to happen */
  36428. + rc = sleep_thread(fsg);
  36429. + if (rc)
  36430. + return rc;
  36431. + }
  36432. +
  36433. + return -EIO; // No default reply
  36434. +}
  36435. +
  36436. +
  36437. +/*-------------------------------------------------------------------------*/
  36438. +
  36439. +static int do_synchronize_cache(struct fsg_dev *fsg)
  36440. +{
  36441. + struct fsg_lun *curlun = fsg->curlun;
  36442. + int rc;
  36443. +
  36444. + /* We ignore the requested LBA and write out all file's
  36445. + * dirty data buffers. */
  36446. + rc = fsg_lun_fsync_sub(curlun);
  36447. + if (rc)
  36448. + curlun->sense_data = SS_WRITE_ERROR;
  36449. + return 0;
  36450. +}
  36451. +
  36452. +
  36453. +/*-------------------------------------------------------------------------*/
  36454. +
  36455. +static void invalidate_sub(struct fsg_lun *curlun)
  36456. +{
  36457. + struct file *filp = curlun->filp;
  36458. + struct inode *inode = filp->f_path.dentry->d_inode;
  36459. + unsigned long rc;
  36460. +
  36461. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  36462. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  36463. +}
  36464. +
  36465. +static int do_verify(struct fsg_dev *fsg)
  36466. +{
  36467. + struct fsg_lun *curlun = fsg->curlun;
  36468. + u32 lba;
  36469. + u32 verification_length;
  36470. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  36471. + loff_t file_offset, file_offset_tmp;
  36472. + u32 amount_left;
  36473. + unsigned int amount;
  36474. + ssize_t nread;
  36475. +
  36476. + /* Get the starting Logical Block Address and check that it's
  36477. + * not too big */
  36478. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  36479. + if (lba >= curlun->num_sectors) {
  36480. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36481. + return -EINVAL;
  36482. + }
  36483. +
  36484. + /* We allow DPO (Disable Page Out = don't save data in the
  36485. + * cache) but we don't implement it. */
  36486. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  36487. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36488. + return -EINVAL;
  36489. + }
  36490. +
  36491. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  36492. + if (unlikely(verification_length == 0))
  36493. + return -EIO; // No default reply
  36494. +
  36495. + /* Prepare to carry out the file verify */
  36496. + amount_left = verification_length << curlun->blkbits;
  36497. + file_offset = ((loff_t) lba) << curlun->blkbits;
  36498. +
  36499. + /* Write out all the dirty buffers before invalidating them */
  36500. + fsg_lun_fsync_sub(curlun);
  36501. + if (signal_pending(current))
  36502. + return -EINTR;
  36503. +
  36504. + invalidate_sub(curlun);
  36505. + if (signal_pending(current))
  36506. + return -EINTR;
  36507. +
  36508. + /* Just try to read the requested blocks */
  36509. + while (amount_left > 0) {
  36510. +
  36511. + /* Figure out how much we need to read:
  36512. + * Try to read the remaining amount, but not more than
  36513. + * the buffer size.
  36514. + * And don't try to read past the end of the file.
  36515. + */
  36516. + amount = min((unsigned int) amount_left, mod_data.buflen);
  36517. + amount = min((loff_t) amount,
  36518. + curlun->file_length - file_offset);
  36519. + if (amount == 0) {
  36520. + curlun->sense_data =
  36521. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36522. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36523. + curlun->info_valid = 1;
  36524. + break;
  36525. + }
  36526. +
  36527. + /* Perform the read */
  36528. + file_offset_tmp = file_offset;
  36529. + nread = vfs_read(curlun->filp,
  36530. + (char __user *) bh->buf,
  36531. + amount, &file_offset_tmp);
  36532. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  36533. + (unsigned long long) file_offset,
  36534. + (int) nread);
  36535. + if (signal_pending(current))
  36536. + return -EINTR;
  36537. +
  36538. + if (nread < 0) {
  36539. + LDBG(curlun, "error in file verify: %d\n",
  36540. + (int) nread);
  36541. + nread = 0;
  36542. + } else if (nread < amount) {
  36543. + LDBG(curlun, "partial file verify: %d/%u\n",
  36544. + (int) nread, amount);
  36545. + nread = round_down(nread, curlun->blksize);
  36546. + }
  36547. + if (nread == 0) {
  36548. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  36549. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36550. + curlun->info_valid = 1;
  36551. + break;
  36552. + }
  36553. + file_offset += nread;
  36554. + amount_left -= nread;
  36555. + }
  36556. + return 0;
  36557. +}
  36558. +
  36559. +
  36560. +/*-------------------------------------------------------------------------*/
  36561. +
  36562. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36563. +{
  36564. + u8 *buf = (u8 *) bh->buf;
  36565. +
  36566. + static char vendor_id[] = "Linux ";
  36567. + static char product_disk_id[] = "File-Stor Gadget";
  36568. + static char product_cdrom_id[] = "File-CD Gadget ";
  36569. +
  36570. + if (!fsg->curlun) { // Unsupported LUNs are okay
  36571. + fsg->bad_lun_okay = 1;
  36572. + memset(buf, 0, 36);
  36573. + buf[0] = 0x7f; // Unsupported, no device-type
  36574. + buf[4] = 31; // Additional length
  36575. + return 36;
  36576. + }
  36577. +
  36578. + memset(buf, 0, 8);
  36579. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  36580. + if (mod_data.removable)
  36581. + buf[1] = 0x80;
  36582. + buf[2] = 2; // ANSI SCSI level 2
  36583. + buf[3] = 2; // SCSI-2 INQUIRY data format
  36584. + buf[4] = 31; // Additional length
  36585. + // No special options
  36586. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  36587. + (mod_data.cdrom ? product_cdrom_id :
  36588. + product_disk_id),
  36589. + mod_data.release);
  36590. + return 36;
  36591. +}
  36592. +
  36593. +
  36594. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36595. +{
  36596. + struct fsg_lun *curlun = fsg->curlun;
  36597. + u8 *buf = (u8 *) bh->buf;
  36598. + u32 sd, sdinfo;
  36599. + int valid;
  36600. +
  36601. + /*
  36602. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  36603. + *
  36604. + * If a REQUEST SENSE command is received from an initiator
  36605. + * with a pending unit attention condition (before the target
  36606. + * generates the contingent allegiance condition), then the
  36607. + * target shall either:
  36608. + * a) report any pending sense data and preserve the unit
  36609. + * attention condition on the logical unit, or,
  36610. + * b) report the unit attention condition, may discard any
  36611. + * pending sense data, and clear the unit attention
  36612. + * condition on the logical unit for that initiator.
  36613. + *
  36614. + * FSG normally uses option a); enable this code to use option b).
  36615. + */
  36616. +#if 0
  36617. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  36618. + curlun->sense_data = curlun->unit_attention_data;
  36619. + curlun->unit_attention_data = SS_NO_SENSE;
  36620. + }
  36621. +#endif
  36622. +
  36623. + if (!curlun) { // Unsupported LUNs are okay
  36624. + fsg->bad_lun_okay = 1;
  36625. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  36626. + sdinfo = 0;
  36627. + valid = 0;
  36628. + } else {
  36629. + sd = curlun->sense_data;
  36630. + sdinfo = curlun->sense_data_info;
  36631. + valid = curlun->info_valid << 7;
  36632. + curlun->sense_data = SS_NO_SENSE;
  36633. + curlun->sense_data_info = 0;
  36634. + curlun->info_valid = 0;
  36635. + }
  36636. +
  36637. + memset(buf, 0, 18);
  36638. + buf[0] = valid | 0x70; // Valid, current error
  36639. + buf[2] = SK(sd);
  36640. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  36641. + buf[7] = 18 - 8; // Additional sense length
  36642. + buf[12] = ASC(sd);
  36643. + buf[13] = ASCQ(sd);
  36644. + return 18;
  36645. +}
  36646. +
  36647. +
  36648. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36649. +{
  36650. + struct fsg_lun *curlun = fsg->curlun;
  36651. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  36652. + int pmi = fsg->cmnd[8];
  36653. + u8 *buf = (u8 *) bh->buf;
  36654. +
  36655. + /* Check the PMI and LBA fields */
  36656. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  36657. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36658. + return -EINVAL;
  36659. + }
  36660. +
  36661. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  36662. + /* Max logical block */
  36663. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  36664. + return 8;
  36665. +}
  36666. +
  36667. +
  36668. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36669. +{
  36670. + struct fsg_lun *curlun = fsg->curlun;
  36671. + int msf = fsg->cmnd[1] & 0x02;
  36672. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  36673. + u8 *buf = (u8 *) bh->buf;
  36674. +
  36675. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  36676. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36677. + return -EINVAL;
  36678. + }
  36679. + if (lba >= curlun->num_sectors) {
  36680. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36681. + return -EINVAL;
  36682. + }
  36683. +
  36684. + memset(buf, 0, 8);
  36685. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  36686. + store_cdrom_address(&buf[4], msf, lba);
  36687. + return 8;
  36688. +}
  36689. +
  36690. +
  36691. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36692. +{
  36693. + struct fsg_lun *curlun = fsg->curlun;
  36694. + int msf = fsg->cmnd[1] & 0x02;
  36695. + int start_track = fsg->cmnd[6];
  36696. + u8 *buf = (u8 *) bh->buf;
  36697. +
  36698. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  36699. + start_track > 1) {
  36700. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36701. + return -EINVAL;
  36702. + }
  36703. +
  36704. + memset(buf, 0, 20);
  36705. + buf[1] = (20-2); /* TOC data length */
  36706. + buf[2] = 1; /* First track number */
  36707. + buf[3] = 1; /* Last track number */
  36708. + buf[5] = 0x16; /* Data track, copying allowed */
  36709. + buf[6] = 0x01; /* Only track is number 1 */
  36710. + store_cdrom_address(&buf[8], msf, 0);
  36711. +
  36712. + buf[13] = 0x16; /* Lead-out track is data */
  36713. + buf[14] = 0xAA; /* Lead-out track number */
  36714. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  36715. + return 20;
  36716. +}
  36717. +
  36718. +
  36719. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36720. +{
  36721. + struct fsg_lun *curlun = fsg->curlun;
  36722. + int mscmnd = fsg->cmnd[0];
  36723. + u8 *buf = (u8 *) bh->buf;
  36724. + u8 *buf0 = buf;
  36725. + int pc, page_code;
  36726. + int changeable_values, all_pages;
  36727. + int valid_page = 0;
  36728. + int len, limit;
  36729. +
  36730. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  36731. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36732. + return -EINVAL;
  36733. + }
  36734. + pc = fsg->cmnd[2] >> 6;
  36735. + page_code = fsg->cmnd[2] & 0x3f;
  36736. + if (pc == 3) {
  36737. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  36738. + return -EINVAL;
  36739. + }
  36740. + changeable_values = (pc == 1);
  36741. + all_pages = (page_code == 0x3f);
  36742. +
  36743. + /* Write the mode parameter header. Fixed values are: default
  36744. + * medium type, no cache control (DPOFUA), and no block descriptors.
  36745. + * The only variable value is the WriteProtect bit. We will fill in
  36746. + * the mode data length later. */
  36747. + memset(buf, 0, 8);
  36748. + if (mscmnd == MODE_SENSE) {
  36749. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  36750. + buf += 4;
  36751. + limit = 255;
  36752. + } else { // MODE_SENSE_10
  36753. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  36754. + buf += 8;
  36755. + limit = 65535; // Should really be mod_data.buflen
  36756. + }
  36757. +
  36758. + /* No block descriptors */
  36759. +
  36760. + /* The mode pages, in numerical order. The only page we support
  36761. + * is the Caching page. */
  36762. + if (page_code == 0x08 || all_pages) {
  36763. + valid_page = 1;
  36764. + buf[0] = 0x08; // Page code
  36765. + buf[1] = 10; // Page length
  36766. + memset(buf+2, 0, 10); // None of the fields are changeable
  36767. +
  36768. + if (!changeable_values) {
  36769. + buf[2] = 0x04; // Write cache enable,
  36770. + // Read cache not disabled
  36771. + // No cache retention priorities
  36772. + put_unaligned_be16(0xffff, &buf[4]);
  36773. + /* Don't disable prefetch */
  36774. + /* Minimum prefetch = 0 */
  36775. + put_unaligned_be16(0xffff, &buf[8]);
  36776. + /* Maximum prefetch */
  36777. + put_unaligned_be16(0xffff, &buf[10]);
  36778. + /* Maximum prefetch ceiling */
  36779. + }
  36780. + buf += 12;
  36781. + }
  36782. +
  36783. + /* Check that a valid page was requested and the mode data length
  36784. + * isn't too long. */
  36785. + len = buf - buf0;
  36786. + if (!valid_page || len > limit) {
  36787. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36788. + return -EINVAL;
  36789. + }
  36790. +
  36791. + /* Store the mode data length */
  36792. + if (mscmnd == MODE_SENSE)
  36793. + buf0[0] = len - 1;
  36794. + else
  36795. + put_unaligned_be16(len - 2, buf0);
  36796. + return len;
  36797. +}
  36798. +
  36799. +
  36800. +static int do_start_stop(struct fsg_dev *fsg)
  36801. +{
  36802. + struct fsg_lun *curlun = fsg->curlun;
  36803. + int loej, start;
  36804. +
  36805. + if (!mod_data.removable) {
  36806. + curlun->sense_data = SS_INVALID_COMMAND;
  36807. + return -EINVAL;
  36808. + }
  36809. +
  36810. + // int immed = fsg->cmnd[1] & 0x01;
  36811. + loej = fsg->cmnd[4] & 0x02;
  36812. + start = fsg->cmnd[4] & 0x01;
  36813. +
  36814. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  36815. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  36816. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  36817. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36818. + return -EINVAL;
  36819. + }
  36820. +
  36821. + if (!start) {
  36822. +
  36823. + /* Are we allowed to unload the media? */
  36824. + if (curlun->prevent_medium_removal) {
  36825. + LDBG(curlun, "unload attempt prevented\n");
  36826. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  36827. + return -EINVAL;
  36828. + }
  36829. + if (loej) { // Simulate an unload/eject
  36830. + up_read(&fsg->filesem);
  36831. + down_write(&fsg->filesem);
  36832. + fsg_lun_close(curlun);
  36833. + up_write(&fsg->filesem);
  36834. + down_read(&fsg->filesem);
  36835. + }
  36836. + } else {
  36837. +
  36838. + /* Our emulation doesn't support mounting; the medium is
  36839. + * available for use as soon as it is loaded. */
  36840. + if (!fsg_lun_is_open(curlun)) {
  36841. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  36842. + return -EINVAL;
  36843. + }
  36844. + }
  36845. +#endif
  36846. + return 0;
  36847. +}
  36848. +
  36849. +
  36850. +static int do_prevent_allow(struct fsg_dev *fsg)
  36851. +{
  36852. + struct fsg_lun *curlun = fsg->curlun;
  36853. + int prevent;
  36854. +
  36855. + if (!mod_data.removable) {
  36856. + curlun->sense_data = SS_INVALID_COMMAND;
  36857. + return -EINVAL;
  36858. + }
  36859. +
  36860. + prevent = fsg->cmnd[4] & 0x01;
  36861. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  36862. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36863. + return -EINVAL;
  36864. + }
  36865. +
  36866. + if (curlun->prevent_medium_removal && !prevent)
  36867. + fsg_lun_fsync_sub(curlun);
  36868. + curlun->prevent_medium_removal = prevent;
  36869. + return 0;
  36870. +}
  36871. +
  36872. +
  36873. +static int do_read_format_capacities(struct fsg_dev *fsg,
  36874. + struct fsg_buffhd *bh)
  36875. +{
  36876. + struct fsg_lun *curlun = fsg->curlun;
  36877. + u8 *buf = (u8 *) bh->buf;
  36878. +
  36879. + buf[0] = buf[1] = buf[2] = 0;
  36880. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  36881. + buf += 4;
  36882. +
  36883. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  36884. + /* Number of blocks */
  36885. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  36886. + buf[4] = 0x02; /* Current capacity */
  36887. + return 12;
  36888. +}
  36889. +
  36890. +
  36891. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36892. +{
  36893. + struct fsg_lun *curlun = fsg->curlun;
  36894. +
  36895. + /* We don't support MODE SELECT */
  36896. + curlun->sense_data = SS_INVALID_COMMAND;
  36897. + return -EINVAL;
  36898. +}
  36899. +
  36900. +
  36901. +/*-------------------------------------------------------------------------*/
  36902. +
  36903. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  36904. +{
  36905. + int rc;
  36906. +
  36907. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  36908. + if (rc == -EAGAIN)
  36909. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  36910. + while (rc != 0) {
  36911. + if (rc != -EAGAIN) {
  36912. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  36913. + rc = 0;
  36914. + break;
  36915. + }
  36916. +
  36917. + /* Wait for a short time and then try again */
  36918. + if (msleep_interruptible(100) != 0)
  36919. + return -EINTR;
  36920. + rc = usb_ep_set_halt(fsg->bulk_in);
  36921. + }
  36922. + return rc;
  36923. +}
  36924. +
  36925. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  36926. +{
  36927. + int rc;
  36928. +
  36929. + DBG(fsg, "bulk-in set wedge\n");
  36930. + rc = usb_ep_set_wedge(fsg->bulk_in);
  36931. + if (rc == -EAGAIN)
  36932. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  36933. + while (rc != 0) {
  36934. + if (rc != -EAGAIN) {
  36935. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  36936. + rc = 0;
  36937. + break;
  36938. + }
  36939. +
  36940. + /* Wait for a short time and then try again */
  36941. + if (msleep_interruptible(100) != 0)
  36942. + return -EINTR;
  36943. + rc = usb_ep_set_wedge(fsg->bulk_in);
  36944. + }
  36945. + return rc;
  36946. +}
  36947. +
  36948. +static int throw_away_data(struct fsg_dev *fsg)
  36949. +{
  36950. + struct fsg_buffhd *bh;
  36951. + u32 amount;
  36952. + int rc;
  36953. +
  36954. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  36955. + fsg->usb_amount_left > 0) {
  36956. +
  36957. + /* Throw away the data in a filled buffer */
  36958. + if (bh->state == BUF_STATE_FULL) {
  36959. + smp_rmb();
  36960. + bh->state = BUF_STATE_EMPTY;
  36961. + fsg->next_buffhd_to_drain = bh->next;
  36962. +
  36963. + /* A short packet or an error ends everything */
  36964. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  36965. + bh->outreq->status != 0) {
  36966. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  36967. + return -EINTR;
  36968. + }
  36969. + continue;
  36970. + }
  36971. +
  36972. + /* Try to submit another request if we need one */
  36973. + bh = fsg->next_buffhd_to_fill;
  36974. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  36975. + amount = min(fsg->usb_amount_left,
  36976. + (u32) mod_data.buflen);
  36977. +
  36978. + /* Except at the end of the transfer, amount will be
  36979. + * equal to the buffer size, which is divisible by
  36980. + * the bulk-out maxpacket size.
  36981. + */
  36982. + set_bulk_out_req_length(fsg, bh, amount);
  36983. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  36984. + &bh->outreq_busy, &bh->state);
  36985. + fsg->next_buffhd_to_fill = bh->next;
  36986. + fsg->usb_amount_left -= amount;
  36987. + continue;
  36988. + }
  36989. +
  36990. + /* Otherwise wait for something to happen */
  36991. + rc = sleep_thread(fsg);
  36992. + if (rc)
  36993. + return rc;
  36994. + }
  36995. + return 0;
  36996. +}
  36997. +
  36998. +
  36999. +static int finish_reply(struct fsg_dev *fsg)
  37000. +{
  37001. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  37002. + int rc = 0;
  37003. +
  37004. + switch (fsg->data_dir) {
  37005. + case DATA_DIR_NONE:
  37006. + break; // Nothing to send
  37007. +
  37008. + /* If we don't know whether the host wants to read or write,
  37009. + * this must be CB or CBI with an unknown command. We mustn't
  37010. + * try to send or receive any data. So stall both bulk pipes
  37011. + * if we can and wait for a reset. */
  37012. + case DATA_DIR_UNKNOWN:
  37013. + if (mod_data.can_stall) {
  37014. + fsg_set_halt(fsg, fsg->bulk_out);
  37015. + rc = halt_bulk_in_endpoint(fsg);
  37016. + }
  37017. + break;
  37018. +
  37019. + /* All but the last buffer of data must have already been sent */
  37020. + case DATA_DIR_TO_HOST:
  37021. + if (fsg->data_size == 0)
  37022. + ; // Nothing to send
  37023. +
  37024. + /* If there's no residue, simply send the last buffer */
  37025. + else if (fsg->residue == 0) {
  37026. + bh->inreq->zero = 0;
  37027. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37028. + &bh->inreq_busy, &bh->state);
  37029. + fsg->next_buffhd_to_fill = bh->next;
  37030. + }
  37031. +
  37032. + /* There is a residue. For CB and CBI, simply mark the end
  37033. + * of the data with a short packet. However, if we are
  37034. + * allowed to stall, there was no data at all (residue ==
  37035. + * data_size), and the command failed (invalid LUN or
  37036. + * sense data is set), then halt the bulk-in endpoint
  37037. + * instead. */
  37038. + else if (!transport_is_bbb()) {
  37039. + if (mod_data.can_stall &&
  37040. + fsg->residue == fsg->data_size &&
  37041. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  37042. + bh->state = BUF_STATE_EMPTY;
  37043. + rc = halt_bulk_in_endpoint(fsg);
  37044. + } else {
  37045. + bh->inreq->zero = 1;
  37046. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37047. + &bh->inreq_busy, &bh->state);
  37048. + fsg->next_buffhd_to_fill = bh->next;
  37049. + }
  37050. + }
  37051. +
  37052. + /*
  37053. + * For Bulk-only, mark the end of the data with a short
  37054. + * packet. If we are allowed to stall, halt the bulk-in
  37055. + * endpoint. (Note: This violates the Bulk-Only Transport
  37056. + * specification, which requires us to pad the data if we
  37057. + * don't halt the endpoint. Presumably nobody will mind.)
  37058. + */
  37059. + else {
  37060. + bh->inreq->zero = 1;
  37061. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37062. + &bh->inreq_busy, &bh->state);
  37063. + fsg->next_buffhd_to_fill = bh->next;
  37064. + if (mod_data.can_stall)
  37065. + rc = halt_bulk_in_endpoint(fsg);
  37066. + }
  37067. + break;
  37068. +
  37069. + /* We have processed all we want from the data the host has sent.
  37070. + * There may still be outstanding bulk-out requests. */
  37071. + case DATA_DIR_FROM_HOST:
  37072. + if (fsg->residue == 0)
  37073. + ; // Nothing to receive
  37074. +
  37075. + /* Did the host stop sending unexpectedly early? */
  37076. + else if (fsg->short_packet_received) {
  37077. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  37078. + rc = -EINTR;
  37079. + }
  37080. +
  37081. + /* We haven't processed all the incoming data. Even though
  37082. + * we may be allowed to stall, doing so would cause a race.
  37083. + * The controller may already have ACK'ed all the remaining
  37084. + * bulk-out packets, in which case the host wouldn't see a
  37085. + * STALL. Not realizing the endpoint was halted, it wouldn't
  37086. + * clear the halt -- leading to problems later on. */
  37087. +#if 0
  37088. + else if (mod_data.can_stall) {
  37089. + fsg_set_halt(fsg, fsg->bulk_out);
  37090. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  37091. + rc = -EINTR;
  37092. + }
  37093. +#endif
  37094. +
  37095. + /* We can't stall. Read in the excess data and throw it
  37096. + * all away. */
  37097. + else
  37098. + rc = throw_away_data(fsg);
  37099. + break;
  37100. + }
  37101. + return rc;
  37102. +}
  37103. +
  37104. +
  37105. +static int send_status(struct fsg_dev *fsg)
  37106. +{
  37107. + struct fsg_lun *curlun = fsg->curlun;
  37108. + struct fsg_buffhd *bh;
  37109. + int rc;
  37110. + u8 status = US_BULK_STAT_OK;
  37111. + u32 sd, sdinfo = 0;
  37112. +
  37113. + /* Wait for the next buffer to become available */
  37114. + bh = fsg->next_buffhd_to_fill;
  37115. + while (bh->state != BUF_STATE_EMPTY) {
  37116. + rc = sleep_thread(fsg);
  37117. + if (rc)
  37118. + return rc;
  37119. + }
  37120. +
  37121. + if (curlun) {
  37122. + sd = curlun->sense_data;
  37123. + sdinfo = curlun->sense_data_info;
  37124. + } else if (fsg->bad_lun_okay)
  37125. + sd = SS_NO_SENSE;
  37126. + else
  37127. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  37128. +
  37129. + if (fsg->phase_error) {
  37130. + DBG(fsg, "sending phase-error status\n");
  37131. + status = US_BULK_STAT_PHASE;
  37132. + sd = SS_INVALID_COMMAND;
  37133. + } else if (sd != SS_NO_SENSE) {
  37134. + DBG(fsg, "sending command-failure status\n");
  37135. + status = US_BULK_STAT_FAIL;
  37136. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  37137. + " info x%x\n",
  37138. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  37139. + }
  37140. +
  37141. + if (transport_is_bbb()) {
  37142. + struct bulk_cs_wrap *csw = bh->buf;
  37143. +
  37144. + /* Store and send the Bulk-only CSW */
  37145. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  37146. + csw->Tag = fsg->tag;
  37147. + csw->Residue = cpu_to_le32(fsg->residue);
  37148. + csw->Status = status;
  37149. +
  37150. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  37151. + bh->inreq->zero = 0;
  37152. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37153. + &bh->inreq_busy, &bh->state);
  37154. +
  37155. + } else if (mod_data.transport_type == USB_PR_CB) {
  37156. +
  37157. + /* Control-Bulk transport has no status phase! */
  37158. + return 0;
  37159. +
  37160. + } else { // USB_PR_CBI
  37161. + struct interrupt_data *buf = bh->buf;
  37162. +
  37163. + /* Store and send the Interrupt data. UFI sends the ASC
  37164. + * and ASCQ bytes. Everything else sends a Type (which
  37165. + * is always 0) and the status Value. */
  37166. + if (mod_data.protocol_type == USB_SC_UFI) {
  37167. + buf->bType = ASC(sd);
  37168. + buf->bValue = ASCQ(sd);
  37169. + } else {
  37170. + buf->bType = 0;
  37171. + buf->bValue = status;
  37172. + }
  37173. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  37174. +
  37175. + fsg->intr_buffhd = bh; // Point to the right buffhd
  37176. + fsg->intreq->buf = bh->inreq->buf;
  37177. + fsg->intreq->context = bh;
  37178. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  37179. + &fsg->intreq_busy, &bh->state);
  37180. + }
  37181. +
  37182. + fsg->next_buffhd_to_fill = bh->next;
  37183. + return 0;
  37184. +}
  37185. +
  37186. +
  37187. +/*-------------------------------------------------------------------------*/
  37188. +
  37189. +/* Check whether the command is properly formed and whether its data size
  37190. + * and direction agree with the values we already have. */
  37191. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  37192. + enum data_direction data_dir, unsigned int mask,
  37193. + int needs_medium, const char *name)
  37194. +{
  37195. + int i;
  37196. + int lun = fsg->cmnd[1] >> 5;
  37197. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  37198. + char hdlen[20];
  37199. + struct fsg_lun *curlun;
  37200. +
  37201. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  37202. + * Transparent SCSI doesn't pad. */
  37203. + if (protocol_is_scsi())
  37204. + ;
  37205. +
  37206. + /* There's some disagreement as to whether RBC pads commands or not.
  37207. + * We'll play it safe and accept either form. */
  37208. + else if (mod_data.protocol_type == USB_SC_RBC) {
  37209. + if (fsg->cmnd_size == 12)
  37210. + cmnd_size = 12;
  37211. +
  37212. + /* All the other protocols pad to 12 bytes */
  37213. + } else
  37214. + cmnd_size = 12;
  37215. +
  37216. + hdlen[0] = 0;
  37217. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  37218. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  37219. + fsg->data_size);
  37220. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  37221. + name, cmnd_size, dirletter[(int) data_dir],
  37222. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  37223. +
  37224. + /* We can't reply at all until we know the correct data direction
  37225. + * and size. */
  37226. + if (fsg->data_size_from_cmnd == 0)
  37227. + data_dir = DATA_DIR_NONE;
  37228. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  37229. + fsg->data_dir = data_dir;
  37230. + fsg->data_size = fsg->data_size_from_cmnd;
  37231. +
  37232. + } else { // Bulk-only
  37233. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  37234. +
  37235. + /* Host data size < Device data size is a phase error.
  37236. + * Carry out the command, but only transfer as much
  37237. + * as we are allowed. */
  37238. + fsg->data_size_from_cmnd = fsg->data_size;
  37239. + fsg->phase_error = 1;
  37240. + }
  37241. + }
  37242. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  37243. +
  37244. + /* Conflicting data directions is a phase error */
  37245. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  37246. + fsg->phase_error = 1;
  37247. + return -EINVAL;
  37248. + }
  37249. +
  37250. + /* Verify the length of the command itself */
  37251. + if (cmnd_size != fsg->cmnd_size) {
  37252. +
  37253. + /* Special case workaround: There are plenty of buggy SCSI
  37254. + * implementations. Many have issues with cbw->Length
  37255. + * field passing a wrong command size. For those cases we
  37256. + * always try to work around the problem by using the length
  37257. + * sent by the host side provided it is at least as large
  37258. + * as the correct command length.
  37259. + * Examples of such cases would be MS-Windows, which issues
  37260. + * REQUEST SENSE with cbw->Length == 12 where it should
  37261. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  37262. + * REQUEST SENSE with cbw->Length == 10 where it should
  37263. + * be 6 as well.
  37264. + */
  37265. + if (cmnd_size <= fsg->cmnd_size) {
  37266. + DBG(fsg, "%s is buggy! Expected length %d "
  37267. + "but we got %d\n", name,
  37268. + cmnd_size, fsg->cmnd_size);
  37269. + cmnd_size = fsg->cmnd_size;
  37270. + } else {
  37271. + fsg->phase_error = 1;
  37272. + return -EINVAL;
  37273. + }
  37274. + }
  37275. +
  37276. + /* Check that the LUN values are consistent */
  37277. + if (transport_is_bbb()) {
  37278. + if (fsg->lun != lun)
  37279. + DBG(fsg, "using LUN %d from CBW, "
  37280. + "not LUN %d from CDB\n",
  37281. + fsg->lun, lun);
  37282. + }
  37283. +
  37284. + /* Check the LUN */
  37285. + curlun = fsg->curlun;
  37286. + if (curlun) {
  37287. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  37288. + curlun->sense_data = SS_NO_SENSE;
  37289. + curlun->sense_data_info = 0;
  37290. + curlun->info_valid = 0;
  37291. + }
  37292. + } else {
  37293. + fsg->bad_lun_okay = 0;
  37294. +
  37295. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  37296. + * to use unsupported LUNs; all others may not. */
  37297. + if (fsg->cmnd[0] != INQUIRY &&
  37298. + fsg->cmnd[0] != REQUEST_SENSE) {
  37299. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  37300. + return -EINVAL;
  37301. + }
  37302. + }
  37303. +
  37304. + /* If a unit attention condition exists, only INQUIRY and
  37305. + * REQUEST SENSE commands are allowed; anything else must fail. */
  37306. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  37307. + fsg->cmnd[0] != INQUIRY &&
  37308. + fsg->cmnd[0] != REQUEST_SENSE) {
  37309. + curlun->sense_data = curlun->unit_attention_data;
  37310. + curlun->unit_attention_data = SS_NO_SENSE;
  37311. + return -EINVAL;
  37312. + }
  37313. +
  37314. + /* Check that only command bytes listed in the mask are non-zero */
  37315. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  37316. + for (i = 1; i < cmnd_size; ++i) {
  37317. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  37318. + if (curlun)
  37319. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  37320. + return -EINVAL;
  37321. + }
  37322. + }
  37323. +
  37324. + /* If the medium isn't mounted and the command needs to access
  37325. + * it, return an error. */
  37326. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  37327. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  37328. + return -EINVAL;
  37329. + }
  37330. +
  37331. + return 0;
  37332. +}
  37333. +
  37334. +/* wrapper of check_command for data size in blocks handling */
  37335. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  37336. + enum data_direction data_dir, unsigned int mask,
  37337. + int needs_medium, const char *name)
  37338. +{
  37339. + if (fsg->curlun)
  37340. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  37341. + return check_command(fsg, cmnd_size, data_dir,
  37342. + mask, needs_medium, name);
  37343. +}
  37344. +
  37345. +static int do_scsi_command(struct fsg_dev *fsg)
  37346. +{
  37347. + struct fsg_buffhd *bh;
  37348. + int rc;
  37349. + int reply = -EINVAL;
  37350. + int i;
  37351. + static char unknown[16];
  37352. +
  37353. + dump_cdb(fsg);
  37354. +
  37355. + /* Wait for the next buffer to become available for data or status */
  37356. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  37357. + while (bh->state != BUF_STATE_EMPTY) {
  37358. + rc = sleep_thread(fsg);
  37359. + if (rc)
  37360. + return rc;
  37361. + }
  37362. + fsg->phase_error = 0;
  37363. + fsg->short_packet_received = 0;
  37364. +
  37365. + down_read(&fsg->filesem); // We're using the backing file
  37366. + switch (fsg->cmnd[0]) {
  37367. +
  37368. + case INQUIRY:
  37369. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37370. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  37371. + (1<<4), 0,
  37372. + "INQUIRY")) == 0)
  37373. + reply = do_inquiry(fsg, bh);
  37374. + break;
  37375. +
  37376. + case MODE_SELECT:
  37377. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37378. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  37379. + (1<<1) | (1<<4), 0,
  37380. + "MODE SELECT(6)")) == 0)
  37381. + reply = do_mode_select(fsg, bh);
  37382. + break;
  37383. +
  37384. + case MODE_SELECT_10:
  37385. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37386. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  37387. + (1<<1) | (3<<7), 0,
  37388. + "MODE SELECT(10)")) == 0)
  37389. + reply = do_mode_select(fsg, bh);
  37390. + break;
  37391. +
  37392. + case MODE_SENSE:
  37393. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37394. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  37395. + (1<<1) | (1<<2) | (1<<4), 0,
  37396. + "MODE SENSE(6)")) == 0)
  37397. + reply = do_mode_sense(fsg, bh);
  37398. + break;
  37399. +
  37400. + case MODE_SENSE_10:
  37401. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37402. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37403. + (1<<1) | (1<<2) | (3<<7), 0,
  37404. + "MODE SENSE(10)")) == 0)
  37405. + reply = do_mode_sense(fsg, bh);
  37406. + break;
  37407. +
  37408. + case ALLOW_MEDIUM_REMOVAL:
  37409. + fsg->data_size_from_cmnd = 0;
  37410. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  37411. + (1<<4), 0,
  37412. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  37413. + reply = do_prevent_allow(fsg);
  37414. + break;
  37415. +
  37416. + case READ_6:
  37417. + i = fsg->cmnd[4];
  37418. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  37419. + if ((reply = check_command_size_in_blocks(fsg, 6,
  37420. + DATA_DIR_TO_HOST,
  37421. + (7<<1) | (1<<4), 1,
  37422. + "READ(6)")) == 0)
  37423. + reply = do_read(fsg);
  37424. + break;
  37425. +
  37426. + case READ_10:
  37427. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37428. + if ((reply = check_command_size_in_blocks(fsg, 10,
  37429. + DATA_DIR_TO_HOST,
  37430. + (1<<1) | (0xf<<2) | (3<<7), 1,
  37431. + "READ(10)")) == 0)
  37432. + reply = do_read(fsg);
  37433. + break;
  37434. +
  37435. + case READ_12:
  37436. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  37437. + if ((reply = check_command_size_in_blocks(fsg, 12,
  37438. + DATA_DIR_TO_HOST,
  37439. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  37440. + "READ(12)")) == 0)
  37441. + reply = do_read(fsg);
  37442. + break;
  37443. +
  37444. + case READ_CAPACITY:
  37445. + fsg->data_size_from_cmnd = 8;
  37446. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37447. + (0xf<<2) | (1<<8), 1,
  37448. + "READ CAPACITY")) == 0)
  37449. + reply = do_read_capacity(fsg, bh);
  37450. + break;
  37451. +
  37452. + case READ_HEADER:
  37453. + if (!mod_data.cdrom)
  37454. + goto unknown_cmnd;
  37455. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37456. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37457. + (3<<7) | (0x1f<<1), 1,
  37458. + "READ HEADER")) == 0)
  37459. + reply = do_read_header(fsg, bh);
  37460. + break;
  37461. +
  37462. + case READ_TOC:
  37463. + if (!mod_data.cdrom)
  37464. + goto unknown_cmnd;
  37465. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37466. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37467. + (7<<6) | (1<<1), 1,
  37468. + "READ TOC")) == 0)
  37469. + reply = do_read_toc(fsg, bh);
  37470. + break;
  37471. +
  37472. + case READ_FORMAT_CAPACITIES:
  37473. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37474. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37475. + (3<<7), 1,
  37476. + "READ FORMAT CAPACITIES")) == 0)
  37477. + reply = do_read_format_capacities(fsg, bh);
  37478. + break;
  37479. +
  37480. + case REQUEST_SENSE:
  37481. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37482. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  37483. + (1<<4), 0,
  37484. + "REQUEST SENSE")) == 0)
  37485. + reply = do_request_sense(fsg, bh);
  37486. + break;
  37487. +
  37488. + case START_STOP:
  37489. + fsg->data_size_from_cmnd = 0;
  37490. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  37491. + (1<<1) | (1<<4), 0,
  37492. + "START-STOP UNIT")) == 0)
  37493. + reply = do_start_stop(fsg);
  37494. + break;
  37495. +
  37496. + case SYNCHRONIZE_CACHE:
  37497. + fsg->data_size_from_cmnd = 0;
  37498. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  37499. + (0xf<<2) | (3<<7), 1,
  37500. + "SYNCHRONIZE CACHE")) == 0)
  37501. + reply = do_synchronize_cache(fsg);
  37502. + break;
  37503. +
  37504. + case TEST_UNIT_READY:
  37505. + fsg->data_size_from_cmnd = 0;
  37506. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  37507. + 0, 1,
  37508. + "TEST UNIT READY");
  37509. + break;
  37510. +
  37511. + /* Although optional, this command is used by MS-Windows. We
  37512. + * support a minimal version: BytChk must be 0. */
  37513. + case VERIFY:
  37514. + fsg->data_size_from_cmnd = 0;
  37515. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  37516. + (1<<1) | (0xf<<2) | (3<<7), 1,
  37517. + "VERIFY")) == 0)
  37518. + reply = do_verify(fsg);
  37519. + break;
  37520. +
  37521. + case WRITE_6:
  37522. + i = fsg->cmnd[4];
  37523. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  37524. + if ((reply = check_command_size_in_blocks(fsg, 6,
  37525. + DATA_DIR_FROM_HOST,
  37526. + (7<<1) | (1<<4), 1,
  37527. + "WRITE(6)")) == 0)
  37528. + reply = do_write(fsg);
  37529. + break;
  37530. +
  37531. + case WRITE_10:
  37532. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37533. + if ((reply = check_command_size_in_blocks(fsg, 10,
  37534. + DATA_DIR_FROM_HOST,
  37535. + (1<<1) | (0xf<<2) | (3<<7), 1,
  37536. + "WRITE(10)")) == 0)
  37537. + reply = do_write(fsg);
  37538. + break;
  37539. +
  37540. + case WRITE_12:
  37541. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  37542. + if ((reply = check_command_size_in_blocks(fsg, 12,
  37543. + DATA_DIR_FROM_HOST,
  37544. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  37545. + "WRITE(12)")) == 0)
  37546. + reply = do_write(fsg);
  37547. + break;
  37548. +
  37549. + /* Some mandatory commands that we recognize but don't implement.
  37550. + * They don't mean much in this setting. It's left as an exercise
  37551. + * for anyone interested to implement RESERVE and RELEASE in terms
  37552. + * of Posix locks. */
  37553. + case FORMAT_UNIT:
  37554. + case RELEASE:
  37555. + case RESERVE:
  37556. + case SEND_DIAGNOSTIC:
  37557. + // Fall through
  37558. +
  37559. + default:
  37560. + unknown_cmnd:
  37561. + fsg->data_size_from_cmnd = 0;
  37562. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  37563. + if ((reply = check_command(fsg, fsg->cmnd_size,
  37564. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  37565. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  37566. + reply = -EINVAL;
  37567. + }
  37568. + break;
  37569. + }
  37570. + up_read(&fsg->filesem);
  37571. +
  37572. + if (reply == -EINTR || signal_pending(current))
  37573. + return -EINTR;
  37574. +
  37575. + /* Set up the single reply buffer for finish_reply() */
  37576. + if (reply == -EINVAL)
  37577. + reply = 0; // Error reply length
  37578. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  37579. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  37580. + bh->inreq->length = reply;
  37581. + bh->state = BUF_STATE_FULL;
  37582. + fsg->residue -= reply;
  37583. + } // Otherwise it's already set
  37584. +
  37585. + return 0;
  37586. +}
  37587. +
  37588. +
  37589. +/*-------------------------------------------------------------------------*/
  37590. +
  37591. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  37592. +{
  37593. + struct usb_request *req = bh->outreq;
  37594. + struct bulk_cb_wrap *cbw = req->buf;
  37595. +
  37596. + /* Was this a real packet? Should it be ignored? */
  37597. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  37598. + return -EINVAL;
  37599. +
  37600. + /* Is the CBW valid? */
  37601. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  37602. + cbw->Signature != cpu_to_le32(
  37603. + US_BULK_CB_SIGN)) {
  37604. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  37605. + req->actual,
  37606. + le32_to_cpu(cbw->Signature));
  37607. +
  37608. + /* The Bulk-only spec says we MUST stall the IN endpoint
  37609. + * (6.6.1), so it's unavoidable. It also says we must
  37610. + * retain this state until the next reset, but there's
  37611. + * no way to tell the controller driver it should ignore
  37612. + * Clear-Feature(HALT) requests.
  37613. + *
  37614. + * We aren't required to halt the OUT endpoint; instead
  37615. + * we can simply accept and discard any data received
  37616. + * until the next reset. */
  37617. + wedge_bulk_in_endpoint(fsg);
  37618. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  37619. + return -EINVAL;
  37620. + }
  37621. +
  37622. + /* Is the CBW meaningful? */
  37623. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  37624. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  37625. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  37626. + "cmdlen %u\n",
  37627. + cbw->Lun, cbw->Flags, cbw->Length);
  37628. +
  37629. + /* We can do anything we want here, so let's stall the
  37630. + * bulk pipes if we are allowed to. */
  37631. + if (mod_data.can_stall) {
  37632. + fsg_set_halt(fsg, fsg->bulk_out);
  37633. + halt_bulk_in_endpoint(fsg);
  37634. + }
  37635. + return -EINVAL;
  37636. + }
  37637. +
  37638. + /* Save the command for later */
  37639. + fsg->cmnd_size = cbw->Length;
  37640. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  37641. + if (cbw->Flags & US_BULK_FLAG_IN)
  37642. + fsg->data_dir = DATA_DIR_TO_HOST;
  37643. + else
  37644. + fsg->data_dir = DATA_DIR_FROM_HOST;
  37645. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  37646. + if (fsg->data_size == 0)
  37647. + fsg->data_dir = DATA_DIR_NONE;
  37648. + fsg->lun = cbw->Lun;
  37649. + fsg->tag = cbw->Tag;
  37650. + return 0;
  37651. +}
  37652. +
  37653. +
  37654. +static int get_next_command(struct fsg_dev *fsg)
  37655. +{
  37656. + struct fsg_buffhd *bh;
  37657. + int rc = 0;
  37658. +
  37659. + if (transport_is_bbb()) {
  37660. +
  37661. + /* Wait for the next buffer to become available */
  37662. + bh = fsg->next_buffhd_to_fill;
  37663. + while (bh->state != BUF_STATE_EMPTY) {
  37664. + rc = sleep_thread(fsg);
  37665. + if (rc)
  37666. + return rc;
  37667. + }
  37668. +
  37669. + /* Queue a request to read a Bulk-only CBW */
  37670. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  37671. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  37672. + &bh->outreq_busy, &bh->state);
  37673. +
  37674. + /* We will drain the buffer in software, which means we
  37675. + * can reuse it for the next filling. No need to advance
  37676. + * next_buffhd_to_fill. */
  37677. +
  37678. + /* Wait for the CBW to arrive */
  37679. + while (bh->state != BUF_STATE_FULL) {
  37680. + rc = sleep_thread(fsg);
  37681. + if (rc)
  37682. + return rc;
  37683. + }
  37684. + smp_rmb();
  37685. + rc = received_cbw(fsg, bh);
  37686. + bh->state = BUF_STATE_EMPTY;
  37687. +
  37688. + } else { // USB_PR_CB or USB_PR_CBI
  37689. +
  37690. + /* Wait for the next command to arrive */
  37691. + while (fsg->cbbuf_cmnd_size == 0) {
  37692. + rc = sleep_thread(fsg);
  37693. + if (rc)
  37694. + return rc;
  37695. + }
  37696. +
  37697. + /* Is the previous status interrupt request still busy?
  37698. + * The host is allowed to skip reading the status,
  37699. + * so we must cancel it. */
  37700. + if (fsg->intreq_busy)
  37701. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  37702. +
  37703. + /* Copy the command and mark the buffer empty */
  37704. + fsg->data_dir = DATA_DIR_UNKNOWN;
  37705. + spin_lock_irq(&fsg->lock);
  37706. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  37707. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  37708. + fsg->cbbuf_cmnd_size = 0;
  37709. + spin_unlock_irq(&fsg->lock);
  37710. +
  37711. + /* Use LUN from the command */
  37712. + fsg->lun = fsg->cmnd[1] >> 5;
  37713. + }
  37714. +
  37715. + /* Update current lun */
  37716. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  37717. + fsg->curlun = &fsg->luns[fsg->lun];
  37718. + else
  37719. + fsg->curlun = NULL;
  37720. +
  37721. + return rc;
  37722. +}
  37723. +
  37724. +
  37725. +/*-------------------------------------------------------------------------*/
  37726. +
  37727. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  37728. + const struct usb_endpoint_descriptor *d)
  37729. +{
  37730. + int rc;
  37731. +
  37732. + ep->driver_data = fsg;
  37733. + ep->desc = d;
  37734. + rc = usb_ep_enable(ep);
  37735. + if (rc)
  37736. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  37737. + return rc;
  37738. +}
  37739. +
  37740. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  37741. + struct usb_request **preq)
  37742. +{
  37743. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  37744. + if (*preq)
  37745. + return 0;
  37746. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  37747. + return -ENOMEM;
  37748. +}
  37749. +
  37750. +/*
  37751. + * Reset interface setting and re-init endpoint state (toggle etc).
  37752. + * Call with altsetting < 0 to disable the interface. The only other
  37753. + * available altsetting is 0, which enables the interface.
  37754. + */
  37755. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  37756. +{
  37757. + int rc = 0;
  37758. + int i;
  37759. + const struct usb_endpoint_descriptor *d;
  37760. +
  37761. + if (fsg->running)
  37762. + DBG(fsg, "reset interface\n");
  37763. +
  37764. +reset:
  37765. + /* Deallocate the requests */
  37766. + for (i = 0; i < fsg_num_buffers; ++i) {
  37767. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  37768. +
  37769. + if (bh->inreq) {
  37770. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  37771. + bh->inreq = NULL;
  37772. + }
  37773. + if (bh->outreq) {
  37774. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  37775. + bh->outreq = NULL;
  37776. + }
  37777. + }
  37778. + if (fsg->intreq) {
  37779. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  37780. + fsg->intreq = NULL;
  37781. + }
  37782. +
  37783. + /* Disable the endpoints */
  37784. + if (fsg->bulk_in_enabled) {
  37785. + usb_ep_disable(fsg->bulk_in);
  37786. + fsg->bulk_in_enabled = 0;
  37787. + }
  37788. + if (fsg->bulk_out_enabled) {
  37789. + usb_ep_disable(fsg->bulk_out);
  37790. + fsg->bulk_out_enabled = 0;
  37791. + }
  37792. + if (fsg->intr_in_enabled) {
  37793. + usb_ep_disable(fsg->intr_in);
  37794. + fsg->intr_in_enabled = 0;
  37795. + }
  37796. +
  37797. + fsg->running = 0;
  37798. + if (altsetting < 0 || rc != 0)
  37799. + return rc;
  37800. +
  37801. + DBG(fsg, "set interface %d\n", altsetting);
  37802. +
  37803. + /* Enable the endpoints */
  37804. + d = fsg_ep_desc(fsg->gadget,
  37805. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  37806. + &fsg_ss_bulk_in_desc);
  37807. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  37808. + goto reset;
  37809. + fsg->bulk_in_enabled = 1;
  37810. +
  37811. + d = fsg_ep_desc(fsg->gadget,
  37812. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  37813. + &fsg_ss_bulk_out_desc);
  37814. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  37815. + goto reset;
  37816. + fsg->bulk_out_enabled = 1;
  37817. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  37818. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  37819. +
  37820. + if (transport_is_cbi()) {
  37821. + d = fsg_ep_desc(fsg->gadget,
  37822. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  37823. + &fsg_ss_intr_in_desc);
  37824. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  37825. + goto reset;
  37826. + fsg->intr_in_enabled = 1;
  37827. + }
  37828. +
  37829. + /* Allocate the requests */
  37830. + for (i = 0; i < fsg_num_buffers; ++i) {
  37831. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  37832. +
  37833. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  37834. + goto reset;
  37835. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  37836. + goto reset;
  37837. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  37838. + bh->inreq->context = bh->outreq->context = bh;
  37839. + bh->inreq->complete = bulk_in_complete;
  37840. + bh->outreq->complete = bulk_out_complete;
  37841. + }
  37842. + if (transport_is_cbi()) {
  37843. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  37844. + goto reset;
  37845. + fsg->intreq->complete = intr_in_complete;
  37846. + }
  37847. +
  37848. + fsg->running = 1;
  37849. + for (i = 0; i < fsg->nluns; ++i)
  37850. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  37851. + return rc;
  37852. +}
  37853. +
  37854. +
  37855. +/*
  37856. + * Change our operational configuration. This code must agree with the code
  37857. + * that returns config descriptors, and with interface altsetting code.
  37858. + *
  37859. + * It's also responsible for power management interactions. Some
  37860. + * configurations might not work with our current power sources.
  37861. + * For now we just assume the gadget is always self-powered.
  37862. + */
  37863. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  37864. +{
  37865. + int rc = 0;
  37866. +
  37867. + /* Disable the single interface */
  37868. + if (fsg->config != 0) {
  37869. + DBG(fsg, "reset config\n");
  37870. + fsg->config = 0;
  37871. + rc = do_set_interface(fsg, -1);
  37872. + }
  37873. +
  37874. + /* Enable the interface */
  37875. + if (new_config != 0) {
  37876. + fsg->config = new_config;
  37877. + if ((rc = do_set_interface(fsg, 0)) != 0)
  37878. + fsg->config = 0; // Reset on errors
  37879. + else
  37880. + INFO(fsg, "%s config #%d\n",
  37881. + usb_speed_string(fsg->gadget->speed),
  37882. + fsg->config);
  37883. + }
  37884. + return rc;
  37885. +}
  37886. +
  37887. +
  37888. +/*-------------------------------------------------------------------------*/
  37889. +
  37890. +static void handle_exception(struct fsg_dev *fsg)
  37891. +{
  37892. + siginfo_t info;
  37893. + int sig;
  37894. + int i;
  37895. + int num_active;
  37896. + struct fsg_buffhd *bh;
  37897. + enum fsg_state old_state;
  37898. + u8 new_config;
  37899. + struct fsg_lun *curlun;
  37900. + unsigned int exception_req_tag;
  37901. + int rc;
  37902. +
  37903. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  37904. + * into a high-priority EXIT exception. */
  37905. + for (;;) {
  37906. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  37907. + if (!sig)
  37908. + break;
  37909. + if (sig != SIGUSR1) {
  37910. + if (fsg->state < FSG_STATE_EXIT)
  37911. + DBG(fsg, "Main thread exiting on signal\n");
  37912. + raise_exception(fsg, FSG_STATE_EXIT);
  37913. + }
  37914. + }
  37915. +
  37916. + /* Cancel all the pending transfers */
  37917. + if (fsg->intreq_busy)
  37918. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  37919. + for (i = 0; i < fsg_num_buffers; ++i) {
  37920. + bh = &fsg->buffhds[i];
  37921. + if (bh->inreq_busy)
  37922. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  37923. + if (bh->outreq_busy)
  37924. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  37925. + }
  37926. +
  37927. + /* Wait until everything is idle */
  37928. + for (;;) {
  37929. + num_active = fsg->intreq_busy;
  37930. + for (i = 0; i < fsg_num_buffers; ++i) {
  37931. + bh = &fsg->buffhds[i];
  37932. + num_active += bh->inreq_busy + bh->outreq_busy;
  37933. + }
  37934. + if (num_active == 0)
  37935. + break;
  37936. + if (sleep_thread(fsg))
  37937. + return;
  37938. + }
  37939. +
  37940. + /* Clear out the controller's fifos */
  37941. + if (fsg->bulk_in_enabled)
  37942. + usb_ep_fifo_flush(fsg->bulk_in);
  37943. + if (fsg->bulk_out_enabled)
  37944. + usb_ep_fifo_flush(fsg->bulk_out);
  37945. + if (fsg->intr_in_enabled)
  37946. + usb_ep_fifo_flush(fsg->intr_in);
  37947. +
  37948. + /* Reset the I/O buffer states and pointers, the SCSI
  37949. + * state, and the exception. Then invoke the handler. */
  37950. + spin_lock_irq(&fsg->lock);
  37951. +
  37952. + for (i = 0; i < fsg_num_buffers; ++i) {
  37953. + bh = &fsg->buffhds[i];
  37954. + bh->state = BUF_STATE_EMPTY;
  37955. + }
  37956. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  37957. + &fsg->buffhds[0];
  37958. +
  37959. + exception_req_tag = fsg->exception_req_tag;
  37960. + new_config = fsg->new_config;
  37961. + old_state = fsg->state;
  37962. +
  37963. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  37964. + fsg->state = FSG_STATE_STATUS_PHASE;
  37965. + else {
  37966. + for (i = 0; i < fsg->nluns; ++i) {
  37967. + curlun = &fsg->luns[i];
  37968. + curlun->prevent_medium_removal = 0;
  37969. + curlun->sense_data = curlun->unit_attention_data =
  37970. + SS_NO_SENSE;
  37971. + curlun->sense_data_info = 0;
  37972. + curlun->info_valid = 0;
  37973. + }
  37974. + fsg->state = FSG_STATE_IDLE;
  37975. + }
  37976. + spin_unlock_irq(&fsg->lock);
  37977. +
  37978. + /* Carry out any extra actions required for the exception */
  37979. + switch (old_state) {
  37980. + default:
  37981. + break;
  37982. +
  37983. + case FSG_STATE_ABORT_BULK_OUT:
  37984. + send_status(fsg);
  37985. + spin_lock_irq(&fsg->lock);
  37986. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  37987. + fsg->state = FSG_STATE_IDLE;
  37988. + spin_unlock_irq(&fsg->lock);
  37989. + break;
  37990. +
  37991. + case FSG_STATE_RESET:
  37992. + /* In case we were forced against our will to halt a
  37993. + * bulk endpoint, clear the halt now. (The SuperH UDC
  37994. + * requires this.) */
  37995. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  37996. + usb_ep_clear_halt(fsg->bulk_in);
  37997. +
  37998. + if (transport_is_bbb()) {
  37999. + if (fsg->ep0_req_tag == exception_req_tag)
  38000. + ep0_queue(fsg); // Complete the status stage
  38001. +
  38002. + } else if (transport_is_cbi())
  38003. + send_status(fsg); // Status by interrupt pipe
  38004. +
  38005. + /* Technically this should go here, but it would only be
  38006. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  38007. + * CONFIG_CHANGE cases. */
  38008. + // for (i = 0; i < fsg->nluns; ++i)
  38009. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  38010. + break;
  38011. +
  38012. + case FSG_STATE_INTERFACE_CHANGE:
  38013. + rc = do_set_interface(fsg, 0);
  38014. + if (fsg->ep0_req_tag != exception_req_tag)
  38015. + break;
  38016. + if (rc != 0) // STALL on errors
  38017. + fsg_set_halt(fsg, fsg->ep0);
  38018. + else // Complete the status stage
  38019. + ep0_queue(fsg);
  38020. + break;
  38021. +
  38022. + case FSG_STATE_CONFIG_CHANGE:
  38023. + rc = do_set_config(fsg, new_config);
  38024. + if (fsg->ep0_req_tag != exception_req_tag)
  38025. + break;
  38026. + if (rc != 0) // STALL on errors
  38027. + fsg_set_halt(fsg, fsg->ep0);
  38028. + else // Complete the status stage
  38029. + ep0_queue(fsg);
  38030. + break;
  38031. +
  38032. + case FSG_STATE_DISCONNECT:
  38033. + for (i = 0; i < fsg->nluns; ++i)
  38034. + fsg_lun_fsync_sub(fsg->luns + i);
  38035. + do_set_config(fsg, 0); // Unconfigured state
  38036. + break;
  38037. +
  38038. + case FSG_STATE_EXIT:
  38039. + case FSG_STATE_TERMINATED:
  38040. + do_set_config(fsg, 0); // Free resources
  38041. + spin_lock_irq(&fsg->lock);
  38042. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  38043. + spin_unlock_irq(&fsg->lock);
  38044. + break;
  38045. + }
  38046. +}
  38047. +
  38048. +
  38049. +/*-------------------------------------------------------------------------*/
  38050. +
  38051. +static int fsg_main_thread(void *fsg_)
  38052. +{
  38053. + struct fsg_dev *fsg = fsg_;
  38054. +
  38055. + /* Allow the thread to be killed by a signal, but set the signal mask
  38056. + * to block everything but INT, TERM, KILL, and USR1. */
  38057. + allow_signal(SIGINT);
  38058. + allow_signal(SIGTERM);
  38059. + allow_signal(SIGKILL);
  38060. + allow_signal(SIGUSR1);
  38061. +
  38062. + /* Allow the thread to be frozen */
  38063. + set_freezable();
  38064. +
  38065. + /* Arrange for userspace references to be interpreted as kernel
  38066. + * pointers. That way we can pass a kernel pointer to a routine
  38067. + * that expects a __user pointer and it will work okay. */
  38068. + set_fs(get_ds());
  38069. +
  38070. + /* The main loop */
  38071. + while (fsg->state != FSG_STATE_TERMINATED) {
  38072. + if (exception_in_progress(fsg) || signal_pending(current)) {
  38073. + handle_exception(fsg);
  38074. + continue;
  38075. + }
  38076. +
  38077. + if (!fsg->running) {
  38078. + sleep_thread(fsg);
  38079. + continue;
  38080. + }
  38081. +
  38082. + if (get_next_command(fsg))
  38083. + continue;
  38084. +
  38085. + spin_lock_irq(&fsg->lock);
  38086. + if (!exception_in_progress(fsg))
  38087. + fsg->state = FSG_STATE_DATA_PHASE;
  38088. + spin_unlock_irq(&fsg->lock);
  38089. +
  38090. + if (do_scsi_command(fsg) || finish_reply(fsg))
  38091. + continue;
  38092. +
  38093. + spin_lock_irq(&fsg->lock);
  38094. + if (!exception_in_progress(fsg))
  38095. + fsg->state = FSG_STATE_STATUS_PHASE;
  38096. + spin_unlock_irq(&fsg->lock);
  38097. +
  38098. + if (send_status(fsg))
  38099. + continue;
  38100. +
  38101. + spin_lock_irq(&fsg->lock);
  38102. + if (!exception_in_progress(fsg))
  38103. + fsg->state = FSG_STATE_IDLE;
  38104. + spin_unlock_irq(&fsg->lock);
  38105. + }
  38106. +
  38107. + spin_lock_irq(&fsg->lock);
  38108. + fsg->thread_task = NULL;
  38109. + spin_unlock_irq(&fsg->lock);
  38110. +
  38111. + /* If we are exiting because of a signal, unregister the
  38112. + * gadget driver. */
  38113. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  38114. + usb_gadget_unregister_driver(&fsg_driver);
  38115. +
  38116. + /* Let the unbind and cleanup routines know the thread has exited */
  38117. + complete_and_exit(&fsg->thread_notifier, 0);
  38118. +}
  38119. +
  38120. +
  38121. +/*-------------------------------------------------------------------------*/
  38122. +
  38123. +
  38124. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  38125. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  38126. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  38127. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  38128. +
  38129. +
  38130. +/*-------------------------------------------------------------------------*/
  38131. +
  38132. +static void fsg_release(struct kref *ref)
  38133. +{
  38134. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  38135. +
  38136. + kfree(fsg->luns);
  38137. + kfree(fsg);
  38138. +}
  38139. +
  38140. +static void lun_release(struct device *dev)
  38141. +{
  38142. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  38143. + struct fsg_dev *fsg =
  38144. + container_of(filesem, struct fsg_dev, filesem);
  38145. +
  38146. + kref_put(&fsg->ref, fsg_release);
  38147. +}
  38148. +
  38149. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  38150. +{
  38151. + struct fsg_dev *fsg = get_gadget_data(gadget);
  38152. + int i;
  38153. + struct fsg_lun *curlun;
  38154. + struct usb_request *req = fsg->ep0req;
  38155. +
  38156. + DBG(fsg, "unbind\n");
  38157. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  38158. +
  38159. + /* If the thread isn't already dead, tell it to exit now */
  38160. + if (fsg->state != FSG_STATE_TERMINATED) {
  38161. + raise_exception(fsg, FSG_STATE_EXIT);
  38162. + wait_for_completion(&fsg->thread_notifier);
  38163. +
  38164. + /* The cleanup routine waits for this completion also */
  38165. + complete(&fsg->thread_notifier);
  38166. + }
  38167. +
  38168. + /* Unregister the sysfs attribute files and the LUNs */
  38169. + for (i = 0; i < fsg->nluns; ++i) {
  38170. + curlun = &fsg->luns[i];
  38171. + if (curlun->registered) {
  38172. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  38173. + device_remove_file(&curlun->dev, &dev_attr_ro);
  38174. + device_remove_file(&curlun->dev, &dev_attr_file);
  38175. + fsg_lun_close(curlun);
  38176. + device_unregister(&curlun->dev);
  38177. + curlun->registered = 0;
  38178. + }
  38179. + }
  38180. +
  38181. + /* Free the data buffers */
  38182. + for (i = 0; i < fsg_num_buffers; ++i)
  38183. + kfree(fsg->buffhds[i].buf);
  38184. +
  38185. + /* Free the request and buffer for endpoint 0 */
  38186. + if (req) {
  38187. + kfree(req->buf);
  38188. + usb_ep_free_request(fsg->ep0, req);
  38189. + }
  38190. +
  38191. + set_gadget_data(gadget, NULL);
  38192. +}
  38193. +
  38194. +
  38195. +static int __init check_parameters(struct fsg_dev *fsg)
  38196. +{
  38197. + int prot;
  38198. + int gcnum;
  38199. +
  38200. + /* Store the default values */
  38201. + mod_data.transport_type = USB_PR_BULK;
  38202. + mod_data.transport_name = "Bulk-only";
  38203. + mod_data.protocol_type = USB_SC_SCSI;
  38204. + mod_data.protocol_name = "Transparent SCSI";
  38205. +
  38206. + /* Some peripheral controllers are known not to be able to
  38207. + * halt bulk endpoints correctly. If one of them is present,
  38208. + * disable stalls.
  38209. + */
  38210. + if (gadget_is_at91(fsg->gadget))
  38211. + mod_data.can_stall = 0;
  38212. +
  38213. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  38214. + gcnum = usb_gadget_controller_number(fsg->gadget);
  38215. + if (gcnum >= 0)
  38216. + mod_data.release = 0x0300 + gcnum;
  38217. + else {
  38218. + WARNING(fsg, "controller '%s' not recognized\n",
  38219. + fsg->gadget->name);
  38220. + mod_data.release = 0x0399;
  38221. + }
  38222. + }
  38223. +
  38224. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  38225. +
  38226. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  38227. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  38228. + ; // Use default setting
  38229. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  38230. + mod_data.transport_type = USB_PR_CB;
  38231. + mod_data.transport_name = "Control-Bulk";
  38232. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  38233. + mod_data.transport_type = USB_PR_CBI;
  38234. + mod_data.transport_name = "Control-Bulk-Interrupt";
  38235. + } else {
  38236. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  38237. + return -EINVAL;
  38238. + }
  38239. +
  38240. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  38241. + prot == USB_SC_SCSI) {
  38242. + ; // Use default setting
  38243. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  38244. + prot == USB_SC_RBC) {
  38245. + mod_data.protocol_type = USB_SC_RBC;
  38246. + mod_data.protocol_name = "RBC";
  38247. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  38248. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  38249. + prot == USB_SC_8020) {
  38250. + mod_data.protocol_type = USB_SC_8020;
  38251. + mod_data.protocol_name = "8020i (ATAPI)";
  38252. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  38253. + prot == USB_SC_QIC) {
  38254. + mod_data.protocol_type = USB_SC_QIC;
  38255. + mod_data.protocol_name = "QIC-157";
  38256. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  38257. + prot == USB_SC_UFI) {
  38258. + mod_data.protocol_type = USB_SC_UFI;
  38259. + mod_data.protocol_name = "UFI";
  38260. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  38261. + prot == USB_SC_8070) {
  38262. + mod_data.protocol_type = USB_SC_8070;
  38263. + mod_data.protocol_name = "8070i";
  38264. + } else {
  38265. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  38266. + return -EINVAL;
  38267. + }
  38268. +
  38269. + mod_data.buflen &= PAGE_CACHE_MASK;
  38270. + if (mod_data.buflen <= 0) {
  38271. + ERROR(fsg, "invalid buflen\n");
  38272. + return -ETOOSMALL;
  38273. + }
  38274. +
  38275. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  38276. +
  38277. + /* Serial string handling.
  38278. + * On a real device, the serial string would be loaded
  38279. + * from permanent storage. */
  38280. + if (mod_data.serial) {
  38281. + const char *ch;
  38282. + unsigned len = 0;
  38283. +
  38284. + /* Sanity check :
  38285. + * The CB[I] specification limits the serial string to
  38286. + * 12 uppercase hexadecimal characters.
  38287. + * BBB need at least 12 uppercase hexadecimal characters,
  38288. + * with a maximum of 126. */
  38289. + for (ch = mod_data.serial; *ch; ++ch) {
  38290. + ++len;
  38291. + if ((*ch < '0' || *ch > '9') &&
  38292. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  38293. + WARNING(fsg,
  38294. + "Invalid serial string character: %c\n",
  38295. + *ch);
  38296. + goto no_serial;
  38297. + }
  38298. + }
  38299. + if (len > 126 ||
  38300. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  38301. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  38302. + WARNING(fsg, "Invalid serial string length!\n");
  38303. + goto no_serial;
  38304. + }
  38305. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  38306. + } else {
  38307. + WARNING(fsg, "No serial-number string provided!\n");
  38308. + no_serial:
  38309. + device_desc.iSerialNumber = 0;
  38310. + }
  38311. +
  38312. + return 0;
  38313. +}
  38314. +
  38315. +
  38316. +static int __init fsg_bind(struct usb_gadget *gadget)
  38317. +{
  38318. + struct fsg_dev *fsg = the_fsg;
  38319. + int rc;
  38320. + int i;
  38321. + struct fsg_lun *curlun;
  38322. + struct usb_ep *ep;
  38323. + struct usb_request *req;
  38324. + char *pathbuf, *p;
  38325. +
  38326. + fsg->gadget = gadget;
  38327. + set_gadget_data(gadget, fsg);
  38328. + fsg->ep0 = gadget->ep0;
  38329. + fsg->ep0->driver_data = fsg;
  38330. +
  38331. + if ((rc = check_parameters(fsg)) != 0)
  38332. + goto out;
  38333. +
  38334. + if (mod_data.removable) { // Enable the store_xxx attributes
  38335. + dev_attr_file.attr.mode = 0644;
  38336. + dev_attr_file.store = fsg_store_file;
  38337. + if (!mod_data.cdrom) {
  38338. + dev_attr_ro.attr.mode = 0644;
  38339. + dev_attr_ro.store = fsg_store_ro;
  38340. + }
  38341. + }
  38342. +
  38343. + /* Only for removable media? */
  38344. + dev_attr_nofua.attr.mode = 0644;
  38345. + dev_attr_nofua.store = fsg_store_nofua;
  38346. +
  38347. + /* Find out how many LUNs there should be */
  38348. + i = mod_data.nluns;
  38349. + if (i == 0)
  38350. + i = max(mod_data.num_filenames, 1u);
  38351. + if (i > FSG_MAX_LUNS) {
  38352. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  38353. + rc = -EINVAL;
  38354. + goto out;
  38355. + }
  38356. +
  38357. + /* Create the LUNs, open their backing files, and register the
  38358. + * LUN devices in sysfs. */
  38359. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  38360. + if (!fsg->luns) {
  38361. + rc = -ENOMEM;
  38362. + goto out;
  38363. + }
  38364. + fsg->nluns = i;
  38365. +
  38366. + for (i = 0; i < fsg->nluns; ++i) {
  38367. + curlun = &fsg->luns[i];
  38368. + curlun->cdrom = !!mod_data.cdrom;
  38369. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  38370. + curlun->initially_ro = curlun->ro;
  38371. + curlun->removable = mod_data.removable;
  38372. + curlun->nofua = mod_data.nofua[i];
  38373. + curlun->dev.release = lun_release;
  38374. + curlun->dev.parent = &gadget->dev;
  38375. + curlun->dev.driver = &fsg_driver.driver;
  38376. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  38377. + dev_set_name(&curlun->dev,"%s-lun%d",
  38378. + dev_name(&gadget->dev), i);
  38379. +
  38380. + kref_get(&fsg->ref);
  38381. + rc = device_register(&curlun->dev);
  38382. + if (rc) {
  38383. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  38384. + put_device(&curlun->dev);
  38385. + goto out;
  38386. + }
  38387. + curlun->registered = 1;
  38388. +
  38389. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  38390. + if (rc)
  38391. + goto out;
  38392. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  38393. + if (rc)
  38394. + goto out;
  38395. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  38396. + if (rc)
  38397. + goto out;
  38398. +
  38399. + if (mod_data.file[i] && *mod_data.file[i]) {
  38400. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  38401. + if (rc)
  38402. + goto out;
  38403. + } else if (!mod_data.removable) {
  38404. + ERROR(fsg, "no file given for LUN%d\n", i);
  38405. + rc = -EINVAL;
  38406. + goto out;
  38407. + }
  38408. + }
  38409. +
  38410. + /* Find all the endpoints we will use */
  38411. + usb_ep_autoconfig_reset(gadget);
  38412. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  38413. + if (!ep)
  38414. + goto autoconf_fail;
  38415. + ep->driver_data = fsg; // claim the endpoint
  38416. + fsg->bulk_in = ep;
  38417. +
  38418. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  38419. + if (!ep)
  38420. + goto autoconf_fail;
  38421. + ep->driver_data = fsg; // claim the endpoint
  38422. + fsg->bulk_out = ep;
  38423. +
  38424. + if (transport_is_cbi()) {
  38425. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  38426. + if (!ep)
  38427. + goto autoconf_fail;
  38428. + ep->driver_data = fsg; // claim the endpoint
  38429. + fsg->intr_in = ep;
  38430. + }
  38431. +
  38432. + /* Fix up the descriptors */
  38433. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  38434. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  38435. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  38436. +
  38437. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  38438. + fsg_intf_desc.bNumEndpoints = i;
  38439. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  38440. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  38441. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  38442. +
  38443. + if (gadget_is_dualspeed(gadget)) {
  38444. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  38445. +
  38446. + /* Assume endpoint addresses are the same for both speeds */
  38447. + fsg_hs_bulk_in_desc.bEndpointAddress =
  38448. + fsg_fs_bulk_in_desc.bEndpointAddress;
  38449. + fsg_hs_bulk_out_desc.bEndpointAddress =
  38450. + fsg_fs_bulk_out_desc.bEndpointAddress;
  38451. + fsg_hs_intr_in_desc.bEndpointAddress =
  38452. + fsg_fs_intr_in_desc.bEndpointAddress;
  38453. + }
  38454. +
  38455. + if (gadget_is_superspeed(gadget)) {
  38456. + unsigned max_burst;
  38457. +
  38458. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  38459. +
  38460. + /* Calculate bMaxBurst, we know packet size is 1024 */
  38461. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  38462. +
  38463. + /* Assume endpoint addresses are the same for both speeds */
  38464. + fsg_ss_bulk_in_desc.bEndpointAddress =
  38465. + fsg_fs_bulk_in_desc.bEndpointAddress;
  38466. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  38467. +
  38468. + fsg_ss_bulk_out_desc.bEndpointAddress =
  38469. + fsg_fs_bulk_out_desc.bEndpointAddress;
  38470. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  38471. + }
  38472. +
  38473. + if (gadget_is_otg(gadget))
  38474. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  38475. +
  38476. + rc = -ENOMEM;
  38477. +
  38478. + /* Allocate the request and buffer for endpoint 0 */
  38479. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  38480. + if (!req)
  38481. + goto out;
  38482. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  38483. + if (!req->buf)
  38484. + goto out;
  38485. + req->complete = ep0_complete;
  38486. +
  38487. + /* Allocate the data buffers */
  38488. + for (i = 0; i < fsg_num_buffers; ++i) {
  38489. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  38490. +
  38491. + /* Allocate for the bulk-in endpoint. We assume that
  38492. + * the buffer will also work with the bulk-out (and
  38493. + * interrupt-in) endpoint. */
  38494. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  38495. + if (!bh->buf)
  38496. + goto out;
  38497. + bh->next = bh + 1;
  38498. + }
  38499. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  38500. +
  38501. + /* This should reflect the actual gadget power source */
  38502. + usb_gadget_set_selfpowered(gadget);
  38503. +
  38504. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  38505. + "%s %s with %s",
  38506. + init_utsname()->sysname, init_utsname()->release,
  38507. + gadget->name);
  38508. +
  38509. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  38510. + "file-storage-gadget");
  38511. + if (IS_ERR(fsg->thread_task)) {
  38512. + rc = PTR_ERR(fsg->thread_task);
  38513. + goto out;
  38514. + }
  38515. +
  38516. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  38517. + INFO(fsg, "NOTE: This driver is deprecated. "
  38518. + "Consider using g_mass_storage instead.\n");
  38519. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  38520. +
  38521. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  38522. + for (i = 0; i < fsg->nluns; ++i) {
  38523. + curlun = &fsg->luns[i];
  38524. + if (fsg_lun_is_open(curlun)) {
  38525. + p = NULL;
  38526. + if (pathbuf) {
  38527. + p = d_path(&curlun->filp->f_path,
  38528. + pathbuf, PATH_MAX);
  38529. + if (IS_ERR(p))
  38530. + p = NULL;
  38531. + }
  38532. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  38533. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  38534. + }
  38535. + }
  38536. + kfree(pathbuf);
  38537. +
  38538. + DBG(fsg, "transport=%s (x%02x)\n",
  38539. + mod_data.transport_name, mod_data.transport_type);
  38540. + DBG(fsg, "protocol=%s (x%02x)\n",
  38541. + mod_data.protocol_name, mod_data.protocol_type);
  38542. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  38543. + mod_data.vendor, mod_data.product, mod_data.release);
  38544. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  38545. + mod_data.removable, mod_data.can_stall,
  38546. + mod_data.cdrom, mod_data.buflen);
  38547. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  38548. +
  38549. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  38550. +
  38551. + /* Tell the thread to start working */
  38552. + wake_up_process(fsg->thread_task);
  38553. + return 0;
  38554. +
  38555. +autoconf_fail:
  38556. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  38557. + rc = -ENOTSUPP;
  38558. +
  38559. +out:
  38560. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  38561. + fsg_unbind(gadget);
  38562. + complete(&fsg->thread_notifier);
  38563. + return rc;
  38564. +}
  38565. +
  38566. +
  38567. +/*-------------------------------------------------------------------------*/
  38568. +
  38569. +static void fsg_suspend(struct usb_gadget *gadget)
  38570. +{
  38571. + struct fsg_dev *fsg = get_gadget_data(gadget);
  38572. +
  38573. + DBG(fsg, "suspend\n");
  38574. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  38575. +}
  38576. +
  38577. +static void fsg_resume(struct usb_gadget *gadget)
  38578. +{
  38579. + struct fsg_dev *fsg = get_gadget_data(gadget);
  38580. +
  38581. + DBG(fsg, "resume\n");
  38582. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  38583. +}
  38584. +
  38585. +
  38586. +/*-------------------------------------------------------------------------*/
  38587. +
  38588. +static struct usb_gadget_driver fsg_driver = {
  38589. + .max_speed = USB_SPEED_SUPER,
  38590. + .function = (char *) fsg_string_product,
  38591. + .unbind = fsg_unbind,
  38592. + .disconnect = fsg_disconnect,
  38593. + .setup = fsg_setup,
  38594. + .suspend = fsg_suspend,
  38595. + .resume = fsg_resume,
  38596. +
  38597. + .driver = {
  38598. + .name = DRIVER_NAME,
  38599. + .owner = THIS_MODULE,
  38600. + // .release = ...
  38601. + // .suspend = ...
  38602. + // .resume = ...
  38603. + },
  38604. +};
  38605. +
  38606. +
  38607. +static int __init fsg_alloc(void)
  38608. +{
  38609. + struct fsg_dev *fsg;
  38610. +
  38611. + fsg = kzalloc(sizeof *fsg +
  38612. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  38613. +
  38614. + if (!fsg)
  38615. + return -ENOMEM;
  38616. + spin_lock_init(&fsg->lock);
  38617. + init_rwsem(&fsg->filesem);
  38618. + kref_init(&fsg->ref);
  38619. + init_completion(&fsg->thread_notifier);
  38620. +
  38621. + the_fsg = fsg;
  38622. + return 0;
  38623. +}
  38624. +
  38625. +
  38626. +static int __init fsg_init(void)
  38627. +{
  38628. + int rc;
  38629. + struct fsg_dev *fsg;
  38630. +
  38631. + rc = fsg_num_buffers_validate();
  38632. + if (rc != 0)
  38633. + return rc;
  38634. +
  38635. + if ((rc = fsg_alloc()) != 0)
  38636. + return rc;
  38637. + fsg = the_fsg;
  38638. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  38639. + kref_put(&fsg->ref, fsg_release);
  38640. + return rc;
  38641. +}
  38642. +module_init(fsg_init);
  38643. +
  38644. +
  38645. +static void __exit fsg_cleanup(void)
  38646. +{
  38647. + struct fsg_dev *fsg = the_fsg;
  38648. +
  38649. + /* Unregister the driver iff the thread hasn't already done so */
  38650. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  38651. + usb_gadget_unregister_driver(&fsg_driver);
  38652. +
  38653. + /* Wait for the thread to finish up */
  38654. + wait_for_completion(&fsg->thread_notifier);
  38655. +
  38656. + kref_put(&fsg->ref, fsg_release);
  38657. +}
  38658. +module_exit(fsg_cleanup);
  38659. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/changes.txt linux-raspberry-pi/drivers/usb/host/dwc_common_port/changes.txt
  38660. --- linux-3.12.13/drivers/usb/host/dwc_common_port/changes.txt 1970-01-01 01:00:00.000000000 +0100
  38661. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/changes.txt 2014-03-11 17:33:06.000000000 +0100
  38662. @@ -0,0 +1,174 @@
  38663. +
  38664. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  38665. +IO context struct. The IO context struct should live in an os-dependent struct
  38666. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  38667. +named 'os_dep' embedded in the main device struct. So there these calls look
  38668. +like this:
  38669. +
  38670. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  38671. +
  38672. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  38673. + &pcd->dev_global_regs->dcfg, 0);
  38674. +
  38675. +Note that for the existing Linux driver ports, it is not necessary to actually
  38676. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  38677. +require an IO context, its macros for dwc_read_reg32() and friends do not
  38678. +use the context pointer, so it is optimized away by the compiler. But it is
  38679. +necessary to add the pointer parameter to all of the call sites, to be ready
  38680. +for any future ports (such as FreeBSD) which do require an IO context.
  38681. +
  38682. +
  38683. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  38684. +take an additional parameter, a pointer to a memory context. Examples:
  38685. +
  38686. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  38687. +
  38688. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  38689. +
  38690. +Again, for the Linux ports, it is not necessary to actually define the memctx
  38691. +member, but it is necessary to add the pointer parameter to all of the call
  38692. +sites.
  38693. +
  38694. +
  38695. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  38696. +
  38697. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  38698. +
  38699. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  38700. +
  38701. +
  38702. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  38703. +
  38704. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  38705. +
  38706. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  38707. +
  38708. +
  38709. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  38710. +
  38711. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  38712. +
  38713. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  38714. +
  38715. +
  38716. +Same for dwc_timer_alloc(). Example:
  38717. +
  38718. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  38719. + cb_func, cb_data);
  38720. +
  38721. +
  38722. +Same for dwc_waitq_alloc(). Example:
  38723. +
  38724. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  38725. +
  38726. +
  38727. +Same for dwc_thread_run(). Example:
  38728. +
  38729. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  38730. + "dwc_usb3_thd1", data);
  38731. +
  38732. +
  38733. +Same for dwc_workq_alloc(). Example:
  38734. +
  38735. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  38736. +
  38737. +
  38738. +Same for dwc_task_alloc(). Example:
  38739. +
  38740. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  38741. + cb_func, cb_data);
  38742. +
  38743. +
  38744. +In addition to the context pointer additions, a few core functions have had
  38745. +other changes made to their parameters:
  38746. +
  38747. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  38748. +has been changed from a uint64_t to a dwc_irqflags_t.
  38749. +
  38750. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  38751. +FreeBSD equivalent of that function requires it.
  38752. +
  38753. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  38754. +'char *name' parameter, to be consistent with dwc_thread_run() and
  38755. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  38756. +requires a unique name.
  38757. +
  38758. +
  38759. +Here is a complete list of the core functions that now take a pointer to a
  38760. +context as their first parameter:
  38761. +
  38762. + dwc_read_reg32
  38763. + dwc_read_reg64
  38764. + dwc_write_reg32
  38765. + dwc_write_reg64
  38766. + dwc_modify_reg32
  38767. + dwc_modify_reg64
  38768. + dwc_alloc
  38769. + dwc_alloc_atomic
  38770. + dwc_strdup
  38771. + dwc_free
  38772. + dwc_dma_alloc
  38773. + dwc_dma_free
  38774. + dwc_mutex_alloc
  38775. + dwc_mutex_free
  38776. + dwc_spinlock_alloc
  38777. + dwc_spinlock_free
  38778. + dwc_timer_alloc
  38779. + dwc_waitq_alloc
  38780. + dwc_thread_run
  38781. + dwc_workq_alloc
  38782. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  38783. +
  38784. +And here are the core functions that have other changes to their parameters:
  38785. +
  38786. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  38787. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  38788. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  38789. +
  38790. +
  38791. +
  38792. +The changes to the core functions also require some of the other library
  38793. +functions to change:
  38794. +
  38795. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  38796. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  38797. + (for mutex allocation) as the 2nd param.
  38798. +
  38799. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  38800. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  38801. + 'void *memctx' as the 1st param.
  38802. +
  38803. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  38804. + 'void *memctx' as the 1st param.
  38805. +
  38806. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  38807. +
  38808. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  38809. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  38810. + param, and also now returns an integer value that is non-zero if
  38811. + allocation of its data structures or work queue fails.
  38812. +
  38813. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  38814. +
  38815. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  38816. + param, and also now returns an integer value that is non-zero if
  38817. + allocation of its data structures fails.
  38818. +
  38819. +
  38820. +
  38821. +Other miscellaneous changes:
  38822. +
  38823. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  38824. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  38825. +
  38826. +The following #define's have been added to allow selectively compiling library
  38827. +features:
  38828. +
  38829. + DWC_CCLIB
  38830. + DWC_CRYPTOLIB
  38831. + DWC_NOTIFYLIB
  38832. + DWC_UTFLIB
  38833. +
  38834. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  38835. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  38836. +library code directly into a driver module, instead of as a standalone module.
  38837. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/doc/doxygen.cfg linux-raspberry-pi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  38838. --- linux-3.12.13/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  38839. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 2014-03-11 17:51:27.000000000 +0100
  38840. @@ -0,0 +1,270 @@
  38841. +# Doxyfile 1.4.5
  38842. +
  38843. +#---------------------------------------------------------------------------
  38844. +# Project related configuration options
  38845. +#---------------------------------------------------------------------------
  38846. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  38847. +PROJECT_NUMBER =
  38848. +OUTPUT_DIRECTORY = doc
  38849. +CREATE_SUBDIRS = NO
  38850. +OUTPUT_LANGUAGE = English
  38851. +BRIEF_MEMBER_DESC = YES
  38852. +REPEAT_BRIEF = YES
  38853. +ABBREVIATE_BRIEF = "The $name class" \
  38854. + "The $name widget" \
  38855. + "The $name file" \
  38856. + is \
  38857. + provides \
  38858. + specifies \
  38859. + contains \
  38860. + represents \
  38861. + a \
  38862. + an \
  38863. + the
  38864. +ALWAYS_DETAILED_SEC = YES
  38865. +INLINE_INHERITED_MEMB = NO
  38866. +FULL_PATH_NAMES = NO
  38867. +STRIP_FROM_PATH = ..
  38868. +STRIP_FROM_INC_PATH =
  38869. +SHORT_NAMES = NO
  38870. +JAVADOC_AUTOBRIEF = YES
  38871. +MULTILINE_CPP_IS_BRIEF = NO
  38872. +DETAILS_AT_TOP = YES
  38873. +INHERIT_DOCS = YES
  38874. +SEPARATE_MEMBER_PAGES = NO
  38875. +TAB_SIZE = 8
  38876. +ALIASES =
  38877. +OPTIMIZE_OUTPUT_FOR_C = YES
  38878. +OPTIMIZE_OUTPUT_JAVA = NO
  38879. +BUILTIN_STL_SUPPORT = NO
  38880. +DISTRIBUTE_GROUP_DOC = NO
  38881. +SUBGROUPING = NO
  38882. +#---------------------------------------------------------------------------
  38883. +# Build related configuration options
  38884. +#---------------------------------------------------------------------------
  38885. +EXTRACT_ALL = NO
  38886. +EXTRACT_PRIVATE = NO
  38887. +EXTRACT_STATIC = YES
  38888. +EXTRACT_LOCAL_CLASSES = NO
  38889. +EXTRACT_LOCAL_METHODS = NO
  38890. +HIDE_UNDOC_MEMBERS = NO
  38891. +HIDE_UNDOC_CLASSES = NO
  38892. +HIDE_FRIEND_COMPOUNDS = NO
  38893. +HIDE_IN_BODY_DOCS = NO
  38894. +INTERNAL_DOCS = NO
  38895. +CASE_SENSE_NAMES = YES
  38896. +HIDE_SCOPE_NAMES = NO
  38897. +SHOW_INCLUDE_FILES = NO
  38898. +INLINE_INFO = YES
  38899. +SORT_MEMBER_DOCS = NO
  38900. +SORT_BRIEF_DOCS = NO
  38901. +SORT_BY_SCOPE_NAME = NO
  38902. +GENERATE_TODOLIST = YES
  38903. +GENERATE_TESTLIST = YES
  38904. +GENERATE_BUGLIST = YES
  38905. +GENERATE_DEPRECATEDLIST= YES
  38906. +ENABLED_SECTIONS =
  38907. +MAX_INITIALIZER_LINES = 30
  38908. +SHOW_USED_FILES = YES
  38909. +SHOW_DIRECTORIES = YES
  38910. +FILE_VERSION_FILTER =
  38911. +#---------------------------------------------------------------------------
  38912. +# configuration options related to warning and progress messages
  38913. +#---------------------------------------------------------------------------
  38914. +QUIET = YES
  38915. +WARNINGS = YES
  38916. +WARN_IF_UNDOCUMENTED = NO
  38917. +WARN_IF_DOC_ERROR = YES
  38918. +WARN_NO_PARAMDOC = YES
  38919. +WARN_FORMAT = "$file:$line: $text"
  38920. +WARN_LOGFILE =
  38921. +#---------------------------------------------------------------------------
  38922. +# configuration options related to the input files
  38923. +#---------------------------------------------------------------------------
  38924. +INPUT = .
  38925. +FILE_PATTERNS = *.c \
  38926. + *.cc \
  38927. + *.cxx \
  38928. + *.cpp \
  38929. + *.c++ \
  38930. + *.d \
  38931. + *.java \
  38932. + *.ii \
  38933. + *.ixx \
  38934. + *.ipp \
  38935. + *.i++ \
  38936. + *.inl \
  38937. + *.h \
  38938. + *.hh \
  38939. + *.hxx \
  38940. + *.hpp \
  38941. + *.h++ \
  38942. + *.idl \
  38943. + *.odl \
  38944. + *.cs \
  38945. + *.php \
  38946. + *.php3 \
  38947. + *.inc \
  38948. + *.m \
  38949. + *.mm \
  38950. + *.dox \
  38951. + *.py \
  38952. + *.C \
  38953. + *.CC \
  38954. + *.C++ \
  38955. + *.II \
  38956. + *.I++ \
  38957. + *.H \
  38958. + *.HH \
  38959. + *.H++ \
  38960. + *.CS \
  38961. + *.PHP \
  38962. + *.PHP3 \
  38963. + *.M \
  38964. + *.MM \
  38965. + *.PY
  38966. +RECURSIVE = NO
  38967. +EXCLUDE =
  38968. +EXCLUDE_SYMLINKS = NO
  38969. +EXCLUDE_PATTERNS =
  38970. +EXAMPLE_PATH =
  38971. +EXAMPLE_PATTERNS = *
  38972. +EXAMPLE_RECURSIVE = NO
  38973. +IMAGE_PATH =
  38974. +INPUT_FILTER =
  38975. +FILTER_PATTERNS =
  38976. +FILTER_SOURCE_FILES = NO
  38977. +#---------------------------------------------------------------------------
  38978. +# configuration options related to source browsing
  38979. +#---------------------------------------------------------------------------
  38980. +SOURCE_BROWSER = NO
  38981. +INLINE_SOURCES = NO
  38982. +STRIP_CODE_COMMENTS = YES
  38983. +REFERENCED_BY_RELATION = YES
  38984. +REFERENCES_RELATION = YES
  38985. +USE_HTAGS = NO
  38986. +VERBATIM_HEADERS = NO
  38987. +#---------------------------------------------------------------------------
  38988. +# configuration options related to the alphabetical class index
  38989. +#---------------------------------------------------------------------------
  38990. +ALPHABETICAL_INDEX = NO
  38991. +COLS_IN_ALPHA_INDEX = 5
  38992. +IGNORE_PREFIX =
  38993. +#---------------------------------------------------------------------------
  38994. +# configuration options related to the HTML output
  38995. +#---------------------------------------------------------------------------
  38996. +GENERATE_HTML = YES
  38997. +HTML_OUTPUT = html
  38998. +HTML_FILE_EXTENSION = .html
  38999. +HTML_HEADER =
  39000. +HTML_FOOTER =
  39001. +HTML_STYLESHEET =
  39002. +HTML_ALIGN_MEMBERS = YES
  39003. +GENERATE_HTMLHELP = NO
  39004. +CHM_FILE =
  39005. +HHC_LOCATION =
  39006. +GENERATE_CHI = NO
  39007. +BINARY_TOC = NO
  39008. +TOC_EXPAND = NO
  39009. +DISABLE_INDEX = NO
  39010. +ENUM_VALUES_PER_LINE = 4
  39011. +GENERATE_TREEVIEW = YES
  39012. +TREEVIEW_WIDTH = 250
  39013. +#---------------------------------------------------------------------------
  39014. +# configuration options related to the LaTeX output
  39015. +#---------------------------------------------------------------------------
  39016. +GENERATE_LATEX = NO
  39017. +LATEX_OUTPUT = latex
  39018. +LATEX_CMD_NAME = latex
  39019. +MAKEINDEX_CMD_NAME = makeindex
  39020. +COMPACT_LATEX = NO
  39021. +PAPER_TYPE = a4wide
  39022. +EXTRA_PACKAGES =
  39023. +LATEX_HEADER =
  39024. +PDF_HYPERLINKS = NO
  39025. +USE_PDFLATEX = NO
  39026. +LATEX_BATCHMODE = NO
  39027. +LATEX_HIDE_INDICES = NO
  39028. +#---------------------------------------------------------------------------
  39029. +# configuration options related to the RTF output
  39030. +#---------------------------------------------------------------------------
  39031. +GENERATE_RTF = NO
  39032. +RTF_OUTPUT = rtf
  39033. +COMPACT_RTF = NO
  39034. +RTF_HYPERLINKS = NO
  39035. +RTF_STYLESHEET_FILE =
  39036. +RTF_EXTENSIONS_FILE =
  39037. +#---------------------------------------------------------------------------
  39038. +# configuration options related to the man page output
  39039. +#---------------------------------------------------------------------------
  39040. +GENERATE_MAN = NO
  39041. +MAN_OUTPUT = man
  39042. +MAN_EXTENSION = .3
  39043. +MAN_LINKS = NO
  39044. +#---------------------------------------------------------------------------
  39045. +# configuration options related to the XML output
  39046. +#---------------------------------------------------------------------------
  39047. +GENERATE_XML = NO
  39048. +XML_OUTPUT = xml
  39049. +XML_SCHEMA =
  39050. +XML_DTD =
  39051. +XML_PROGRAMLISTING = YES
  39052. +#---------------------------------------------------------------------------
  39053. +# configuration options for the AutoGen Definitions output
  39054. +#---------------------------------------------------------------------------
  39055. +GENERATE_AUTOGEN_DEF = NO
  39056. +#---------------------------------------------------------------------------
  39057. +# configuration options related to the Perl module output
  39058. +#---------------------------------------------------------------------------
  39059. +GENERATE_PERLMOD = NO
  39060. +PERLMOD_LATEX = NO
  39061. +PERLMOD_PRETTY = YES
  39062. +PERLMOD_MAKEVAR_PREFIX =
  39063. +#---------------------------------------------------------------------------
  39064. +# Configuration options related to the preprocessor
  39065. +#---------------------------------------------------------------------------
  39066. +ENABLE_PREPROCESSING = YES
  39067. +MACRO_EXPANSION = NO
  39068. +EXPAND_ONLY_PREDEF = NO
  39069. +SEARCH_INCLUDES = YES
  39070. +INCLUDE_PATH =
  39071. +INCLUDE_FILE_PATTERNS =
  39072. +PREDEFINED = DEBUG DEBUG_MEMORY
  39073. +EXPAND_AS_DEFINED =
  39074. +SKIP_FUNCTION_MACROS = YES
  39075. +#---------------------------------------------------------------------------
  39076. +# Configuration::additions related to external references
  39077. +#---------------------------------------------------------------------------
  39078. +TAGFILES =
  39079. +GENERATE_TAGFILE =
  39080. +ALLEXTERNALS = NO
  39081. +EXTERNAL_GROUPS = YES
  39082. +PERL_PATH = /usr/bin/perl
  39083. +#---------------------------------------------------------------------------
  39084. +# Configuration options related to the dot tool
  39085. +#---------------------------------------------------------------------------
  39086. +CLASS_DIAGRAMS = YES
  39087. +HIDE_UNDOC_RELATIONS = YES
  39088. +HAVE_DOT = NO
  39089. +CLASS_GRAPH = YES
  39090. +COLLABORATION_GRAPH = YES
  39091. +GROUP_GRAPHS = YES
  39092. +UML_LOOK = NO
  39093. +TEMPLATE_RELATIONS = NO
  39094. +INCLUDE_GRAPH = NO
  39095. +INCLUDED_BY_GRAPH = YES
  39096. +CALL_GRAPH = NO
  39097. +GRAPHICAL_HIERARCHY = YES
  39098. +DIRECTORY_GRAPH = YES
  39099. +DOT_IMAGE_FORMAT = png
  39100. +DOT_PATH =
  39101. +DOTFILE_DIRS =
  39102. +MAX_DOT_GRAPH_DEPTH = 1000
  39103. +DOT_TRANSPARENT = NO
  39104. +DOT_MULTI_TARGETS = NO
  39105. +GENERATE_LEGEND = YES
  39106. +DOT_CLEANUP = YES
  39107. +#---------------------------------------------------------------------------
  39108. +# Configuration::additions related to the search engine
  39109. +#---------------------------------------------------------------------------
  39110. +SEARCHENGINE = NO
  39111. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_cc.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.c
  39112. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_cc.c 1970-01-01 01:00:00.000000000 +0100
  39113. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.c 2014-03-11 17:51:27.000000000 +0100
  39114. @@ -0,0 +1,532 @@
  39115. +/* =========================================================================
  39116. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  39117. + * $Revision: #4 $
  39118. + * $Date: 2010/11/04 $
  39119. + * $Change: 1621692 $
  39120. + *
  39121. + * Synopsys Portability Library Software and documentation
  39122. + * (hereinafter, "Software") is an Unsupported proprietary work of
  39123. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  39124. + * between Synopsys and you.
  39125. + *
  39126. + * The Software IS NOT an item of Licensed Software or Licensed Product
  39127. + * under any End User Software License Agreement or Agreement for
  39128. + * Licensed Product with Synopsys or any supplement thereto. You are
  39129. + * permitted to use and redistribute this Software in source and binary
  39130. + * forms, with or without modification, provided that redistributions
  39131. + * of source code must retain this notice. You may not view, use,
  39132. + * disclose, copy or distribute this file or any information contained
  39133. + * herein except pursuant to this license grant from Synopsys. If you
  39134. + * do not agree with this notice, including the disclaimer below, then
  39135. + * you are not authorized to use the Software.
  39136. + *
  39137. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  39138. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39139. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39140. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  39141. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39142. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  39143. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  39144. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39145. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39146. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39147. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39148. + * DAMAGE.
  39149. + * ========================================================================= */
  39150. +#ifdef DWC_CCLIB
  39151. +
  39152. +#include "dwc_cc.h"
  39153. +
  39154. +typedef struct dwc_cc
  39155. +{
  39156. + uint32_t uid;
  39157. + uint8_t chid[16];
  39158. + uint8_t cdid[16];
  39159. + uint8_t ck[16];
  39160. + uint8_t *name;
  39161. + uint8_t length;
  39162. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  39163. +} dwc_cc_t;
  39164. +
  39165. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  39166. +
  39167. +/** The main structure for CC management. */
  39168. +struct dwc_cc_if
  39169. +{
  39170. + dwc_mutex_t *mutex;
  39171. + char *filename;
  39172. +
  39173. + unsigned is_host:1;
  39174. +
  39175. + dwc_notifier_t *notifier;
  39176. +
  39177. + struct context_list list;
  39178. +};
  39179. +
  39180. +#ifdef DEBUG
  39181. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  39182. +{
  39183. + int i;
  39184. + DWC_PRINTF("%s: ", name);
  39185. + for (i=0; i<len; i++) {
  39186. + DWC_PRINTF("%02x ", bytes[i]);
  39187. + }
  39188. + DWC_PRINTF("\n");
  39189. +}
  39190. +#else
  39191. +#define dump_bytes(x...)
  39192. +#endif
  39193. +
  39194. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  39195. +{
  39196. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  39197. + if (!cc) {
  39198. + return NULL;
  39199. + }
  39200. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  39201. +
  39202. + if (name) {
  39203. + cc->length = length;
  39204. + cc->name = dwc_alloc(mem_ctx, length);
  39205. + if (!cc->name) {
  39206. + dwc_free(mem_ctx, cc);
  39207. + return NULL;
  39208. + }
  39209. +
  39210. + DWC_MEMCPY(cc->name, name, length);
  39211. + }
  39212. +
  39213. + return cc;
  39214. +}
  39215. +
  39216. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  39217. +{
  39218. + if (cc->name) {
  39219. + dwc_free(mem_ctx, cc->name);
  39220. + }
  39221. + dwc_free(mem_ctx, cc);
  39222. +}
  39223. +
  39224. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  39225. +{
  39226. + uint32_t uid = 0;
  39227. + dwc_cc_t *cc;
  39228. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39229. + if (cc->uid > uid) {
  39230. + uid = cc->uid;
  39231. + }
  39232. + }
  39233. +
  39234. + if (uid == 0) {
  39235. + uid = 255;
  39236. + }
  39237. +
  39238. + return uid + 1;
  39239. +}
  39240. +
  39241. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  39242. +{
  39243. + dwc_cc_t *cc;
  39244. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39245. + if (cc->uid == uid) {
  39246. + return cc;
  39247. + }
  39248. + }
  39249. + return NULL;
  39250. +}
  39251. +
  39252. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  39253. +{
  39254. + unsigned int size = 0;
  39255. + dwc_cc_t *cc;
  39256. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39257. + size += (48 + 1);
  39258. + if (cc->name) {
  39259. + size += cc->length;
  39260. + }
  39261. + }
  39262. + return size;
  39263. +}
  39264. +
  39265. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  39266. +{
  39267. + uint32_t uid = 0;
  39268. + dwc_cc_t *cc;
  39269. +
  39270. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39271. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  39272. + uid = cc->uid;
  39273. + break;
  39274. + }
  39275. + }
  39276. + return uid;
  39277. +}
  39278. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  39279. +{
  39280. + uint32_t uid = 0;
  39281. + dwc_cc_t *cc;
  39282. +
  39283. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39284. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  39285. + uid = cc->uid;
  39286. + break;
  39287. + }
  39288. + }
  39289. + return uid;
  39290. +}
  39291. +
  39292. +/* Internal cc_add */
  39293. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  39294. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  39295. +{
  39296. + dwc_cc_t *cc;
  39297. + uint32_t uid;
  39298. +
  39299. + if (cc_if->is_host) {
  39300. + uid = cc_match_cdid(cc_if, cdid);
  39301. + }
  39302. + else {
  39303. + uid = cc_match_chid(cc_if, chid);
  39304. + }
  39305. +
  39306. + if (uid) {
  39307. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  39308. + cc = cc_find(cc_if, uid);
  39309. + }
  39310. + else {
  39311. + cc = alloc_cc(mem_ctx, name, length);
  39312. + cc->uid = next_uid(cc_if);
  39313. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  39314. + }
  39315. +
  39316. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  39317. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  39318. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  39319. +
  39320. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  39321. + dump_bytes("CHID", cc->chid, 16);
  39322. + dump_bytes("CDID", cc->cdid, 16);
  39323. + dump_bytes("CK", cc->ck, 16);
  39324. + return cc->uid;
  39325. +}
  39326. +
  39327. +/* Internal cc_clear */
  39328. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  39329. +{
  39330. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  39331. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  39332. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  39333. + free_cc(mem_ctx, cc);
  39334. + }
  39335. +}
  39336. +
  39337. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  39338. + dwc_notifier_t *notifier, unsigned is_host)
  39339. +{
  39340. + dwc_cc_if_t *cc_if = NULL;
  39341. +
  39342. + /* Allocate a common_cc_if structure */
  39343. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  39344. +
  39345. + if (!cc_if)
  39346. + return NULL;
  39347. +
  39348. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  39349. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  39350. +#else
  39351. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  39352. +#endif
  39353. + if (!cc_if->mutex) {
  39354. + dwc_free(mem_ctx, cc_if);
  39355. + return NULL;
  39356. + }
  39357. +
  39358. + DWC_CIRCLEQ_INIT(&cc_if->list);
  39359. + cc_if->is_host = is_host;
  39360. + cc_if->notifier = notifier;
  39361. + return cc_if;
  39362. +}
  39363. +
  39364. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  39365. +{
  39366. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  39367. + DWC_MUTEX_FREE(cc_if->mutex);
  39368. +#else
  39369. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  39370. +#endif
  39371. + cc_clear(mem_ctx, cc_if);
  39372. + dwc_free(mem_ctx, cc_if);
  39373. +}
  39374. +
  39375. +static void cc_changed(dwc_cc_if_t *cc_if)
  39376. +{
  39377. + if (cc_if->notifier) {
  39378. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  39379. + }
  39380. +}
  39381. +
  39382. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  39383. +{
  39384. + DWC_MUTEX_LOCK(cc_if->mutex);
  39385. + cc_clear(mem_ctx, cc_if);
  39386. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39387. + cc_changed(cc_if);
  39388. +}
  39389. +
  39390. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  39391. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  39392. +{
  39393. + uint32_t uid;
  39394. +
  39395. + DWC_MUTEX_LOCK(cc_if->mutex);
  39396. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  39397. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39398. + cc_changed(cc_if);
  39399. +
  39400. + return uid;
  39401. +}
  39402. +
  39403. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  39404. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  39405. +{
  39406. + dwc_cc_t* cc;
  39407. +
  39408. + DWC_DEBUGC("Change connection context %d", id);
  39409. +
  39410. + DWC_MUTEX_LOCK(cc_if->mutex);
  39411. + cc = cc_find(cc_if, id);
  39412. + if (!cc) {
  39413. + DWC_ERROR("Uid %d not found in cc list\n", id);
  39414. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39415. + return;
  39416. + }
  39417. +
  39418. + if (chid) {
  39419. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  39420. + }
  39421. + if (cdid) {
  39422. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  39423. + }
  39424. + if (ck) {
  39425. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  39426. + }
  39427. +
  39428. + if (name) {
  39429. + if (cc->name) {
  39430. + dwc_free(mem_ctx, cc->name);
  39431. + }
  39432. + cc->name = dwc_alloc(mem_ctx, length);
  39433. + if (!cc->name) {
  39434. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  39435. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39436. + return;
  39437. + }
  39438. + cc->length = length;
  39439. + DWC_MEMCPY(cc->name, name, length);
  39440. + }
  39441. +
  39442. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39443. +
  39444. + cc_changed(cc_if);
  39445. +
  39446. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  39447. + dump_bytes("New CHID", cc->chid, 16);
  39448. + dump_bytes("New CDID", cc->cdid, 16);
  39449. + dump_bytes("New CK", cc->ck, 16);
  39450. +}
  39451. +
  39452. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  39453. +{
  39454. + dwc_cc_t *cc;
  39455. +
  39456. + DWC_DEBUGC("Removing connection context %d", id);
  39457. +
  39458. + DWC_MUTEX_LOCK(cc_if->mutex);
  39459. + cc = cc_find(cc_if, id);
  39460. + if (!cc) {
  39461. + DWC_ERROR("Uid %d not found in cc list\n", id);
  39462. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39463. + return;
  39464. + }
  39465. +
  39466. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  39467. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39468. + free_cc(mem_ctx, cc);
  39469. +
  39470. + cc_changed(cc_if);
  39471. +}
  39472. +
  39473. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  39474. +{
  39475. + uint8_t *buf, *x;
  39476. + uint8_t zero = 0;
  39477. + dwc_cc_t *cc;
  39478. +
  39479. + DWC_MUTEX_LOCK(cc_if->mutex);
  39480. + *length = cc_data_size(cc_if);
  39481. + if (!(*length)) {
  39482. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39483. + return NULL;
  39484. + }
  39485. +
  39486. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  39487. +
  39488. + buf = dwc_alloc(mem_ctx, *length);
  39489. + if (!buf) {
  39490. + *length = 0;
  39491. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39492. + return NULL;
  39493. + }
  39494. +
  39495. + x = buf;
  39496. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39497. + DWC_MEMCPY(x, cc->chid, 16);
  39498. + x += 16;
  39499. + DWC_MEMCPY(x, cc->cdid, 16);
  39500. + x += 16;
  39501. + DWC_MEMCPY(x, cc->ck, 16);
  39502. + x += 16;
  39503. + if (cc->name) {
  39504. + DWC_MEMCPY(x, &cc->length, 1);
  39505. + x += 1;
  39506. + DWC_MEMCPY(x, cc->name, cc->length);
  39507. + x += cc->length;
  39508. + }
  39509. + else {
  39510. + DWC_MEMCPY(x, &zero, 1);
  39511. + x += 1;
  39512. + }
  39513. + }
  39514. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39515. +
  39516. + return buf;
  39517. +}
  39518. +
  39519. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  39520. +{
  39521. + uint8_t name_length;
  39522. + uint8_t *name;
  39523. + uint8_t *chid;
  39524. + uint8_t *cdid;
  39525. + uint8_t *ck;
  39526. + uint32_t i = 0;
  39527. +
  39528. + DWC_MUTEX_LOCK(cc_if->mutex);
  39529. + cc_clear(mem_ctx, cc_if);
  39530. +
  39531. + while (i < length) {
  39532. + chid = &data[i];
  39533. + i += 16;
  39534. + cdid = &data[i];
  39535. + i += 16;
  39536. + ck = &data[i];
  39537. + i += 16;
  39538. +
  39539. + name_length = data[i];
  39540. + i ++;
  39541. +
  39542. + if (name_length) {
  39543. + name = &data[i];
  39544. + i += name_length;
  39545. + }
  39546. + else {
  39547. + name = NULL;
  39548. + }
  39549. +
  39550. + /* check to see if we haven't overflown the buffer */
  39551. + if (i > length) {
  39552. + DWC_ERROR("Data format error while attempting to load CCs "
  39553. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  39554. + break;
  39555. + }
  39556. +
  39557. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  39558. + }
  39559. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39560. +
  39561. + cc_changed(cc_if);
  39562. +}
  39563. +
  39564. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  39565. +{
  39566. + uint32_t uid = 0;
  39567. +
  39568. + DWC_MUTEX_LOCK(cc_if->mutex);
  39569. + uid = cc_match_chid(cc_if, chid);
  39570. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39571. + return uid;
  39572. +}
  39573. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  39574. +{
  39575. + uint32_t uid = 0;
  39576. +
  39577. + DWC_MUTEX_LOCK(cc_if->mutex);
  39578. + uid = cc_match_cdid(cc_if, cdid);
  39579. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39580. + return uid;
  39581. +}
  39582. +
  39583. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  39584. +{
  39585. + uint8_t *ck = NULL;
  39586. + dwc_cc_t *cc;
  39587. +
  39588. + DWC_MUTEX_LOCK(cc_if->mutex);
  39589. + cc = cc_find(cc_if, id);
  39590. + if (cc) {
  39591. + ck = cc->ck;
  39592. + }
  39593. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39594. +
  39595. + return ck;
  39596. +
  39597. +}
  39598. +
  39599. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  39600. +{
  39601. + uint8_t *retval = NULL;
  39602. + dwc_cc_t *cc;
  39603. +
  39604. + DWC_MUTEX_LOCK(cc_if->mutex);
  39605. + cc = cc_find(cc_if, id);
  39606. + if (cc) {
  39607. + retval = cc->chid;
  39608. + }
  39609. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39610. +
  39611. + return retval;
  39612. +}
  39613. +
  39614. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  39615. +{
  39616. + uint8_t *retval = NULL;
  39617. + dwc_cc_t *cc;
  39618. +
  39619. + DWC_MUTEX_LOCK(cc_if->mutex);
  39620. + cc = cc_find(cc_if, id);
  39621. + if (cc) {
  39622. + retval = cc->cdid;
  39623. + }
  39624. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39625. +
  39626. + return retval;
  39627. +}
  39628. +
  39629. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  39630. +{
  39631. + uint8_t *retval = NULL;
  39632. + dwc_cc_t *cc;
  39633. +
  39634. + DWC_MUTEX_LOCK(cc_if->mutex);
  39635. + *length = 0;
  39636. + cc = cc_find(cc_if, id);
  39637. + if (cc) {
  39638. + *length = cc->length;
  39639. + retval = cc->name;
  39640. + }
  39641. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39642. +
  39643. + return retval;
  39644. +}
  39645. +
  39646. +#endif /* DWC_CCLIB */
  39647. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_cc.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.h
  39648. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_cc.h 1970-01-01 01:00:00.000000000 +0100
  39649. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.h 2014-03-11 17:51:27.000000000 +0100
  39650. @@ -0,0 +1,224 @@
  39651. +/* =========================================================================
  39652. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  39653. + * $Revision: #4 $
  39654. + * $Date: 2010/09/28 $
  39655. + * $Change: 1596182 $
  39656. + *
  39657. + * Synopsys Portability Library Software and documentation
  39658. + * (hereinafter, "Software") is an Unsupported proprietary work of
  39659. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  39660. + * between Synopsys and you.
  39661. + *
  39662. + * The Software IS NOT an item of Licensed Software or Licensed Product
  39663. + * under any End User Software License Agreement or Agreement for
  39664. + * Licensed Product with Synopsys or any supplement thereto. You are
  39665. + * permitted to use and redistribute this Software in source and binary
  39666. + * forms, with or without modification, provided that redistributions
  39667. + * of source code must retain this notice. You may not view, use,
  39668. + * disclose, copy or distribute this file or any information contained
  39669. + * herein except pursuant to this license grant from Synopsys. If you
  39670. + * do not agree with this notice, including the disclaimer below, then
  39671. + * you are not authorized to use the Software.
  39672. + *
  39673. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  39674. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39675. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39676. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  39677. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39678. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  39679. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  39680. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39681. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39682. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39683. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39684. + * DAMAGE.
  39685. + * ========================================================================= */
  39686. +#ifndef _DWC_CC_H_
  39687. +#define _DWC_CC_H_
  39688. +
  39689. +#ifdef __cplusplus
  39690. +extern "C" {
  39691. +#endif
  39692. +
  39693. +/** @file
  39694. + *
  39695. + * This file defines the Context Context library.
  39696. + *
  39697. + * The main data structure is dwc_cc_if_t which is returned by either the
  39698. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  39699. + * function. The data structure is opaque and should only be manipulated via the
  39700. + * functions provied in this API.
  39701. + *
  39702. + * It manages a list of connection contexts and operations can be performed to
  39703. + * add, remove, query, search, and change, those contexts. Additionally,
  39704. + * a dwc_notifier_t object can be requested from the manager so that
  39705. + * the user can be notified whenever the context list has changed.
  39706. + */
  39707. +
  39708. +#include "dwc_os.h"
  39709. +#include "dwc_list.h"
  39710. +#include "dwc_notifier.h"
  39711. +
  39712. +
  39713. +/* Notifications */
  39714. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  39715. +
  39716. +struct dwc_cc_if;
  39717. +typedef struct dwc_cc_if dwc_cc_if_t;
  39718. +
  39719. +
  39720. +/** @name Connection Context Operations */
  39721. +/** @{ */
  39722. +
  39723. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  39724. + * fields to default values, and returns a pointer to the structure or NULL on
  39725. + * error. */
  39726. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  39727. + dwc_notifier_t *notifier, unsigned is_host);
  39728. +
  39729. +/** Frees the memory for the specified CC structure allocated from
  39730. + * dwc_cc_if_alloc(). */
  39731. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  39732. +
  39733. +/** Removes all contexts from the connection context list */
  39734. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  39735. +
  39736. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  39737. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  39738. + * not overwritten.
  39739. + *
  39740. + * @param cc_if The cc_if structure.
  39741. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  39742. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  39743. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  39744. + * @param name An optional host friendly name as defined in the association model
  39745. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  39746. + * @param length The length othe unicode string.
  39747. + * @return A unique identifier used to refer to this context that is valid for
  39748. + * as long as this context is still in the list. */
  39749. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  39750. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  39751. + uint8_t length);
  39752. +
  39753. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  39754. + * list, preserving any accumulated statistics. This would typically be called
  39755. + * if the host decideds to change the context with a SET_CONNECTION request.
  39756. + *
  39757. + * @param cc_if The cc_if structure.
  39758. + * @param id The identifier of the connection context.
  39759. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  39760. + * indicates no change.
  39761. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  39762. + * indicates no change.
  39763. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  39764. + * indicates no change.
  39765. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  39766. + * @param length Length of name. */
  39767. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  39768. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  39769. + uint8_t *name, uint8_t length);
  39770. +
  39771. +/** Remove the specified connection context.
  39772. + * @param cc_if The cc_if structure.
  39773. + * @param id The identifier of the connection context to remove. */
  39774. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  39775. +
  39776. +/** Get a binary block of data for the connection context list and attributes.
  39777. + * This data can be used by the OS specific driver to save the connection
  39778. + * context list into non-volatile memory.
  39779. + *
  39780. + * @param cc_if The cc_if structure.
  39781. + * @param length Return the length of the data buffer.
  39782. + * @return A pointer to the data buffer. The memory for this buffer should be
  39783. + * freed with DWC_FREE() after use. */
  39784. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  39785. + unsigned int *length);
  39786. +
  39787. +/** Restore the connection context list from the binary data that was previously
  39788. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  39789. + * driver to load a connection context list from non-volatile memory.
  39790. + *
  39791. + * @param cc_if The cc_if structure.
  39792. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  39793. + * @param length The length of the data. */
  39794. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  39795. + uint8_t *data, unsigned int length);
  39796. +
  39797. +/** Find the connection context from the specified CHID.
  39798. + *
  39799. + * @param cc_if The cc_if structure.
  39800. + * @param chid A pointer to the CHID data.
  39801. + * @return A non-zero identifier of the connection context if the CHID matches.
  39802. + * Otherwise returns 0. */
  39803. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  39804. +
  39805. +/** Find the connection context from the specified CDID.
  39806. + *
  39807. + * @param cc_if The cc_if structure.
  39808. + * @param cdid A pointer to the CDID data.
  39809. + * @return A non-zero identifier of the connection context if the CHID matches.
  39810. + * Otherwise returns 0. */
  39811. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  39812. +
  39813. +/** Retrieve the CK from the specified connection context.
  39814. + *
  39815. + * @param cc_if The cc_if structure.
  39816. + * @param id The identifier of the connection context.
  39817. + * @return A pointer to the CK data. The memory does not need to be freed. */
  39818. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  39819. +
  39820. +/** Retrieve the CHID from the specified connection context.
  39821. + *
  39822. + * @param cc_if The cc_if structure.
  39823. + * @param id The identifier of the connection context.
  39824. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  39825. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  39826. +
  39827. +/** Retrieve the CDID from the specified connection context.
  39828. + *
  39829. + * @param cc_if The cc_if structure.
  39830. + * @param id The identifier of the connection context.
  39831. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  39832. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  39833. +
  39834. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  39835. +
  39836. +/** Checks a buffer for non-zero.
  39837. + * @param id A pointer to a 16 byte buffer.
  39838. + * @return true if the 16 byte value is non-zero. */
  39839. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  39840. + int i;
  39841. + for (i=0; i<16; i++) {
  39842. + if (id[i]) return 1;
  39843. + }
  39844. + return 0;
  39845. +}
  39846. +
  39847. +/** Checks a buffer for zero.
  39848. + * @param id A pointer to a 16 byte buffer.
  39849. + * @return true if the 16 byte value is zero. */
  39850. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  39851. + return !dwc_assoc_is_not_zero_id(id);
  39852. +}
  39853. +
  39854. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  39855. + * buffer. */
  39856. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  39857. + char *ptr = buffer;
  39858. + int i;
  39859. + for (i=0; i<16; i++) {
  39860. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  39861. + if (i < 15) {
  39862. + ptr += DWC_SPRINTF(ptr, " ");
  39863. + }
  39864. + }
  39865. + return ptr - buffer;
  39866. +}
  39867. +
  39868. +/** @} */
  39869. +
  39870. +#ifdef __cplusplus
  39871. +}
  39872. +#endif
  39873. +
  39874. +#endif /* _DWC_CC_H_ */
  39875. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  39876. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 1970-01-01 01:00:00.000000000 +0100
  39877. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 2014-03-11 17:33:06.000000000 +0100
  39878. @@ -0,0 +1,1308 @@
  39879. +#include "dwc_os.h"
  39880. +#include "dwc_list.h"
  39881. +
  39882. +#ifdef DWC_CCLIB
  39883. +# include "dwc_cc.h"
  39884. +#endif
  39885. +
  39886. +#ifdef DWC_CRYPTOLIB
  39887. +# include "dwc_modpow.h"
  39888. +# include "dwc_dh.h"
  39889. +# include "dwc_crypto.h"
  39890. +#endif
  39891. +
  39892. +#ifdef DWC_NOTIFYLIB
  39893. +# include "dwc_notifier.h"
  39894. +#endif
  39895. +
  39896. +/* OS-Level Implementations */
  39897. +
  39898. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  39899. +
  39900. +
  39901. +/* MISC */
  39902. +
  39903. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  39904. +{
  39905. + return memset(dest, byte, size);
  39906. +}
  39907. +
  39908. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  39909. +{
  39910. + return memcpy(dest, src, size);
  39911. +}
  39912. +
  39913. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  39914. +{
  39915. + bcopy(src, dest, size);
  39916. + return dest;
  39917. +}
  39918. +
  39919. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  39920. +{
  39921. + return memcmp(m1, m2, size);
  39922. +}
  39923. +
  39924. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  39925. +{
  39926. + return strncmp(s1, s2, size);
  39927. +}
  39928. +
  39929. +int DWC_STRCMP(void *s1, void *s2)
  39930. +{
  39931. + return strcmp(s1, s2);
  39932. +}
  39933. +
  39934. +int DWC_STRLEN(char const *str)
  39935. +{
  39936. + return strlen(str);
  39937. +}
  39938. +
  39939. +char *DWC_STRCPY(char *to, char const *from)
  39940. +{
  39941. + return strcpy(to, from);
  39942. +}
  39943. +
  39944. +char *DWC_STRDUP(char const *str)
  39945. +{
  39946. + int len = DWC_STRLEN(str) + 1;
  39947. + char *new = DWC_ALLOC_ATOMIC(len);
  39948. +
  39949. + if (!new) {
  39950. + return NULL;
  39951. + }
  39952. +
  39953. + DWC_MEMCPY(new, str, len);
  39954. + return new;
  39955. +}
  39956. +
  39957. +int DWC_ATOI(char *str, int32_t *value)
  39958. +{
  39959. + char *end = NULL;
  39960. +
  39961. + *value = strtol(str, &end, 0);
  39962. + if (*end == '\0') {
  39963. + return 0;
  39964. + }
  39965. +
  39966. + return -1;
  39967. +}
  39968. +
  39969. +int DWC_ATOUI(char *str, uint32_t *value)
  39970. +{
  39971. + char *end = NULL;
  39972. +
  39973. + *value = strtoul(str, &end, 0);
  39974. + if (*end == '\0') {
  39975. + return 0;
  39976. + }
  39977. +
  39978. + return -1;
  39979. +}
  39980. +
  39981. +
  39982. +#ifdef DWC_UTFLIB
  39983. +/* From usbstring.c */
  39984. +
  39985. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  39986. +{
  39987. + int count = 0;
  39988. + u8 c;
  39989. + u16 uchar;
  39990. +
  39991. + /* this insists on correct encodings, though not minimal ones.
  39992. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  39993. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  39994. + */
  39995. + while (len != 0 && (c = (u8) *s++) != 0) {
  39996. + if (unlikely(c & 0x80)) {
  39997. + // 2-byte sequence:
  39998. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  39999. + if ((c & 0xe0) == 0xc0) {
  40000. + uchar = (c & 0x1f) << 6;
  40001. +
  40002. + c = (u8) *s++;
  40003. + if ((c & 0xc0) != 0xc0)
  40004. + goto fail;
  40005. + c &= 0x3f;
  40006. + uchar |= c;
  40007. +
  40008. + // 3-byte sequence (most CJKV characters):
  40009. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  40010. + } else if ((c & 0xf0) == 0xe0) {
  40011. + uchar = (c & 0x0f) << 12;
  40012. +
  40013. + c = (u8) *s++;
  40014. + if ((c & 0xc0) != 0xc0)
  40015. + goto fail;
  40016. + c &= 0x3f;
  40017. + uchar |= c << 6;
  40018. +
  40019. + c = (u8) *s++;
  40020. + if ((c & 0xc0) != 0xc0)
  40021. + goto fail;
  40022. + c &= 0x3f;
  40023. + uchar |= c;
  40024. +
  40025. + /* no bogus surrogates */
  40026. + if (0xd800 <= uchar && uchar <= 0xdfff)
  40027. + goto fail;
  40028. +
  40029. + // 4-byte sequence (surrogate pairs, currently rare):
  40030. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  40031. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  40032. + // (uuuuu = wwww + 1)
  40033. + // FIXME accept the surrogate code points (only)
  40034. + } else
  40035. + goto fail;
  40036. + } else
  40037. + uchar = c;
  40038. + put_unaligned (cpu_to_le16 (uchar), cp++);
  40039. + count++;
  40040. + len--;
  40041. + }
  40042. + return count;
  40043. +fail:
  40044. + return -1;
  40045. +}
  40046. +
  40047. +#endif /* DWC_UTFLIB */
  40048. +
  40049. +
  40050. +/* dwc_debug.h */
  40051. +
  40052. +dwc_bool_t DWC_IN_IRQ(void)
  40053. +{
  40054. +// return in_irq();
  40055. + return 0;
  40056. +}
  40057. +
  40058. +dwc_bool_t DWC_IN_BH(void)
  40059. +{
  40060. +// return in_softirq();
  40061. + return 0;
  40062. +}
  40063. +
  40064. +void DWC_VPRINTF(char *format, va_list args)
  40065. +{
  40066. + vprintf(format, args);
  40067. +}
  40068. +
  40069. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  40070. +{
  40071. + return vsnprintf(str, size, format, args);
  40072. +}
  40073. +
  40074. +void DWC_PRINTF(char *format, ...)
  40075. +{
  40076. + va_list args;
  40077. +
  40078. + va_start(args, format);
  40079. + DWC_VPRINTF(format, args);
  40080. + va_end(args);
  40081. +}
  40082. +
  40083. +int DWC_SPRINTF(char *buffer, char *format, ...)
  40084. +{
  40085. + int retval;
  40086. + va_list args;
  40087. +
  40088. + va_start(args, format);
  40089. + retval = vsprintf(buffer, format, args);
  40090. + va_end(args);
  40091. + return retval;
  40092. +}
  40093. +
  40094. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  40095. +{
  40096. + int retval;
  40097. + va_list args;
  40098. +
  40099. + va_start(args, format);
  40100. + retval = vsnprintf(buffer, size, format, args);
  40101. + va_end(args);
  40102. + return retval;
  40103. +}
  40104. +
  40105. +void __DWC_WARN(char *format, ...)
  40106. +{
  40107. + va_list args;
  40108. +
  40109. + va_start(args, format);
  40110. + DWC_VPRINTF(format, args);
  40111. + va_end(args);
  40112. +}
  40113. +
  40114. +void __DWC_ERROR(char *format, ...)
  40115. +{
  40116. + va_list args;
  40117. +
  40118. + va_start(args, format);
  40119. + DWC_VPRINTF(format, args);
  40120. + va_end(args);
  40121. +}
  40122. +
  40123. +void DWC_EXCEPTION(char *format, ...)
  40124. +{
  40125. + va_list args;
  40126. +
  40127. + va_start(args, format);
  40128. + DWC_VPRINTF(format, args);
  40129. + va_end(args);
  40130. +// BUG_ON(1); ???
  40131. +}
  40132. +
  40133. +#ifdef DEBUG
  40134. +void __DWC_DEBUG(char *format, ...)
  40135. +{
  40136. + va_list args;
  40137. +
  40138. + va_start(args, format);
  40139. + DWC_VPRINTF(format, args);
  40140. + va_end(args);
  40141. +}
  40142. +#endif
  40143. +
  40144. +
  40145. +/* dwc_mem.h */
  40146. +
  40147. +#if 0
  40148. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  40149. + uint32_t align,
  40150. + uint32_t alloc)
  40151. +{
  40152. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  40153. + size, align, alloc);
  40154. + return (dwc_pool_t *)pool;
  40155. +}
  40156. +
  40157. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  40158. +{
  40159. + dma_pool_destroy((struct dma_pool *)pool);
  40160. +}
  40161. +
  40162. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  40163. +{
  40164. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  40165. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  40166. +}
  40167. +
  40168. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  40169. +{
  40170. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  40171. + memset(..);
  40172. +}
  40173. +
  40174. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  40175. +{
  40176. + dma_pool_free(pool, vaddr, daddr);
  40177. +}
  40178. +#endif
  40179. +
  40180. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  40181. +{
  40182. + if (error)
  40183. + return;
  40184. + *(bus_addr_t *)arg = segs[0].ds_addr;
  40185. +}
  40186. +
  40187. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  40188. +{
  40189. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  40190. + int error;
  40191. +
  40192. + error = bus_dma_tag_create(
  40193. +#if __FreeBSD_version >= 700000
  40194. + bus_get_dma_tag(dma->dev), /* parent */
  40195. +#else
  40196. + NULL, /* parent */
  40197. +#endif
  40198. + 4, 0, /* alignment, bounds */
  40199. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  40200. + BUS_SPACE_MAXADDR, /* highaddr */
  40201. + NULL, NULL, /* filter, filterarg */
  40202. + size, /* maxsize */
  40203. + 1, /* nsegments */
  40204. + size, /* maxsegsize */
  40205. + 0, /* flags */
  40206. + NULL, /* lockfunc */
  40207. + NULL, /* lockarg */
  40208. + &dma->dma_tag);
  40209. + if (error) {
  40210. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  40211. + __func__, error);
  40212. + goto fail_0;
  40213. + }
  40214. +
  40215. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  40216. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  40217. + if (error) {
  40218. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  40219. + __func__, (uintmax_t)size, error);
  40220. + goto fail_1;
  40221. + }
  40222. +
  40223. + dma->dma_paddr = 0;
  40224. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  40225. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  40226. + if (error || dma->dma_paddr == 0) {
  40227. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  40228. + __func__, error);
  40229. + goto fail_2;
  40230. + }
  40231. +
  40232. + *dma_addr = dma->dma_paddr;
  40233. + return dma->dma_vaddr;
  40234. +
  40235. +fail_2:
  40236. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  40237. +fail_1:
  40238. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  40239. + bus_dma_tag_destroy(dma->dma_tag);
  40240. +fail_0:
  40241. + dma->dma_map = NULL;
  40242. + dma->dma_tag = NULL;
  40243. +
  40244. + return NULL;
  40245. +}
  40246. +
  40247. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  40248. +{
  40249. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  40250. +
  40251. + if (dma->dma_tag == NULL)
  40252. + return;
  40253. + if (dma->dma_map != NULL) {
  40254. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  40255. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  40256. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  40257. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  40258. + dma->dma_map = NULL;
  40259. + }
  40260. +
  40261. + bus_dma_tag_destroy(dma->dma_tag);
  40262. + dma->dma_tag = NULL;
  40263. +}
  40264. +
  40265. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  40266. +{
  40267. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  40268. +}
  40269. +
  40270. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  40271. +{
  40272. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  40273. +}
  40274. +
  40275. +void __DWC_FREE(void *mem_ctx, void *addr)
  40276. +{
  40277. + free(addr, M_DEVBUF);
  40278. +}
  40279. +
  40280. +
  40281. +#ifdef DWC_CRYPTOLIB
  40282. +/* dwc_crypto.h */
  40283. +
  40284. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  40285. +{
  40286. + get_random_bytes(buffer, length);
  40287. +}
  40288. +
  40289. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  40290. +{
  40291. + struct crypto_blkcipher *tfm;
  40292. + struct blkcipher_desc desc;
  40293. + struct scatterlist sgd;
  40294. + struct scatterlist sgs;
  40295. +
  40296. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  40297. + if (tfm == NULL) {
  40298. + printk("failed to load transform for aes CBC\n");
  40299. + return -1;
  40300. + }
  40301. +
  40302. + crypto_blkcipher_setkey(tfm, key, keylen);
  40303. + crypto_blkcipher_set_iv(tfm, iv, 16);
  40304. +
  40305. + sg_init_one(&sgd, out, messagelen);
  40306. + sg_init_one(&sgs, message, messagelen);
  40307. +
  40308. + desc.tfm = tfm;
  40309. + desc.flags = 0;
  40310. +
  40311. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  40312. + crypto_free_blkcipher(tfm);
  40313. + DWC_ERROR("AES CBC encryption failed");
  40314. + return -1;
  40315. + }
  40316. +
  40317. + crypto_free_blkcipher(tfm);
  40318. + return 0;
  40319. +}
  40320. +
  40321. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  40322. +{
  40323. + struct crypto_hash *tfm;
  40324. + struct hash_desc desc;
  40325. + struct scatterlist sg;
  40326. +
  40327. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  40328. + if (IS_ERR(tfm)) {
  40329. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  40330. + return 0;
  40331. + }
  40332. + desc.tfm = tfm;
  40333. + desc.flags = 0;
  40334. +
  40335. + sg_init_one(&sg, message, len);
  40336. + crypto_hash_digest(&desc, &sg, len, out);
  40337. + crypto_free_hash(tfm);
  40338. +
  40339. + return 1;
  40340. +}
  40341. +
  40342. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  40343. + uint8_t *key, uint32_t keylen, uint8_t *out)
  40344. +{
  40345. + struct crypto_hash *tfm;
  40346. + struct hash_desc desc;
  40347. + struct scatterlist sg;
  40348. +
  40349. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  40350. + if (IS_ERR(tfm)) {
  40351. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  40352. + return 0;
  40353. + }
  40354. + desc.tfm = tfm;
  40355. + desc.flags = 0;
  40356. +
  40357. + sg_init_one(&sg, message, messagelen);
  40358. + crypto_hash_setkey(tfm, key, keylen);
  40359. + crypto_hash_digest(&desc, &sg, messagelen, out);
  40360. + crypto_free_hash(tfm);
  40361. +
  40362. + return 1;
  40363. +}
  40364. +
  40365. +#endif /* DWC_CRYPTOLIB */
  40366. +
  40367. +
  40368. +/* Byte Ordering Conversions */
  40369. +
  40370. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  40371. +{
  40372. +#ifdef __LITTLE_ENDIAN
  40373. + return *p;
  40374. +#else
  40375. + uint8_t *u_p = (uint8_t *)p;
  40376. +
  40377. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40378. +#endif
  40379. +}
  40380. +
  40381. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  40382. +{
  40383. +#ifdef __BIG_ENDIAN
  40384. + return *p;
  40385. +#else
  40386. + uint8_t *u_p = (uint8_t *)p;
  40387. +
  40388. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40389. +#endif
  40390. +}
  40391. +
  40392. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  40393. +{
  40394. +#ifdef __LITTLE_ENDIAN
  40395. + return *p;
  40396. +#else
  40397. + uint8_t *u_p = (uint8_t *)p;
  40398. +
  40399. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40400. +#endif
  40401. +}
  40402. +
  40403. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  40404. +{
  40405. +#ifdef __BIG_ENDIAN
  40406. + return *p;
  40407. +#else
  40408. + uint8_t *u_p = (uint8_t *)p;
  40409. +
  40410. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40411. +#endif
  40412. +}
  40413. +
  40414. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  40415. +{
  40416. +#ifdef __LITTLE_ENDIAN
  40417. + return *p;
  40418. +#else
  40419. + uint8_t *u_p = (uint8_t *)p;
  40420. + return (u_p[1] | (u_p[0] << 8));
  40421. +#endif
  40422. +}
  40423. +
  40424. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  40425. +{
  40426. +#ifdef __BIG_ENDIAN
  40427. + return *p;
  40428. +#else
  40429. + uint8_t *u_p = (uint8_t *)p;
  40430. + return (u_p[1] | (u_p[0] << 8));
  40431. +#endif
  40432. +}
  40433. +
  40434. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  40435. +{
  40436. +#ifdef __LITTLE_ENDIAN
  40437. + return *p;
  40438. +#else
  40439. + uint8_t *u_p = (uint8_t *)p;
  40440. + return (u_p[1] | (u_p[0] << 8));
  40441. +#endif
  40442. +}
  40443. +
  40444. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  40445. +{
  40446. +#ifdef __BIG_ENDIAN
  40447. + return *p;
  40448. +#else
  40449. + uint8_t *u_p = (uint8_t *)p;
  40450. + return (u_p[1] | (u_p[0] << 8));
  40451. +#endif
  40452. +}
  40453. +
  40454. +
  40455. +/* Registers */
  40456. +
  40457. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  40458. +{
  40459. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40460. + bus_size_t ior = (bus_size_t)reg;
  40461. +
  40462. + return bus_space_read_4(io->iot, io->ioh, ior);
  40463. +}
  40464. +
  40465. +#if 0
  40466. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  40467. +{
  40468. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40469. + bus_size_t ior = (bus_size_t)reg;
  40470. +
  40471. + return bus_space_read_8(io->iot, io->ioh, ior);
  40472. +}
  40473. +#endif
  40474. +
  40475. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  40476. +{
  40477. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40478. + bus_size_t ior = (bus_size_t)reg;
  40479. +
  40480. + bus_space_write_4(io->iot, io->ioh, ior, value);
  40481. +}
  40482. +
  40483. +#if 0
  40484. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  40485. +{
  40486. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40487. + bus_size_t ior = (bus_size_t)reg;
  40488. +
  40489. + bus_space_write_8(io->iot, io->ioh, ior, value);
  40490. +}
  40491. +#endif
  40492. +
  40493. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  40494. + uint32_t set_mask)
  40495. +{
  40496. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40497. + bus_size_t ior = (bus_size_t)reg;
  40498. +
  40499. + bus_space_write_4(io->iot, io->ioh, ior,
  40500. + (bus_space_read_4(io->iot, io->ioh, ior) &
  40501. + ~clear_mask) | set_mask);
  40502. +}
  40503. +
  40504. +#if 0
  40505. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  40506. + uint64_t set_mask)
  40507. +{
  40508. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40509. + bus_size_t ior = (bus_size_t)reg;
  40510. +
  40511. + bus_space_write_8(io->iot, io->ioh, ior,
  40512. + (bus_space_read_8(io->iot, io->ioh, ior) &
  40513. + ~clear_mask) | set_mask);
  40514. +}
  40515. +#endif
  40516. +
  40517. +
  40518. +/* Locking */
  40519. +
  40520. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  40521. +{
  40522. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  40523. +
  40524. + if (!sl) {
  40525. + DWC_ERROR("Cannot allocate memory for spinlock");
  40526. + return NULL;
  40527. + }
  40528. +
  40529. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  40530. + return (dwc_spinlock_t *)sl;
  40531. +}
  40532. +
  40533. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  40534. +{
  40535. + struct mtx *sl = (struct mtx *)lock;
  40536. +
  40537. + mtx_destroy(sl);
  40538. + DWC_FREE(sl);
  40539. +}
  40540. +
  40541. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  40542. +{
  40543. + mtx_lock_spin((struct mtx *)lock); // ???
  40544. +}
  40545. +
  40546. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  40547. +{
  40548. + mtx_unlock_spin((struct mtx *)lock); // ???
  40549. +}
  40550. +
  40551. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  40552. +{
  40553. + mtx_lock_spin((struct mtx *)lock);
  40554. +}
  40555. +
  40556. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  40557. +{
  40558. + mtx_unlock_spin((struct mtx *)lock);
  40559. +}
  40560. +
  40561. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  40562. +{
  40563. + struct mtx *m;
  40564. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  40565. +
  40566. + if (!mutex) {
  40567. + DWC_ERROR("Cannot allocate memory for mutex");
  40568. + return NULL;
  40569. + }
  40570. +
  40571. + m = (struct mtx *)mutex;
  40572. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  40573. + return mutex;
  40574. +}
  40575. +
  40576. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  40577. +#else
  40578. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  40579. +{
  40580. + mtx_destroy((struct mtx *)mutex);
  40581. + DWC_FREE(mutex);
  40582. +}
  40583. +#endif
  40584. +
  40585. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  40586. +{
  40587. + struct mtx *m = (struct mtx *)mutex;
  40588. +
  40589. + mtx_lock(m);
  40590. +}
  40591. +
  40592. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  40593. +{
  40594. + struct mtx *m = (struct mtx *)mutex;
  40595. +
  40596. + return mtx_trylock(m);
  40597. +}
  40598. +
  40599. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  40600. +{
  40601. + struct mtx *m = (struct mtx *)mutex;
  40602. +
  40603. + mtx_unlock(m);
  40604. +}
  40605. +
  40606. +
  40607. +/* Timing */
  40608. +
  40609. +void DWC_UDELAY(uint32_t usecs)
  40610. +{
  40611. + DELAY(usecs);
  40612. +}
  40613. +
  40614. +void DWC_MDELAY(uint32_t msecs)
  40615. +{
  40616. + do {
  40617. + DELAY(1000);
  40618. + } while (--msecs);
  40619. +}
  40620. +
  40621. +void DWC_MSLEEP(uint32_t msecs)
  40622. +{
  40623. + struct timeval tv;
  40624. +
  40625. + tv.tv_sec = msecs / 1000;
  40626. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  40627. + pause("dw3slp", tvtohz(&tv));
  40628. +}
  40629. +
  40630. +uint32_t DWC_TIME(void)
  40631. +{
  40632. + struct timeval tv;
  40633. +
  40634. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  40635. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  40636. +}
  40637. +
  40638. +
  40639. +/* Timers */
  40640. +
  40641. +struct dwc_timer {
  40642. + struct callout t;
  40643. + char *name;
  40644. + dwc_spinlock_t *lock;
  40645. + dwc_timer_callback_t cb;
  40646. + void *data;
  40647. +};
  40648. +
  40649. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  40650. +{
  40651. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  40652. +
  40653. + if (!t) {
  40654. + DWC_ERROR("Cannot allocate memory for timer");
  40655. + return NULL;
  40656. + }
  40657. +
  40658. + callout_init(&t->t, 1);
  40659. +
  40660. + t->name = DWC_STRDUP(name);
  40661. + if (!t->name) {
  40662. + DWC_ERROR("Cannot allocate memory for timer->name");
  40663. + goto no_name;
  40664. + }
  40665. +
  40666. + t->lock = DWC_SPINLOCK_ALLOC();
  40667. + if (!t->lock) {
  40668. + DWC_ERROR("Cannot allocate memory for lock");
  40669. + goto no_lock;
  40670. + }
  40671. +
  40672. + t->cb = cb;
  40673. + t->data = data;
  40674. +
  40675. + return t;
  40676. +
  40677. + no_lock:
  40678. + DWC_FREE(t->name);
  40679. + no_name:
  40680. + DWC_FREE(t);
  40681. +
  40682. + return NULL;
  40683. +}
  40684. +
  40685. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  40686. +{
  40687. + callout_stop(&timer->t);
  40688. + DWC_SPINLOCK_FREE(timer->lock);
  40689. + DWC_FREE(timer->name);
  40690. + DWC_FREE(timer);
  40691. +}
  40692. +
  40693. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  40694. +{
  40695. + struct timeval tv;
  40696. +
  40697. + tv.tv_sec = time / 1000;
  40698. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  40699. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  40700. +}
  40701. +
  40702. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  40703. +{
  40704. + callout_stop(&timer->t);
  40705. +}
  40706. +
  40707. +
  40708. +/* Wait Queues */
  40709. +
  40710. +struct dwc_waitq {
  40711. + struct mtx lock;
  40712. + int abort;
  40713. +};
  40714. +
  40715. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  40716. +{
  40717. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  40718. +
  40719. + if (!wq) {
  40720. + DWC_ERROR("Cannot allocate memory for waitqueue");
  40721. + return NULL;
  40722. + }
  40723. +
  40724. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  40725. + wq->abort = 0;
  40726. +
  40727. + return wq;
  40728. +}
  40729. +
  40730. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  40731. +{
  40732. + mtx_destroy(&wq->lock);
  40733. + DWC_FREE(wq);
  40734. +}
  40735. +
  40736. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  40737. +{
  40738. +// intrmask_t ipl;
  40739. + int result = 0;
  40740. +
  40741. + mtx_lock(&wq->lock);
  40742. +// ipl = splbio();
  40743. +
  40744. + /* Skip the sleep if already aborted or triggered */
  40745. + if (!wq->abort && !cond(data)) {
  40746. +// splx(ipl);
  40747. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  40748. +// ipl = splbio();
  40749. + }
  40750. +
  40751. + if (result == ERESTART) { // signaled - restart
  40752. + result = -DWC_E_RESTART;
  40753. +
  40754. + } else if (result == EINTR) { // signaled - interrupt
  40755. + result = -DWC_E_ABORT;
  40756. +
  40757. + } else if (wq->abort) {
  40758. + result = -DWC_E_ABORT;
  40759. +
  40760. + } else {
  40761. + result = 0;
  40762. + }
  40763. +
  40764. + wq->abort = 0;
  40765. +// splx(ipl);
  40766. + mtx_unlock(&wq->lock);
  40767. + return result;
  40768. +}
  40769. +
  40770. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  40771. + void *data, int32_t msecs)
  40772. +{
  40773. + struct timeval tv, tv1, tv2;
  40774. +// intrmask_t ipl;
  40775. + int result = 0;
  40776. +
  40777. + tv.tv_sec = msecs / 1000;
  40778. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  40779. +
  40780. + mtx_lock(&wq->lock);
  40781. +// ipl = splbio();
  40782. +
  40783. + /* Skip the sleep if already aborted or triggered */
  40784. + if (!wq->abort && !cond(data)) {
  40785. +// splx(ipl);
  40786. + getmicrouptime(&tv1);
  40787. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  40788. + getmicrouptime(&tv2);
  40789. +// ipl = splbio();
  40790. + }
  40791. +
  40792. + if (result == 0) { // awoken
  40793. + if (wq->abort) {
  40794. + result = -DWC_E_ABORT;
  40795. + } else {
  40796. + tv2.tv_usec -= tv1.tv_usec;
  40797. + if (tv2.tv_usec < 0) {
  40798. + tv2.tv_usec += 1000000;
  40799. + tv2.tv_sec--;
  40800. + }
  40801. +
  40802. + tv2.tv_sec -= tv1.tv_sec;
  40803. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  40804. + result = msecs - result;
  40805. + if (result <= 0)
  40806. + result = 1;
  40807. + }
  40808. + } else if (result == ERESTART) { // signaled - restart
  40809. + result = -DWC_E_RESTART;
  40810. +
  40811. + } else if (result == EINTR) { // signaled - interrupt
  40812. + result = -DWC_E_ABORT;
  40813. +
  40814. + } else { // timed out
  40815. + result = -DWC_E_TIMEOUT;
  40816. + }
  40817. +
  40818. + wq->abort = 0;
  40819. +// splx(ipl);
  40820. + mtx_unlock(&wq->lock);
  40821. + return result;
  40822. +}
  40823. +
  40824. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  40825. +{
  40826. + wakeup(wq);
  40827. +}
  40828. +
  40829. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  40830. +{
  40831. +// intrmask_t ipl;
  40832. +
  40833. + mtx_lock(&wq->lock);
  40834. +// ipl = splbio();
  40835. + wq->abort = 1;
  40836. + wakeup(wq);
  40837. +// splx(ipl);
  40838. + mtx_unlock(&wq->lock);
  40839. +}
  40840. +
  40841. +
  40842. +/* Threading */
  40843. +
  40844. +struct dwc_thread {
  40845. + struct proc *proc;
  40846. + int abort;
  40847. +};
  40848. +
  40849. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  40850. +{
  40851. + int retval;
  40852. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  40853. +
  40854. + if (!thread) {
  40855. + return NULL;
  40856. + }
  40857. +
  40858. + thread->abort = 0;
  40859. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  40860. + RFPROC | RFNOWAIT, 0, "%s", name);
  40861. + if (retval) {
  40862. + DWC_FREE(thread);
  40863. + return NULL;
  40864. + }
  40865. +
  40866. + return thread;
  40867. +}
  40868. +
  40869. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  40870. +{
  40871. + int retval;
  40872. +
  40873. + thread->abort = 1;
  40874. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  40875. +
  40876. + if (retval == 0) {
  40877. + /* DWC_THREAD_EXIT() will free the thread struct */
  40878. + return 0;
  40879. + }
  40880. +
  40881. + /* NOTE: We leak the thread struct if thread doesn't die */
  40882. +
  40883. + if (retval == EWOULDBLOCK) {
  40884. + return -DWC_E_TIMEOUT;
  40885. + }
  40886. +
  40887. + return -DWC_E_UNKNOWN;
  40888. +}
  40889. +
  40890. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  40891. +{
  40892. + return thread->abort;
  40893. +}
  40894. +
  40895. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  40896. +{
  40897. + wakeup(&thread->abort);
  40898. + DWC_FREE(thread);
  40899. + kthread_exit(0);
  40900. +}
  40901. +
  40902. +
  40903. +/* tasklets
  40904. + - Runs in interrupt context (cannot sleep)
  40905. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  40906. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  40907. + */
  40908. +struct dwc_tasklet {
  40909. + struct task t;
  40910. + dwc_tasklet_callback_t cb;
  40911. + void *data;
  40912. +};
  40913. +
  40914. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  40915. +{
  40916. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  40917. +
  40918. + task->cb(task->data);
  40919. +}
  40920. +
  40921. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  40922. +{
  40923. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  40924. +
  40925. + if (task) {
  40926. + task->cb = cb;
  40927. + task->data = data;
  40928. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  40929. + } else {
  40930. + DWC_ERROR("Cannot allocate memory for tasklet");
  40931. + }
  40932. +
  40933. + return task;
  40934. +}
  40935. +
  40936. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  40937. +{
  40938. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  40939. + DWC_FREE(task);
  40940. +}
  40941. +
  40942. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  40943. +{
  40944. + /* Uses predefined system queue */
  40945. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  40946. +}
  40947. +
  40948. +
  40949. +/* workqueues
  40950. + - Runs in process context (can sleep)
  40951. + */
  40952. +typedef struct work_container {
  40953. + dwc_work_callback_t cb;
  40954. + void *data;
  40955. + dwc_workq_t *wq;
  40956. + char *name;
  40957. + int hz;
  40958. +
  40959. +#ifdef DEBUG
  40960. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  40961. +#endif
  40962. + struct task task;
  40963. +} work_container_t;
  40964. +
  40965. +#ifdef DEBUG
  40966. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  40967. +#endif
  40968. +
  40969. +struct dwc_workq {
  40970. + struct taskqueue *taskq;
  40971. + dwc_spinlock_t *lock;
  40972. + dwc_waitq_t *waitq;
  40973. + int pending;
  40974. +
  40975. +#ifdef DEBUG
  40976. + struct work_container_queue entries;
  40977. +#endif
  40978. +};
  40979. +
  40980. +static void do_work(void *data, int pending) // what to do with pending ???
  40981. +{
  40982. + work_container_t *container = (work_container_t *)data;
  40983. + dwc_workq_t *wq = container->wq;
  40984. + dwc_irqflags_t flags;
  40985. +
  40986. + if (container->hz) {
  40987. + pause("dw3wrk", container->hz);
  40988. + }
  40989. +
  40990. + container->cb(container->data);
  40991. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  40992. +
  40993. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  40994. +
  40995. +#ifdef DEBUG
  40996. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  40997. +#endif
  40998. + if (container->name)
  40999. + DWC_FREE(container->name);
  41000. + DWC_FREE(container);
  41001. + wq->pending--;
  41002. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41003. + DWC_WAITQ_TRIGGER(wq->waitq);
  41004. +}
  41005. +
  41006. +static int work_done(void *data)
  41007. +{
  41008. + dwc_workq_t *workq = (dwc_workq_t *)data;
  41009. +
  41010. + return workq->pending == 0;
  41011. +}
  41012. +
  41013. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  41014. +{
  41015. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  41016. +}
  41017. +
  41018. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  41019. +{
  41020. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  41021. +
  41022. + if (!wq) {
  41023. + DWC_ERROR("Cannot allocate memory for workqueue");
  41024. + return NULL;
  41025. + }
  41026. +
  41027. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  41028. + if (!wq->taskq) {
  41029. + DWC_ERROR("Cannot allocate memory for taskqueue");
  41030. + goto no_taskq;
  41031. + }
  41032. +
  41033. + wq->pending = 0;
  41034. +
  41035. + wq->lock = DWC_SPINLOCK_ALLOC();
  41036. + if (!wq->lock) {
  41037. + DWC_ERROR("Cannot allocate memory for spinlock");
  41038. + goto no_lock;
  41039. + }
  41040. +
  41041. + wq->waitq = DWC_WAITQ_ALLOC();
  41042. + if (!wq->waitq) {
  41043. + DWC_ERROR("Cannot allocate memory for waitqueue");
  41044. + goto no_waitq;
  41045. + }
  41046. +
  41047. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  41048. +
  41049. +#ifdef DEBUG
  41050. + DWC_CIRCLEQ_INIT(&wq->entries);
  41051. +#endif
  41052. + return wq;
  41053. +
  41054. + no_waitq:
  41055. + DWC_SPINLOCK_FREE(wq->lock);
  41056. + no_lock:
  41057. + taskqueue_free(wq->taskq);
  41058. + no_taskq:
  41059. + DWC_FREE(wq);
  41060. +
  41061. + return NULL;
  41062. +}
  41063. +
  41064. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  41065. +{
  41066. +#ifdef DEBUG
  41067. + dwc_irqflags_t flags;
  41068. +
  41069. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41070. +
  41071. + if (wq->pending != 0) {
  41072. + struct work_container *container;
  41073. +
  41074. + DWC_ERROR("Destroying work queue with pending work");
  41075. +
  41076. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  41077. + DWC_ERROR("Work %s still pending", container->name);
  41078. + }
  41079. + }
  41080. +
  41081. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41082. +#endif
  41083. + DWC_WAITQ_FREE(wq->waitq);
  41084. + DWC_SPINLOCK_FREE(wq->lock);
  41085. + taskqueue_free(wq->taskq);
  41086. + DWC_FREE(wq);
  41087. +}
  41088. +
  41089. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  41090. + char *format, ...)
  41091. +{
  41092. + dwc_irqflags_t flags;
  41093. + work_container_t *container;
  41094. + static char name[128];
  41095. + va_list args;
  41096. +
  41097. + va_start(args, format);
  41098. + DWC_VSNPRINTF(name, 128, format, args);
  41099. + va_end(args);
  41100. +
  41101. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41102. + wq->pending++;
  41103. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41104. + DWC_WAITQ_TRIGGER(wq->waitq);
  41105. +
  41106. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  41107. + if (!container) {
  41108. + DWC_ERROR("Cannot allocate memory for container");
  41109. + return;
  41110. + }
  41111. +
  41112. + container->name = DWC_STRDUP(name);
  41113. + if (!container->name) {
  41114. + DWC_ERROR("Cannot allocate memory for container->name");
  41115. + DWC_FREE(container);
  41116. + return;
  41117. + }
  41118. +
  41119. + container->cb = cb;
  41120. + container->data = data;
  41121. + container->wq = wq;
  41122. + container->hz = 0;
  41123. +
  41124. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  41125. +
  41126. + TASK_INIT(&container->task, 0, do_work, container);
  41127. +
  41128. +#ifdef DEBUG
  41129. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  41130. +#endif
  41131. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  41132. +}
  41133. +
  41134. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  41135. + void *data, uint32_t time, char *format, ...)
  41136. +{
  41137. + dwc_irqflags_t flags;
  41138. + work_container_t *container;
  41139. + static char name[128];
  41140. + struct timeval tv;
  41141. + va_list args;
  41142. +
  41143. + va_start(args, format);
  41144. + DWC_VSNPRINTF(name, 128, format, args);
  41145. + va_end(args);
  41146. +
  41147. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41148. + wq->pending++;
  41149. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41150. + DWC_WAITQ_TRIGGER(wq->waitq);
  41151. +
  41152. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  41153. + if (!container) {
  41154. + DWC_ERROR("Cannot allocate memory for container");
  41155. + return;
  41156. + }
  41157. +
  41158. + container->name = DWC_STRDUP(name);
  41159. + if (!container->name) {
  41160. + DWC_ERROR("Cannot allocate memory for container->name");
  41161. + DWC_FREE(container);
  41162. + return;
  41163. + }
  41164. +
  41165. + container->cb = cb;
  41166. + container->data = data;
  41167. + container->wq = wq;
  41168. +
  41169. + tv.tv_sec = time / 1000;
  41170. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  41171. + container->hz = tvtohz(&tv);
  41172. +
  41173. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  41174. +
  41175. + TASK_INIT(&container->task, 0, do_work, container);
  41176. +
  41177. +#ifdef DEBUG
  41178. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  41179. +#endif
  41180. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  41181. +}
  41182. +
  41183. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  41184. +{
  41185. + return wq->pending;
  41186. +}
  41187. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_common_linux.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  41188. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_common_linux.c 1970-01-01 01:00:00.000000000 +0100
  41189. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_linux.c 2014-03-11 17:33:06.000000000 +0100
  41190. @@ -0,0 +1,1431 @@
  41191. +#include <linux/kernel.h>
  41192. +#include <linux/init.h>
  41193. +#include <linux/module.h>
  41194. +#include <linux/kthread.h>
  41195. +
  41196. +#ifdef DWC_CCLIB
  41197. +# include "dwc_cc.h"
  41198. +#endif
  41199. +
  41200. +#ifdef DWC_CRYPTOLIB
  41201. +# include "dwc_modpow.h"
  41202. +# include "dwc_dh.h"
  41203. +# include "dwc_crypto.h"
  41204. +#endif
  41205. +
  41206. +#ifdef DWC_NOTIFYLIB
  41207. +# include "dwc_notifier.h"
  41208. +#endif
  41209. +
  41210. +/* OS-Level Implementations */
  41211. +
  41212. +/* This is the Linux kernel implementation of the DWC platform library. */
  41213. +#include <linux/moduleparam.h>
  41214. +#include <linux/ctype.h>
  41215. +#include <linux/crypto.h>
  41216. +#include <linux/delay.h>
  41217. +#include <linux/device.h>
  41218. +#include <linux/dma-mapping.h>
  41219. +#include <linux/cdev.h>
  41220. +#include <linux/errno.h>
  41221. +#include <linux/interrupt.h>
  41222. +#include <linux/jiffies.h>
  41223. +#include <linux/list.h>
  41224. +#include <linux/pci.h>
  41225. +#include <linux/random.h>
  41226. +#include <linux/scatterlist.h>
  41227. +#include <linux/slab.h>
  41228. +#include <linux/stat.h>
  41229. +#include <linux/string.h>
  41230. +#include <linux/timer.h>
  41231. +#include <linux/usb.h>
  41232. +
  41233. +#include <linux/version.h>
  41234. +
  41235. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  41236. +# include <linux/usb/gadget.h>
  41237. +#else
  41238. +# include <linux/usb_gadget.h>
  41239. +#endif
  41240. +
  41241. +#include <asm/io.h>
  41242. +#include <asm/page.h>
  41243. +#include <asm/uaccess.h>
  41244. +#include <asm/unaligned.h>
  41245. +
  41246. +#include "dwc_os.h"
  41247. +#include "dwc_list.h"
  41248. +
  41249. +
  41250. +/* MISC */
  41251. +
  41252. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  41253. +{
  41254. + return memset(dest, byte, size);
  41255. +}
  41256. +
  41257. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  41258. +{
  41259. + return memcpy(dest, src, size);
  41260. +}
  41261. +
  41262. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  41263. +{
  41264. + return memmove(dest, src, size);
  41265. +}
  41266. +
  41267. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  41268. +{
  41269. + return memcmp(m1, m2, size);
  41270. +}
  41271. +
  41272. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  41273. +{
  41274. + return strncmp(s1, s2, size);
  41275. +}
  41276. +
  41277. +int DWC_STRCMP(void *s1, void *s2)
  41278. +{
  41279. + return strcmp(s1, s2);
  41280. +}
  41281. +
  41282. +int DWC_STRLEN(char const *str)
  41283. +{
  41284. + return strlen(str);
  41285. +}
  41286. +
  41287. +char *DWC_STRCPY(char *to, char const *from)
  41288. +{
  41289. + return strcpy(to, from);
  41290. +}
  41291. +
  41292. +char *DWC_STRDUP(char const *str)
  41293. +{
  41294. + int len = DWC_STRLEN(str) + 1;
  41295. + char *new = DWC_ALLOC_ATOMIC(len);
  41296. +
  41297. + if (!new) {
  41298. + return NULL;
  41299. + }
  41300. +
  41301. + DWC_MEMCPY(new, str, len);
  41302. + return new;
  41303. +}
  41304. +
  41305. +int DWC_ATOI(const char *str, int32_t *value)
  41306. +{
  41307. + char *end = NULL;
  41308. +
  41309. + *value = simple_strtol(str, &end, 0);
  41310. + if (*end == '\0') {
  41311. + return 0;
  41312. + }
  41313. +
  41314. + return -1;
  41315. +}
  41316. +
  41317. +int DWC_ATOUI(const char *str, uint32_t *value)
  41318. +{
  41319. + char *end = NULL;
  41320. +
  41321. + *value = simple_strtoul(str, &end, 0);
  41322. + if (*end == '\0') {
  41323. + return 0;
  41324. + }
  41325. +
  41326. + return -1;
  41327. +}
  41328. +
  41329. +
  41330. +#ifdef DWC_UTFLIB
  41331. +/* From usbstring.c */
  41332. +
  41333. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  41334. +{
  41335. + int count = 0;
  41336. + u8 c;
  41337. + u16 uchar;
  41338. +
  41339. + /* this insists on correct encodings, though not minimal ones.
  41340. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  41341. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  41342. + */
  41343. + while (len != 0 && (c = (u8) *s++) != 0) {
  41344. + if (unlikely(c & 0x80)) {
  41345. + // 2-byte sequence:
  41346. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  41347. + if ((c & 0xe0) == 0xc0) {
  41348. + uchar = (c & 0x1f) << 6;
  41349. +
  41350. + c = (u8) *s++;
  41351. + if ((c & 0xc0) != 0xc0)
  41352. + goto fail;
  41353. + c &= 0x3f;
  41354. + uchar |= c;
  41355. +
  41356. + // 3-byte sequence (most CJKV characters):
  41357. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  41358. + } else if ((c & 0xf0) == 0xe0) {
  41359. + uchar = (c & 0x0f) << 12;
  41360. +
  41361. + c = (u8) *s++;
  41362. + if ((c & 0xc0) != 0xc0)
  41363. + goto fail;
  41364. + c &= 0x3f;
  41365. + uchar |= c << 6;
  41366. +
  41367. + c = (u8) *s++;
  41368. + if ((c & 0xc0) != 0xc0)
  41369. + goto fail;
  41370. + c &= 0x3f;
  41371. + uchar |= c;
  41372. +
  41373. + /* no bogus surrogates */
  41374. + if (0xd800 <= uchar && uchar <= 0xdfff)
  41375. + goto fail;
  41376. +
  41377. + // 4-byte sequence (surrogate pairs, currently rare):
  41378. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  41379. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  41380. + // (uuuuu = wwww + 1)
  41381. + // FIXME accept the surrogate code points (only)
  41382. + } else
  41383. + goto fail;
  41384. + } else
  41385. + uchar = c;
  41386. + put_unaligned (cpu_to_le16 (uchar), cp++);
  41387. + count++;
  41388. + len--;
  41389. + }
  41390. + return count;
  41391. +fail:
  41392. + return -1;
  41393. +}
  41394. +#endif /* DWC_UTFLIB */
  41395. +
  41396. +
  41397. +/* dwc_debug.h */
  41398. +
  41399. +dwc_bool_t DWC_IN_IRQ(void)
  41400. +{
  41401. + return in_irq();
  41402. +}
  41403. +
  41404. +dwc_bool_t DWC_IN_BH(void)
  41405. +{
  41406. + return in_softirq();
  41407. +}
  41408. +
  41409. +void DWC_VPRINTF(char *format, va_list args)
  41410. +{
  41411. + vprintk(format, args);
  41412. +}
  41413. +
  41414. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  41415. +{
  41416. + return vsnprintf(str, size, format, args);
  41417. +}
  41418. +
  41419. +void DWC_PRINTF(char *format, ...)
  41420. +{
  41421. + va_list args;
  41422. +
  41423. + va_start(args, format);
  41424. + DWC_VPRINTF(format, args);
  41425. + va_end(args);
  41426. +}
  41427. +
  41428. +int DWC_SPRINTF(char *buffer, char *format, ...)
  41429. +{
  41430. + int retval;
  41431. + va_list args;
  41432. +
  41433. + va_start(args, format);
  41434. + retval = vsprintf(buffer, format, args);
  41435. + va_end(args);
  41436. + return retval;
  41437. +}
  41438. +
  41439. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  41440. +{
  41441. + int retval;
  41442. + va_list args;
  41443. +
  41444. + va_start(args, format);
  41445. + retval = vsnprintf(buffer, size, format, args);
  41446. + va_end(args);
  41447. + return retval;
  41448. +}
  41449. +
  41450. +void __DWC_WARN(char *format, ...)
  41451. +{
  41452. + va_list args;
  41453. +
  41454. + va_start(args, format);
  41455. + DWC_PRINTF(KERN_WARNING);
  41456. + DWC_VPRINTF(format, args);
  41457. + va_end(args);
  41458. +}
  41459. +
  41460. +void __DWC_ERROR(char *format, ...)
  41461. +{
  41462. + va_list args;
  41463. +
  41464. + va_start(args, format);
  41465. + DWC_PRINTF(KERN_ERR);
  41466. + DWC_VPRINTF(format, args);
  41467. + va_end(args);
  41468. +}
  41469. +
  41470. +void DWC_EXCEPTION(char *format, ...)
  41471. +{
  41472. + va_list args;
  41473. +
  41474. + va_start(args, format);
  41475. + DWC_PRINTF(KERN_ERR);
  41476. + DWC_VPRINTF(format, args);
  41477. + va_end(args);
  41478. + BUG_ON(1);
  41479. +}
  41480. +
  41481. +#ifdef DEBUG
  41482. +void __DWC_DEBUG(char *format, ...)
  41483. +{
  41484. + va_list args;
  41485. +
  41486. + va_start(args, format);
  41487. + DWC_PRINTF(KERN_DEBUG);
  41488. + DWC_VPRINTF(format, args);
  41489. + va_end(args);
  41490. +}
  41491. +#endif
  41492. +
  41493. +
  41494. +/* dwc_mem.h */
  41495. +
  41496. +#if 0
  41497. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  41498. + uint32_t align,
  41499. + uint32_t alloc)
  41500. +{
  41501. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  41502. + size, align, alloc);
  41503. + return (dwc_pool_t *)pool;
  41504. +}
  41505. +
  41506. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  41507. +{
  41508. + dma_pool_destroy((struct dma_pool *)pool);
  41509. +}
  41510. +
  41511. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  41512. +{
  41513. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  41514. +}
  41515. +
  41516. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  41517. +{
  41518. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  41519. + memset(..);
  41520. +}
  41521. +
  41522. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  41523. +{
  41524. + dma_pool_free(pool, vaddr, daddr);
  41525. +}
  41526. +#endif
  41527. +
  41528. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  41529. +{
  41530. +#ifdef xxCOSIM /* Only works for 32-bit cosim */
  41531. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL);
  41532. +#else
  41533. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
  41534. +#endif
  41535. + if (!buf) {
  41536. + return NULL;
  41537. + }
  41538. +
  41539. + memset(buf, 0, (size_t)size);
  41540. + return buf;
  41541. +}
  41542. +
  41543. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  41544. +{
  41545. + void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
  41546. + if (!buf) {
  41547. + return NULL;
  41548. + }
  41549. + memset(buf, 0, (size_t)size);
  41550. + return buf;
  41551. +}
  41552. +
  41553. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  41554. +{
  41555. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  41556. +}
  41557. +
  41558. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  41559. +{
  41560. + return kzalloc(size, GFP_KERNEL);
  41561. +}
  41562. +
  41563. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  41564. +{
  41565. + return kzalloc(size, GFP_ATOMIC);
  41566. +}
  41567. +
  41568. +void __DWC_FREE(void *mem_ctx, void *addr)
  41569. +{
  41570. + kfree(addr);
  41571. +}
  41572. +
  41573. +
  41574. +#ifdef DWC_CRYPTOLIB
  41575. +/* dwc_crypto.h */
  41576. +
  41577. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  41578. +{
  41579. + get_random_bytes(buffer, length);
  41580. +}
  41581. +
  41582. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  41583. +{
  41584. + struct crypto_blkcipher *tfm;
  41585. + struct blkcipher_desc desc;
  41586. + struct scatterlist sgd;
  41587. + struct scatterlist sgs;
  41588. +
  41589. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  41590. + if (tfm == NULL) {
  41591. + printk("failed to load transform for aes CBC\n");
  41592. + return -1;
  41593. + }
  41594. +
  41595. + crypto_blkcipher_setkey(tfm, key, keylen);
  41596. + crypto_blkcipher_set_iv(tfm, iv, 16);
  41597. +
  41598. + sg_init_one(&sgd, out, messagelen);
  41599. + sg_init_one(&sgs, message, messagelen);
  41600. +
  41601. + desc.tfm = tfm;
  41602. + desc.flags = 0;
  41603. +
  41604. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  41605. + crypto_free_blkcipher(tfm);
  41606. + DWC_ERROR("AES CBC encryption failed");
  41607. + return -1;
  41608. + }
  41609. +
  41610. + crypto_free_blkcipher(tfm);
  41611. + return 0;
  41612. +}
  41613. +
  41614. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  41615. +{
  41616. + struct crypto_hash *tfm;
  41617. + struct hash_desc desc;
  41618. + struct scatterlist sg;
  41619. +
  41620. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  41621. + if (IS_ERR(tfm)) {
  41622. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  41623. + return 0;
  41624. + }
  41625. + desc.tfm = tfm;
  41626. + desc.flags = 0;
  41627. +
  41628. + sg_init_one(&sg, message, len);
  41629. + crypto_hash_digest(&desc, &sg, len, out);
  41630. + crypto_free_hash(tfm);
  41631. +
  41632. + return 1;
  41633. +}
  41634. +
  41635. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  41636. + uint8_t *key, uint32_t keylen, uint8_t *out)
  41637. +{
  41638. + struct crypto_hash *tfm;
  41639. + struct hash_desc desc;
  41640. + struct scatterlist sg;
  41641. +
  41642. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  41643. + if (IS_ERR(tfm)) {
  41644. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  41645. + return 0;
  41646. + }
  41647. + desc.tfm = tfm;
  41648. + desc.flags = 0;
  41649. +
  41650. + sg_init_one(&sg, message, messagelen);
  41651. + crypto_hash_setkey(tfm, key, keylen);
  41652. + crypto_hash_digest(&desc, &sg, messagelen, out);
  41653. + crypto_free_hash(tfm);
  41654. +
  41655. + return 1;
  41656. +}
  41657. +#endif /* DWC_CRYPTOLIB */
  41658. +
  41659. +
  41660. +/* Byte Ordering Conversions */
  41661. +
  41662. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  41663. +{
  41664. +#ifdef __LITTLE_ENDIAN
  41665. + return *p;
  41666. +#else
  41667. + uint8_t *u_p = (uint8_t *)p;
  41668. +
  41669. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41670. +#endif
  41671. +}
  41672. +
  41673. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  41674. +{
  41675. +#ifdef __BIG_ENDIAN
  41676. + return *p;
  41677. +#else
  41678. + uint8_t *u_p = (uint8_t *)p;
  41679. +
  41680. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41681. +#endif
  41682. +}
  41683. +
  41684. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  41685. +{
  41686. +#ifdef __LITTLE_ENDIAN
  41687. + return *p;
  41688. +#else
  41689. + uint8_t *u_p = (uint8_t *)p;
  41690. +
  41691. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41692. +#endif
  41693. +}
  41694. +
  41695. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  41696. +{
  41697. +#ifdef __BIG_ENDIAN
  41698. + return *p;
  41699. +#else
  41700. + uint8_t *u_p = (uint8_t *)p;
  41701. +
  41702. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41703. +#endif
  41704. +}
  41705. +
  41706. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  41707. +{
  41708. +#ifdef __LITTLE_ENDIAN
  41709. + return *p;
  41710. +#else
  41711. + uint8_t *u_p = (uint8_t *)p;
  41712. + return (u_p[1] | (u_p[0] << 8));
  41713. +#endif
  41714. +}
  41715. +
  41716. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  41717. +{
  41718. +#ifdef __BIG_ENDIAN
  41719. + return *p;
  41720. +#else
  41721. + uint8_t *u_p = (uint8_t *)p;
  41722. + return (u_p[1] | (u_p[0] << 8));
  41723. +#endif
  41724. +}
  41725. +
  41726. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  41727. +{
  41728. +#ifdef __LITTLE_ENDIAN
  41729. + return *p;
  41730. +#else
  41731. + uint8_t *u_p = (uint8_t *)p;
  41732. + return (u_p[1] | (u_p[0] << 8));
  41733. +#endif
  41734. +}
  41735. +
  41736. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  41737. +{
  41738. +#ifdef __BIG_ENDIAN
  41739. + return *p;
  41740. +#else
  41741. + uint8_t *u_p = (uint8_t *)p;
  41742. + return (u_p[1] | (u_p[0] << 8));
  41743. +#endif
  41744. +}
  41745. +
  41746. +
  41747. +/* Registers */
  41748. +
  41749. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  41750. +{
  41751. + return readl(reg);
  41752. +}
  41753. +
  41754. +#if 0
  41755. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  41756. +{
  41757. +}
  41758. +#endif
  41759. +
  41760. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  41761. +{
  41762. + writel(value, reg);
  41763. +}
  41764. +
  41765. +#if 0
  41766. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  41767. +{
  41768. +}
  41769. +#endif
  41770. +
  41771. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  41772. +{
  41773. + unsigned long flags;
  41774. +
  41775. + local_irq_save(flags);
  41776. + local_fiq_disable();
  41777. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  41778. + local_irq_restore(flags);
  41779. +}
  41780. +
  41781. +#if 0
  41782. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  41783. +{
  41784. +}
  41785. +#endif
  41786. +
  41787. +
  41788. +/* Locking */
  41789. +
  41790. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  41791. +{
  41792. + spinlock_t *sl = (spinlock_t *)1;
  41793. +
  41794. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41795. + sl = DWC_ALLOC(sizeof(*sl));
  41796. + if (!sl) {
  41797. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  41798. + return NULL;
  41799. + }
  41800. +
  41801. + spin_lock_init(sl);
  41802. +#endif
  41803. + return (dwc_spinlock_t *)sl;
  41804. +}
  41805. +
  41806. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  41807. +{
  41808. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41809. + DWC_FREE(lock);
  41810. +#endif
  41811. +}
  41812. +
  41813. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  41814. +{
  41815. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41816. + spin_lock((spinlock_t *)lock);
  41817. +#endif
  41818. +}
  41819. +
  41820. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  41821. +{
  41822. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41823. + spin_unlock((spinlock_t *)lock);
  41824. +#endif
  41825. +}
  41826. +
  41827. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  41828. +{
  41829. + dwc_irqflags_t f;
  41830. +
  41831. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41832. + spin_lock_irqsave((spinlock_t *)lock, f);
  41833. +#else
  41834. + local_irq_save(f);
  41835. +#endif
  41836. + *flags = f;
  41837. +}
  41838. +
  41839. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  41840. +{
  41841. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41842. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  41843. +#else
  41844. + local_irq_restore(flags);
  41845. +#endif
  41846. +}
  41847. +
  41848. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  41849. +{
  41850. + struct mutex *m;
  41851. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  41852. +
  41853. + if (!mutex) {
  41854. + DWC_ERROR("Cannot allocate memory for mutex\n");
  41855. + return NULL;
  41856. + }
  41857. +
  41858. + m = (struct mutex *)mutex;
  41859. + mutex_init(m);
  41860. + return mutex;
  41861. +}
  41862. +
  41863. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  41864. +#else
  41865. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  41866. +{
  41867. + mutex_destroy((struct mutex *)mutex);
  41868. + DWC_FREE(mutex);
  41869. +}
  41870. +#endif
  41871. +
  41872. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  41873. +{
  41874. + struct mutex *m = (struct mutex *)mutex;
  41875. + mutex_lock(m);
  41876. +}
  41877. +
  41878. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  41879. +{
  41880. + struct mutex *m = (struct mutex *)mutex;
  41881. + return mutex_trylock(m);
  41882. +}
  41883. +
  41884. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  41885. +{
  41886. + struct mutex *m = (struct mutex *)mutex;
  41887. + mutex_unlock(m);
  41888. +}
  41889. +
  41890. +
  41891. +/* Timing */
  41892. +
  41893. +void DWC_UDELAY(uint32_t usecs)
  41894. +{
  41895. + udelay(usecs);
  41896. +}
  41897. +
  41898. +void DWC_MDELAY(uint32_t msecs)
  41899. +{
  41900. + mdelay(msecs);
  41901. +}
  41902. +
  41903. +void DWC_MSLEEP(uint32_t msecs)
  41904. +{
  41905. + msleep(msecs);
  41906. +}
  41907. +
  41908. +uint32_t DWC_TIME(void)
  41909. +{
  41910. + return jiffies_to_msecs(jiffies);
  41911. +}
  41912. +
  41913. +
  41914. +/* Timers */
  41915. +
  41916. +struct dwc_timer {
  41917. + struct timer_list *t;
  41918. + char *name;
  41919. + dwc_timer_callback_t cb;
  41920. + void *data;
  41921. + uint8_t scheduled;
  41922. + dwc_spinlock_t *lock;
  41923. +};
  41924. +
  41925. +static void timer_callback(unsigned long data)
  41926. +{
  41927. + dwc_timer_t *timer = (dwc_timer_t *)data;
  41928. + dwc_irqflags_t flags;
  41929. +
  41930. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  41931. + timer->scheduled = 0;
  41932. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  41933. + DWC_DEBUGC("Timer %s callback", timer->name);
  41934. + timer->cb(timer->data);
  41935. +}
  41936. +
  41937. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  41938. +{
  41939. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  41940. +
  41941. + if (!t) {
  41942. + DWC_ERROR("Cannot allocate memory for timer");
  41943. + return NULL;
  41944. + }
  41945. +
  41946. + t->t = DWC_ALLOC(sizeof(*t->t));
  41947. + if (!t->t) {
  41948. + DWC_ERROR("Cannot allocate memory for timer->t");
  41949. + goto no_timer;
  41950. + }
  41951. +
  41952. + t->name = DWC_STRDUP(name);
  41953. + if (!t->name) {
  41954. + DWC_ERROR("Cannot allocate memory for timer->name");
  41955. + goto no_name;
  41956. + }
  41957. +
  41958. + t->lock = DWC_SPINLOCK_ALLOC();
  41959. + if (!t->lock) {
  41960. + DWC_ERROR("Cannot allocate memory for lock");
  41961. + goto no_lock;
  41962. + }
  41963. +
  41964. + t->scheduled = 0;
  41965. + t->t->base = &boot_tvec_bases;
  41966. + t->t->expires = jiffies;
  41967. + setup_timer(t->t, timer_callback, (unsigned long)t);
  41968. +
  41969. + t->cb = cb;
  41970. + t->data = data;
  41971. +
  41972. + return t;
  41973. +
  41974. + no_lock:
  41975. + DWC_FREE(t->name);
  41976. + no_name:
  41977. + DWC_FREE(t->t);
  41978. + no_timer:
  41979. + DWC_FREE(t);
  41980. + return NULL;
  41981. +}
  41982. +
  41983. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  41984. +{
  41985. + dwc_irqflags_t flags;
  41986. +
  41987. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  41988. +
  41989. + if (timer->scheduled) {
  41990. + del_timer(timer->t);
  41991. + timer->scheduled = 0;
  41992. + }
  41993. +
  41994. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  41995. + DWC_SPINLOCK_FREE(timer->lock);
  41996. + DWC_FREE(timer->t);
  41997. + DWC_FREE(timer->name);
  41998. + DWC_FREE(timer);
  41999. +}
  42000. +
  42001. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  42002. +{
  42003. + dwc_irqflags_t flags;
  42004. +
  42005. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  42006. +
  42007. + if (!timer->scheduled) {
  42008. + timer->scheduled = 1;
  42009. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  42010. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  42011. + add_timer(timer->t);
  42012. + } else {
  42013. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  42014. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  42015. + }
  42016. +
  42017. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  42018. +}
  42019. +
  42020. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  42021. +{
  42022. + del_timer(timer->t);
  42023. +}
  42024. +
  42025. +
  42026. +/* Wait Queues */
  42027. +
  42028. +struct dwc_waitq {
  42029. + wait_queue_head_t queue;
  42030. + int abort;
  42031. +};
  42032. +
  42033. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  42034. +{
  42035. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  42036. +
  42037. + if (!wq) {
  42038. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  42039. + return NULL;
  42040. + }
  42041. +
  42042. + init_waitqueue_head(&wq->queue);
  42043. + wq->abort = 0;
  42044. + return wq;
  42045. +}
  42046. +
  42047. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  42048. +{
  42049. + DWC_FREE(wq);
  42050. +}
  42051. +
  42052. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  42053. +{
  42054. + int result = wait_event_interruptible(wq->queue,
  42055. + cond(data) || wq->abort);
  42056. + if (result == -ERESTARTSYS) {
  42057. + wq->abort = 0;
  42058. + return -DWC_E_RESTART;
  42059. + }
  42060. +
  42061. + if (wq->abort == 1) {
  42062. + wq->abort = 0;
  42063. + return -DWC_E_ABORT;
  42064. + }
  42065. +
  42066. + wq->abort = 0;
  42067. +
  42068. + if (result == 0) {
  42069. + return 0;
  42070. + }
  42071. +
  42072. + return -DWC_E_UNKNOWN;
  42073. +}
  42074. +
  42075. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  42076. + void *data, int32_t msecs)
  42077. +{
  42078. + int32_t tmsecs;
  42079. + int result = wait_event_interruptible_timeout(wq->queue,
  42080. + cond(data) || wq->abort,
  42081. + msecs_to_jiffies(msecs));
  42082. + if (result == -ERESTARTSYS) {
  42083. + wq->abort = 0;
  42084. + return -DWC_E_RESTART;
  42085. + }
  42086. +
  42087. + if (wq->abort == 1) {
  42088. + wq->abort = 0;
  42089. + return -DWC_E_ABORT;
  42090. + }
  42091. +
  42092. + wq->abort = 0;
  42093. +
  42094. + if (result > 0) {
  42095. + tmsecs = jiffies_to_msecs(result);
  42096. + if (!tmsecs) {
  42097. + return 1;
  42098. + }
  42099. +
  42100. + return tmsecs;
  42101. + }
  42102. +
  42103. + if (result == 0) {
  42104. + return -DWC_E_TIMEOUT;
  42105. + }
  42106. +
  42107. + return -DWC_E_UNKNOWN;
  42108. +}
  42109. +
  42110. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  42111. +{
  42112. + wq->abort = 0;
  42113. + wake_up_interruptible(&wq->queue);
  42114. +}
  42115. +
  42116. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  42117. +{
  42118. + wq->abort = 1;
  42119. + wake_up_interruptible(&wq->queue);
  42120. +}
  42121. +
  42122. +
  42123. +/* Threading */
  42124. +
  42125. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  42126. +{
  42127. + struct task_struct *thread = kthread_run(func, data, name);
  42128. +
  42129. + if (thread == ERR_PTR(-ENOMEM)) {
  42130. + return NULL;
  42131. + }
  42132. +
  42133. + return (dwc_thread_t *)thread;
  42134. +}
  42135. +
  42136. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  42137. +{
  42138. + return kthread_stop((struct task_struct *)thread);
  42139. +}
  42140. +
  42141. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  42142. +{
  42143. + return kthread_should_stop();
  42144. +}
  42145. +
  42146. +
  42147. +/* tasklets
  42148. + - run in interrupt context (cannot sleep)
  42149. + - each tasklet runs on a single CPU
  42150. + - different tasklets can be running simultaneously on different CPUs
  42151. + */
  42152. +struct dwc_tasklet {
  42153. + struct tasklet_struct t;
  42154. + dwc_tasklet_callback_t cb;
  42155. + void *data;
  42156. +};
  42157. +
  42158. +static void tasklet_callback(unsigned long data)
  42159. +{
  42160. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  42161. + t->cb(t->data);
  42162. +}
  42163. +
  42164. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  42165. +{
  42166. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  42167. +
  42168. + if (t) {
  42169. + t->cb = cb;
  42170. + t->data = data;
  42171. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  42172. + } else {
  42173. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  42174. + }
  42175. +
  42176. + return t;
  42177. +}
  42178. +
  42179. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  42180. +{
  42181. + DWC_FREE(task);
  42182. +}
  42183. +
  42184. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  42185. +{
  42186. + tasklet_schedule(&task->t);
  42187. +}
  42188. +
  42189. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  42190. +{
  42191. + tasklet_hi_schedule(&task->t);
  42192. +}
  42193. +
  42194. +
  42195. +/* workqueues
  42196. + - run in process context (can sleep)
  42197. + */
  42198. +typedef struct work_container {
  42199. + dwc_work_callback_t cb;
  42200. + void *data;
  42201. + dwc_workq_t *wq;
  42202. + char *name;
  42203. +
  42204. +#ifdef DEBUG
  42205. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  42206. +#endif
  42207. + struct delayed_work work;
  42208. +} work_container_t;
  42209. +
  42210. +#ifdef DEBUG
  42211. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  42212. +#endif
  42213. +
  42214. +struct dwc_workq {
  42215. + struct workqueue_struct *wq;
  42216. + dwc_spinlock_t *lock;
  42217. + dwc_waitq_t *waitq;
  42218. + int pending;
  42219. +
  42220. +#ifdef DEBUG
  42221. + struct work_container_queue entries;
  42222. +#endif
  42223. +};
  42224. +
  42225. +static void do_work(struct work_struct *work)
  42226. +{
  42227. + dwc_irqflags_t flags;
  42228. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  42229. + work_container_t *container = container_of(dw, struct work_container, work);
  42230. + dwc_workq_t *wq = container->wq;
  42231. +
  42232. + container->cb(container->data);
  42233. +
  42234. +#ifdef DEBUG
  42235. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  42236. +#endif
  42237. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  42238. + if (container->name) {
  42239. + DWC_FREE(container->name);
  42240. + }
  42241. + DWC_FREE(container);
  42242. +
  42243. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  42244. + wq->pending--;
  42245. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  42246. + DWC_WAITQ_TRIGGER(wq->waitq);
  42247. +}
  42248. +
  42249. +static int work_done(void *data)
  42250. +{
  42251. + dwc_workq_t *workq = (dwc_workq_t *)data;
  42252. + return workq->pending == 0;
  42253. +}
  42254. +
  42255. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  42256. +{
  42257. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  42258. +}
  42259. +
  42260. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  42261. +{
  42262. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  42263. +
  42264. + if (!wq) {
  42265. + return NULL;
  42266. + }
  42267. +
  42268. + wq->wq = create_singlethread_workqueue(name);
  42269. + if (!wq->wq) {
  42270. + goto no_wq;
  42271. + }
  42272. +
  42273. + wq->pending = 0;
  42274. +
  42275. + wq->lock = DWC_SPINLOCK_ALLOC();
  42276. + if (!wq->lock) {
  42277. + goto no_lock;
  42278. + }
  42279. +
  42280. + wq->waitq = DWC_WAITQ_ALLOC();
  42281. + if (!wq->waitq) {
  42282. + goto no_waitq;
  42283. + }
  42284. +
  42285. +#ifdef DEBUG
  42286. + DWC_CIRCLEQ_INIT(&wq->entries);
  42287. +#endif
  42288. + return wq;
  42289. +
  42290. + no_waitq:
  42291. + DWC_SPINLOCK_FREE(wq->lock);
  42292. + no_lock:
  42293. + destroy_workqueue(wq->wq);
  42294. + no_wq:
  42295. + DWC_FREE(wq);
  42296. +
  42297. + return NULL;
  42298. +}
  42299. +
  42300. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  42301. +{
  42302. +#ifdef DEBUG
  42303. + if (wq->pending != 0) {
  42304. + struct work_container *wc;
  42305. + DWC_ERROR("Destroying work queue with pending work");
  42306. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  42307. + DWC_ERROR("Work %s still pending", wc->name);
  42308. + }
  42309. + }
  42310. +#endif
  42311. + destroy_workqueue(wq->wq);
  42312. + DWC_SPINLOCK_FREE(wq->lock);
  42313. + DWC_WAITQ_FREE(wq->waitq);
  42314. + DWC_FREE(wq);
  42315. +}
  42316. +
  42317. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  42318. + char *format, ...)
  42319. +{
  42320. + dwc_irqflags_t flags;
  42321. + work_container_t *container;
  42322. + static char name[128];
  42323. + va_list args;
  42324. +
  42325. + va_start(args, format);
  42326. + DWC_VSNPRINTF(name, 128, format, args);
  42327. + va_end(args);
  42328. +
  42329. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  42330. + wq->pending++;
  42331. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  42332. + DWC_WAITQ_TRIGGER(wq->waitq);
  42333. +
  42334. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  42335. + if (!container) {
  42336. + DWC_ERROR("Cannot allocate memory for container\n");
  42337. + return;
  42338. + }
  42339. +
  42340. + container->name = DWC_STRDUP(name);
  42341. + if (!container->name) {
  42342. + DWC_ERROR("Cannot allocate memory for container->name\n");
  42343. + DWC_FREE(container);
  42344. + return;
  42345. + }
  42346. +
  42347. + container->cb = cb;
  42348. + container->data = data;
  42349. + container->wq = wq;
  42350. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  42351. + INIT_WORK(&container->work.work, do_work);
  42352. +
  42353. +#ifdef DEBUG
  42354. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  42355. +#endif
  42356. + queue_work(wq->wq, &container->work.work);
  42357. +}
  42358. +
  42359. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  42360. + void *data, uint32_t time, char *format, ...)
  42361. +{
  42362. + dwc_irqflags_t flags;
  42363. + work_container_t *container;
  42364. + static char name[128];
  42365. + va_list args;
  42366. +
  42367. + va_start(args, format);
  42368. + DWC_VSNPRINTF(name, 128, format, args);
  42369. + va_end(args);
  42370. +
  42371. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  42372. + wq->pending++;
  42373. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  42374. + DWC_WAITQ_TRIGGER(wq->waitq);
  42375. +
  42376. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  42377. + if (!container) {
  42378. + DWC_ERROR("Cannot allocate memory for container\n");
  42379. + return;
  42380. + }
  42381. +
  42382. + container->name = DWC_STRDUP(name);
  42383. + if (!container->name) {
  42384. + DWC_ERROR("Cannot allocate memory for container->name\n");
  42385. + DWC_FREE(container);
  42386. + return;
  42387. + }
  42388. +
  42389. + container->cb = cb;
  42390. + container->data = data;
  42391. + container->wq = wq;
  42392. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  42393. + INIT_DELAYED_WORK(&container->work, do_work);
  42394. +
  42395. +#ifdef DEBUG
  42396. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  42397. +#endif
  42398. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  42399. +}
  42400. +
  42401. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  42402. +{
  42403. + return wq->pending;
  42404. +}
  42405. +
  42406. +
  42407. +#ifdef DWC_LIBMODULE
  42408. +
  42409. +#ifdef DWC_CCLIB
  42410. +/* CC */
  42411. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  42412. +EXPORT_SYMBOL(dwc_cc_if_free);
  42413. +EXPORT_SYMBOL(dwc_cc_clear);
  42414. +EXPORT_SYMBOL(dwc_cc_add);
  42415. +EXPORT_SYMBOL(dwc_cc_remove);
  42416. +EXPORT_SYMBOL(dwc_cc_change);
  42417. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  42418. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  42419. +EXPORT_SYMBOL(dwc_cc_match_chid);
  42420. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  42421. +EXPORT_SYMBOL(dwc_cc_ck);
  42422. +EXPORT_SYMBOL(dwc_cc_chid);
  42423. +EXPORT_SYMBOL(dwc_cc_cdid);
  42424. +EXPORT_SYMBOL(dwc_cc_name);
  42425. +#endif /* DWC_CCLIB */
  42426. +
  42427. +#ifdef DWC_CRYPTOLIB
  42428. +# ifndef CONFIG_MACH_IPMATE
  42429. +/* Modpow */
  42430. +EXPORT_SYMBOL(dwc_modpow);
  42431. +
  42432. +/* DH */
  42433. +EXPORT_SYMBOL(dwc_dh_modpow);
  42434. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  42435. +EXPORT_SYMBOL(dwc_dh_pk);
  42436. +# endif /* CONFIG_MACH_IPMATE */
  42437. +
  42438. +/* Crypto */
  42439. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  42440. +EXPORT_SYMBOL(dwc_wusb_cmf);
  42441. +EXPORT_SYMBOL(dwc_wusb_prf);
  42442. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  42443. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  42444. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  42445. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  42446. +#endif /* DWC_CRYPTOLIB */
  42447. +
  42448. +/* Notification */
  42449. +#ifdef DWC_NOTIFYLIB
  42450. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  42451. +EXPORT_SYMBOL(dwc_free_notification_manager);
  42452. +EXPORT_SYMBOL(dwc_register_notifier);
  42453. +EXPORT_SYMBOL(dwc_unregister_notifier);
  42454. +EXPORT_SYMBOL(dwc_add_observer);
  42455. +EXPORT_SYMBOL(dwc_remove_observer);
  42456. +EXPORT_SYMBOL(dwc_notify);
  42457. +#endif
  42458. +
  42459. +/* Memory Debugging Routines */
  42460. +#ifdef DWC_DEBUG_MEMORY
  42461. +EXPORT_SYMBOL(dwc_alloc_debug);
  42462. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  42463. +EXPORT_SYMBOL(dwc_free_debug);
  42464. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  42465. +EXPORT_SYMBOL(dwc_dma_free_debug);
  42466. +#endif
  42467. +
  42468. +EXPORT_SYMBOL(DWC_MEMSET);
  42469. +EXPORT_SYMBOL(DWC_MEMCPY);
  42470. +EXPORT_SYMBOL(DWC_MEMMOVE);
  42471. +EXPORT_SYMBOL(DWC_MEMCMP);
  42472. +EXPORT_SYMBOL(DWC_STRNCMP);
  42473. +EXPORT_SYMBOL(DWC_STRCMP);
  42474. +EXPORT_SYMBOL(DWC_STRLEN);
  42475. +EXPORT_SYMBOL(DWC_STRCPY);
  42476. +EXPORT_SYMBOL(DWC_STRDUP);
  42477. +EXPORT_SYMBOL(DWC_ATOI);
  42478. +EXPORT_SYMBOL(DWC_ATOUI);
  42479. +
  42480. +#ifdef DWC_UTFLIB
  42481. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  42482. +#endif /* DWC_UTFLIB */
  42483. +
  42484. +EXPORT_SYMBOL(DWC_IN_IRQ);
  42485. +EXPORT_SYMBOL(DWC_IN_BH);
  42486. +EXPORT_SYMBOL(DWC_VPRINTF);
  42487. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  42488. +EXPORT_SYMBOL(DWC_PRINTF);
  42489. +EXPORT_SYMBOL(DWC_SPRINTF);
  42490. +EXPORT_SYMBOL(DWC_SNPRINTF);
  42491. +EXPORT_SYMBOL(__DWC_WARN);
  42492. +EXPORT_SYMBOL(__DWC_ERROR);
  42493. +EXPORT_SYMBOL(DWC_EXCEPTION);
  42494. +
  42495. +#ifdef DEBUG
  42496. +EXPORT_SYMBOL(__DWC_DEBUG);
  42497. +#endif
  42498. +
  42499. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  42500. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  42501. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  42502. +EXPORT_SYMBOL(__DWC_ALLOC);
  42503. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  42504. +EXPORT_SYMBOL(__DWC_FREE);
  42505. +
  42506. +#ifdef DWC_CRYPTOLIB
  42507. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  42508. +EXPORT_SYMBOL(DWC_AES_CBC);
  42509. +EXPORT_SYMBOL(DWC_SHA256);
  42510. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  42511. +#endif
  42512. +
  42513. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  42514. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  42515. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  42516. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  42517. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  42518. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  42519. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  42520. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  42521. +EXPORT_SYMBOL(DWC_READ_REG32);
  42522. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  42523. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  42524. +
  42525. +#if 0
  42526. +EXPORT_SYMBOL(DWC_READ_REG64);
  42527. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  42528. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  42529. +#endif
  42530. +
  42531. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  42532. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  42533. +EXPORT_SYMBOL(DWC_SPINLOCK);
  42534. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  42535. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  42536. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  42537. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  42538. +
  42539. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  42540. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  42541. +#endif
  42542. +
  42543. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  42544. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  42545. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  42546. +EXPORT_SYMBOL(DWC_UDELAY);
  42547. +EXPORT_SYMBOL(DWC_MDELAY);
  42548. +EXPORT_SYMBOL(DWC_MSLEEP);
  42549. +EXPORT_SYMBOL(DWC_TIME);
  42550. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  42551. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  42552. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  42553. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  42554. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  42555. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  42556. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  42557. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  42558. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  42559. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  42560. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  42561. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  42562. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  42563. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  42564. +EXPORT_SYMBOL(DWC_TASK_FREE);
  42565. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  42566. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  42567. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  42568. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  42569. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  42570. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  42571. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  42572. +
  42573. +static int dwc_common_port_init_module(void)
  42574. +{
  42575. + int result = 0;
  42576. +
  42577. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  42578. +
  42579. +#ifdef DWC_DEBUG_MEMORY
  42580. + result = dwc_memory_debug_start(NULL);
  42581. + if (result) {
  42582. + printk(KERN_ERR
  42583. + "dwc_memory_debug_start() failed with error %d\n",
  42584. + result);
  42585. + return result;
  42586. + }
  42587. +#endif
  42588. +
  42589. +#ifdef DWC_NOTIFYLIB
  42590. + result = dwc_alloc_notification_manager(NULL, NULL);
  42591. + if (result) {
  42592. + printk(KERN_ERR
  42593. + "dwc_alloc_notification_manager() failed with error %d\n",
  42594. + result);
  42595. + return result;
  42596. + }
  42597. +#endif
  42598. + return result;
  42599. +}
  42600. +
  42601. +static void dwc_common_port_exit_module(void)
  42602. +{
  42603. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  42604. +
  42605. +#ifdef DWC_NOTIFYLIB
  42606. + dwc_free_notification_manager();
  42607. +#endif
  42608. +
  42609. +#ifdef DWC_DEBUG_MEMORY
  42610. + dwc_memory_debug_stop();
  42611. +#endif
  42612. +}
  42613. +
  42614. +module_init(dwc_common_port_init_module);
  42615. +module_exit(dwc_common_port_exit_module);
  42616. +
  42617. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  42618. +MODULE_AUTHOR("Synopsys Inc.");
  42619. +MODULE_LICENSE ("GPL");
  42620. +
  42621. +#endif /* DWC_LIBMODULE */
  42622. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  42623. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 1970-01-01 01:00:00.000000000 +0100
  42624. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 2014-03-11 17:33:06.000000000 +0100
  42625. @@ -0,0 +1,1275 @@
  42626. +#include "dwc_os.h"
  42627. +#include "dwc_list.h"
  42628. +
  42629. +#ifdef DWC_CCLIB
  42630. +# include "dwc_cc.h"
  42631. +#endif
  42632. +
  42633. +#ifdef DWC_CRYPTOLIB
  42634. +# include "dwc_modpow.h"
  42635. +# include "dwc_dh.h"
  42636. +# include "dwc_crypto.h"
  42637. +#endif
  42638. +
  42639. +#ifdef DWC_NOTIFYLIB
  42640. +# include "dwc_notifier.h"
  42641. +#endif
  42642. +
  42643. +/* OS-Level Implementations */
  42644. +
  42645. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  42646. +
  42647. +
  42648. +/* MISC */
  42649. +
  42650. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  42651. +{
  42652. + return memset(dest, byte, size);
  42653. +}
  42654. +
  42655. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  42656. +{
  42657. + return memcpy(dest, src, size);
  42658. +}
  42659. +
  42660. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  42661. +{
  42662. + bcopy(src, dest, size);
  42663. + return dest;
  42664. +}
  42665. +
  42666. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  42667. +{
  42668. + return memcmp(m1, m2, size);
  42669. +}
  42670. +
  42671. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  42672. +{
  42673. + return strncmp(s1, s2, size);
  42674. +}
  42675. +
  42676. +int DWC_STRCMP(void *s1, void *s2)
  42677. +{
  42678. + return strcmp(s1, s2);
  42679. +}
  42680. +
  42681. +int DWC_STRLEN(char const *str)
  42682. +{
  42683. + return strlen(str);
  42684. +}
  42685. +
  42686. +char *DWC_STRCPY(char *to, char const *from)
  42687. +{
  42688. + return strcpy(to, from);
  42689. +}
  42690. +
  42691. +char *DWC_STRDUP(char const *str)
  42692. +{
  42693. + int len = DWC_STRLEN(str) + 1;
  42694. + char *new = DWC_ALLOC_ATOMIC(len);
  42695. +
  42696. + if (!new) {
  42697. + return NULL;
  42698. + }
  42699. +
  42700. + DWC_MEMCPY(new, str, len);
  42701. + return new;
  42702. +}
  42703. +
  42704. +int DWC_ATOI(char *str, int32_t *value)
  42705. +{
  42706. + char *end = NULL;
  42707. +
  42708. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  42709. + * should be equivalent on 2's complement machines
  42710. + */
  42711. + *value = strtoul(str, &end, 0);
  42712. + if (*end == '\0') {
  42713. + return 0;
  42714. + }
  42715. +
  42716. + return -1;
  42717. +}
  42718. +
  42719. +int DWC_ATOUI(char *str, uint32_t *value)
  42720. +{
  42721. + char *end = NULL;
  42722. +
  42723. + *value = strtoul(str, &end, 0);
  42724. + if (*end == '\0') {
  42725. + return 0;
  42726. + }
  42727. +
  42728. + return -1;
  42729. +}
  42730. +
  42731. +
  42732. +#ifdef DWC_UTFLIB
  42733. +/* From usbstring.c */
  42734. +
  42735. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  42736. +{
  42737. + int count = 0;
  42738. + u8 c;
  42739. + u16 uchar;
  42740. +
  42741. + /* this insists on correct encodings, though not minimal ones.
  42742. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  42743. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  42744. + */
  42745. + while (len != 0 && (c = (u8) *s++) != 0) {
  42746. + if (unlikely(c & 0x80)) {
  42747. + // 2-byte sequence:
  42748. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  42749. + if ((c & 0xe0) == 0xc0) {
  42750. + uchar = (c & 0x1f) << 6;
  42751. +
  42752. + c = (u8) *s++;
  42753. + if ((c & 0xc0) != 0xc0)
  42754. + goto fail;
  42755. + c &= 0x3f;
  42756. + uchar |= c;
  42757. +
  42758. + // 3-byte sequence (most CJKV characters):
  42759. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  42760. + } else if ((c & 0xf0) == 0xe0) {
  42761. + uchar = (c & 0x0f) << 12;
  42762. +
  42763. + c = (u8) *s++;
  42764. + if ((c & 0xc0) != 0xc0)
  42765. + goto fail;
  42766. + c &= 0x3f;
  42767. + uchar |= c << 6;
  42768. +
  42769. + c = (u8) *s++;
  42770. + if ((c & 0xc0) != 0xc0)
  42771. + goto fail;
  42772. + c &= 0x3f;
  42773. + uchar |= c;
  42774. +
  42775. + /* no bogus surrogates */
  42776. + if (0xd800 <= uchar && uchar <= 0xdfff)
  42777. + goto fail;
  42778. +
  42779. + // 4-byte sequence (surrogate pairs, currently rare):
  42780. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  42781. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  42782. + // (uuuuu = wwww + 1)
  42783. + // FIXME accept the surrogate code points (only)
  42784. + } else
  42785. + goto fail;
  42786. + } else
  42787. + uchar = c;
  42788. + put_unaligned (cpu_to_le16 (uchar), cp++);
  42789. + count++;
  42790. + len--;
  42791. + }
  42792. + return count;
  42793. +fail:
  42794. + return -1;
  42795. +}
  42796. +
  42797. +#endif /* DWC_UTFLIB */
  42798. +
  42799. +
  42800. +/* dwc_debug.h */
  42801. +
  42802. +dwc_bool_t DWC_IN_IRQ(void)
  42803. +{
  42804. +// return in_irq();
  42805. + return 0;
  42806. +}
  42807. +
  42808. +dwc_bool_t DWC_IN_BH(void)
  42809. +{
  42810. +// return in_softirq();
  42811. + return 0;
  42812. +}
  42813. +
  42814. +void DWC_VPRINTF(char *format, va_list args)
  42815. +{
  42816. + vprintf(format, args);
  42817. +}
  42818. +
  42819. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  42820. +{
  42821. + return vsnprintf(str, size, format, args);
  42822. +}
  42823. +
  42824. +void DWC_PRINTF(char *format, ...)
  42825. +{
  42826. + va_list args;
  42827. +
  42828. + va_start(args, format);
  42829. + DWC_VPRINTF(format, args);
  42830. + va_end(args);
  42831. +}
  42832. +
  42833. +int DWC_SPRINTF(char *buffer, char *format, ...)
  42834. +{
  42835. + int retval;
  42836. + va_list args;
  42837. +
  42838. + va_start(args, format);
  42839. + retval = vsprintf(buffer, format, args);
  42840. + va_end(args);
  42841. + return retval;
  42842. +}
  42843. +
  42844. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  42845. +{
  42846. + int retval;
  42847. + va_list args;
  42848. +
  42849. + va_start(args, format);
  42850. + retval = vsnprintf(buffer, size, format, args);
  42851. + va_end(args);
  42852. + return retval;
  42853. +}
  42854. +
  42855. +void __DWC_WARN(char *format, ...)
  42856. +{
  42857. + va_list args;
  42858. +
  42859. + va_start(args, format);
  42860. + DWC_VPRINTF(format, args);
  42861. + va_end(args);
  42862. +}
  42863. +
  42864. +void __DWC_ERROR(char *format, ...)
  42865. +{
  42866. + va_list args;
  42867. +
  42868. + va_start(args, format);
  42869. + DWC_VPRINTF(format, args);
  42870. + va_end(args);
  42871. +}
  42872. +
  42873. +void DWC_EXCEPTION(char *format, ...)
  42874. +{
  42875. + va_list args;
  42876. +
  42877. + va_start(args, format);
  42878. + DWC_VPRINTF(format, args);
  42879. + va_end(args);
  42880. +// BUG_ON(1); ???
  42881. +}
  42882. +
  42883. +#ifdef DEBUG
  42884. +void __DWC_DEBUG(char *format, ...)
  42885. +{
  42886. + va_list args;
  42887. +
  42888. + va_start(args, format);
  42889. + DWC_VPRINTF(format, args);
  42890. + va_end(args);
  42891. +}
  42892. +#endif
  42893. +
  42894. +
  42895. +/* dwc_mem.h */
  42896. +
  42897. +#if 0
  42898. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  42899. + uint32_t align,
  42900. + uint32_t alloc)
  42901. +{
  42902. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  42903. + size, align, alloc);
  42904. + return (dwc_pool_t *)pool;
  42905. +}
  42906. +
  42907. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  42908. +{
  42909. + dma_pool_destroy((struct dma_pool *)pool);
  42910. +}
  42911. +
  42912. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  42913. +{
  42914. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  42915. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  42916. +}
  42917. +
  42918. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  42919. +{
  42920. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  42921. + memset(..);
  42922. +}
  42923. +
  42924. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  42925. +{
  42926. + dma_pool_free(pool, vaddr, daddr);
  42927. +}
  42928. +#endif
  42929. +
  42930. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  42931. +{
  42932. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  42933. + int error;
  42934. +
  42935. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  42936. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  42937. + &dma->nsegs, BUS_DMA_NOWAIT);
  42938. + if (error) {
  42939. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  42940. + (uintmax_t)size, error);
  42941. + goto fail_0;
  42942. + }
  42943. +
  42944. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  42945. + (caddr_t *)&dma->dma_vaddr,
  42946. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  42947. + if (error) {
  42948. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  42949. + goto fail_1;
  42950. + }
  42951. +
  42952. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  42953. + BUS_DMA_NOWAIT, &dma->dma_map);
  42954. + if (error) {
  42955. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  42956. + goto fail_2;
  42957. + }
  42958. +
  42959. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  42960. + size, NULL, BUS_DMA_NOWAIT);
  42961. + if (error) {
  42962. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  42963. + goto fail_3;
  42964. + }
  42965. +
  42966. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  42967. + *dma_addr = dma->dma_paddr;
  42968. + return dma->dma_vaddr;
  42969. +
  42970. +fail_3:
  42971. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  42972. +fail_2:
  42973. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  42974. +fail_1:
  42975. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  42976. +fail_0:
  42977. + dma->dma_map = NULL;
  42978. + dma->dma_vaddr = NULL;
  42979. + dma->nsegs = 0;
  42980. +
  42981. + return NULL;
  42982. +}
  42983. +
  42984. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  42985. +{
  42986. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  42987. +
  42988. + if (dma->dma_map != NULL) {
  42989. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  42990. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  42991. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  42992. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  42993. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  42994. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  42995. + dma->dma_paddr = 0;
  42996. + dma->dma_map = NULL;
  42997. + dma->dma_vaddr = NULL;
  42998. + dma->nsegs = 0;
  42999. + }
  43000. +}
  43001. +
  43002. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  43003. +{
  43004. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  43005. +}
  43006. +
  43007. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  43008. +{
  43009. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  43010. +}
  43011. +
  43012. +void __DWC_FREE(void *mem_ctx, void *addr)
  43013. +{
  43014. + free(addr, M_DEVBUF);
  43015. +}
  43016. +
  43017. +
  43018. +#ifdef DWC_CRYPTOLIB
  43019. +/* dwc_crypto.h */
  43020. +
  43021. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  43022. +{
  43023. + get_random_bytes(buffer, length);
  43024. +}
  43025. +
  43026. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  43027. +{
  43028. + struct crypto_blkcipher *tfm;
  43029. + struct blkcipher_desc desc;
  43030. + struct scatterlist sgd;
  43031. + struct scatterlist sgs;
  43032. +
  43033. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  43034. + if (tfm == NULL) {
  43035. + printk("failed to load transform for aes CBC\n");
  43036. + return -1;
  43037. + }
  43038. +
  43039. + crypto_blkcipher_setkey(tfm, key, keylen);
  43040. + crypto_blkcipher_set_iv(tfm, iv, 16);
  43041. +
  43042. + sg_init_one(&sgd, out, messagelen);
  43043. + sg_init_one(&sgs, message, messagelen);
  43044. +
  43045. + desc.tfm = tfm;
  43046. + desc.flags = 0;
  43047. +
  43048. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  43049. + crypto_free_blkcipher(tfm);
  43050. + DWC_ERROR("AES CBC encryption failed");
  43051. + return -1;
  43052. + }
  43053. +
  43054. + crypto_free_blkcipher(tfm);
  43055. + return 0;
  43056. +}
  43057. +
  43058. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  43059. +{
  43060. + struct crypto_hash *tfm;
  43061. + struct hash_desc desc;
  43062. + struct scatterlist sg;
  43063. +
  43064. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  43065. + if (IS_ERR(tfm)) {
  43066. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  43067. + return 0;
  43068. + }
  43069. + desc.tfm = tfm;
  43070. + desc.flags = 0;
  43071. +
  43072. + sg_init_one(&sg, message, len);
  43073. + crypto_hash_digest(&desc, &sg, len, out);
  43074. + crypto_free_hash(tfm);
  43075. +
  43076. + return 1;
  43077. +}
  43078. +
  43079. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  43080. + uint8_t *key, uint32_t keylen, uint8_t *out)
  43081. +{
  43082. + struct crypto_hash *tfm;
  43083. + struct hash_desc desc;
  43084. + struct scatterlist sg;
  43085. +
  43086. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  43087. + if (IS_ERR(tfm)) {
  43088. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  43089. + return 0;
  43090. + }
  43091. + desc.tfm = tfm;
  43092. + desc.flags = 0;
  43093. +
  43094. + sg_init_one(&sg, message, messagelen);
  43095. + crypto_hash_setkey(tfm, key, keylen);
  43096. + crypto_hash_digest(&desc, &sg, messagelen, out);
  43097. + crypto_free_hash(tfm);
  43098. +
  43099. + return 1;
  43100. +}
  43101. +
  43102. +#endif /* DWC_CRYPTOLIB */
  43103. +
  43104. +
  43105. +/* Byte Ordering Conversions */
  43106. +
  43107. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  43108. +{
  43109. +#ifdef __LITTLE_ENDIAN
  43110. + return *p;
  43111. +#else
  43112. + uint8_t *u_p = (uint8_t *)p;
  43113. +
  43114. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43115. +#endif
  43116. +}
  43117. +
  43118. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  43119. +{
  43120. +#ifdef __BIG_ENDIAN
  43121. + return *p;
  43122. +#else
  43123. + uint8_t *u_p = (uint8_t *)p;
  43124. +
  43125. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43126. +#endif
  43127. +}
  43128. +
  43129. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  43130. +{
  43131. +#ifdef __LITTLE_ENDIAN
  43132. + return *p;
  43133. +#else
  43134. + uint8_t *u_p = (uint8_t *)p;
  43135. +
  43136. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43137. +#endif
  43138. +}
  43139. +
  43140. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  43141. +{
  43142. +#ifdef __BIG_ENDIAN
  43143. + return *p;
  43144. +#else
  43145. + uint8_t *u_p = (uint8_t *)p;
  43146. +
  43147. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43148. +#endif
  43149. +}
  43150. +
  43151. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  43152. +{
  43153. +#ifdef __LITTLE_ENDIAN
  43154. + return *p;
  43155. +#else
  43156. + uint8_t *u_p = (uint8_t *)p;
  43157. + return (u_p[1] | (u_p[0] << 8));
  43158. +#endif
  43159. +}
  43160. +
  43161. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  43162. +{
  43163. +#ifdef __BIG_ENDIAN
  43164. + return *p;
  43165. +#else
  43166. + uint8_t *u_p = (uint8_t *)p;
  43167. + return (u_p[1] | (u_p[0] << 8));
  43168. +#endif
  43169. +}
  43170. +
  43171. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  43172. +{
  43173. +#ifdef __LITTLE_ENDIAN
  43174. + return *p;
  43175. +#else
  43176. + uint8_t *u_p = (uint8_t *)p;
  43177. + return (u_p[1] | (u_p[0] << 8));
  43178. +#endif
  43179. +}
  43180. +
  43181. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  43182. +{
  43183. +#ifdef __BIG_ENDIAN
  43184. + return *p;
  43185. +#else
  43186. + uint8_t *u_p = (uint8_t *)p;
  43187. + return (u_p[1] | (u_p[0] << 8));
  43188. +#endif
  43189. +}
  43190. +
  43191. +
  43192. +/* Registers */
  43193. +
  43194. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  43195. +{
  43196. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43197. + bus_size_t ior = (bus_size_t)reg;
  43198. +
  43199. + return bus_space_read_4(io->iot, io->ioh, ior);
  43200. +}
  43201. +
  43202. +#if 0
  43203. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  43204. +{
  43205. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43206. + bus_size_t ior = (bus_size_t)reg;
  43207. +
  43208. + return bus_space_read_8(io->iot, io->ioh, ior);
  43209. +}
  43210. +#endif
  43211. +
  43212. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  43213. +{
  43214. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43215. + bus_size_t ior = (bus_size_t)reg;
  43216. +
  43217. + bus_space_write_4(io->iot, io->ioh, ior, value);
  43218. +}
  43219. +
  43220. +#if 0
  43221. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  43222. +{
  43223. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43224. + bus_size_t ior = (bus_size_t)reg;
  43225. +
  43226. + bus_space_write_8(io->iot, io->ioh, ior, value);
  43227. +}
  43228. +#endif
  43229. +
  43230. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  43231. + uint32_t set_mask)
  43232. +{
  43233. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43234. + bus_size_t ior = (bus_size_t)reg;
  43235. +
  43236. + bus_space_write_4(io->iot, io->ioh, ior,
  43237. + (bus_space_read_4(io->iot, io->ioh, ior) &
  43238. + ~clear_mask) | set_mask);
  43239. +}
  43240. +
  43241. +#if 0
  43242. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  43243. + uint64_t set_mask)
  43244. +{
  43245. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43246. + bus_size_t ior = (bus_size_t)reg;
  43247. +
  43248. + bus_space_write_8(io->iot, io->ioh, ior,
  43249. + (bus_space_read_8(io->iot, io->ioh, ior) &
  43250. + ~clear_mask) | set_mask);
  43251. +}
  43252. +#endif
  43253. +
  43254. +
  43255. +/* Locking */
  43256. +
  43257. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  43258. +{
  43259. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  43260. +
  43261. + if (!sl) {
  43262. + DWC_ERROR("Cannot allocate memory for spinlock");
  43263. + return NULL;
  43264. + }
  43265. +
  43266. + simple_lock_init(sl);
  43267. + return (dwc_spinlock_t *)sl;
  43268. +}
  43269. +
  43270. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  43271. +{
  43272. + struct simplelock *sl = (struct simplelock *)lock;
  43273. +
  43274. + DWC_FREE(sl);
  43275. +}
  43276. +
  43277. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  43278. +{
  43279. + simple_lock((struct simplelock *)lock);
  43280. +}
  43281. +
  43282. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  43283. +{
  43284. + simple_unlock((struct simplelock *)lock);
  43285. +}
  43286. +
  43287. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  43288. +{
  43289. + simple_lock((struct simplelock *)lock);
  43290. + *flags = splbio();
  43291. +}
  43292. +
  43293. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  43294. +{
  43295. + splx(flags);
  43296. + simple_unlock((struct simplelock *)lock);
  43297. +}
  43298. +
  43299. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  43300. +{
  43301. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  43302. +
  43303. + if (!mutex) {
  43304. + DWC_ERROR("Cannot allocate memory for mutex");
  43305. + return NULL;
  43306. + }
  43307. +
  43308. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  43309. + return mutex;
  43310. +}
  43311. +
  43312. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  43313. +#else
  43314. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  43315. +{
  43316. + DWC_FREE(mutex);
  43317. +}
  43318. +#endif
  43319. +
  43320. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  43321. +{
  43322. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  43323. +}
  43324. +
  43325. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  43326. +{
  43327. + int status;
  43328. +
  43329. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  43330. + return status == 0;
  43331. +}
  43332. +
  43333. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  43334. +{
  43335. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  43336. +}
  43337. +
  43338. +
  43339. +/* Timing */
  43340. +
  43341. +void DWC_UDELAY(uint32_t usecs)
  43342. +{
  43343. + DELAY(usecs);
  43344. +}
  43345. +
  43346. +void DWC_MDELAY(uint32_t msecs)
  43347. +{
  43348. + do {
  43349. + DELAY(1000);
  43350. + } while (--msecs);
  43351. +}
  43352. +
  43353. +void DWC_MSLEEP(uint32_t msecs)
  43354. +{
  43355. + struct timeval tv;
  43356. +
  43357. + tv.tv_sec = msecs / 1000;
  43358. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  43359. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  43360. +}
  43361. +
  43362. +uint32_t DWC_TIME(void)
  43363. +{
  43364. + struct timeval tv;
  43365. +
  43366. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  43367. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  43368. +}
  43369. +
  43370. +
  43371. +/* Timers */
  43372. +
  43373. +struct dwc_timer {
  43374. + struct callout t;
  43375. + char *name;
  43376. + dwc_spinlock_t *lock;
  43377. + dwc_timer_callback_t cb;
  43378. + void *data;
  43379. +};
  43380. +
  43381. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  43382. +{
  43383. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  43384. +
  43385. + if (!t) {
  43386. + DWC_ERROR("Cannot allocate memory for timer");
  43387. + return NULL;
  43388. + }
  43389. +
  43390. + callout_init(&t->t);
  43391. +
  43392. + t->name = DWC_STRDUP(name);
  43393. + if (!t->name) {
  43394. + DWC_ERROR("Cannot allocate memory for timer->name");
  43395. + goto no_name;
  43396. + }
  43397. +
  43398. + t->lock = DWC_SPINLOCK_ALLOC();
  43399. + if (!t->lock) {
  43400. + DWC_ERROR("Cannot allocate memory for timer->lock");
  43401. + goto no_lock;
  43402. + }
  43403. +
  43404. + t->cb = cb;
  43405. + t->data = data;
  43406. +
  43407. + return t;
  43408. +
  43409. + no_lock:
  43410. + DWC_FREE(t->name);
  43411. + no_name:
  43412. + DWC_FREE(t);
  43413. +
  43414. + return NULL;
  43415. +}
  43416. +
  43417. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  43418. +{
  43419. + callout_stop(&timer->t);
  43420. + DWC_SPINLOCK_FREE(timer->lock);
  43421. + DWC_FREE(timer->name);
  43422. + DWC_FREE(timer);
  43423. +}
  43424. +
  43425. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  43426. +{
  43427. + struct timeval tv;
  43428. +
  43429. + tv.tv_sec = time / 1000;
  43430. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  43431. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  43432. +}
  43433. +
  43434. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  43435. +{
  43436. + callout_stop(&timer->t);
  43437. +}
  43438. +
  43439. +
  43440. +/* Wait Queues */
  43441. +
  43442. +struct dwc_waitq {
  43443. + struct simplelock lock;
  43444. + int abort;
  43445. +};
  43446. +
  43447. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  43448. +{
  43449. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  43450. +
  43451. + if (!wq) {
  43452. + DWC_ERROR("Cannot allocate memory for waitqueue");
  43453. + return NULL;
  43454. + }
  43455. +
  43456. + simple_lock_init(&wq->lock);
  43457. + wq->abort = 0;
  43458. +
  43459. + return wq;
  43460. +}
  43461. +
  43462. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  43463. +{
  43464. + DWC_FREE(wq);
  43465. +}
  43466. +
  43467. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  43468. +{
  43469. + int ipl;
  43470. + int result = 0;
  43471. +
  43472. + simple_lock(&wq->lock);
  43473. + ipl = splbio();
  43474. +
  43475. + /* Skip the sleep if already aborted or triggered */
  43476. + if (!wq->abort && !cond(data)) {
  43477. + splx(ipl);
  43478. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  43479. + ipl = splbio();
  43480. + }
  43481. +
  43482. + if (result == 0) { // awoken
  43483. + if (wq->abort) {
  43484. + wq->abort = 0;
  43485. + result = -DWC_E_ABORT;
  43486. + } else {
  43487. + result = 0;
  43488. + }
  43489. +
  43490. + splx(ipl);
  43491. + simple_unlock(&wq->lock);
  43492. + } else {
  43493. + wq->abort = 0;
  43494. + splx(ipl);
  43495. + simple_unlock(&wq->lock);
  43496. +
  43497. + if (result == ERESTART) { // signaled - restart
  43498. + result = -DWC_E_RESTART;
  43499. + } else { // signaled - must be EINTR
  43500. + result = -DWC_E_ABORT;
  43501. + }
  43502. + }
  43503. +
  43504. + return result;
  43505. +}
  43506. +
  43507. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  43508. + void *data, int32_t msecs)
  43509. +{
  43510. + struct timeval tv, tv1, tv2;
  43511. + int ipl;
  43512. + int result = 0;
  43513. +
  43514. + tv.tv_sec = msecs / 1000;
  43515. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  43516. +
  43517. + simple_lock(&wq->lock);
  43518. + ipl = splbio();
  43519. +
  43520. + /* Skip the sleep if already aborted or triggered */
  43521. + if (!wq->abort && !cond(data)) {
  43522. + splx(ipl);
  43523. + getmicrouptime(&tv1);
  43524. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  43525. + getmicrouptime(&tv2);
  43526. + ipl = splbio();
  43527. + }
  43528. +
  43529. + if (result == 0) { // awoken
  43530. + if (wq->abort) {
  43531. + wq->abort = 0;
  43532. + splx(ipl);
  43533. + simple_unlock(&wq->lock);
  43534. + result = -DWC_E_ABORT;
  43535. + } else {
  43536. + splx(ipl);
  43537. + simple_unlock(&wq->lock);
  43538. +
  43539. + tv2.tv_usec -= tv1.tv_usec;
  43540. + if (tv2.tv_usec < 0) {
  43541. + tv2.tv_usec += 1000000;
  43542. + tv2.tv_sec--;
  43543. + }
  43544. +
  43545. + tv2.tv_sec -= tv1.tv_sec;
  43546. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  43547. + result = msecs - result;
  43548. + if (result <= 0)
  43549. + result = 1;
  43550. + }
  43551. + } else {
  43552. + wq->abort = 0;
  43553. + splx(ipl);
  43554. + simple_unlock(&wq->lock);
  43555. +
  43556. + if (result == ERESTART) { // signaled - restart
  43557. + result = -DWC_E_RESTART;
  43558. +
  43559. + } else if (result == EINTR) { // signaled - interrupt
  43560. + result = -DWC_E_ABORT;
  43561. +
  43562. + } else { // timed out
  43563. + result = -DWC_E_TIMEOUT;
  43564. + }
  43565. + }
  43566. +
  43567. + return result;
  43568. +}
  43569. +
  43570. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  43571. +{
  43572. + wakeup(wq);
  43573. +}
  43574. +
  43575. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  43576. +{
  43577. + int ipl;
  43578. +
  43579. + simple_lock(&wq->lock);
  43580. + ipl = splbio();
  43581. + wq->abort = 1;
  43582. + wakeup(wq);
  43583. + splx(ipl);
  43584. + simple_unlock(&wq->lock);
  43585. +}
  43586. +
  43587. +
  43588. +/* Threading */
  43589. +
  43590. +struct dwc_thread {
  43591. + struct proc *proc;
  43592. + int abort;
  43593. +};
  43594. +
  43595. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  43596. +{
  43597. + int retval;
  43598. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  43599. +
  43600. + if (!thread) {
  43601. + return NULL;
  43602. + }
  43603. +
  43604. + thread->abort = 0;
  43605. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  43606. + "%s", name);
  43607. + if (retval) {
  43608. + DWC_FREE(thread);
  43609. + return NULL;
  43610. + }
  43611. +
  43612. + return thread;
  43613. +}
  43614. +
  43615. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  43616. +{
  43617. + int retval;
  43618. +
  43619. + thread->abort = 1;
  43620. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  43621. +
  43622. + if (retval == 0) {
  43623. + /* DWC_THREAD_EXIT() will free the thread struct */
  43624. + return 0;
  43625. + }
  43626. +
  43627. + /* NOTE: We leak the thread struct if thread doesn't die */
  43628. +
  43629. + if (retval == EWOULDBLOCK) {
  43630. + return -DWC_E_TIMEOUT;
  43631. + }
  43632. +
  43633. + return -DWC_E_UNKNOWN;
  43634. +}
  43635. +
  43636. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  43637. +{
  43638. + return thread->abort;
  43639. +}
  43640. +
  43641. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  43642. +{
  43643. + wakeup(&thread->abort);
  43644. + DWC_FREE(thread);
  43645. + kthread_exit(0);
  43646. +}
  43647. +
  43648. +/* tasklets
  43649. + - Runs in interrupt context (cannot sleep)
  43650. + - Each tasklet runs on a single CPU
  43651. + - Different tasklets can be running simultaneously on different CPUs
  43652. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  43653. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  43654. + */
  43655. +struct dwc_tasklet {
  43656. + dwc_tasklet_callback_t cb;
  43657. + void *data;
  43658. +};
  43659. +
  43660. +static void tasklet_callback(void *data)
  43661. +{
  43662. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  43663. +
  43664. + task->cb(task->data);
  43665. +}
  43666. +
  43667. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  43668. +{
  43669. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  43670. +
  43671. + if (task) {
  43672. + task->cb = cb;
  43673. + task->data = data;
  43674. + } else {
  43675. + DWC_ERROR("Cannot allocate memory for tasklet");
  43676. + }
  43677. +
  43678. + return task;
  43679. +}
  43680. +
  43681. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  43682. +{
  43683. + DWC_FREE(task);
  43684. +}
  43685. +
  43686. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  43687. +{
  43688. + tasklet_callback(task);
  43689. +}
  43690. +
  43691. +
  43692. +/* workqueues
  43693. + - Runs in process context (can sleep)
  43694. + */
  43695. +typedef struct work_container {
  43696. + dwc_work_callback_t cb;
  43697. + void *data;
  43698. + dwc_workq_t *wq;
  43699. + char *name;
  43700. + int hz;
  43701. + struct work task;
  43702. +} work_container_t;
  43703. +
  43704. +struct dwc_workq {
  43705. + struct workqueue *taskq;
  43706. + dwc_spinlock_t *lock;
  43707. + dwc_waitq_t *waitq;
  43708. + int pending;
  43709. + struct work_container *container;
  43710. +};
  43711. +
  43712. +static void do_work(struct work *task, void *data)
  43713. +{
  43714. + dwc_workq_t *wq = (dwc_workq_t *)data;
  43715. + work_container_t *container = wq->container;
  43716. + dwc_irqflags_t flags;
  43717. +
  43718. + if (container->hz) {
  43719. + tsleep(container, 0, "dw3wrk", container->hz);
  43720. + }
  43721. +
  43722. + container->cb(container->data);
  43723. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  43724. +
  43725. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43726. + if (container->name)
  43727. + DWC_FREE(container->name);
  43728. + DWC_FREE(container);
  43729. + wq->pending--;
  43730. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43731. + DWC_WAITQ_TRIGGER(wq->waitq);
  43732. +}
  43733. +
  43734. +static int work_done(void *data)
  43735. +{
  43736. + dwc_workq_t *workq = (dwc_workq_t *)data;
  43737. +
  43738. + return workq->pending == 0;
  43739. +}
  43740. +
  43741. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  43742. +{
  43743. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  43744. +}
  43745. +
  43746. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  43747. +{
  43748. + int result;
  43749. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  43750. +
  43751. + if (!wq) {
  43752. + DWC_ERROR("Cannot allocate memory for workqueue");
  43753. + return NULL;
  43754. + }
  43755. +
  43756. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  43757. + IPL_BIO, 0);
  43758. + if (result) {
  43759. + DWC_ERROR("Cannot create workqueue");
  43760. + goto no_taskq;
  43761. + }
  43762. +
  43763. + wq->pending = 0;
  43764. +
  43765. + wq->lock = DWC_SPINLOCK_ALLOC();
  43766. + if (!wq->lock) {
  43767. + DWC_ERROR("Cannot allocate memory for spinlock");
  43768. + goto no_lock;
  43769. + }
  43770. +
  43771. + wq->waitq = DWC_WAITQ_ALLOC();
  43772. + if (!wq->waitq) {
  43773. + DWC_ERROR("Cannot allocate memory for waitqueue");
  43774. + goto no_waitq;
  43775. + }
  43776. +
  43777. + return wq;
  43778. +
  43779. + no_waitq:
  43780. + DWC_SPINLOCK_FREE(wq->lock);
  43781. + no_lock:
  43782. + workqueue_destroy(wq->taskq);
  43783. + no_taskq:
  43784. + DWC_FREE(wq);
  43785. +
  43786. + return NULL;
  43787. +}
  43788. +
  43789. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  43790. +{
  43791. +#ifdef DEBUG
  43792. + dwc_irqflags_t flags;
  43793. +
  43794. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43795. +
  43796. + if (wq->pending != 0) {
  43797. + struct work_container *container = wq->container;
  43798. +
  43799. + DWC_ERROR("Destroying work queue with pending work");
  43800. +
  43801. + if (container && container->name) {
  43802. + DWC_ERROR("Work %s still pending", container->name);
  43803. + }
  43804. + }
  43805. +
  43806. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43807. +#endif
  43808. + DWC_WAITQ_FREE(wq->waitq);
  43809. + DWC_SPINLOCK_FREE(wq->lock);
  43810. + workqueue_destroy(wq->taskq);
  43811. + DWC_FREE(wq);
  43812. +}
  43813. +
  43814. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  43815. + char *format, ...)
  43816. +{
  43817. + dwc_irqflags_t flags;
  43818. + work_container_t *container;
  43819. + static char name[128];
  43820. + va_list args;
  43821. +
  43822. + va_start(args, format);
  43823. + DWC_VSNPRINTF(name, 128, format, args);
  43824. + va_end(args);
  43825. +
  43826. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43827. + wq->pending++;
  43828. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43829. + DWC_WAITQ_TRIGGER(wq->waitq);
  43830. +
  43831. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  43832. + if (!container) {
  43833. + DWC_ERROR("Cannot allocate memory for container");
  43834. + return;
  43835. + }
  43836. +
  43837. + container->name = DWC_STRDUP(name);
  43838. + if (!container->name) {
  43839. + DWC_ERROR("Cannot allocate memory for container->name");
  43840. + DWC_FREE(container);
  43841. + return;
  43842. + }
  43843. +
  43844. + container->cb = cb;
  43845. + container->data = data;
  43846. + container->wq = wq;
  43847. + container->hz = 0;
  43848. + wq->container = container;
  43849. +
  43850. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  43851. + workqueue_enqueue(wq->taskq, &container->task);
  43852. +}
  43853. +
  43854. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  43855. + void *data, uint32_t time, char *format, ...)
  43856. +{
  43857. + dwc_irqflags_t flags;
  43858. + work_container_t *container;
  43859. + static char name[128];
  43860. + struct timeval tv;
  43861. + va_list args;
  43862. +
  43863. + va_start(args, format);
  43864. + DWC_VSNPRINTF(name, 128, format, args);
  43865. + va_end(args);
  43866. +
  43867. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43868. + wq->pending++;
  43869. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43870. + DWC_WAITQ_TRIGGER(wq->waitq);
  43871. +
  43872. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  43873. + if (!container) {
  43874. + DWC_ERROR("Cannot allocate memory for container");
  43875. + return;
  43876. + }
  43877. +
  43878. + container->name = DWC_STRDUP(name);
  43879. + if (!container->name) {
  43880. + DWC_ERROR("Cannot allocate memory for container->name");
  43881. + DWC_FREE(container);
  43882. + return;
  43883. + }
  43884. +
  43885. + container->cb = cb;
  43886. + container->data = data;
  43887. + container->wq = wq;
  43888. + tv.tv_sec = time / 1000;
  43889. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  43890. + container->hz = tvtohz(&tv);
  43891. + wq->container = container;
  43892. +
  43893. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  43894. + workqueue_enqueue(wq->taskq, &container->task);
  43895. +}
  43896. +
  43897. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  43898. +{
  43899. + return wq->pending;
  43900. +}
  43901. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_crypto.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.c
  43902. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_crypto.c 1970-01-01 01:00:00.000000000 +0100
  43903. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.c 2014-03-11 17:33:06.000000000 +0100
  43904. @@ -0,0 +1,308 @@
  43905. +/* =========================================================================
  43906. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  43907. + * $Revision: #5 $
  43908. + * $Date: 2010/09/28 $
  43909. + * $Change: 1596182 $
  43910. + *
  43911. + * Synopsys Portability Library Software and documentation
  43912. + * (hereinafter, "Software") is an Unsupported proprietary work of
  43913. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  43914. + * between Synopsys and you.
  43915. + *
  43916. + * The Software IS NOT an item of Licensed Software or Licensed Product
  43917. + * under any End User Software License Agreement or Agreement for
  43918. + * Licensed Product with Synopsys or any supplement thereto. You are
  43919. + * permitted to use and redistribute this Software in source and binary
  43920. + * forms, with or without modification, provided that redistributions
  43921. + * of source code must retain this notice. You may not view, use,
  43922. + * disclose, copy or distribute this file or any information contained
  43923. + * herein except pursuant to this license grant from Synopsys. If you
  43924. + * do not agree with this notice, including the disclaimer below, then
  43925. + * you are not authorized to use the Software.
  43926. + *
  43927. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  43928. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43929. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  43930. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  43931. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43932. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  43933. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  43934. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  43935. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43936. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  43937. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  43938. + * DAMAGE.
  43939. + * ========================================================================= */
  43940. +
  43941. +/** @file
  43942. + * This file contains the WUSB cryptographic routines.
  43943. + */
  43944. +
  43945. +#ifdef DWC_CRYPTOLIB
  43946. +
  43947. +#include "dwc_crypto.h"
  43948. +#include "usb.h"
  43949. +
  43950. +#ifdef DEBUG
  43951. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  43952. +{
  43953. + int i;
  43954. + DWC_PRINTF("%s: ", name);
  43955. + for (i=0; i<len; i++) {
  43956. + DWC_PRINTF("%02x ", bytes[i]);
  43957. + }
  43958. + DWC_PRINTF("\n");
  43959. +}
  43960. +#else
  43961. +#define dump_bytes(x...)
  43962. +#endif
  43963. +
  43964. +/* Display a block */
  43965. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  43966. +{
  43967. +#ifdef DWC_DEBUG_CRYPTO
  43968. + int i, blksize = 16;
  43969. +
  43970. + DWC_DEBUG("%s", prefix);
  43971. +
  43972. + if (suffix == NULL) {
  43973. + suffix = "\n";
  43974. + blksize = a;
  43975. + }
  43976. +
  43977. + for (i = 0; i < blksize; i++)
  43978. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  43979. + DWC_PRINT(suffix);
  43980. +#endif
  43981. +}
  43982. +
  43983. +/**
  43984. + * Encrypts an array of bytes using the AES encryption engine.
  43985. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  43986. + * in-place.
  43987. + *
  43988. + * @return 0 on success, negative error code on error.
  43989. + */
  43990. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  43991. +{
  43992. + u8 block_t[16];
  43993. + DWC_MEMSET(block_t, 0, 16);
  43994. +
  43995. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  43996. +}
  43997. +
  43998. +/**
  43999. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  44000. + * This function takes a data string and returns the encrypted CBC
  44001. + * Counter-mode MIC.
  44002. + *
  44003. + * @param key The 128-bit symmetric key.
  44004. + * @param nonce The CCM nonce.
  44005. + * @param label The unique 14-byte ASCII text label.
  44006. + * @param bytes The byte array to be encrypted.
  44007. + * @param len Length of the byte array.
  44008. + * @param result Byte array to receive the 8-byte encrypted MIC.
  44009. + */
  44010. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  44011. + char *label, u8 *bytes, int len, u8 *result)
  44012. +{
  44013. + u8 block_m[16];
  44014. + u8 block_x[16];
  44015. + u8 block_t[8];
  44016. + int idx, blkNum;
  44017. + u16 la = (u16)(len + 14);
  44018. +
  44019. + /* Set the AES-128 key */
  44020. + //dwc_aes_setkey(tfm, key, 16);
  44021. +
  44022. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  44023. + block_m[0] = 0x59;
  44024. + for (idx = 0; idx < 13; idx++)
  44025. + block_m[idx + 1] = nonce[idx];
  44026. + block_m[14] = 0;
  44027. + block_m[15] = 0;
  44028. +
  44029. + /* Produce the CBC IV */
  44030. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  44031. + show_block(block_m, "CBC IV in: ", "\n", 0);
  44032. + show_block(block_x, "CBC IV out:", "\n", 0);
  44033. +
  44034. + /* Fill block B1 from l(a) = Blen + 14, and A */
  44035. + block_x[0] ^= (u8)(la >> 8);
  44036. + block_x[1] ^= (u8)la;
  44037. + for (idx = 0; idx < 14; idx++)
  44038. + block_x[idx + 2] ^= label[idx];
  44039. + show_block(block_x, "After xor: ", "b1\n", 16);
  44040. +
  44041. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  44042. + show_block(block_x, "After AES: ", "b1\n", 16);
  44043. +
  44044. + idx = 0;
  44045. + blkNum = 0;
  44046. +
  44047. + /* Fill remaining blocks with B */
  44048. + while (len-- > 0) {
  44049. + block_x[idx] ^= *bytes++;
  44050. + if (++idx >= 16) {
  44051. + idx = 0;
  44052. + show_block(block_x, "After xor: ", "\n", blkNum);
  44053. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  44054. + show_block(block_x, "After AES: ", "\n", blkNum);
  44055. + blkNum++;
  44056. + }
  44057. + }
  44058. +
  44059. + /* Handle partial last block */
  44060. + if (idx > 0) {
  44061. + show_block(block_x, "After xor: ", "\n", blkNum);
  44062. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  44063. + show_block(block_x, "After AES: ", "\n", blkNum);
  44064. + }
  44065. +
  44066. + /* Save the MIC tag */
  44067. + DWC_MEMCPY(block_t, block_x, 8);
  44068. + show_block(block_t, "MIC tag : ", NULL, 8);
  44069. +
  44070. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  44071. + block_m[0] = 0x01;
  44072. + block_m[14] = 0;
  44073. + block_m[15] = 0;
  44074. +
  44075. + /* Encrypt the counter */
  44076. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  44077. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  44078. +
  44079. + /* XOR with MIC tag */
  44080. + for (idx = 0; idx < 8; idx++) {
  44081. + block_t[idx] ^= block_x[idx];
  44082. + }
  44083. +
  44084. + /* Return result to caller */
  44085. + DWC_MEMCPY(result, block_t, 8);
  44086. + show_block(result, "CCM-MIC : ", NULL, 8);
  44087. +
  44088. +}
  44089. +
  44090. +/**
  44091. + * The PRF function described in section 6.5 of the WUSB spec. This function
  44092. + * concatenates MIC values returned from dwc_cmf() to create a value of
  44093. + * the requested length.
  44094. + *
  44095. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  44096. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  44097. + * @param result Byte array to receive the result.
  44098. + */
  44099. +void dwc_wusb_prf(int prf_len, u8 *key,
  44100. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  44101. +{
  44102. + int i;
  44103. +
  44104. + nonce[0] = 0;
  44105. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  44106. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  44107. + result += 8;
  44108. + }
  44109. +}
  44110. +
  44111. +/**
  44112. + * Fills in CCM Nonce per the WUSB spec.
  44113. + *
  44114. + * @param[in] haddr Host address.
  44115. + * @param[in] daddr Device address.
  44116. + * @param[in] tkid Session Key(PTK) identifier.
  44117. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  44118. + */
  44119. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  44120. + uint8_t *nonce)
  44121. +{
  44122. +
  44123. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  44124. +
  44125. + DWC_MEMSET(&nonce[0], 0, 16);
  44126. +
  44127. + DWC_MEMCPY(&nonce[6], tkid, 3);
  44128. + nonce[9] = daddr & 0xFF;
  44129. + nonce[10] = (daddr >> 8) & 0xFF;
  44130. + nonce[11] = haddr & 0xFF;
  44131. + nonce[12] = (haddr >> 8) & 0xFF;
  44132. +
  44133. + dump_bytes("CCM nonce", nonce, 16);
  44134. +}
  44135. +
  44136. +/**
  44137. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  44138. + * Nonce.
  44139. + */
  44140. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  44141. +{
  44142. + uint8_t inonce[16];
  44143. + uint32_t temp[4];
  44144. +
  44145. + /* Fill in the Nonce */
  44146. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  44147. + inonce[9] = addr & 0xFF;
  44148. + inonce[10] = (addr >> 8) & 0xFF;
  44149. + inonce[11] = inonce[9];
  44150. + inonce[12] = inonce[10];
  44151. +
  44152. + /* Collect "randomness samples" */
  44153. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  44154. +
  44155. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  44156. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  44157. + nonce);
  44158. +}
  44159. +
  44160. +/**
  44161. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  44162. + * WUSB spec.
  44163. + *
  44164. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  44165. + * @param[in] mk Master Key to derive the session from
  44166. + * @param[in] hnonce Pointer to Host Nonce.
  44167. + * @param[in] dnonce Pointer to Device Nonce.
  44168. + * @param[out] kck Pointer to where the KCK output is to be written.
  44169. + * @param[out] ptk Pointer to where the PTK output is to be written.
  44170. + */
  44171. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  44172. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  44173. +{
  44174. + uint8_t idata[32];
  44175. + uint8_t odata[32];
  44176. +
  44177. + dump_bytes("ck", mk, 16);
  44178. + dump_bytes("hnonce", hnonce, 16);
  44179. + dump_bytes("dnonce", dnonce, 16);
  44180. +
  44181. + /* The data is the HNonce and DNonce concatenated */
  44182. + DWC_MEMCPY(&idata[0], hnonce, 16);
  44183. + DWC_MEMCPY(&idata[16], dnonce, 16);
  44184. +
  44185. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  44186. +
  44187. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  44188. + DWC_MEMCPY(kck, &odata[0], 16);
  44189. + DWC_MEMCPY(ptk, &odata[16], 16);
  44190. +
  44191. + dump_bytes("kck", kck, 16);
  44192. + dump_bytes("ptk", ptk, 16);
  44193. +}
  44194. +
  44195. +/**
  44196. + * Generates the Message Integrity Code over the Handshake data per the
  44197. + * WUSB spec.
  44198. + *
  44199. + * @param ccm_nonce Pointer to CCM Nonce.
  44200. + * @param kck Pointer to Key Confirmation Key.
  44201. + * @param data Pointer to Handshake data to be checked.
  44202. + * @param mic Pointer to where the MIC output is to be written.
  44203. + */
  44204. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  44205. + uint8_t *data, uint8_t *mic)
  44206. +{
  44207. +
  44208. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  44209. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  44210. +}
  44211. +
  44212. +#endif /* DWC_CRYPTOLIB */
  44213. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_crypto.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.h
  44214. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_crypto.h 1970-01-01 01:00:00.000000000 +0100
  44215. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.h 2014-03-11 17:33:06.000000000 +0100
  44216. @@ -0,0 +1,111 @@
  44217. +/* =========================================================================
  44218. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  44219. + * $Revision: #3 $
  44220. + * $Date: 2010/09/28 $
  44221. + * $Change: 1596182 $
  44222. + *
  44223. + * Synopsys Portability Library Software and documentation
  44224. + * (hereinafter, "Software") is an Unsupported proprietary work of
  44225. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  44226. + * between Synopsys and you.
  44227. + *
  44228. + * The Software IS NOT an item of Licensed Software or Licensed Product
  44229. + * under any End User Software License Agreement or Agreement for
  44230. + * Licensed Product with Synopsys or any supplement thereto. You are
  44231. + * permitted to use and redistribute this Software in source and binary
  44232. + * forms, with or without modification, provided that redistributions
  44233. + * of source code must retain this notice. You may not view, use,
  44234. + * disclose, copy or distribute this file or any information contained
  44235. + * herein except pursuant to this license grant from Synopsys. If you
  44236. + * do not agree with this notice, including the disclaimer below, then
  44237. + * you are not authorized to use the Software.
  44238. + *
  44239. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44240. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44241. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44242. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44243. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44244. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44245. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44246. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44247. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44248. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44249. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44250. + * DAMAGE.
  44251. + * ========================================================================= */
  44252. +
  44253. +#ifndef _DWC_CRYPTO_H_
  44254. +#define _DWC_CRYPTO_H_
  44255. +
  44256. +#ifdef __cplusplus
  44257. +extern "C" {
  44258. +#endif
  44259. +
  44260. +/** @file
  44261. + *
  44262. + * This file contains declarations for the WUSB Cryptographic routines as
  44263. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  44264. + * modules.
  44265. + */
  44266. +
  44267. +#include "dwc_os.h"
  44268. +
  44269. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  44270. +
  44271. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  44272. + char *label, u8 *bytes, int len, u8 *result);
  44273. +void dwc_wusb_prf(int prf_len, u8 *key,
  44274. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  44275. +
  44276. +/**
  44277. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  44278. + *
  44279. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  44280. + */
  44281. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  44282. + char *label, u8 *bytes, int len, u8 *result)
  44283. +{
  44284. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  44285. +}
  44286. +
  44287. +/**
  44288. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  44289. + *
  44290. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  44291. + */
  44292. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  44293. + char *label, u8 *bytes, int len, u8 *result)
  44294. +{
  44295. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  44296. +}
  44297. +
  44298. +/**
  44299. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  44300. + *
  44301. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  44302. + */
  44303. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  44304. + char *label, u8 *bytes, int len, u8 *result)
  44305. +{
  44306. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  44307. +}
  44308. +
  44309. +
  44310. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  44311. + uint8_t *nonce);
  44312. +void dwc_wusb_gen_nonce(uint16_t addr,
  44313. + uint8_t *nonce);
  44314. +
  44315. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  44316. + uint8_t *hnonce, uint8_t *dnonce,
  44317. + uint8_t *kck, uint8_t *ptk);
  44318. +
  44319. +
  44320. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  44321. + *kck, uint8_t *data, uint8_t *mic);
  44322. +
  44323. +#ifdef __cplusplus
  44324. +}
  44325. +#endif
  44326. +
  44327. +#endif /* _DWC_CRYPTO_H_ */
  44328. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_dh.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.c
  44329. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_dh.c 1970-01-01 01:00:00.000000000 +0100
  44330. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.c 2014-03-11 17:51:27.000000000 +0100
  44331. @@ -0,0 +1,291 @@
  44332. +/* =========================================================================
  44333. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  44334. + * $Revision: #3 $
  44335. + * $Date: 2010/09/28 $
  44336. + * $Change: 1596182 $
  44337. + *
  44338. + * Synopsys Portability Library Software and documentation
  44339. + * (hereinafter, "Software") is an Unsupported proprietary work of
  44340. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  44341. + * between Synopsys and you.
  44342. + *
  44343. + * The Software IS NOT an item of Licensed Software or Licensed Product
  44344. + * under any End User Software License Agreement or Agreement for
  44345. + * Licensed Product with Synopsys or any supplement thereto. You are
  44346. + * permitted to use and redistribute this Software in source and binary
  44347. + * forms, with or without modification, provided that redistributions
  44348. + * of source code must retain this notice. You may not view, use,
  44349. + * disclose, copy or distribute this file or any information contained
  44350. + * herein except pursuant to this license grant from Synopsys. If you
  44351. + * do not agree with this notice, including the disclaimer below, then
  44352. + * you are not authorized to use the Software.
  44353. + *
  44354. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44355. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44356. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44357. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44358. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44359. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44360. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44361. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44362. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44363. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44364. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44365. + * DAMAGE.
  44366. + * ========================================================================= */
  44367. +#ifdef DWC_CRYPTOLIB
  44368. +
  44369. +#ifndef CONFIG_MACH_IPMATE
  44370. +
  44371. +#include "dwc_dh.h"
  44372. +#include "dwc_modpow.h"
  44373. +
  44374. +#ifdef DEBUG
  44375. +/* This function prints out a buffer in the format described in the Association
  44376. + * Model specification. */
  44377. +static void dh_dump(char *str, void *_num, int len)
  44378. +{
  44379. + uint8_t *num = _num;
  44380. + int i;
  44381. + DWC_PRINTF("%s\n", str);
  44382. + for (i = 0; i < len; i ++) {
  44383. + DWC_PRINTF("%02x", num[i]);
  44384. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  44385. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  44386. + }
  44387. +
  44388. + DWC_PRINTF("\n");
  44389. +}
  44390. +#else
  44391. +#define dh_dump(_x...) do {; } while(0)
  44392. +#endif
  44393. +
  44394. +/* Constant g value */
  44395. +static __u32 dh_g[] = {
  44396. + 0x02000000,
  44397. +};
  44398. +
  44399. +/* Constant p value */
  44400. +static __u32 dh_p[] = {
  44401. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  44402. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  44403. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  44404. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  44405. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  44406. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  44407. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  44408. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  44409. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  44410. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  44411. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  44412. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  44413. +};
  44414. +
  44415. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  44416. +{
  44417. + uint8_t *in = _in;
  44418. + uint8_t *out = _out;
  44419. + int i;
  44420. + for (i=0; i<len; i++) {
  44421. + out[i] = in[len-1-i];
  44422. + }
  44423. +}
  44424. +
  44425. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  44426. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  44427. + * of 4. */
  44428. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  44429. + void *exp, uint32_t exp_len,
  44430. + void *mod, uint32_t mod_len,
  44431. + void *out)
  44432. +{
  44433. + /* modpow() takes little endian numbers. AM uses big-endian. This
  44434. + * function swaps bytes of numbers before passing onto modpow. */
  44435. +
  44436. + int retval = 0;
  44437. + uint32_t *result;
  44438. +
  44439. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  44440. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  44441. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  44442. +
  44443. + dh_swap_bytes(num, &bignum_num[1], num_len);
  44444. + bignum_num[0] = num_len / 4;
  44445. +
  44446. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  44447. + bignum_exp[0] = exp_len / 4;
  44448. +
  44449. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  44450. + bignum_mod[0] = mod_len / 4;
  44451. +
  44452. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  44453. + if (!result) {
  44454. + retval = -1;
  44455. + goto dh_modpow_nomem;
  44456. + }
  44457. +
  44458. + dh_swap_bytes(&result[1], out, result[0] * 4);
  44459. + dwc_free(mem_ctx, result);
  44460. +
  44461. + dh_modpow_nomem:
  44462. + dwc_free(mem_ctx, bignum_num);
  44463. + dwc_free(mem_ctx, bignum_exp);
  44464. + dwc_free(mem_ctx, bignum_mod);
  44465. + return retval;
  44466. +}
  44467. +
  44468. +
  44469. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  44470. +{
  44471. + int retval;
  44472. + uint8_t m3[385];
  44473. +
  44474. +#ifndef DH_TEST_VECTORS
  44475. + DWC_RANDOM_BYTES(exp, 32);
  44476. +#endif
  44477. +
  44478. + /* Compute the pkd */
  44479. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  44480. + exp, 32,
  44481. + dh_p, 384, pk))) {
  44482. + return retval;
  44483. + }
  44484. +
  44485. + m3[384] = nd;
  44486. + DWC_MEMCPY(&m3[0], pk, 384);
  44487. + DWC_SHA256(m3, 385, hash);
  44488. +
  44489. + dh_dump("PK", pk, 384);
  44490. + dh_dump("SHA-256(M3)", hash, 32);
  44491. + return 0;
  44492. +}
  44493. +
  44494. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  44495. + uint8_t *exp, int is_host,
  44496. + char *dd, uint8_t *ck, uint8_t *kdk)
  44497. +{
  44498. + int retval;
  44499. + uint8_t mv[784];
  44500. + uint8_t sha_result[32];
  44501. + uint8_t dhkey[384];
  44502. + uint8_t shared_secret[384];
  44503. + char *message;
  44504. + uint32_t vd;
  44505. +
  44506. + uint8_t *pk;
  44507. +
  44508. + if (is_host) {
  44509. + pk = pkd;
  44510. + }
  44511. + else {
  44512. + pk = pkh;
  44513. + }
  44514. +
  44515. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  44516. + exp, 32,
  44517. + dh_p, 384, shared_secret))) {
  44518. + return retval;
  44519. + }
  44520. + dh_dump("Shared Secret", shared_secret, 384);
  44521. +
  44522. + DWC_SHA256(shared_secret, 384, dhkey);
  44523. + dh_dump("DHKEY", dhkey, 384);
  44524. +
  44525. + DWC_MEMCPY(&mv[0], pkd, 384);
  44526. + DWC_MEMCPY(&mv[384], pkh, 384);
  44527. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  44528. + dh_dump("MV", mv, 784);
  44529. +
  44530. + DWC_SHA256(mv, 784, sha_result);
  44531. + dh_dump("SHA-256(MV)", sha_result, 32);
  44532. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  44533. +
  44534. + dh_swap_bytes(sha_result, &vd, 4);
  44535. +#ifdef DEBUG
  44536. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  44537. +#endif
  44538. +
  44539. + switch (nd) {
  44540. + case 2:
  44541. + vd = vd % 100;
  44542. + DWC_SPRINTF(dd, "%02d", vd);
  44543. + break;
  44544. + case 3:
  44545. + vd = vd % 1000;
  44546. + DWC_SPRINTF(dd, "%03d", vd);
  44547. + break;
  44548. + case 4:
  44549. + vd = vd % 10000;
  44550. + DWC_SPRINTF(dd, "%04d", vd);
  44551. + break;
  44552. + }
  44553. +#ifdef DEBUG
  44554. + DWC_PRINTF("Display Digits: %s\n", dd);
  44555. +#endif
  44556. +
  44557. + message = "connection key";
  44558. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  44559. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  44560. + DWC_MEMCPY(ck, sha_result, 16);
  44561. +
  44562. + message = "key derivation key";
  44563. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  44564. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  44565. + DWC_MEMCPY(kdk, sha_result, 32);
  44566. +
  44567. + return 0;
  44568. +}
  44569. +
  44570. +
  44571. +#ifdef DH_TEST_VECTORS
  44572. +
  44573. +static __u8 dh_a[] = {
  44574. + 0x44, 0x00, 0x51, 0xd6,
  44575. + 0xf0, 0xb5, 0x5e, 0xa9,
  44576. + 0x67, 0xab, 0x31, 0xc6,
  44577. + 0x8a, 0x8b, 0x5e, 0x37,
  44578. + 0xd9, 0x10, 0xda, 0xe0,
  44579. + 0xe2, 0xd4, 0x59, 0xa4,
  44580. + 0x86, 0x45, 0x9c, 0xaa,
  44581. + 0xdf, 0x36, 0x75, 0x16,
  44582. +};
  44583. +
  44584. +static __u8 dh_b[] = {
  44585. + 0x5d, 0xae, 0xc7, 0x86,
  44586. + 0x79, 0x80, 0xa3, 0x24,
  44587. + 0x8c, 0xe3, 0x57, 0x8f,
  44588. + 0xc7, 0x5f, 0x1b, 0x0f,
  44589. + 0x2d, 0xf8, 0x9d, 0x30,
  44590. + 0x6f, 0xa4, 0x52, 0xcd,
  44591. + 0xe0, 0x7a, 0x04, 0x8a,
  44592. + 0xde, 0xd9, 0x26, 0x56,
  44593. +};
  44594. +
  44595. +void dwc_run_dh_test_vectors(void *mem_ctx)
  44596. +{
  44597. + uint8_t pkd[384];
  44598. + uint8_t pkh[384];
  44599. + uint8_t hashd[32];
  44600. + uint8_t hashh[32];
  44601. + uint8_t ck[16];
  44602. + uint8_t kdk[32];
  44603. + char dd[5];
  44604. +
  44605. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  44606. +
  44607. + /* compute the PKd and SHA-256(PKd || Nd) */
  44608. + DWC_PRINTF("Computing PKd\n");
  44609. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  44610. +
  44611. + /* compute the PKd and SHA-256(PKh || Nd) */
  44612. + DWC_PRINTF("Computing PKh\n");
  44613. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  44614. +
  44615. + /* compute the dhkey */
  44616. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  44617. +}
  44618. +#endif /* DH_TEST_VECTORS */
  44619. +
  44620. +#endif /* !CONFIG_MACH_IPMATE */
  44621. +
  44622. +#endif /* DWC_CRYPTOLIB */
  44623. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_dh.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.h
  44624. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_dh.h 1970-01-01 01:00:00.000000000 +0100
  44625. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.h 2014-03-11 17:33:06.000000000 +0100
  44626. @@ -0,0 +1,106 @@
  44627. +/* =========================================================================
  44628. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  44629. + * $Revision: #4 $
  44630. + * $Date: 2010/09/28 $
  44631. + * $Change: 1596182 $
  44632. + *
  44633. + * Synopsys Portability Library Software and documentation
  44634. + * (hereinafter, "Software") is an Unsupported proprietary work of
  44635. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  44636. + * between Synopsys and you.
  44637. + *
  44638. + * The Software IS NOT an item of Licensed Software or Licensed Product
  44639. + * under any End User Software License Agreement or Agreement for
  44640. + * Licensed Product with Synopsys or any supplement thereto. You are
  44641. + * permitted to use and redistribute this Software in source and binary
  44642. + * forms, with or without modification, provided that redistributions
  44643. + * of source code must retain this notice. You may not view, use,
  44644. + * disclose, copy or distribute this file or any information contained
  44645. + * herein except pursuant to this license grant from Synopsys. If you
  44646. + * do not agree with this notice, including the disclaimer below, then
  44647. + * you are not authorized to use the Software.
  44648. + *
  44649. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44650. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44651. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44652. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44653. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44654. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44655. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44656. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44657. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44658. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44659. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44660. + * DAMAGE.
  44661. + * ========================================================================= */
  44662. +#ifndef _DWC_DH_H_
  44663. +#define _DWC_DH_H_
  44664. +
  44665. +#ifdef __cplusplus
  44666. +extern "C" {
  44667. +#endif
  44668. +
  44669. +#include "dwc_os.h"
  44670. +
  44671. +/** @file
  44672. + *
  44673. + * This file defines the common functions on device and host for performing
  44674. + * numeric association as defined in the WUSB spec. They are only to be
  44675. + * used internally by the DWC UWB modules. */
  44676. +
  44677. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  44678. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  44679. + uint8_t *key, uint32_t keylen,
  44680. + uint8_t *out);
  44681. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  44682. + void *exp, uint32_t exp_len,
  44683. + void *mod, uint32_t mod_len,
  44684. + void *out);
  44685. +
  44686. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  44687. + *
  44688. + * PK = g^exp mod p.
  44689. + *
  44690. + * Input:
  44691. + * Nd = Number of digits on the device.
  44692. + *
  44693. + * Output:
  44694. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  44695. + * used as either A or B.
  44696. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  44697. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  44698. + */
  44699. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  44700. +
  44701. +/** Computes the DHKEY, and VD.
  44702. + *
  44703. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  44704. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  44705. + *
  44706. + * Input:
  44707. + * pkd = The PKD value.
  44708. + * pkh = The PKH value.
  44709. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  44710. + * is_host = Set to non zero if a WUSB host is calling this function.
  44711. + *
  44712. + * Output:
  44713. +
  44714. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  44715. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  44716. + * null termination character. This buffer can be used directly for display.
  44717. + * ck = A 16-byte buffer to be filled with the CK.
  44718. + * kdk = A 32-byte buffer to be filled with the KDK.
  44719. + */
  44720. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  44721. + uint8_t *exp, int is_host,
  44722. + char *dd, uint8_t *ck, uint8_t *kdk);
  44723. +
  44724. +#ifdef DH_TEST_VECTORS
  44725. +extern void dwc_run_dh_test_vectors(void);
  44726. +#endif
  44727. +
  44728. +#ifdef __cplusplus
  44729. +}
  44730. +#endif
  44731. +
  44732. +#endif /* _DWC_DH_H_ */
  44733. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_list.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_list.h
  44734. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_list.h 1970-01-01 01:00:00.000000000 +0100
  44735. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_list.h 2014-03-11 17:33:06.000000000 +0100
  44736. @@ -0,0 +1,594 @@
  44737. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  44738. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  44739. +
  44740. +/*
  44741. + * Copyright (c) 1991, 1993
  44742. + * The Regents of the University of California. All rights reserved.
  44743. + *
  44744. + * Redistribution and use in source and binary forms, with or without
  44745. + * modification, are permitted provided that the following conditions
  44746. + * are met:
  44747. + * 1. Redistributions of source code must retain the above copyright
  44748. + * notice, this list of conditions and the following disclaimer.
  44749. + * 2. Redistributions in binary form must reproduce the above copyright
  44750. + * notice, this list of conditions and the following disclaimer in the
  44751. + * documentation and/or other materials provided with the distribution.
  44752. + * 3. Neither the name of the University nor the names of its contributors
  44753. + * may be used to endorse or promote products derived from this software
  44754. + * without specific prior written permission.
  44755. + *
  44756. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  44757. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44758. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44759. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  44760. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44761. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44762. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44763. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44764. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44765. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44766. + * SUCH DAMAGE.
  44767. + *
  44768. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  44769. + */
  44770. +
  44771. +#ifndef _DWC_LIST_H_
  44772. +#define _DWC_LIST_H_
  44773. +
  44774. +#ifdef __cplusplus
  44775. +extern "C" {
  44776. +#endif
  44777. +
  44778. +/** @file
  44779. + *
  44780. + * This file defines linked list operations. It is derived from BSD with
  44781. + * only the MACRO names being prefixed with DWC_. This is because a few of
  44782. + * these names conflict with those on Linux. For documentation on use, see the
  44783. + * inline comments in the source code. The original license for this source
  44784. + * code applies and is preserved in the dwc_list.h source file.
  44785. + */
  44786. +
  44787. +/*
  44788. + * This file defines five types of data structures: singly-linked lists,
  44789. + * lists, simple queues, tail queues, and circular queues.
  44790. + *
  44791. + *
  44792. + * A singly-linked list is headed by a single forward pointer. The elements
  44793. + * are singly linked for minimum space and pointer manipulation overhead at
  44794. + * the expense of O(n) removal for arbitrary elements. New elements can be
  44795. + * added to the list after an existing element or at the head of the list.
  44796. + * Elements being removed from the head of the list should use the explicit
  44797. + * macro for this purpose for optimum efficiency. A singly-linked list may
  44798. + * only be traversed in the forward direction. Singly-linked lists are ideal
  44799. + * for applications with large datasets and few or no removals or for
  44800. + * implementing a LIFO queue.
  44801. + *
  44802. + * A list is headed by a single forward pointer (or an array of forward
  44803. + * pointers for a hash table header). The elements are doubly linked
  44804. + * so that an arbitrary element can be removed without a need to
  44805. + * traverse the list. New elements can be added to the list before
  44806. + * or after an existing element or at the head of the list. A list
  44807. + * may only be traversed in the forward direction.
  44808. + *
  44809. + * A simple queue is headed by a pair of pointers, one the head of the
  44810. + * list and the other to the tail of the list. The elements are singly
  44811. + * linked to save space, so elements can only be removed from the
  44812. + * head of the list. New elements can be added to the list before or after
  44813. + * an existing element, at the head of the list, or at the end of the
  44814. + * list. A simple queue may only be traversed in the forward direction.
  44815. + *
  44816. + * A tail queue is headed by a pair of pointers, one to the head of the
  44817. + * list and the other to the tail of the list. The elements are doubly
  44818. + * linked so that an arbitrary element can be removed without a need to
  44819. + * traverse the list. New elements can be added to the list before or
  44820. + * after an existing element, at the head of the list, or at the end of
  44821. + * the list. A tail queue may be traversed in either direction.
  44822. + *
  44823. + * A circle queue is headed by a pair of pointers, one to the head of the
  44824. + * list and the other to the tail of the list. The elements are doubly
  44825. + * linked so that an arbitrary element can be removed without a need to
  44826. + * traverse the list. New elements can be added to the list before or after
  44827. + * an existing element, at the head of the list, or at the end of the list.
  44828. + * A circle queue may be traversed in either direction, but has a more
  44829. + * complex end of list detection.
  44830. + *
  44831. + * For details on the use of these macros, see the queue(3) manual page.
  44832. + */
  44833. +
  44834. +/*
  44835. + * Double-linked List.
  44836. + */
  44837. +
  44838. +typedef struct dwc_list_link {
  44839. + struct dwc_list_link *next;
  44840. + struct dwc_list_link *prev;
  44841. +} dwc_list_link_t;
  44842. +
  44843. +#define DWC_LIST_INIT(link) do { \
  44844. + (link)->next = (link); \
  44845. + (link)->prev = (link); \
  44846. +} while (0)
  44847. +
  44848. +#define DWC_LIST_FIRST(link) ((link)->next)
  44849. +#define DWC_LIST_LAST(link) ((link)->prev)
  44850. +#define DWC_LIST_END(link) (link)
  44851. +#define DWC_LIST_NEXT(link) ((link)->next)
  44852. +#define DWC_LIST_PREV(link) ((link)->prev)
  44853. +#define DWC_LIST_EMPTY(link) \
  44854. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  44855. +#define DWC_LIST_ENTRY(link, type, field) \
  44856. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  44857. +
  44858. +#if 0
  44859. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  44860. + (link)->next = (list)->next; \
  44861. + (link)->prev = (list); \
  44862. + (list)->next->prev = (link); \
  44863. + (list)->next = (link); \
  44864. +} while (0)
  44865. +
  44866. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  44867. + (link)->next = (list); \
  44868. + (link)->prev = (list)->prev; \
  44869. + (list)->prev->next = (link); \
  44870. + (list)->prev = (link); \
  44871. +} while (0)
  44872. +#else
  44873. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  44874. + dwc_list_link_t *__next__ = (list)->next; \
  44875. + __next__->prev = (link); \
  44876. + (link)->next = __next__; \
  44877. + (link)->prev = (list); \
  44878. + (list)->next = (link); \
  44879. +} while (0)
  44880. +
  44881. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  44882. + dwc_list_link_t *__prev__ = (list)->prev; \
  44883. + (list)->prev = (link); \
  44884. + (link)->next = (list); \
  44885. + (link)->prev = __prev__; \
  44886. + __prev__->next = (link); \
  44887. +} while (0)
  44888. +#endif
  44889. +
  44890. +#if 0
  44891. +static inline void __list_add(struct list_head *new,
  44892. + struct list_head *prev,
  44893. + struct list_head *next)
  44894. +{
  44895. + next->prev = new;
  44896. + new->next = next;
  44897. + new->prev = prev;
  44898. + prev->next = new;
  44899. +}
  44900. +
  44901. +static inline void list_add(struct list_head *new, struct list_head *head)
  44902. +{
  44903. + __list_add(new, head, head->next);
  44904. +}
  44905. +
  44906. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  44907. +{
  44908. + __list_add(new, head->prev, head);
  44909. +}
  44910. +
  44911. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  44912. +{
  44913. + next->prev = prev;
  44914. + prev->next = next;
  44915. +}
  44916. +
  44917. +static inline void list_del(struct list_head *entry)
  44918. +{
  44919. + __list_del(entry->prev, entry->next);
  44920. + entry->next = LIST_POISON1;
  44921. + entry->prev = LIST_POISON2;
  44922. +}
  44923. +#endif
  44924. +
  44925. +#define DWC_LIST_REMOVE(link) do { \
  44926. + (link)->next->prev = (link)->prev; \
  44927. + (link)->prev->next = (link)->next; \
  44928. +} while (0)
  44929. +
  44930. +#define DWC_LIST_REMOVE_INIT(link) do { \
  44931. + DWC_LIST_REMOVE(link); \
  44932. + DWC_LIST_INIT(link); \
  44933. +} while (0)
  44934. +
  44935. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  44936. + DWC_LIST_REMOVE(link); \
  44937. + DWC_LIST_INSERT_HEAD(list, link); \
  44938. +} while (0)
  44939. +
  44940. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  44941. + DWC_LIST_REMOVE(link); \
  44942. + DWC_LIST_INSERT_TAIL(list, link); \
  44943. +} while (0)
  44944. +
  44945. +#define DWC_LIST_FOREACH(var, list) \
  44946. + for((var) = DWC_LIST_FIRST(list); \
  44947. + (var) != DWC_LIST_END(list); \
  44948. + (var) = DWC_LIST_NEXT(var))
  44949. +
  44950. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  44951. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  44952. + (var) != DWC_LIST_END(list); \
  44953. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  44954. +
  44955. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  44956. + for((var) = DWC_LIST_LAST(list); \
  44957. + (var) != DWC_LIST_END(list); \
  44958. + (var) = DWC_LIST_PREV(var))
  44959. +
  44960. +/*
  44961. + * Singly-linked List definitions.
  44962. + */
  44963. +#define DWC_SLIST_HEAD(name, type) \
  44964. +struct name { \
  44965. + struct type *slh_first; /* first element */ \
  44966. +}
  44967. +
  44968. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  44969. + { NULL }
  44970. +
  44971. +#define DWC_SLIST_ENTRY(type) \
  44972. +struct { \
  44973. + struct type *sle_next; /* next element */ \
  44974. +}
  44975. +
  44976. +/*
  44977. + * Singly-linked List access methods.
  44978. + */
  44979. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  44980. +#define DWC_SLIST_END(head) NULL
  44981. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  44982. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  44983. +
  44984. +#define DWC_SLIST_FOREACH(var, head, field) \
  44985. + for((var) = SLIST_FIRST(head); \
  44986. + (var) != SLIST_END(head); \
  44987. + (var) = SLIST_NEXT(var, field))
  44988. +
  44989. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  44990. + for((varp) = &SLIST_FIRST((head)); \
  44991. + ((var) = *(varp)) != SLIST_END(head); \
  44992. + (varp) = &SLIST_NEXT((var), field))
  44993. +
  44994. +/*
  44995. + * Singly-linked List functions.
  44996. + */
  44997. +#define DWC_SLIST_INIT(head) { \
  44998. + SLIST_FIRST(head) = SLIST_END(head); \
  44999. +}
  45000. +
  45001. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  45002. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  45003. + (slistelm)->field.sle_next = (elm); \
  45004. +} while (0)
  45005. +
  45006. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  45007. + (elm)->field.sle_next = (head)->slh_first; \
  45008. + (head)->slh_first = (elm); \
  45009. +} while (0)
  45010. +
  45011. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  45012. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  45013. +} while (0)
  45014. +
  45015. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  45016. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  45017. +} while (0)
  45018. +
  45019. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  45020. + if ((head)->slh_first == (elm)) { \
  45021. + SLIST_REMOVE_HEAD((head), field); \
  45022. + } \
  45023. + else { \
  45024. + struct type *curelm = (head)->slh_first; \
  45025. + while( curelm->field.sle_next != (elm) ) \
  45026. + curelm = curelm->field.sle_next; \
  45027. + curelm->field.sle_next = \
  45028. + curelm->field.sle_next->field.sle_next; \
  45029. + } \
  45030. +} while (0)
  45031. +
  45032. +/*
  45033. + * Simple queue definitions.
  45034. + */
  45035. +#define DWC_SIMPLEQ_HEAD(name, type) \
  45036. +struct name { \
  45037. + struct type *sqh_first; /* first element */ \
  45038. + struct type **sqh_last; /* addr of last next element */ \
  45039. +}
  45040. +
  45041. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  45042. + { NULL, &(head).sqh_first }
  45043. +
  45044. +#define DWC_SIMPLEQ_ENTRY(type) \
  45045. +struct { \
  45046. + struct type *sqe_next; /* next element */ \
  45047. +}
  45048. +
  45049. +/*
  45050. + * Simple queue access methods.
  45051. + */
  45052. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  45053. +#define DWC_SIMPLEQ_END(head) NULL
  45054. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  45055. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  45056. +
  45057. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  45058. + for((var) = SIMPLEQ_FIRST(head); \
  45059. + (var) != SIMPLEQ_END(head); \
  45060. + (var) = SIMPLEQ_NEXT(var, field))
  45061. +
  45062. +/*
  45063. + * Simple queue functions.
  45064. + */
  45065. +#define DWC_SIMPLEQ_INIT(head) do { \
  45066. + (head)->sqh_first = NULL; \
  45067. + (head)->sqh_last = &(head)->sqh_first; \
  45068. +} while (0)
  45069. +
  45070. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  45071. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  45072. + (head)->sqh_last = &(elm)->field.sqe_next; \
  45073. + (head)->sqh_first = (elm); \
  45074. +} while (0)
  45075. +
  45076. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  45077. + (elm)->field.sqe_next = NULL; \
  45078. + *(head)->sqh_last = (elm); \
  45079. + (head)->sqh_last = &(elm)->field.sqe_next; \
  45080. +} while (0)
  45081. +
  45082. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  45083. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  45084. + (head)->sqh_last = &(elm)->field.sqe_next; \
  45085. + (listelm)->field.sqe_next = (elm); \
  45086. +} while (0)
  45087. +
  45088. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  45089. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  45090. + (head)->sqh_last = &(head)->sqh_first; \
  45091. +} while (0)
  45092. +
  45093. +/*
  45094. + * Tail queue definitions.
  45095. + */
  45096. +#define DWC_TAILQ_HEAD(name, type) \
  45097. +struct name { \
  45098. + struct type *tqh_first; /* first element */ \
  45099. + struct type **tqh_last; /* addr of last next element */ \
  45100. +}
  45101. +
  45102. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  45103. + { NULL, &(head).tqh_first }
  45104. +
  45105. +#define DWC_TAILQ_ENTRY(type) \
  45106. +struct { \
  45107. + struct type *tqe_next; /* next element */ \
  45108. + struct type **tqe_prev; /* address of previous next element */ \
  45109. +}
  45110. +
  45111. +/*
  45112. + * tail queue access methods
  45113. + */
  45114. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  45115. +#define DWC_TAILQ_END(head) NULL
  45116. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  45117. +#define DWC_TAILQ_LAST(head, headname) \
  45118. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  45119. +/* XXX */
  45120. +#define DWC_TAILQ_PREV(elm, headname, field) \
  45121. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  45122. +#define DWC_TAILQ_EMPTY(head) \
  45123. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  45124. +
  45125. +#define DWC_TAILQ_FOREACH(var, head, field) \
  45126. + for ((var) = DWC_TAILQ_FIRST(head); \
  45127. + (var) != DWC_TAILQ_END(head); \
  45128. + (var) = DWC_TAILQ_NEXT(var, field))
  45129. +
  45130. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  45131. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  45132. + (var) != DWC_TAILQ_END(head); \
  45133. + (var) = DWC_TAILQ_PREV(var, headname, field))
  45134. +
  45135. +/*
  45136. + * Tail queue functions.
  45137. + */
  45138. +#define DWC_TAILQ_INIT(head) do { \
  45139. + (head)->tqh_first = NULL; \
  45140. + (head)->tqh_last = &(head)->tqh_first; \
  45141. +} while (0)
  45142. +
  45143. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  45144. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  45145. + (head)->tqh_first->field.tqe_prev = \
  45146. + &(elm)->field.tqe_next; \
  45147. + else \
  45148. + (head)->tqh_last = &(elm)->field.tqe_next; \
  45149. + (head)->tqh_first = (elm); \
  45150. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  45151. +} while (0)
  45152. +
  45153. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  45154. + (elm)->field.tqe_next = NULL; \
  45155. + (elm)->field.tqe_prev = (head)->tqh_last; \
  45156. + *(head)->tqh_last = (elm); \
  45157. + (head)->tqh_last = &(elm)->field.tqe_next; \
  45158. +} while (0)
  45159. +
  45160. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  45161. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  45162. + (elm)->field.tqe_next->field.tqe_prev = \
  45163. + &(elm)->field.tqe_next; \
  45164. + else \
  45165. + (head)->tqh_last = &(elm)->field.tqe_next; \
  45166. + (listelm)->field.tqe_next = (elm); \
  45167. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  45168. +} while (0)
  45169. +
  45170. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  45171. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  45172. + (elm)->field.tqe_next = (listelm); \
  45173. + *(listelm)->field.tqe_prev = (elm); \
  45174. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  45175. +} while (0)
  45176. +
  45177. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  45178. + if (((elm)->field.tqe_next) != NULL) \
  45179. + (elm)->field.tqe_next->field.tqe_prev = \
  45180. + (elm)->field.tqe_prev; \
  45181. + else \
  45182. + (head)->tqh_last = (elm)->field.tqe_prev; \
  45183. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  45184. +} while (0)
  45185. +
  45186. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  45187. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  45188. + (elm2)->field.tqe_next->field.tqe_prev = \
  45189. + &(elm2)->field.tqe_next; \
  45190. + else \
  45191. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  45192. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  45193. + *(elm2)->field.tqe_prev = (elm2); \
  45194. +} while (0)
  45195. +
  45196. +/*
  45197. + * Circular queue definitions.
  45198. + */
  45199. +#define DWC_CIRCLEQ_HEAD(name, type) \
  45200. +struct name { \
  45201. + struct type *cqh_first; /* first element */ \
  45202. + struct type *cqh_last; /* last element */ \
  45203. +}
  45204. +
  45205. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  45206. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  45207. +
  45208. +#define DWC_CIRCLEQ_ENTRY(type) \
  45209. +struct { \
  45210. + struct type *cqe_next; /* next element */ \
  45211. + struct type *cqe_prev; /* previous element */ \
  45212. +}
  45213. +
  45214. +/*
  45215. + * Circular queue access methods
  45216. + */
  45217. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  45218. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  45219. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  45220. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  45221. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  45222. +#define DWC_CIRCLEQ_EMPTY(head) \
  45223. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  45224. +
  45225. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  45226. +
  45227. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  45228. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  45229. + (var) != DWC_CIRCLEQ_END(head); \
  45230. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  45231. +
  45232. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  45233. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  45234. + (var) != DWC_CIRCLEQ_END(head); \
  45235. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  45236. +
  45237. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  45238. + for((var) = DWC_CIRCLEQ_LAST(head); \
  45239. + (var) != DWC_CIRCLEQ_END(head); \
  45240. + (var) = DWC_CIRCLEQ_PREV(var, field))
  45241. +
  45242. +/*
  45243. + * Circular queue functions.
  45244. + */
  45245. +#define DWC_CIRCLEQ_INIT(head) do { \
  45246. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  45247. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  45248. +} while (0)
  45249. +
  45250. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  45251. + (elm)->field.cqe_next = NULL; \
  45252. + (elm)->field.cqe_prev = NULL; \
  45253. +} while (0)
  45254. +
  45255. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  45256. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  45257. + (elm)->field.cqe_prev = (listelm); \
  45258. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  45259. + (head)->cqh_last = (elm); \
  45260. + else \
  45261. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  45262. + (listelm)->field.cqe_next = (elm); \
  45263. +} while (0)
  45264. +
  45265. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  45266. + (elm)->field.cqe_next = (listelm); \
  45267. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  45268. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  45269. + (head)->cqh_first = (elm); \
  45270. + else \
  45271. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  45272. + (listelm)->field.cqe_prev = (elm); \
  45273. +} while (0)
  45274. +
  45275. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  45276. + (elm)->field.cqe_next = (head)->cqh_first; \
  45277. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  45278. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  45279. + (head)->cqh_last = (elm); \
  45280. + else \
  45281. + (head)->cqh_first->field.cqe_prev = (elm); \
  45282. + (head)->cqh_first = (elm); \
  45283. +} while (0)
  45284. +
  45285. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  45286. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  45287. + (elm)->field.cqe_prev = (head)->cqh_last; \
  45288. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  45289. + (head)->cqh_first = (elm); \
  45290. + else \
  45291. + (head)->cqh_last->field.cqe_next = (elm); \
  45292. + (head)->cqh_last = (elm); \
  45293. +} while (0)
  45294. +
  45295. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  45296. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  45297. + (head)->cqh_last = (elm)->field.cqe_prev; \
  45298. + else \
  45299. + (elm)->field.cqe_next->field.cqe_prev = \
  45300. + (elm)->field.cqe_prev; \
  45301. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  45302. + (head)->cqh_first = (elm)->field.cqe_next; \
  45303. + else \
  45304. + (elm)->field.cqe_prev->field.cqe_next = \
  45305. + (elm)->field.cqe_next; \
  45306. +} while (0)
  45307. +
  45308. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  45309. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  45310. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  45311. +} while (0)
  45312. +
  45313. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  45314. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  45315. + DWC_CIRCLEQ_END(head)) \
  45316. + (head).cqh_last = (elm2); \
  45317. + else \
  45318. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  45319. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  45320. + DWC_CIRCLEQ_END(head)) \
  45321. + (head).cqh_first = (elm2); \
  45322. + else \
  45323. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  45324. +} while (0)
  45325. +
  45326. +#ifdef __cplusplus
  45327. +}
  45328. +#endif
  45329. +
  45330. +#endif /* _DWC_LIST_H_ */
  45331. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_mem.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_mem.c
  45332. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_mem.c 1970-01-01 01:00:00.000000000 +0100
  45333. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_mem.c 2014-03-11 17:33:06.000000000 +0100
  45334. @@ -0,0 +1,245 @@
  45335. +/* Memory Debugging */
  45336. +#ifdef DWC_DEBUG_MEMORY
  45337. +
  45338. +#include "dwc_os.h"
  45339. +#include "dwc_list.h"
  45340. +
  45341. +struct allocation {
  45342. + void *addr;
  45343. + void *ctx;
  45344. + char *func;
  45345. + int line;
  45346. + uint32_t size;
  45347. + int dma;
  45348. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  45349. +};
  45350. +
  45351. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  45352. +
  45353. +struct allocation_manager {
  45354. + void *mem_ctx;
  45355. + struct allocation_queue allocations;
  45356. +
  45357. + /* statistics */
  45358. + int num;
  45359. + int num_freed;
  45360. + int num_active;
  45361. + uint32_t total;
  45362. + uint32_t cur;
  45363. + uint32_t max;
  45364. +};
  45365. +
  45366. +static struct allocation_manager *manager = NULL;
  45367. +
  45368. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  45369. + int dma)
  45370. +{
  45371. + struct allocation *a;
  45372. +
  45373. + DWC_ASSERT(manager != NULL, "manager not allocated");
  45374. +
  45375. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  45376. + if (!a) {
  45377. + return -DWC_E_NO_MEMORY;
  45378. + }
  45379. +
  45380. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  45381. + if (!a->func) {
  45382. + __DWC_FREE(manager->mem_ctx, a);
  45383. + return -DWC_E_NO_MEMORY;
  45384. + }
  45385. +
  45386. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  45387. + a->addr = addr;
  45388. + a->ctx = ctx;
  45389. + a->line = line;
  45390. + a->size = size;
  45391. + a->dma = dma;
  45392. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  45393. +
  45394. + /* Update stats */
  45395. + manager->num++;
  45396. + manager->num_active++;
  45397. + manager->total += size;
  45398. + manager->cur += size;
  45399. +
  45400. + if (manager->max < manager->cur) {
  45401. + manager->max = manager->cur;
  45402. + }
  45403. +
  45404. + return 0;
  45405. +}
  45406. +
  45407. +static struct allocation *find_allocation(void *ctx, void *addr)
  45408. +{
  45409. + struct allocation *a;
  45410. +
  45411. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  45412. + if (a->ctx == ctx && a->addr == addr) {
  45413. + return a;
  45414. + }
  45415. + }
  45416. +
  45417. + return NULL;
  45418. +}
  45419. +
  45420. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  45421. +{
  45422. + struct allocation *a = find_allocation(ctx, addr);
  45423. +
  45424. + if (!a) {
  45425. + DWC_ASSERT(0,
  45426. + "Free of address %p that was never allocated or already freed %s:%d",
  45427. + addr, func, line);
  45428. + return;
  45429. + }
  45430. +
  45431. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  45432. +
  45433. + manager->num_active--;
  45434. + manager->num_freed++;
  45435. + manager->cur -= a->size;
  45436. + __DWC_FREE(manager->mem_ctx, a->func);
  45437. + __DWC_FREE(manager->mem_ctx, a);
  45438. +}
  45439. +
  45440. +int dwc_memory_debug_start(void *mem_ctx)
  45441. +{
  45442. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  45443. +
  45444. + if (manager) {
  45445. + return -DWC_E_BUSY;
  45446. + }
  45447. +
  45448. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  45449. + if (!manager) {
  45450. + return -DWC_E_NO_MEMORY;
  45451. + }
  45452. +
  45453. + DWC_CIRCLEQ_INIT(&manager->allocations);
  45454. + manager->mem_ctx = mem_ctx;
  45455. + manager->num = 0;
  45456. + manager->num_freed = 0;
  45457. + manager->num_active = 0;
  45458. + manager->total = 0;
  45459. + manager->cur = 0;
  45460. + manager->max = 0;
  45461. +
  45462. + return 0;
  45463. +}
  45464. +
  45465. +void dwc_memory_debug_stop(void)
  45466. +{
  45467. + struct allocation *a;
  45468. +
  45469. + dwc_memory_debug_report();
  45470. +
  45471. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  45472. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  45473. + free_allocation(a->ctx, a->addr, NULL, -1);
  45474. + }
  45475. +
  45476. + __DWC_FREE(manager->mem_ctx, manager);
  45477. +}
  45478. +
  45479. +void dwc_memory_debug_report(void)
  45480. +{
  45481. + struct allocation *a;
  45482. +
  45483. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  45484. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  45485. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  45486. + DWC_PRINTF("Active = %d\n", manager->num_active);
  45487. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  45488. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  45489. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  45490. + DWC_PRINTF("Unfreed allocations:\n");
  45491. +
  45492. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  45493. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  45494. + a->addr, a->size, a->func, a->line, a->dma);
  45495. + }
  45496. +}
  45497. +
  45498. +/* The replacement functions */
  45499. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  45500. +{
  45501. + void *addr = __DWC_ALLOC(mem_ctx, size);
  45502. +
  45503. + if (!addr) {
  45504. + return NULL;
  45505. + }
  45506. +
  45507. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  45508. + __DWC_FREE(mem_ctx, addr);
  45509. + return NULL;
  45510. + }
  45511. +
  45512. + return addr;
  45513. +}
  45514. +
  45515. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  45516. + int line)
  45517. +{
  45518. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  45519. +
  45520. + if (!addr) {
  45521. + return NULL;
  45522. + }
  45523. +
  45524. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  45525. + __DWC_FREE(mem_ctx, addr);
  45526. + return NULL;
  45527. + }
  45528. +
  45529. + return addr;
  45530. +}
  45531. +
  45532. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  45533. +{
  45534. + free_allocation(mem_ctx, addr, func, line);
  45535. + __DWC_FREE(mem_ctx, addr);
  45536. +}
  45537. +
  45538. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  45539. + char const *func, int line)
  45540. +{
  45541. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  45542. +
  45543. + if (!addr) {
  45544. + return NULL;
  45545. + }
  45546. +
  45547. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  45548. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  45549. + return NULL;
  45550. + }
  45551. +
  45552. + return addr;
  45553. +}
  45554. +
  45555. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  45556. + dwc_dma_t *dma_addr, char const *func, int line)
  45557. +{
  45558. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  45559. +
  45560. + if (!addr) {
  45561. + return NULL;
  45562. + }
  45563. +
  45564. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  45565. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  45566. + return NULL;
  45567. + }
  45568. +
  45569. + return addr;
  45570. +}
  45571. +
  45572. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  45573. + dwc_dma_t dma_addr, char const *func, int line)
  45574. +{
  45575. + free_allocation(dma_ctx, virt_addr, func, line);
  45576. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  45577. +}
  45578. +
  45579. +#endif /* DWC_DEBUG_MEMORY */
  45580. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_modpow.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.c
  45581. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_modpow.c 1970-01-01 01:00:00.000000000 +0100
  45582. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.c 2014-03-11 17:51:27.000000000 +0100
  45583. @@ -0,0 +1,636 @@
  45584. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  45585. + *
  45586. + * PuTTY is copyright 1997-2007 Simon Tatham.
  45587. + *
  45588. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  45589. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  45590. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  45591. + * Kuhn, and CORE SDI S.A.
  45592. + *
  45593. + * Permission is hereby granted, free of charge, to any person
  45594. + * obtaining a copy of this software and associated documentation files
  45595. + * (the "Software"), to deal in the Software without restriction,
  45596. + * including without limitation the rights to use, copy, modify, merge,
  45597. + * publish, distribute, sublicense, and/or sell copies of the Software,
  45598. + * and to permit persons to whom the Software is furnished to do so,
  45599. + * subject to the following conditions:
  45600. + *
  45601. + * The above copyright notice and this permission notice shall be
  45602. + * included in all copies or substantial portions of the Software.
  45603. +
  45604. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  45605. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  45606. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  45607. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  45608. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  45609. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  45610. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  45611. + *
  45612. + */
  45613. +#ifdef DWC_CRYPTOLIB
  45614. +
  45615. +#ifndef CONFIG_MACH_IPMATE
  45616. +
  45617. +#include "dwc_modpow.h"
  45618. +
  45619. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  45620. +#define BIGNUM_TOP_BIT 0x80000000UL
  45621. +#define BIGNUM_INT_BITS 32
  45622. +
  45623. +
  45624. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  45625. +{
  45626. + void *p;
  45627. + size *= n;
  45628. + if (size == 0) size = 1;
  45629. + p = dwc_alloc(mem_ctx, size);
  45630. + return p;
  45631. +}
  45632. +
  45633. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  45634. +#define sfree dwc_free
  45635. +
  45636. +/*
  45637. + * Usage notes:
  45638. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  45639. + * subscripts, as some implementations object to this (see below).
  45640. + * * Note that none of the division methods below will cope if the
  45641. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  45642. + * to avoid this case.
  45643. + * If this condition occurs, in the case of the x86 DIV instruction,
  45644. + * an overflow exception will occur, which (according to a correspondent)
  45645. + * will manifest on Windows as something like
  45646. + * 0xC0000095: Integer overflow
  45647. + * The C variant won't give the right answer, either.
  45648. + */
  45649. +
  45650. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  45651. +
  45652. +#if defined __GNUC__ && defined __i386__
  45653. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  45654. + __asm__("div %2" : \
  45655. + "=d" (r), "=a" (q) : \
  45656. + "r" (w), "d" (hi), "a" (lo))
  45657. +#else
  45658. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  45659. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  45660. + q = n / w; \
  45661. + r = n % w; \
  45662. +} while (0)
  45663. +#endif
  45664. +
  45665. +// q = n / w;
  45666. +// r = n % w;
  45667. +
  45668. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  45669. +
  45670. +#define BIGNUM_INTERNAL
  45671. +
  45672. +static Bignum newbn(void *mem_ctx, int length)
  45673. +{
  45674. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  45675. + //if (!b)
  45676. + //abort(); /* FIXME */
  45677. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  45678. + b[0] = length;
  45679. + return b;
  45680. +}
  45681. +
  45682. +void freebn(void *mem_ctx, Bignum b)
  45683. +{
  45684. + /*
  45685. + * Burn the evidence, just in case.
  45686. + */
  45687. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  45688. + sfree(mem_ctx, b);
  45689. +}
  45690. +
  45691. +/*
  45692. + * Compute c = a * b.
  45693. + * Input is in the first len words of a and b.
  45694. + * Result is returned in the first 2*len words of c.
  45695. + */
  45696. +static void internal_mul(BignumInt *a, BignumInt *b,
  45697. + BignumInt *c, int len)
  45698. +{
  45699. + int i, j;
  45700. + BignumDblInt t;
  45701. +
  45702. + for (j = 0; j < 2 * len; j++)
  45703. + c[j] = 0;
  45704. +
  45705. + for (i = len - 1; i >= 0; i--) {
  45706. + t = 0;
  45707. + for (j = len - 1; j >= 0; j--) {
  45708. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  45709. + t += (BignumDblInt) c[i + j + 1];
  45710. + c[i + j + 1] = (BignumInt) t;
  45711. + t = t >> BIGNUM_INT_BITS;
  45712. + }
  45713. + c[i] = (BignumInt) t;
  45714. + }
  45715. +}
  45716. +
  45717. +static void internal_add_shifted(BignumInt *number,
  45718. + unsigned n, int shift)
  45719. +{
  45720. + int word = 1 + (shift / BIGNUM_INT_BITS);
  45721. + int bshift = shift % BIGNUM_INT_BITS;
  45722. + BignumDblInt addend;
  45723. +
  45724. + addend = (BignumDblInt)n << bshift;
  45725. +
  45726. + while (addend) {
  45727. + addend += number[word];
  45728. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  45729. + addend >>= BIGNUM_INT_BITS;
  45730. + word++;
  45731. + }
  45732. +}
  45733. +
  45734. +/*
  45735. + * Compute a = a % m.
  45736. + * Input in first alen words of a and first mlen words of m.
  45737. + * Output in first alen words of a
  45738. + * (of which first alen-mlen words will be zero).
  45739. + * The MSW of m MUST have its high bit set.
  45740. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  45741. + * rather than the internal bigendian format. Quotient parts are shifted
  45742. + * left by `qshift' before adding into quot.
  45743. + */
  45744. +static void internal_mod(BignumInt *a, int alen,
  45745. + BignumInt *m, int mlen,
  45746. + BignumInt *quot, int qshift)
  45747. +{
  45748. + BignumInt m0, m1;
  45749. + unsigned int h;
  45750. + int i, k;
  45751. +
  45752. + m0 = m[0];
  45753. + if (mlen > 1)
  45754. + m1 = m[1];
  45755. + else
  45756. + m1 = 0;
  45757. +
  45758. + for (i = 0; i <= alen - mlen; i++) {
  45759. + BignumDblInt t;
  45760. + unsigned int q, r, c, ai1;
  45761. +
  45762. + if (i == 0) {
  45763. + h = 0;
  45764. + } else {
  45765. + h = a[i - 1];
  45766. + a[i - 1] = 0;
  45767. + }
  45768. +
  45769. + if (i == alen - 1)
  45770. + ai1 = 0;
  45771. + else
  45772. + ai1 = a[i + 1];
  45773. +
  45774. + /* Find q = h:a[i] / m0 */
  45775. + if (h >= m0) {
  45776. + /*
  45777. + * Special case.
  45778. + *
  45779. + * To illustrate it, suppose a BignumInt is 8 bits, and
  45780. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  45781. + * our initial division will be 0xA123 / 0xA1, which
  45782. + * will give a quotient of 0x100 and a divide overflow.
  45783. + * However, the invariants in this division algorithm
  45784. + * are not violated, since the full number A1:23:... is
  45785. + * _less_ than the quotient prefix A1:B2:... and so the
  45786. + * following correction loop would have sorted it out.
  45787. + *
  45788. + * In this situation we set q to be the largest
  45789. + * quotient we _can_ stomach (0xFF, of course).
  45790. + */
  45791. + q = BIGNUM_INT_MASK;
  45792. + } else {
  45793. + /* Macro doesn't want an array subscript expression passed
  45794. + * into it (see definition), so use a temporary. */
  45795. + BignumInt tmplo = a[i];
  45796. + DIVMOD_WORD(q, r, h, tmplo, m0);
  45797. +
  45798. + /* Refine our estimate of q by looking at
  45799. + h:a[i]:a[i+1] / m0:m1 */
  45800. + t = MUL_WORD(m1, q);
  45801. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  45802. + q--;
  45803. + t -= m1;
  45804. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  45805. + if (r >= (BignumDblInt) m0 &&
  45806. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  45807. + }
  45808. + }
  45809. +
  45810. + /* Subtract q * m from a[i...] */
  45811. + c = 0;
  45812. + for (k = mlen - 1; k >= 0; k--) {
  45813. + t = MUL_WORD(q, m[k]);
  45814. + t += c;
  45815. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  45816. + if ((BignumInt) t > a[i + k])
  45817. + c++;
  45818. + a[i + k] -= (BignumInt) t;
  45819. + }
  45820. +
  45821. + /* Add back m in case of borrow */
  45822. + if (c != h) {
  45823. + t = 0;
  45824. + for (k = mlen - 1; k >= 0; k--) {
  45825. + t += m[k];
  45826. + t += a[i + k];
  45827. + a[i + k] = (BignumInt) t;
  45828. + t = t >> BIGNUM_INT_BITS;
  45829. + }
  45830. + q--;
  45831. + }
  45832. + if (quot)
  45833. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  45834. + }
  45835. +}
  45836. +
  45837. +/*
  45838. + * Compute p % mod.
  45839. + * The most significant word of mod MUST be non-zero.
  45840. + * We assume that the result array is the same size as the mod array.
  45841. + * We optionally write out a quotient if `quotient' is non-NULL.
  45842. + * We can avoid writing out the result if `result' is NULL.
  45843. + */
  45844. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  45845. +{
  45846. + BignumInt *n, *m;
  45847. + int mshift;
  45848. + int plen, mlen, i, j;
  45849. +
  45850. + /* Allocate m of size mlen, copy mod to m */
  45851. + /* We use big endian internally */
  45852. + mlen = mod[0];
  45853. + m = snewn(mem_ctx, mlen, BignumInt);
  45854. + //if (!m)
  45855. + //abort(); /* FIXME */
  45856. + for (j = 0; j < mlen; j++)
  45857. + m[j] = mod[mod[0] - j];
  45858. +
  45859. + /* Shift m left to make msb bit set */
  45860. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  45861. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  45862. + break;
  45863. + if (mshift) {
  45864. + for (i = 0; i < mlen - 1; i++)
  45865. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  45866. + m[mlen - 1] = m[mlen - 1] << mshift;
  45867. + }
  45868. +
  45869. + plen = p[0];
  45870. + /* Ensure plen > mlen */
  45871. + if (plen <= mlen)
  45872. + plen = mlen + 1;
  45873. +
  45874. + /* Allocate n of size plen, copy p to n */
  45875. + n = snewn(mem_ctx, plen, BignumInt);
  45876. + //if (!n)
  45877. + //abort(); /* FIXME */
  45878. + for (j = 0; j < plen; j++)
  45879. + n[j] = 0;
  45880. + for (j = 1; j <= (int)p[0]; j++)
  45881. + n[plen - j] = p[j];
  45882. +
  45883. + /* Main computation */
  45884. + internal_mod(n, plen, m, mlen, quotient, mshift);
  45885. +
  45886. + /* Fixup result in case the modulus was shifted */
  45887. + if (mshift) {
  45888. + for (i = plen - mlen - 1; i < plen - 1; i++)
  45889. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  45890. + n[plen - 1] = n[plen - 1] << mshift;
  45891. + internal_mod(n, plen, m, mlen, quotient, 0);
  45892. + for (i = plen - 1; i >= plen - mlen; i--)
  45893. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  45894. + }
  45895. +
  45896. + /* Copy result to buffer */
  45897. + if (result) {
  45898. + for (i = 1; i <= (int)result[0]; i++) {
  45899. + int j = plen - i;
  45900. + result[i] = j >= 0 ? n[j] : 0;
  45901. + }
  45902. + }
  45903. +
  45904. + /* Free temporary arrays */
  45905. + for (i = 0; i < mlen; i++)
  45906. + m[i] = 0;
  45907. + sfree(mem_ctx, m);
  45908. + for (i = 0; i < plen; i++)
  45909. + n[i] = 0;
  45910. + sfree(mem_ctx, n);
  45911. +}
  45912. +
  45913. +/*
  45914. + * Simple remainder.
  45915. + */
  45916. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  45917. +{
  45918. + Bignum r = newbn(mem_ctx, b[0]);
  45919. + bigdivmod(mem_ctx, a, b, r, NULL);
  45920. + return r;
  45921. +}
  45922. +
  45923. +/*
  45924. + * Compute (base ^ exp) % mod.
  45925. + */
  45926. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  45927. +{
  45928. + BignumInt *a, *b, *n, *m;
  45929. + int mshift;
  45930. + int mlen, i, j;
  45931. + Bignum base, result;
  45932. +
  45933. + /*
  45934. + * The most significant word of mod needs to be non-zero. It
  45935. + * should already be, but let's make sure.
  45936. + */
  45937. + //assert(mod[mod[0]] != 0);
  45938. +
  45939. + /*
  45940. + * Make sure the base is smaller than the modulus, by reducing
  45941. + * it modulo the modulus if not.
  45942. + */
  45943. + base = bigmod(mem_ctx, base_in, mod);
  45944. +
  45945. + /* Allocate m of size mlen, copy mod to m */
  45946. + /* We use big endian internally */
  45947. + mlen = mod[0];
  45948. + m = snewn(mem_ctx, mlen, BignumInt);
  45949. + //if (!m)
  45950. + //abort(); /* FIXME */
  45951. + for (j = 0; j < mlen; j++)
  45952. + m[j] = mod[mod[0] - j];
  45953. +
  45954. + /* Shift m left to make msb bit set */
  45955. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  45956. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  45957. + break;
  45958. + if (mshift) {
  45959. + for (i = 0; i < mlen - 1; i++)
  45960. + m[i] =
  45961. + (m[i] << mshift) | (m[i + 1] >>
  45962. + (BIGNUM_INT_BITS - mshift));
  45963. + m[mlen - 1] = m[mlen - 1] << mshift;
  45964. + }
  45965. +
  45966. + /* Allocate n of size mlen, copy base to n */
  45967. + n = snewn(mem_ctx, mlen, BignumInt);
  45968. + //if (!n)
  45969. + //abort(); /* FIXME */
  45970. + i = mlen - base[0];
  45971. + for (j = 0; j < i; j++)
  45972. + n[j] = 0;
  45973. + for (j = 0; j < base[0]; j++)
  45974. + n[i + j] = base[base[0] - j];
  45975. +
  45976. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  45977. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  45978. + //if (!a)
  45979. + //abort(); /* FIXME */
  45980. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  45981. + //if (!b)
  45982. + //abort(); /* FIXME */
  45983. + for (i = 0; i < 2 * mlen; i++)
  45984. + a[i] = 0;
  45985. + a[2 * mlen - 1] = 1;
  45986. +
  45987. + /* Skip leading zero bits of exp. */
  45988. + i = 0;
  45989. + j = BIGNUM_INT_BITS - 1;
  45990. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  45991. + j--;
  45992. + if (j < 0) {
  45993. + i++;
  45994. + j = BIGNUM_INT_BITS - 1;
  45995. + }
  45996. + }
  45997. +
  45998. + /* Main computation */
  45999. + while (i < exp[0]) {
  46000. + while (j >= 0) {
  46001. + internal_mul(a + mlen, a + mlen, b, mlen);
  46002. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  46003. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  46004. + internal_mul(b + mlen, n, a, mlen);
  46005. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  46006. + } else {
  46007. + BignumInt *t;
  46008. + t = a;
  46009. + a = b;
  46010. + b = t;
  46011. + }
  46012. + j--;
  46013. + }
  46014. + i++;
  46015. + j = BIGNUM_INT_BITS - 1;
  46016. + }
  46017. +
  46018. + /* Fixup result in case the modulus was shifted */
  46019. + if (mshift) {
  46020. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  46021. + a[i] =
  46022. + (a[i] << mshift) | (a[i + 1] >>
  46023. + (BIGNUM_INT_BITS - mshift));
  46024. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  46025. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  46026. + for (i = 2 * mlen - 1; i >= mlen; i--)
  46027. + a[i] =
  46028. + (a[i] >> mshift) | (a[i - 1] <<
  46029. + (BIGNUM_INT_BITS - mshift));
  46030. + }
  46031. +
  46032. + /* Copy result to buffer */
  46033. + result = newbn(mem_ctx, mod[0]);
  46034. + for (i = 0; i < mlen; i++)
  46035. + result[result[0] - i] = a[i + mlen];
  46036. + while (result[0] > 1 && result[result[0]] == 0)
  46037. + result[0]--;
  46038. +
  46039. + /* Free temporary arrays */
  46040. + for (i = 0; i < 2 * mlen; i++)
  46041. + a[i] = 0;
  46042. + sfree(mem_ctx, a);
  46043. + for (i = 0; i < 2 * mlen; i++)
  46044. + b[i] = 0;
  46045. + sfree(mem_ctx, b);
  46046. + for (i = 0; i < mlen; i++)
  46047. + m[i] = 0;
  46048. + sfree(mem_ctx, m);
  46049. + for (i = 0; i < mlen; i++)
  46050. + n[i] = 0;
  46051. + sfree(mem_ctx, n);
  46052. +
  46053. + freebn(mem_ctx, base);
  46054. +
  46055. + return result;
  46056. +}
  46057. +
  46058. +
  46059. +#ifdef UNITTEST
  46060. +
  46061. +static __u32 dh_p[] = {
  46062. + 96,
  46063. + 0xFFFFFFFF,
  46064. + 0xFFFFFFFF,
  46065. + 0xA93AD2CA,
  46066. + 0x4B82D120,
  46067. + 0xE0FD108E,
  46068. + 0x43DB5BFC,
  46069. + 0x74E5AB31,
  46070. + 0x08E24FA0,
  46071. + 0xBAD946E2,
  46072. + 0x770988C0,
  46073. + 0x7A615D6C,
  46074. + 0xBBE11757,
  46075. + 0x177B200C,
  46076. + 0x521F2B18,
  46077. + 0x3EC86A64,
  46078. + 0xD8760273,
  46079. + 0xD98A0864,
  46080. + 0xF12FFA06,
  46081. + 0x1AD2EE6B,
  46082. + 0xCEE3D226,
  46083. + 0x4A25619D,
  46084. + 0x1E8C94E0,
  46085. + 0xDB0933D7,
  46086. + 0xABF5AE8C,
  46087. + 0xA6E1E4C7,
  46088. + 0xB3970F85,
  46089. + 0x5D060C7D,
  46090. + 0x8AEA7157,
  46091. + 0x58DBEF0A,
  46092. + 0xECFB8504,
  46093. + 0xDF1CBA64,
  46094. + 0xA85521AB,
  46095. + 0x04507A33,
  46096. + 0xAD33170D,
  46097. + 0x8AAAC42D,
  46098. + 0x15728E5A,
  46099. + 0x98FA0510,
  46100. + 0x15D22618,
  46101. + 0xEA956AE5,
  46102. + 0x3995497C,
  46103. + 0x95581718,
  46104. + 0xDE2BCBF6,
  46105. + 0x6F4C52C9,
  46106. + 0xB5C55DF0,
  46107. + 0xEC07A28F,
  46108. + 0x9B2783A2,
  46109. + 0x180E8603,
  46110. + 0xE39E772C,
  46111. + 0x2E36CE3B,
  46112. + 0x32905E46,
  46113. + 0xCA18217C,
  46114. + 0xF1746C08,
  46115. + 0x4ABC9804,
  46116. + 0x670C354E,
  46117. + 0x7096966D,
  46118. + 0x9ED52907,
  46119. + 0x208552BB,
  46120. + 0x1C62F356,
  46121. + 0xDCA3AD96,
  46122. + 0x83655D23,
  46123. + 0xFD24CF5F,
  46124. + 0x69163FA8,
  46125. + 0x1C55D39A,
  46126. + 0x98DA4836,
  46127. + 0xA163BF05,
  46128. + 0xC2007CB8,
  46129. + 0xECE45B3D,
  46130. + 0x49286651,
  46131. + 0x7C4B1FE6,
  46132. + 0xAE9F2411,
  46133. + 0x5A899FA5,
  46134. + 0xEE386BFB,
  46135. + 0xF406B7ED,
  46136. + 0x0BFF5CB6,
  46137. + 0xA637ED6B,
  46138. + 0xF44C42E9,
  46139. + 0x625E7EC6,
  46140. + 0xE485B576,
  46141. + 0x6D51C245,
  46142. + 0x4FE1356D,
  46143. + 0xF25F1437,
  46144. + 0x302B0A6D,
  46145. + 0xCD3A431B,
  46146. + 0xEF9519B3,
  46147. + 0x8E3404DD,
  46148. + 0x514A0879,
  46149. + 0x3B139B22,
  46150. + 0x020BBEA6,
  46151. + 0x8A67CC74,
  46152. + 0x29024E08,
  46153. + 0x80DC1CD1,
  46154. + 0xC4C6628B,
  46155. + 0x2168C234,
  46156. + 0xC90FDAA2,
  46157. + 0xFFFFFFFF,
  46158. + 0xFFFFFFFF,
  46159. +};
  46160. +
  46161. +static __u32 dh_a[] = {
  46162. + 8,
  46163. + 0xdf367516,
  46164. + 0x86459caa,
  46165. + 0xe2d459a4,
  46166. + 0xd910dae0,
  46167. + 0x8a8b5e37,
  46168. + 0x67ab31c6,
  46169. + 0xf0b55ea9,
  46170. + 0x440051d6,
  46171. +};
  46172. +
  46173. +static __u32 dh_b[] = {
  46174. + 8,
  46175. + 0xded92656,
  46176. + 0xe07a048a,
  46177. + 0x6fa452cd,
  46178. + 0x2df89d30,
  46179. + 0xc75f1b0f,
  46180. + 0x8ce3578f,
  46181. + 0x7980a324,
  46182. + 0x5daec786,
  46183. +};
  46184. +
  46185. +static __u32 dh_g[] = {
  46186. + 1,
  46187. + 2,
  46188. +};
  46189. +
  46190. +int main(void)
  46191. +{
  46192. + int i;
  46193. + __u32 *k;
  46194. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  46195. +
  46196. + printf("\n\n");
  46197. + for (i=0; i<k[0]; i++) {
  46198. + __u32 word32 = k[k[0] - i];
  46199. + __u16 l = word32 & 0xffff;
  46200. + __u16 m = (word32 & 0xffff0000) >> 16;
  46201. + printf("%04x %04x ", m, l);
  46202. + if (!((i + 1)%13)) printf("\n");
  46203. + }
  46204. + printf("\n\n");
  46205. +
  46206. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  46207. + printf("PASS\n\n");
  46208. + }
  46209. + else {
  46210. + printf("FAIL\n\n");
  46211. + }
  46212. +
  46213. +}
  46214. +
  46215. +#endif /* UNITTEST */
  46216. +
  46217. +#endif /* CONFIG_MACH_IPMATE */
  46218. +
  46219. +#endif /*DWC_CRYPTOLIB */
  46220. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_modpow.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.h
  46221. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_modpow.h 1970-01-01 01:00:00.000000000 +0100
  46222. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.h 2014-03-11 17:33:06.000000000 +0100
  46223. @@ -0,0 +1,34 @@
  46224. +/*
  46225. + * dwc_modpow.h
  46226. + * See dwc_modpow.c for license and changes
  46227. + */
  46228. +#ifndef _DWC_MODPOW_H
  46229. +#define _DWC_MODPOW_H
  46230. +
  46231. +#ifdef __cplusplus
  46232. +extern "C" {
  46233. +#endif
  46234. +
  46235. +#include "dwc_os.h"
  46236. +
  46237. +/** @file
  46238. + *
  46239. + * This file defines the module exponentiation function which is only used
  46240. + * internally by the DWC UWB modules for calculation of PKs during numeric
  46241. + * association. The routine is taken from the PUTTY, an open source terminal
  46242. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  46243. + *
  46244. + */
  46245. +
  46246. +typedef uint32_t BignumInt;
  46247. +typedef uint64_t BignumDblInt;
  46248. +typedef BignumInt *Bignum;
  46249. +
  46250. +/* Compute modular exponentiaion */
  46251. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  46252. +
  46253. +#ifdef __cplusplus
  46254. +}
  46255. +#endif
  46256. +
  46257. +#endif /* _LINUX_BIGNUM_H */
  46258. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_notifier.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.c
  46259. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_notifier.c 1970-01-01 01:00:00.000000000 +0100
  46260. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.c 2014-03-11 17:33:06.000000000 +0100
  46261. @@ -0,0 +1,319 @@
  46262. +#ifdef DWC_NOTIFYLIB
  46263. +
  46264. +#include "dwc_notifier.h"
  46265. +#include "dwc_list.h"
  46266. +
  46267. +typedef struct dwc_observer {
  46268. + void *observer;
  46269. + dwc_notifier_callback_t callback;
  46270. + void *data;
  46271. + char *notification;
  46272. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  46273. +} observer_t;
  46274. +
  46275. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  46276. +
  46277. +typedef struct dwc_notifier {
  46278. + void *mem_ctx;
  46279. + void *object;
  46280. + struct observer_queue observers;
  46281. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  46282. +} notifier_t;
  46283. +
  46284. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  46285. +
  46286. +typedef struct manager {
  46287. + void *mem_ctx;
  46288. + void *wkq_ctx;
  46289. + dwc_workq_t *wq;
  46290. +// dwc_mutex_t *mutex;
  46291. + struct notifier_queue notifiers;
  46292. +} manager_t;
  46293. +
  46294. +static manager_t *manager = NULL;
  46295. +
  46296. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  46297. +{
  46298. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  46299. + if (!manager) {
  46300. + return -DWC_E_NO_MEMORY;
  46301. + }
  46302. +
  46303. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  46304. +
  46305. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  46306. + if (!manager->wq) {
  46307. + return -DWC_E_NO_MEMORY;
  46308. + }
  46309. +
  46310. + return 0;
  46311. +}
  46312. +
  46313. +static void free_manager(void)
  46314. +{
  46315. + dwc_workq_free(manager->wq);
  46316. +
  46317. + /* All notifiers must have unregistered themselves before this module
  46318. + * can be removed. Hitting this assertion indicates a programmer
  46319. + * error. */
  46320. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  46321. + "Notification manager being freed before all notifiers have been removed");
  46322. + dwc_free(manager->mem_ctx, manager);
  46323. +}
  46324. +
  46325. +#ifdef DEBUG
  46326. +static void dump_manager(void)
  46327. +{
  46328. + notifier_t *n;
  46329. + observer_t *o;
  46330. +
  46331. + DWC_ASSERT(manager, "Notification manager not found");
  46332. +
  46333. + DWC_DEBUG("List of all notifiers and observers:\n");
  46334. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  46335. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  46336. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  46337. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  46338. + }
  46339. + }
  46340. +}
  46341. +#else
  46342. +#define dump_manager(...)
  46343. +#endif
  46344. +
  46345. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  46346. + dwc_notifier_callback_t callback, void *data)
  46347. +{
  46348. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  46349. +
  46350. + if (!new_observer) {
  46351. + return NULL;
  46352. + }
  46353. +
  46354. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  46355. + new_observer->observer = observer;
  46356. + new_observer->notification = notification;
  46357. + new_observer->callback = callback;
  46358. + new_observer->data = data;
  46359. + return new_observer;
  46360. +}
  46361. +
  46362. +static void free_observer(void *mem_ctx, observer_t *observer)
  46363. +{
  46364. + dwc_free(mem_ctx, observer);
  46365. +}
  46366. +
  46367. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  46368. +{
  46369. + notifier_t *notifier;
  46370. +
  46371. + if (!object) {
  46372. + return NULL;
  46373. + }
  46374. +
  46375. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  46376. + if (!notifier) {
  46377. + return NULL;
  46378. + }
  46379. +
  46380. + DWC_CIRCLEQ_INIT(&notifier->observers);
  46381. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  46382. +
  46383. + notifier->mem_ctx = mem_ctx;
  46384. + notifier->object = object;
  46385. + return notifier;
  46386. +}
  46387. +
  46388. +static void free_notifier(notifier_t *notifier)
  46389. +{
  46390. + observer_t *observer;
  46391. +
  46392. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  46393. + free_observer(notifier->mem_ctx, observer);
  46394. + }
  46395. +
  46396. + dwc_free(notifier->mem_ctx, notifier);
  46397. +}
  46398. +
  46399. +static notifier_t *find_notifier(void *object)
  46400. +{
  46401. + notifier_t *notifier;
  46402. +
  46403. + DWC_ASSERT(manager, "Notification manager not found");
  46404. +
  46405. + if (!object) {
  46406. + return NULL;
  46407. + }
  46408. +
  46409. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  46410. + if (notifier->object == object) {
  46411. + return notifier;
  46412. + }
  46413. + }
  46414. +
  46415. + return NULL;
  46416. +}
  46417. +
  46418. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  46419. +{
  46420. + return create_manager(mem_ctx, wkq_ctx);
  46421. +}
  46422. +
  46423. +void dwc_free_notification_manager(void)
  46424. +{
  46425. + free_manager();
  46426. +}
  46427. +
  46428. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  46429. +{
  46430. + notifier_t *notifier;
  46431. +
  46432. + DWC_ASSERT(manager, "Notification manager not found");
  46433. +
  46434. + notifier = find_notifier(object);
  46435. + if (notifier) {
  46436. + DWC_ERROR("Notifier %p is already registered\n", object);
  46437. + return NULL;
  46438. + }
  46439. +
  46440. + notifier = alloc_notifier(mem_ctx, object);
  46441. + if (!notifier) {
  46442. + return NULL;
  46443. + }
  46444. +
  46445. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  46446. +
  46447. + DWC_INFO("Notifier %p registered", object);
  46448. + dump_manager();
  46449. +
  46450. + return notifier;
  46451. +}
  46452. +
  46453. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  46454. +{
  46455. + DWC_ASSERT(manager, "Notification manager not found");
  46456. +
  46457. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  46458. + observer_t *o;
  46459. +
  46460. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  46461. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  46462. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  46463. + }
  46464. +
  46465. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  46466. + "Notifier %p has active observers when removing", notifier);
  46467. + }
  46468. +
  46469. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  46470. + free_notifier(notifier);
  46471. +
  46472. + DWC_INFO("Notifier unregistered");
  46473. + dump_manager();
  46474. +}
  46475. +
  46476. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  46477. +int dwc_add_observer(void *observer, void *object, char *notification,
  46478. + dwc_notifier_callback_t callback, void *data)
  46479. +{
  46480. + notifier_t *notifier = find_notifier(object);
  46481. + observer_t *new_observer;
  46482. +
  46483. + if (!notifier) {
  46484. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  46485. + return -DWC_E_INVALID;
  46486. + }
  46487. +
  46488. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  46489. + if (!new_observer) {
  46490. + return -DWC_E_NO_MEMORY;
  46491. + }
  46492. +
  46493. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  46494. +
  46495. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  46496. + observer, object, notification, callback, data);
  46497. +
  46498. + dump_manager();
  46499. + return 0;
  46500. +}
  46501. +
  46502. +int dwc_remove_observer(void *observer)
  46503. +{
  46504. + notifier_t *n;
  46505. +
  46506. + DWC_ASSERT(manager, "Notification manager not found");
  46507. +
  46508. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  46509. + observer_t *o;
  46510. + observer_t *o2;
  46511. +
  46512. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  46513. + if (o->observer == observer) {
  46514. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  46515. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  46516. + o->observer, n->object, o->notification);
  46517. + free_observer(n->mem_ctx, o);
  46518. + }
  46519. + }
  46520. + }
  46521. +
  46522. + dump_manager();
  46523. + return 0;
  46524. +}
  46525. +
  46526. +typedef struct callback_data {
  46527. + void *mem_ctx;
  46528. + dwc_notifier_callback_t cb;
  46529. + void *observer;
  46530. + void *data;
  46531. + void *object;
  46532. + char *notification;
  46533. + void *notification_data;
  46534. +} cb_data_t;
  46535. +
  46536. +static void cb_task(void *data)
  46537. +{
  46538. + cb_data_t *cb = (cb_data_t *)data;
  46539. +
  46540. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  46541. + dwc_free(cb->mem_ctx, cb);
  46542. +}
  46543. +
  46544. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  46545. +{
  46546. + observer_t *o;
  46547. +
  46548. + DWC_ASSERT(manager, "Notification manager not found");
  46549. +
  46550. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  46551. + int len = DWC_STRLEN(notification);
  46552. +
  46553. + if (DWC_STRLEN(o->notification) != len) {
  46554. + continue;
  46555. + }
  46556. +
  46557. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  46558. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  46559. +
  46560. + if (!cb_data) {
  46561. + DWC_ERROR("Failed to allocate callback data\n");
  46562. + return;
  46563. + }
  46564. +
  46565. + cb_data->mem_ctx = notifier->mem_ctx;
  46566. + cb_data->cb = o->callback;
  46567. + cb_data->observer = o->observer;
  46568. + cb_data->data = o->data;
  46569. + cb_data->object = notifier->object;
  46570. + cb_data->notification = notification;
  46571. + cb_data->notification_data = notification_data;
  46572. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  46573. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  46574. + "Notify callback from %p for Notification %s, to observer %p",
  46575. + cb_data->object, notification, cb_data->observer);
  46576. + }
  46577. + }
  46578. +}
  46579. +
  46580. +#endif /* DWC_NOTIFYLIB */
  46581. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_notifier.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.h
  46582. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_notifier.h 1970-01-01 01:00:00.000000000 +0100
  46583. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.h 2014-03-11 17:33:06.000000000 +0100
  46584. @@ -0,0 +1,122 @@
  46585. +
  46586. +#ifndef __DWC_NOTIFIER_H__
  46587. +#define __DWC_NOTIFIER_H__
  46588. +
  46589. +#ifdef __cplusplus
  46590. +extern "C" {
  46591. +#endif
  46592. +
  46593. +#include "dwc_os.h"
  46594. +
  46595. +/** @file
  46596. + *
  46597. + * A simple implementation of the Observer pattern. Any "module" can
  46598. + * register as an observer or notifier. The notion of "module" is abstract and
  46599. + * can mean anything used to identify either an observer or notifier. Usually
  46600. + * it will be a pointer to a data structure which contains some state, ie an
  46601. + * object.
  46602. + *
  46603. + * Before any notifiers can be added, the global notification manager must be
  46604. + * brought up with dwc_alloc_notification_manager().
  46605. + * dwc_free_notification_manager() will bring it down and free all resources.
  46606. + * These would typically be called upon module load and unload. The
  46607. + * notification manager is a single global instance that handles all registered
  46608. + * observable modules and observers so this should be done only once.
  46609. + *
  46610. + * A module can be observable by using Notifications to publicize some general
  46611. + * information about it's state or operation. It does not care who listens, or
  46612. + * even if anyone listens, or what they do with the information. The observable
  46613. + * modules do not need to know any information about it's observers or their
  46614. + * interface, or their state or data.
  46615. + *
  46616. + * Any module can register to emit Notifications. It should publish a list of
  46617. + * notifications that it can emit and their behavior, such as when they will get
  46618. + * triggered, and what information will be provided to the observer. Then it
  46619. + * should register itself as an observable module. See dwc_register_notifier().
  46620. + *
  46621. + * Any module can observe any observable, registered module, provided it has a
  46622. + * handle to the other module and knows what notifications to observe. See
  46623. + * dwc_add_observer().
  46624. + *
  46625. + * A function of type dwc_notifier_callback_t is called whenever a notification
  46626. + * is triggered with one or more observers observing it. This function is
  46627. + * called in it's own process so it may sleep or block if needed. It is
  46628. + * guaranteed to be called sometime after the notification has occurred and will
  46629. + * be called once per each time the notification is triggered. It will NOT be
  46630. + * called in the same process context used to trigger the notification.
  46631. + *
  46632. + * @section Limitiations
  46633. + *
  46634. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  46635. + * schedule too many processes too handle. Be aware of this limitation when
  46636. + * designing to use notifications, and only add notifications for appropriate
  46637. + * observable information.
  46638. + *
  46639. + * Also Notification callbacks are not synchronous. If you need to synchronize
  46640. + * the behavior between module/observer you must use other means. And perhaps
  46641. + * that will mean Notifications are not the proper solution.
  46642. + */
  46643. +
  46644. +struct dwc_notifier;
  46645. +typedef struct dwc_notifier dwc_notifier_t;
  46646. +
  46647. +/** The callback function must be of this type.
  46648. + *
  46649. + * @param object This is the object that is being observed.
  46650. + * @param notification This is the notification that was triggered.
  46651. + * @param observer This is the observer
  46652. + * @param notification_data This is notification-specific data that the notifier
  46653. + * has included in this notification. The value of this should be published in
  46654. + * the documentation of the observable module with the notifications.
  46655. + * @param user_data This is any custom data that the observer provided when
  46656. + * adding itself as an observer to the notification. */
  46657. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  46658. + void *notification_data, void *user_data);
  46659. +
  46660. +/** Brings up the notification manager. */
  46661. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  46662. +/** Brings down the notification manager. */
  46663. +extern void dwc_free_notification_manager(void);
  46664. +
  46665. +/** This function registers an observable module. A dwc_notifier_t object is
  46666. + * returned to the observable module. This is an opaque object that is used by
  46667. + * the observable module to trigger notifications. This object should only be
  46668. + * accessible to functions that are authorized to trigger notifications for this
  46669. + * module. Observers do not need this object. */
  46670. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  46671. +
  46672. +/** This function unregisters an observable module. All observers have to be
  46673. + * removed prior to unregistration. */
  46674. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  46675. +
  46676. +/** Add a module as an observer to the observable module. The observable module
  46677. + * needs to have previously registered with the notification manager.
  46678. + *
  46679. + * @param observer The observer module
  46680. + * @param object The module to observe
  46681. + * @param notification The notification to observe
  46682. + * @param callback The callback function to call
  46683. + * @param user_data Any additional user data to pass into the callback function */
  46684. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  46685. + dwc_notifier_callback_t callback, void *user_data);
  46686. +
  46687. +/** Removes the specified observer from all notifications that it is currently
  46688. + * observing. */
  46689. +extern int dwc_remove_observer(void *observer);
  46690. +
  46691. +/** This function triggers a Notification. It should be called by the
  46692. + * observable module, or any module or library which the observable module
  46693. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  46694. + *
  46695. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  46696. + * their own process context for each trigger. Callbacks can be blocking.
  46697. + * dwc_notify can be called from interrupt context if needed.
  46698. + *
  46699. + */
  46700. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  46701. +
  46702. +#ifdef __cplusplus
  46703. +}
  46704. +#endif
  46705. +
  46706. +#endif /* __DWC_NOTIFIER_H__ */
  46707. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_os.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_os.h
  46708. --- linux-3.12.13/drivers/usb/host/dwc_common_port/dwc_os.h 1970-01-01 01:00:00.000000000 +0100
  46709. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_os.h 2014-03-11 17:51:27.000000000 +0100
  46710. @@ -0,0 +1,1262 @@
  46711. +/* =========================================================================
  46712. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  46713. + * $Revision: #14 $
  46714. + * $Date: 2010/11/04 $
  46715. + * $Change: 1621695 $
  46716. + *
  46717. + * Synopsys Portability Library Software and documentation
  46718. + * (hereinafter, "Software") is an Unsupported proprietary work of
  46719. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  46720. + * between Synopsys and you.
  46721. + *
  46722. + * The Software IS NOT an item of Licensed Software or Licensed Product
  46723. + * under any End User Software License Agreement or Agreement for
  46724. + * Licensed Product with Synopsys or any supplement thereto. You are
  46725. + * permitted to use and redistribute this Software in source and binary
  46726. + * forms, with or without modification, provided that redistributions
  46727. + * of source code must retain this notice. You may not view, use,
  46728. + * disclose, copy or distribute this file or any information contained
  46729. + * herein except pursuant to this license grant from Synopsys. If you
  46730. + * do not agree with this notice, including the disclaimer below, then
  46731. + * you are not authorized to use the Software.
  46732. + *
  46733. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  46734. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46735. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  46736. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  46737. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  46738. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  46739. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  46740. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  46741. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  46742. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  46743. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46744. + * DAMAGE.
  46745. + * ========================================================================= */
  46746. +#ifndef _DWC_OS_H_
  46747. +#define _DWC_OS_H_
  46748. +
  46749. +#ifdef __cplusplus
  46750. +extern "C" {
  46751. +#endif
  46752. +
  46753. +/** @file
  46754. + *
  46755. + * DWC portability library, low level os-wrapper functions
  46756. + *
  46757. + */
  46758. +
  46759. +/* These basic types need to be defined by some OS header file or custom header
  46760. + * file for your specific target architecture.
  46761. + *
  46762. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  46763. + *
  46764. + * Any custom or alternate header file must be added and enabled here.
  46765. + */
  46766. +
  46767. +#ifdef DWC_LINUX
  46768. +# include <linux/types.h>
  46769. +# ifdef CONFIG_DEBUG_MUTEXES
  46770. +# include <linux/mutex.h>
  46771. +# endif
  46772. +# include <linux/errno.h>
  46773. +# include <stdarg.h>
  46774. +#endif
  46775. +
  46776. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46777. +# include <os_dep.h>
  46778. +#endif
  46779. +
  46780. +
  46781. +/** @name Primitive Types and Values */
  46782. +
  46783. +/** We define a boolean type for consistency. Can be either YES or NO */
  46784. +typedef uint8_t dwc_bool_t;
  46785. +#define YES 1
  46786. +#define NO 0
  46787. +
  46788. +#ifdef DWC_LINUX
  46789. +
  46790. +/** @name Error Codes */
  46791. +#define DWC_E_INVALID EINVAL
  46792. +#define DWC_E_NO_MEMORY ENOMEM
  46793. +#define DWC_E_NO_DEVICE ENODEV
  46794. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  46795. +#define DWC_E_TIMEOUT ETIMEDOUT
  46796. +#define DWC_E_BUSY EBUSY
  46797. +#define DWC_E_AGAIN EAGAIN
  46798. +#define DWC_E_RESTART ERESTART
  46799. +#define DWC_E_ABORT ECONNABORTED
  46800. +#define DWC_E_SHUTDOWN ESHUTDOWN
  46801. +#define DWC_E_NO_DATA ENODATA
  46802. +#define DWC_E_DISCONNECT ECONNRESET
  46803. +#define DWC_E_UNKNOWN EINVAL
  46804. +#define DWC_E_NO_STREAM_RES ENOSR
  46805. +#define DWC_E_COMMUNICATION ECOMM
  46806. +#define DWC_E_OVERFLOW EOVERFLOW
  46807. +#define DWC_E_PROTOCOL EPROTO
  46808. +#define DWC_E_IN_PROGRESS EINPROGRESS
  46809. +#define DWC_E_PIPE EPIPE
  46810. +#define DWC_E_IO EIO
  46811. +#define DWC_E_NO_SPACE ENOSPC
  46812. +
  46813. +#else
  46814. +
  46815. +/** @name Error Codes */
  46816. +#define DWC_E_INVALID 1001
  46817. +#define DWC_E_NO_MEMORY 1002
  46818. +#define DWC_E_NO_DEVICE 1003
  46819. +#define DWC_E_NOT_SUPPORTED 1004
  46820. +#define DWC_E_TIMEOUT 1005
  46821. +#define DWC_E_BUSY 1006
  46822. +#define DWC_E_AGAIN 1007
  46823. +#define DWC_E_RESTART 1008
  46824. +#define DWC_E_ABORT 1009
  46825. +#define DWC_E_SHUTDOWN 1010
  46826. +#define DWC_E_NO_DATA 1011
  46827. +#define DWC_E_DISCONNECT 2000
  46828. +#define DWC_E_UNKNOWN 3000
  46829. +#define DWC_E_NO_STREAM_RES 4001
  46830. +#define DWC_E_COMMUNICATION 4002
  46831. +#define DWC_E_OVERFLOW 4003
  46832. +#define DWC_E_PROTOCOL 4004
  46833. +#define DWC_E_IN_PROGRESS 4005
  46834. +#define DWC_E_PIPE 4006
  46835. +#define DWC_E_IO 4007
  46836. +#define DWC_E_NO_SPACE 4008
  46837. +
  46838. +#endif
  46839. +
  46840. +
  46841. +/** @name Tracing/Logging Functions
  46842. + *
  46843. + * These function provide the capability to add tracing, debugging, and error
  46844. + * messages, as well exceptions as assertions. The WUDEV uses these
  46845. + * extensively. These could be logged to the main console, the serial port, an
  46846. + * internal buffer, etc. These functions could also be no-op if they are too
  46847. + * expensive on your system. By default undefining the DEBUG macro already
  46848. + * no-ops some of these functions. */
  46849. +
  46850. +/** Returns non-zero if in interrupt context. */
  46851. +extern dwc_bool_t DWC_IN_IRQ(void);
  46852. +#define dwc_in_irq DWC_IN_IRQ
  46853. +
  46854. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  46855. +static inline char *dwc_irq(void) {
  46856. + return DWC_IN_IRQ() ? "IRQ" : "";
  46857. +}
  46858. +
  46859. +/** Returns non-zero if in bottom-half context. */
  46860. +extern dwc_bool_t DWC_IN_BH(void);
  46861. +#define dwc_in_bh DWC_IN_BH
  46862. +
  46863. +/** Returns "BH" if DWC_IN_BH is true. */
  46864. +static inline char *dwc_bh(void) {
  46865. + return DWC_IN_BH() ? "BH" : "";
  46866. +}
  46867. +
  46868. +/**
  46869. + * A vprintf() clone. Just call vprintf if you've got it.
  46870. + */
  46871. +extern void DWC_VPRINTF(char *format, va_list args);
  46872. +#define dwc_vprintf DWC_VPRINTF
  46873. +
  46874. +/**
  46875. + * A vsnprintf() clone. Just call vprintf if you've got it.
  46876. + */
  46877. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  46878. +#define dwc_vsnprintf DWC_VSNPRINTF
  46879. +
  46880. +/**
  46881. + * printf() clone. Just call printf if you've go it.
  46882. + */
  46883. +extern void DWC_PRINTF(char *format, ...)
  46884. +/* This provides compiler level static checking of the parameters if you're
  46885. + * using GCC. */
  46886. +#ifdef __GNUC__
  46887. + __attribute__ ((format(printf, 1, 2)));
  46888. +#else
  46889. + ;
  46890. +#endif
  46891. +#define dwc_printf DWC_PRINTF
  46892. +
  46893. +/**
  46894. + * sprintf() clone. Just call sprintf if you've got it.
  46895. + */
  46896. +extern int DWC_SPRINTF(char *string, char *format, ...)
  46897. +#ifdef __GNUC__
  46898. + __attribute__ ((format(printf, 2, 3)));
  46899. +#else
  46900. + ;
  46901. +#endif
  46902. +#define dwc_sprintf DWC_SPRINTF
  46903. +
  46904. +/**
  46905. + * snprintf() clone. Just call snprintf if you've got it.
  46906. + */
  46907. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  46908. +#ifdef __GNUC__
  46909. + __attribute__ ((format(printf, 3, 4)));
  46910. +#else
  46911. + ;
  46912. +#endif
  46913. +#define dwc_snprintf DWC_SNPRINTF
  46914. +
  46915. +/**
  46916. + * Prints a WARNING message. On systems that don't differentiate between
  46917. + * warnings and regular log messages, just print it. Indicates that something
  46918. + * may be wrong with the driver. Works like printf().
  46919. + *
  46920. + * Use the DWC_WARN macro to call this function.
  46921. + */
  46922. +extern void __DWC_WARN(char *format, ...)
  46923. +#ifdef __GNUC__
  46924. + __attribute__ ((format(printf, 1, 2)));
  46925. +#else
  46926. + ;
  46927. +#endif
  46928. +
  46929. +/**
  46930. + * Prints an error message. On systems that don't differentiate between errors
  46931. + * and regular log messages, just print it. Indicates that something went wrong
  46932. + * with the driver. Works like printf().
  46933. + *
  46934. + * Use the DWC_ERROR macro to call this function.
  46935. + */
  46936. +extern void __DWC_ERROR(char *format, ...)
  46937. +#ifdef __GNUC__
  46938. + __attribute__ ((format(printf, 1, 2)));
  46939. +#else
  46940. + ;
  46941. +#endif
  46942. +
  46943. +/**
  46944. + * Prints an exception error message and takes some user-defined action such as
  46945. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  46946. + * abnormally wrong with the driver such as programmer error, or other
  46947. + * exceptional condition. It should not be ignored so even on systems without
  46948. + * printing capability, some action should be taken to notify the developer of
  46949. + * it. Works like printf().
  46950. + */
  46951. +extern void DWC_EXCEPTION(char *format, ...)
  46952. +#ifdef __GNUC__
  46953. + __attribute__ ((format(printf, 1, 2)));
  46954. +#else
  46955. + ;
  46956. +#endif
  46957. +#define dwc_exception DWC_EXCEPTION
  46958. +
  46959. +#ifndef DWC_OTG_DEBUG_LEV
  46960. +#define DWC_OTG_DEBUG_LEV 0
  46961. +#endif
  46962. +
  46963. +#ifdef DEBUG
  46964. +/**
  46965. + * Prints out a debug message. Used for logging/trace messages.
  46966. + *
  46967. + * Use the DWC_DEBUG macro to call this function
  46968. + */
  46969. +extern void __DWC_DEBUG(char *format, ...)
  46970. +#ifdef __GNUC__
  46971. + __attribute__ ((format(printf, 1, 2)));
  46972. +#else
  46973. + ;
  46974. +#endif
  46975. +#else
  46976. +#define __DWC_DEBUG printk
  46977. +#endif
  46978. +
  46979. +/**
  46980. + * Prints out a Debug message.
  46981. + */
  46982. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  46983. + __func__, dwc_irq(), ## _args)
  46984. +#define dwc_debug DWC_DEBUG
  46985. +/**
  46986. + * Prints out a Debug message if enabled at compile time.
  46987. + */
  46988. +#if DWC_OTG_DEBUG_LEV > 0
  46989. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  46990. +#else
  46991. +#define DWC_DEBUGC(_format, _args...)
  46992. +#endif
  46993. +#define dwc_debugc DWC_DEBUGC
  46994. +/**
  46995. + * Prints out an informative message.
  46996. + */
  46997. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  46998. + dwc_irq(), ## _args)
  46999. +#define dwc_info DWC_INFO
  47000. +/**
  47001. + * Prints out an informative message if enabled at compile time.
  47002. + */
  47003. +#if DWC_OTG_DEBUG_LEV > 1
  47004. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  47005. +#else
  47006. +#define DWC_INFOC(_format, _args...)
  47007. +#endif
  47008. +#define dwc_infoc DWC_INFOC
  47009. +/**
  47010. + * Prints out a warning message.
  47011. + */
  47012. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  47013. + dwc_irq(), __func__, __LINE__, ## _args)
  47014. +#define dwc_warn DWC_WARN
  47015. +/**
  47016. + * Prints out an error message.
  47017. + */
  47018. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  47019. + dwc_irq(), __func__, __LINE__, ## _args)
  47020. +#define dwc_error DWC_ERROR
  47021. +
  47022. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  47023. + dwc_irq(), __func__, __LINE__, ## _args)
  47024. +#define dwc_proto_error DWC_PROTO_ERROR
  47025. +
  47026. +#ifdef DEBUG
  47027. +/** Prints out a exception error message if the _expr expression fails. Disabled
  47028. + * if DEBUG is not enabled. */
  47029. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  47030. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  47031. + __FILE__, __LINE__, ## _args); } \
  47032. + } while (0)
  47033. +#else
  47034. +#define DWC_ASSERT(_x...)
  47035. +#endif
  47036. +#define dwc_assert DWC_ASSERT
  47037. +
  47038. +
  47039. +/** @name Byte Ordering
  47040. + * The following functions are for conversions between processor's byte ordering
  47041. + * and specific ordering you want.
  47042. + */
  47043. +
  47044. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  47045. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  47046. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  47047. +
  47048. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  47049. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  47050. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  47051. +
  47052. +/** Converts 32 bit little endian data to CPU byte ordering. */
  47053. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  47054. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  47055. +
  47056. +/** Converts 32 bit big endian data to CPU byte ordering. */
  47057. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  47058. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  47059. +
  47060. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  47061. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  47062. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  47063. +
  47064. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  47065. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  47066. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  47067. +
  47068. +/** Converts 16 bit little endian data to CPU byte ordering. */
  47069. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  47070. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  47071. +
  47072. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  47073. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  47074. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  47075. +
  47076. +
  47077. +/** @name Register Read/Write
  47078. + *
  47079. + * The following six functions should be implemented to read/write registers of
  47080. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  47081. + * The reg value is a pointer to the register calculated from the void *base
  47082. + * variable passed into the driver when it is started. */
  47083. +
  47084. +#ifdef DWC_LINUX
  47085. +/* Linux doesn't need any extra parameters for register read/write, so we
  47086. + * just throw away the IO context parameter.
  47087. + */
  47088. +/** Reads the content of a 32-bit register. */
  47089. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  47090. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  47091. +
  47092. +/** Reads the content of a 64-bit register. */
  47093. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  47094. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  47095. +
  47096. +/** Writes to a 32-bit register. */
  47097. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  47098. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  47099. +
  47100. +/** Writes to a 64-bit register. */
  47101. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  47102. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  47103. +
  47104. +/**
  47105. + * Modify bit values in a register. Using the
  47106. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  47107. + */
  47108. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  47109. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  47110. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  47111. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  47112. +
  47113. +#endif /* DWC_LINUX */
  47114. +
  47115. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47116. +typedef struct dwc_ioctx {
  47117. + struct device *dev;
  47118. + bus_space_tag_t iot;
  47119. + bus_space_handle_t ioh;
  47120. +} dwc_ioctx_t;
  47121. +
  47122. +/** BSD needs two extra parameters for register read/write, so we pass
  47123. + * them in using the IO context parameter.
  47124. + */
  47125. +/** Reads the content of a 32-bit register. */
  47126. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  47127. +#define dwc_read_reg32 DWC_READ_REG32
  47128. +
  47129. +/** Reads the content of a 64-bit register. */
  47130. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  47131. +#define dwc_read_reg64 DWC_READ_REG64
  47132. +
  47133. +/** Writes to a 32-bit register. */
  47134. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  47135. +#define dwc_write_reg32 DWC_WRITE_REG32
  47136. +
  47137. +/** Writes to a 64-bit register. */
  47138. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  47139. +#define dwc_write_reg64 DWC_WRITE_REG64
  47140. +
  47141. +/**
  47142. + * Modify bit values in a register. Using the
  47143. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  47144. + */
  47145. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  47146. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  47147. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  47148. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  47149. +
  47150. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  47151. +
  47152. +/** @cond */
  47153. +
  47154. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  47155. + * register writes. */
  47156. +
  47157. +#ifdef DWC_LINUX
  47158. +
  47159. +# ifdef DWC_DEBUG_REGS
  47160. +
  47161. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47162. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  47163. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  47164. +} \
  47165. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  47166. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  47167. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  47168. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  47169. +}
  47170. +
  47171. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47172. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  47173. + return DWC_READ_REG32(&container->regs->_reg); \
  47174. +} \
  47175. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  47176. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  47177. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  47178. +}
  47179. +
  47180. +# else /* DWC_DEBUG_REGS */
  47181. +
  47182. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47183. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  47184. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  47185. +} \
  47186. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  47187. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  47188. +}
  47189. +
  47190. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47191. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  47192. + return DWC_READ_REG32(&container->regs->_reg); \
  47193. +} \
  47194. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  47195. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  47196. +}
  47197. +
  47198. +# endif /* DWC_DEBUG_REGS */
  47199. +
  47200. +#endif /* DWC_LINUX */
  47201. +
  47202. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47203. +
  47204. +# ifdef DWC_DEBUG_REGS
  47205. +
  47206. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47207. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  47208. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  47209. +} \
  47210. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  47211. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  47212. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  47213. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  47214. +}
  47215. +
  47216. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47217. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  47218. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  47219. +} \
  47220. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  47221. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  47222. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  47223. +}
  47224. +
  47225. +# else /* DWC_DEBUG_REGS */
  47226. +
  47227. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47228. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  47229. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  47230. +} \
  47231. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  47232. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  47233. +}
  47234. +
  47235. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47236. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  47237. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  47238. +} \
  47239. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  47240. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  47241. +}
  47242. +
  47243. +# endif /* DWC_DEBUG_REGS */
  47244. +
  47245. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  47246. +
  47247. +/** @endcond */
  47248. +
  47249. +
  47250. +#ifdef DWC_CRYPTOLIB
  47251. +/** @name Crypto Functions
  47252. + *
  47253. + * These are the low-level cryptographic functions used by the driver. */
  47254. +
  47255. +/** Perform AES CBC */
  47256. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  47257. +#define dwc_aes_cbc DWC_AES_CBC
  47258. +
  47259. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  47260. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  47261. +#define dwc_random_bytes DWC_RANDOM_BYTES
  47262. +
  47263. +/** Perform the SHA-256 hash function */
  47264. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  47265. +#define dwc_sha256 DWC_SHA256
  47266. +
  47267. +/** Calculated the HMAC-SHA256 */
  47268. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  47269. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  47270. +
  47271. +#endif /* DWC_CRYPTOLIB */
  47272. +
  47273. +
  47274. +/** @name Memory Allocation
  47275. + *
  47276. + * These function provide access to memory allocation. There are only 2 DMA
  47277. + * functions and 3 Regular memory functions that need to be implemented. None
  47278. + * of the memory debugging routines need to be implemented. The allocation
  47279. + * routines all ZERO the contents of the memory.
  47280. + *
  47281. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  47282. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  47283. + * keeps track of how much memory the driver is using at any given time. */
  47284. +
  47285. +#define DWC_PAGE_SIZE 4096
  47286. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  47287. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  47288. +
  47289. +#define DWC_INVALID_DMA_ADDR 0x0
  47290. +
  47291. +#ifdef DWC_LINUX
  47292. +/** Type for a DMA address */
  47293. +typedef dma_addr_t dwc_dma_t;
  47294. +#endif
  47295. +
  47296. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47297. +typedef bus_addr_t dwc_dma_t;
  47298. +#endif
  47299. +
  47300. +#ifdef DWC_FREEBSD
  47301. +typedef struct dwc_dmactx {
  47302. + struct device *dev;
  47303. + bus_dma_tag_t dma_tag;
  47304. + bus_dmamap_t dma_map;
  47305. + bus_addr_t dma_paddr;
  47306. + void *dma_vaddr;
  47307. +} dwc_dmactx_t;
  47308. +#endif
  47309. +
  47310. +#ifdef DWC_NETBSD
  47311. +typedef struct dwc_dmactx {
  47312. + struct device *dev;
  47313. + bus_dma_tag_t dma_tag;
  47314. + bus_dmamap_t dma_map;
  47315. + bus_dma_segment_t segs[1];
  47316. + int nsegs;
  47317. + bus_addr_t dma_paddr;
  47318. + void *dma_vaddr;
  47319. +} dwc_dmactx_t;
  47320. +#endif
  47321. +
  47322. +/* @todo these functions will be added in the future */
  47323. +#if 0
  47324. +/**
  47325. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  47326. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  47327. + * boundary requirements specified.
  47328. + *
  47329. + * @param[in] size Specifies the size of the buffers that will be allocated from
  47330. + * this pool.
  47331. + * @param[in] align Specifies the byte alignment requirements of the buffers
  47332. + * allocated from this pool. Must be a power of 2.
  47333. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  47334. + * this pool must not cross.
  47335. + *
  47336. + * @returns A pointer to an internal opaque structure which is not to be
  47337. + * accessed outside of these library functions. Use this handle to specify
  47338. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  47339. + * when you are done with it.
  47340. + */
  47341. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  47342. +
  47343. +/**
  47344. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  47345. + */
  47346. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  47347. +
  47348. +/**
  47349. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  47350. + */
  47351. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  47352. +
  47353. +/**
  47354. + * Free a previously allocated buffer from the DMA pool.
  47355. + */
  47356. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  47357. +#endif
  47358. +
  47359. +/** Allocates a DMA capable buffer and zeroes its contents. */
  47360. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  47361. +
  47362. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  47363. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  47364. +
  47365. +/** Frees a previously allocated buffer. */
  47366. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  47367. +
  47368. +/** Allocates a block of memory and zeroes its contents. */
  47369. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  47370. +
  47371. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  47372. + * which can be used inside interrupt context. The size should be sufficiently
  47373. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  47374. + * __DWC_ALLOC if it is atomic. */
  47375. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  47376. +
  47377. +/** Frees a previously allocated buffer. */
  47378. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  47379. +
  47380. +#ifndef DWC_DEBUG_MEMORY
  47381. +
  47382. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  47383. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  47384. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  47385. +
  47386. +# ifdef DWC_LINUX
  47387. +#define DWC_DMA_ALLOC(_size_,_dma_) __DWC_DMA_ALLOC(NULL, _size_, _dma_)
  47388. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) __DWC_DMA_ALLOC_ATOMIC(NULL, _size_,_dma_)
  47389. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) __DWC_DMA_FREE(NULL, _size_, _virt_, _dma_)
  47390. +# endif
  47391. +
  47392. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47393. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  47394. +#define DWC_DMA_FREE __DWC_DMA_FREE
  47395. +# endif
  47396. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  47397. +
  47398. +#else /* DWC_DEBUG_MEMORY */
  47399. +
  47400. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  47401. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  47402. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  47403. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  47404. + char const *func, int line);
  47405. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  47406. + char const *func, int line);
  47407. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  47408. + dwc_dma_t dma_addr, char const *func, int line);
  47409. +
  47410. +extern int dwc_memory_debug_start(void *mem_ctx);
  47411. +extern void dwc_memory_debug_stop(void);
  47412. +extern void dwc_memory_debug_report(void);
  47413. +
  47414. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  47415. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  47416. + __func__, __LINE__)
  47417. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  47418. +
  47419. +# ifdef DWC_LINUX
  47420. +#define DWC_DMA_ALLOC(_size_,_dma_) dwc_dma_alloc_debug(NULL, _size_, \
  47421. + _dma_, __func__, __LINE__)
  47422. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) dwc_dma_alloc_atomic_debug(NULL, _size_, \
  47423. + _dma_, __func__, __LINE__)
  47424. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) dwc_dma_free_debug(NULL, _size_, \
  47425. + _virt_, _dma_, __func__, __LINE__)
  47426. +# endif
  47427. +
  47428. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47429. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  47430. + _dma_, __func__, __LINE__)
  47431. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  47432. + _virt_, _dma_, __func__, __LINE__)
  47433. +# endif
  47434. +
  47435. +#endif /* DWC_DEBUG_MEMORY */
  47436. +
  47437. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  47438. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  47439. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  47440. +
  47441. +#ifdef DWC_LINUX
  47442. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  47443. + * just throw away the DMA context parameter.
  47444. + */
  47445. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  47446. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  47447. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  47448. +#endif
  47449. +
  47450. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47451. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  47452. + * them in using the DMA context parameter.
  47453. + */
  47454. +#define dwc_dma_alloc DWC_DMA_ALLOC
  47455. +#define dwc_dma_free DWC_DMA_FREE
  47456. +#endif
  47457. +
  47458. +
  47459. +/** @name Memory and String Processing */
  47460. +
  47461. +/** memset() clone */
  47462. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  47463. +#define dwc_memset DWC_MEMSET
  47464. +
  47465. +/** memcpy() clone */
  47466. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  47467. +#define dwc_memcpy DWC_MEMCPY
  47468. +
  47469. +/** memmove() clone */
  47470. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  47471. +#define dwc_memmove DWC_MEMMOVE
  47472. +
  47473. +/** memcmp() clone */
  47474. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  47475. +#define dwc_memcmp DWC_MEMCMP
  47476. +
  47477. +/** strcmp() clone */
  47478. +extern int DWC_STRCMP(void *s1, void *s2);
  47479. +#define dwc_strcmp DWC_STRCMP
  47480. +
  47481. +/** strncmp() clone */
  47482. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  47483. +#define dwc_strncmp DWC_STRNCMP
  47484. +
  47485. +/** strlen() clone, for NULL terminated ASCII strings */
  47486. +extern int DWC_STRLEN(char const *str);
  47487. +#define dwc_strlen DWC_STRLEN
  47488. +
  47489. +/** strcpy() clone, for NULL terminated ASCII strings */
  47490. +extern char *DWC_STRCPY(char *to, const char *from);
  47491. +#define dwc_strcpy DWC_STRCPY
  47492. +
  47493. +/** strdup() clone. If you wish to use memory allocation debugging, this
  47494. + * implementation of strdup should use the DWC_* memory routines instead of
  47495. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  47496. + * will not be seen by the debugging routines. */
  47497. +extern char *DWC_STRDUP(char const *str);
  47498. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  47499. +
  47500. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  47501. + * converted from the string str in base 10 unless the string begins with a "0x"
  47502. + * in which case it is base 16. String must be a NULL terminated sequence of
  47503. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  47504. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  47505. + * the number and end with a NULL character. If any invalid characters are
  47506. + * encountered or it returns with a negative error code and the results of the
  47507. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  47508. + * undefined. An example implementation using atoi() can be referenced from the
  47509. + * Linux implementation. */
  47510. +extern int DWC_ATOI(const char *str, int32_t *value);
  47511. +#define dwc_atoi DWC_ATOI
  47512. +
  47513. +/** Same as above but for unsigned. */
  47514. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  47515. +#define dwc_atoui DWC_ATOUI
  47516. +
  47517. +#ifdef DWC_UTFLIB
  47518. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  47519. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  47520. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  47521. +#endif
  47522. +
  47523. +
  47524. +/** @name Wait queues
  47525. + *
  47526. + * Wait queues provide a means of synchronizing between threads or processes. A
  47527. + * process can block on a waitq if some condition is not true, waiting for it to
  47528. + * become true. When the waitq is triggered all waiting process will get
  47529. + * unblocked and the condition will be check again. Waitqs should be triggered
  47530. + * every time a condition can potentially change.*/
  47531. +struct dwc_waitq;
  47532. +
  47533. +/** Type for a waitq */
  47534. +typedef struct dwc_waitq dwc_waitq_t;
  47535. +
  47536. +/** The type of waitq condition callback function. This is called every time
  47537. + * condition is evaluated. */
  47538. +typedef int (*dwc_waitq_condition_t)(void *data);
  47539. +
  47540. +/** Allocate a waitq */
  47541. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  47542. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  47543. +
  47544. +/** Free a waitq */
  47545. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  47546. +#define dwc_waitq_free DWC_WAITQ_FREE
  47547. +
  47548. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  47549. + * condition again. The function returns when the condition becomes true. The return value
  47550. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  47551. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  47552. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  47553. +
  47554. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  47555. + * check the condition again. The function returns when the condition become
  47556. + * true or the timeout has passed. The return value is 0 on condition true or
  47557. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  47558. + * error. */
  47559. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  47560. + void *data, int32_t msecs);
  47561. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  47562. +
  47563. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  47564. + * has potentially changed. */
  47565. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  47566. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  47567. +
  47568. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  47569. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  47570. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  47571. +
  47572. +
  47573. +/** @name Threads
  47574. + *
  47575. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  47576. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  47577. + * returns the value from the thread.
  47578. + */
  47579. +
  47580. +struct dwc_thread;
  47581. +
  47582. +/** Type for a thread */
  47583. +typedef struct dwc_thread dwc_thread_t;
  47584. +
  47585. +/** The thread function */
  47586. +typedef int (*dwc_thread_function_t)(void *data);
  47587. +
  47588. +/** Create a thread and start it running the thread_function. Returns a handle
  47589. + * to the thread */
  47590. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  47591. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  47592. +
  47593. +/** Stops a thread. Return the value returned by the thread. Or will return
  47594. + * DWC_ABORT if the thread never started. */
  47595. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  47596. +#define dwc_thread_stop DWC_THREAD_STOP
  47597. +
  47598. +/** Signifies to the thread that it must stop. */
  47599. +#ifdef DWC_LINUX
  47600. +/* Linux doesn't need any parameters for kthread_should_stop() */
  47601. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  47602. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  47603. +
  47604. +/* No thread_exit function in Linux */
  47605. +#define dwc_thread_exit(_thrd_)
  47606. +#endif
  47607. +
  47608. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47609. +/** BSD needs the thread pointer for kthread_suspend_check() */
  47610. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  47611. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  47612. +
  47613. +/** The thread must call this to exit. */
  47614. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  47615. +#define dwc_thread_exit DWC_THREAD_EXIT
  47616. +#endif
  47617. +
  47618. +
  47619. +/** @name Work queues
  47620. + *
  47621. + * Workqs are used to queue a callback function to be called at some later time,
  47622. + * in another thread. */
  47623. +struct dwc_workq;
  47624. +
  47625. +/** Type for a workq */
  47626. +typedef struct dwc_workq dwc_workq_t;
  47627. +
  47628. +/** The type of the callback function to be called. */
  47629. +typedef void (*dwc_work_callback_t)(void *data);
  47630. +
  47631. +/** Allocate a workq */
  47632. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  47633. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  47634. +
  47635. +/** Free a workq. All work must be completed before being freed. */
  47636. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  47637. +#define dwc_workq_free DWC_WORKQ_FREE
  47638. +
  47639. +/** Schedule a callback on the workq, passing in data. The function will be
  47640. + * scheduled at some later time. */
  47641. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  47642. + void *data, char *format, ...)
  47643. +#ifdef __GNUC__
  47644. + __attribute__ ((format(printf, 4, 5)));
  47645. +#else
  47646. + ;
  47647. +#endif
  47648. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  47649. +
  47650. +/** Schedule a callback on the workq, that will be called until at least
  47651. + * given number miliseconds have passed. */
  47652. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  47653. + void *data, uint32_t time, char *format, ...)
  47654. +#ifdef __GNUC__
  47655. + __attribute__ ((format(printf, 5, 6)));
  47656. +#else
  47657. + ;
  47658. +#endif
  47659. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  47660. +
  47661. +/** The number of processes in the workq */
  47662. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  47663. +#define dwc_workq_pending DWC_WORKQ_PENDING
  47664. +
  47665. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  47666. + * 0 on timeout. */
  47667. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  47668. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  47669. +
  47670. +
  47671. +/** @name Tasklets
  47672. + *
  47673. + */
  47674. +struct dwc_tasklet;
  47675. +
  47676. +/** Type for a tasklet */
  47677. +typedef struct dwc_tasklet dwc_tasklet_t;
  47678. +
  47679. +/** The type of the callback function to be called */
  47680. +typedef void (*dwc_tasklet_callback_t)(void *data);
  47681. +
  47682. +/** Allocates a tasklet */
  47683. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  47684. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  47685. +
  47686. +/** Frees a tasklet */
  47687. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  47688. +#define dwc_task_free DWC_TASK_FREE
  47689. +
  47690. +/** Schedules a tasklet to run */
  47691. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  47692. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  47693. +
  47694. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  47695. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  47696. +
  47697. +/** @name Timer
  47698. + *
  47699. + * Callbacks must be small and atomic.
  47700. + */
  47701. +struct dwc_timer;
  47702. +
  47703. +/** Type for a timer */
  47704. +typedef struct dwc_timer dwc_timer_t;
  47705. +
  47706. +/** The type of the callback function to be called */
  47707. +typedef void (*dwc_timer_callback_t)(void *data);
  47708. +
  47709. +/** Allocates a timer */
  47710. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  47711. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  47712. +
  47713. +/** Frees a timer */
  47714. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  47715. +#define dwc_timer_free DWC_TIMER_FREE
  47716. +
  47717. +/** Schedules the timer to run at time ms from now. And will repeat at every
  47718. + * repeat_interval msec therafter
  47719. + *
  47720. + * Modifies a timer that is still awaiting execution to a new expiration time.
  47721. + * The mod_time is added to the old time. */
  47722. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  47723. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  47724. +
  47725. +/** Disables the timer from execution. */
  47726. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  47727. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  47728. +
  47729. +
  47730. +/** @name Spinlocks
  47731. + *
  47732. + * These locks are used when the work between the lock/unlock is atomic and
  47733. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  47734. + * suitable to lock between interrupt/non-interrupt context. They also lock
  47735. + * between processes if you have multiple CPUs or Preemption. If you don't have
  47736. + * multiple CPUS or Preemption, then the you can simply implement the
  47737. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  47738. + * the work between the lock/unlock is atomic, the process context will never
  47739. + * change, and so you never have to lock between processes. */
  47740. +
  47741. +struct dwc_spinlock;
  47742. +
  47743. +/** Type for a spinlock */
  47744. +typedef struct dwc_spinlock dwc_spinlock_t;
  47745. +
  47746. +/** Type for the 'flags' argument to spinlock funtions */
  47747. +typedef unsigned long dwc_irqflags_t;
  47748. +
  47749. +/** Returns an initialized lock variable. This function should allocate and
  47750. + * initialize the OS-specific data structure used for locking. This data
  47751. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  47752. + * be freed by the DWC_FREE_LOCK when it is no longer used. */
  47753. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  47754. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  47755. +
  47756. +/** Frees an initialized lock variable. */
  47757. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  47758. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  47759. +
  47760. +/** Disables interrupts and blocks until it acquires the lock.
  47761. + *
  47762. + * @param lock Pointer to the spinlock.
  47763. + * @param flags Unsigned long for irq flags storage.
  47764. + */
  47765. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  47766. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  47767. +
  47768. +/** Re-enables the interrupt and releases the lock.
  47769. + *
  47770. + * @param lock Pointer to the spinlock.
  47771. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  47772. + * passed into DWC_LOCK.
  47773. + */
  47774. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  47775. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  47776. +
  47777. +/** Blocks until it acquires the lock.
  47778. + *
  47779. + * @param lock Pointer to the spinlock.
  47780. + */
  47781. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  47782. +#define dwc_spinlock DWC_SPINLOCK
  47783. +
  47784. +/** Releases the lock.
  47785. + *
  47786. + * @param lock Pointer to the spinlock.
  47787. + */
  47788. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  47789. +#define dwc_spinunlock DWC_SPINUNLOCK
  47790. +
  47791. +
  47792. +/** @name Mutexes
  47793. + *
  47794. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  47795. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  47796. + */
  47797. +
  47798. +struct dwc_mutex;
  47799. +
  47800. +/** Type for a mutex */
  47801. +typedef struct dwc_mutex dwc_mutex_t;
  47802. +
  47803. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  47804. + * the symbol to determine recursive locking. This makes it falsely think
  47805. + * recursive locking occurs. */
  47806. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  47807. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  47808. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  47809. + mutex_init((struct mutex *)__mutexp); \
  47810. +})
  47811. +#endif
  47812. +
  47813. +/** Allocate a mutex */
  47814. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  47815. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  47816. +
  47817. +/* For memory leak debugging when using Linux Mutex Debugging */
  47818. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  47819. +#define DWC_MUTEX_FREE(__mutexp) do { \
  47820. + mutex_destroy((struct mutex *)__mutexp); \
  47821. + DWC_FREE(__mutexp); \
  47822. +} while(0)
  47823. +#else
  47824. +/** Free a mutex */
  47825. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  47826. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  47827. +#endif
  47828. +
  47829. +/** Lock a mutex */
  47830. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  47831. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  47832. +
  47833. +/** Non-blocking lock returns 1 on successful lock. */
  47834. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  47835. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  47836. +
  47837. +/** Unlock a mutex */
  47838. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  47839. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  47840. +
  47841. +
  47842. +/** @name Time */
  47843. +
  47844. +/** Microsecond delay.
  47845. + *
  47846. + * @param usecs Microseconds to delay.
  47847. + */
  47848. +extern void DWC_UDELAY(uint32_t usecs);
  47849. +#define dwc_udelay DWC_UDELAY
  47850. +
  47851. +/** Millisecond delay.
  47852. + *
  47853. + * @param msecs Milliseconds to delay.
  47854. + */
  47855. +extern void DWC_MDELAY(uint32_t msecs);
  47856. +#define dwc_mdelay DWC_MDELAY
  47857. +
  47858. +/** Non-busy waiting.
  47859. + * Sleeps for specified number of milliseconds.
  47860. + *
  47861. + * @param msecs Milliseconds to sleep.
  47862. + */
  47863. +extern void DWC_MSLEEP(uint32_t msecs);
  47864. +#define dwc_msleep DWC_MSLEEP
  47865. +
  47866. +/**
  47867. + * Returns number of milliseconds since boot.
  47868. + */
  47869. +extern uint32_t DWC_TIME(void);
  47870. +#define dwc_time DWC_TIME
  47871. +
  47872. +
  47873. +
  47874. +
  47875. +/* @mainpage DWC Portability and Common Library
  47876. + *
  47877. + * This is the documentation for the DWC Portability and Common Library.
  47878. + *
  47879. + * @section intro Introduction
  47880. + *
  47881. + * The DWC Portability library consists of wrapper calls and data structures to
  47882. + * all low-level functions which are typically provided by the OS. The WUDEV
  47883. + * driver uses only these functions. In order to port the WUDEV driver, only
  47884. + * the functions in this library need to be re-implemented, with the same
  47885. + * behavior as documented here.
  47886. + *
  47887. + * The Common library consists of higher level functions, which rely only on
  47888. + * calling the functions from the DWC Portability library. These common
  47889. + * routines are shared across modules. Some of the common libraries need to be
  47890. + * used directly by the driver programmer when porting WUDEV. Such as the
  47891. + * parameter and notification libraries.
  47892. + *
  47893. + * @section low Portability Library OS Wrapper Functions
  47894. + *
  47895. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  47896. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  47897. + * these functions are included in the dwc_os.h file.
  47898. + *
  47899. + * There are many functions here covering a wide array of OS services. Please
  47900. + * see dwc_os.h for details, and implementation notes for each function.
  47901. + *
  47902. + * @section common Common Library Functions
  47903. + *
  47904. + * Any function starting with dwc and in all lowercase is a common library
  47905. + * routine. These functions have a portable implementation and do not need to
  47906. + * be reimplemented when porting. The common routines can be used by any
  47907. + * driver, and some must be used by the end user to control the drivers. For
  47908. + * example, you must use the Parameter common library in order to set the
  47909. + * parameters in the WUDEV module.
  47910. + *
  47911. + * The common libraries consist of the following:
  47912. + *
  47913. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  47914. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  47915. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  47916. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  47917. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  47918. + * - Modpow - Used internally only. See dwc_modpow.h
  47919. + * - DH - Used internally only. See dwc_dh.h
  47920. + * - Crypto - Used internally only. See dwc_crypto.h
  47921. + *
  47922. + *
  47923. + * @section prereq Prerequistes For dwc_os.h
  47924. + * @subsection types Data Types
  47925. + *
  47926. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  47927. + * compilation environment. These data types are:
  47928. + *
  47929. + * - uint8_t - unsigned 8-bit data type
  47930. + * - int8_t - signed 8-bit data type
  47931. + * - uint16_t - unsigned 16-bit data type
  47932. + * - int16_t - signed 16-bit data type
  47933. + * - uint32_t - unsigned 32-bit data type
  47934. + * - int32_t - signed 32-bit data type
  47935. + * - uint64_t - unsigned 64-bit data type
  47936. + * - int64_t - signed 64-bit data type
  47937. + *
  47938. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  47939. + * that is to modify the top of the file to include the appropriate header.
  47940. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  47941. + * defined, the correct header will be added. A standard header <stdint.h> is
  47942. + * also used for environments where standard C headers are available.
  47943. + *
  47944. + * @subsection stdarg Variable Arguments
  47945. + *
  47946. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  47947. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  47948. + * provided in your enviornment in order to use dwc_os.h with the debug and
  47949. + * tracing message functionality.
  47950. + *
  47951. + * @subsection thread Threading
  47952. + *
  47953. + * WUDEV Core must be run on an operating system that provides for multiple
  47954. + * threads/processes. Threading can be implemented in many ways, even in
  47955. + * embedded systems without an operating system. At the bare minimum, the
  47956. + * system should be able to start any number of processes at any time to handle
  47957. + * special work. It need not be a pre-emptive system. Process context can
  47958. + * change upon a call to a blocking function. The hardware interrupt context
  47959. + * that calls the module's ISR() function must be differentiable from process
  47960. + * context, even if your processes are impemented via a hardware interrupt.
  47961. + * Further locking mechanism between process must exist (or be implemented), and
  47962. + * process context must have a way to disable interrupts for a period of time to
  47963. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  47964. + * threading should be able to be implemented with the defined behavior.
  47965. + *
  47966. + */
  47967. +
  47968. +#ifdef __cplusplus
  47969. +}
  47970. +#endif
  47971. +
  47972. +#endif /* _DWC_OS_H_ */
  47973. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/Makefile linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile
  47974. --- linux-3.12.13/drivers/usb/host/dwc_common_port/Makefile 1970-01-01 01:00:00.000000000 +0100
  47975. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile 2014-03-11 17:51:27.000000000 +0100
  47976. @@ -0,0 +1,58 @@
  47977. +#
  47978. +# Makefile for DWC_common library
  47979. +#
  47980. +
  47981. +ifneq ($(KERNELRELEASE),)
  47982. +
  47983. +EXTRA_CFLAGS += -DDWC_LINUX
  47984. +#EXTRA_CFLAGS += -DDEBUG
  47985. +#EXTRA_CFLAGS += -DDWC_DEBUG_REGS
  47986. +#EXTRA_CFLAGS += -DDWC_DEBUG_MEMORY
  47987. +
  47988. +EXTRA_CFLAGS += -DDWC_LIBMODULE
  47989. +EXTRA_CFLAGS += -DDWC_CCLIB
  47990. +#EXTRA_CFLAGS += -DDWC_CRYPTOLIB
  47991. +EXTRA_CFLAGS += -DDWC_NOTIFYLIB
  47992. +EXTRA_CFLAGS += -DDWC_UTFLIB
  47993. +
  47994. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  47995. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  47996. + dwc_crypto.o dwc_notifier.o \
  47997. + dwc_common_linux.o dwc_mem.o
  47998. +
  47999. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  48000. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  48001. +
  48002. +ifneq ($(kernrel3),2.6.20)
  48003. +# grayg - I only know that we use EXTRA_CFLAGS in 2.6.31 actually
  48004. +EXTRA_CFLAGS += $(CPPFLAGS)
  48005. +endif
  48006. +
  48007. +else
  48008. +
  48009. +#ifeq ($(KDIR),)
  48010. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  48011. +#endif
  48012. +
  48013. +ifeq ($(ARCH),)
  48014. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  48015. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  48016. +endif
  48017. +
  48018. +ifeq ($(DOXYGEN),)
  48019. +DOXYGEN := doxygen
  48020. +endif
  48021. +
  48022. +default:
  48023. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  48024. +
  48025. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  48026. + $(DOXYGEN) doc/doxygen.cfg
  48027. +
  48028. +tags: $(wildcard *.[hc])
  48029. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  48030. +
  48031. +endif
  48032. +
  48033. +clean:
  48034. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  48035. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/Makefile.fbsd linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.fbsd
  48036. --- linux-3.12.13/drivers/usb/host/dwc_common_port/Makefile.fbsd 1970-01-01 01:00:00.000000000 +0100
  48037. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.fbsd 2014-03-11 17:33:06.000000000 +0100
  48038. @@ -0,0 +1,17 @@
  48039. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  48040. +CFLAGS += -DDWC_FREEBSD
  48041. +CFLAGS += -DDEBUG
  48042. +#CFLAGS += -DDWC_DEBUG_REGS
  48043. +#CFLAGS += -DDWC_DEBUG_MEMORY
  48044. +
  48045. +#CFLAGS += -DDWC_LIBMODULE
  48046. +#CFLAGS += -DDWC_CCLIB
  48047. +#CFLAGS += -DDWC_CRYPTOLIB
  48048. +#CFLAGS += -DDWC_NOTIFYLIB
  48049. +#CFLAGS += -DDWC_UTFLIB
  48050. +
  48051. +KMOD = dwc_common_port_lib
  48052. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  48053. + dwc_common_fbsd.c dwc_mem.c
  48054. +
  48055. +.include <bsd.kmod.mk>
  48056. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/Makefile.linux linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.linux
  48057. --- linux-3.12.13/drivers/usb/host/dwc_common_port/Makefile.linux 1970-01-01 01:00:00.000000000 +0100
  48058. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.linux 2014-03-11 17:51:27.000000000 +0100
  48059. @@ -0,0 +1,49 @@
  48060. +#
  48061. +# Makefile for DWC_common library
  48062. +#
  48063. +ifneq ($(KERNELRELEASE),)
  48064. +
  48065. +EXTRA_CFLAGS += -DDWC_LINUX
  48066. +#EXTRA_CFLAGS += -DDEBUG
  48067. +#EXTRA_CFLAGS += -DDWC_DEBUG_REGS
  48068. +#EXTRA_CFLAGS += -DDWC_DEBUG_MEMORY
  48069. +
  48070. +EXTRA_CFLAGS += -DDWC_LIBMODULE
  48071. +EXTRA_CFLAGS += -DDWC_CCLIB
  48072. +EXTRA_CFLAGS += -DDWC_CRYPTOLIB
  48073. +EXTRA_CFLAGS += -DDWC_NOTIFYLIB
  48074. +EXTRA_CFLAGS += -DDWC_UTFLIB
  48075. +
  48076. +obj-m := dwc_common_port_lib.o
  48077. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  48078. + dwc_crypto.o dwc_notifier.o \
  48079. + dwc_common_linux.o dwc_mem.o
  48080. +
  48081. +else
  48082. +
  48083. +ifeq ($(KDIR),)
  48084. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  48085. +endif
  48086. +
  48087. +ifeq ($(ARCH),)
  48088. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  48089. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  48090. +endif
  48091. +
  48092. +ifeq ($(DOXYGEN),)
  48093. +DOXYGEN := doxygen
  48094. +endif
  48095. +
  48096. +default:
  48097. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  48098. +
  48099. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  48100. + $(DOXYGEN) doc/doxygen.cfg
  48101. +
  48102. +tags: $(wildcard *.[hc])
  48103. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  48104. +
  48105. +endif
  48106. +
  48107. +clean:
  48108. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  48109. diff -Nur linux-3.12.13/drivers/usb/host/dwc_common_port/usb.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/usb.h
  48110. --- linux-3.12.13/drivers/usb/host/dwc_common_port/usb.h 1970-01-01 01:00:00.000000000 +0100
  48111. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/usb.h 2014-03-11 17:33:06.000000000 +0100
  48112. @@ -0,0 +1,946 @@
  48113. +/*
  48114. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  48115. + * All rights reserved.
  48116. + *
  48117. + * This code is derived from software contributed to The NetBSD Foundation
  48118. + * by Lennart Augustsson (lennart@augustsson.net) at
  48119. + * Carlstedt Research & Technology.
  48120. + *
  48121. + * Redistribution and use in source and binary forms, with or without
  48122. + * modification, are permitted provided that the following conditions
  48123. + * are met:
  48124. + * 1. Redistributions of source code must retain the above copyright
  48125. + * notice, this list of conditions and the following disclaimer.
  48126. + * 2. Redistributions in binary form must reproduce the above copyright
  48127. + * notice, this list of conditions and the following disclaimer in the
  48128. + * documentation and/or other materials provided with the distribution.
  48129. + * 3. All advertising materials mentioning features or use of this software
  48130. + * must display the following acknowledgement:
  48131. + * This product includes software developed by the NetBSD
  48132. + * Foundation, Inc. and its contributors.
  48133. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  48134. + * contributors may be used to endorse or promote products derived
  48135. + * from this software without specific prior written permission.
  48136. + *
  48137. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  48138. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  48139. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48140. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  48141. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  48142. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  48143. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48144. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  48145. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48146. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48147. + * POSSIBILITY OF SUCH DAMAGE.
  48148. + */
  48149. +
  48150. +/* Modified by Synopsys, Inc, 12/12/2007 */
  48151. +
  48152. +
  48153. +#ifndef _USB_H_
  48154. +#define _USB_H_
  48155. +
  48156. +#ifdef __cplusplus
  48157. +extern "C" {
  48158. +#endif
  48159. +
  48160. +/*
  48161. + * The USB records contain some unaligned little-endian word
  48162. + * components. The U[SG]ETW macros take care of both the alignment
  48163. + * and endian problem and should always be used to access non-byte
  48164. + * values.
  48165. + */
  48166. +typedef u_int8_t uByte;
  48167. +typedef u_int8_t uWord[2];
  48168. +typedef u_int8_t uDWord[4];
  48169. +
  48170. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  48171. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  48172. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  48173. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  48174. +
  48175. +#if 1
  48176. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  48177. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  48178. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  48179. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  48180. + (w)[1] = (u_int8_t)((v) >> 8), \
  48181. + (w)[2] = (u_int8_t)((v) >> 16), \
  48182. + (w)[3] = (u_int8_t)((v) >> 24))
  48183. +#else
  48184. +/*
  48185. + * On little-endian machines that can handle unanliged accesses
  48186. + * (e.g. i386) these macros can be replaced by the following.
  48187. + */
  48188. +#define UGETW(w) (*(u_int16_t *)(w))
  48189. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  48190. +#define UGETDW(w) (*(u_int32_t *)(w))
  48191. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  48192. +#endif
  48193. +
  48194. +/*
  48195. + * Macros for accessing UAS IU fields, which are big-endian
  48196. + */
  48197. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  48198. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  48199. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  48200. + ((x) >> 8) & 0xff, (x) & 0xff }
  48201. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  48202. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  48203. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  48204. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  48205. + (w)[1] = (u_int8_t)((v) >> 16), \
  48206. + (w)[2] = (u_int8_t)((v) >> 8), \
  48207. + (w)[3] = (u_int8_t)(v))
  48208. +
  48209. +#define UPACKED __attribute__((__packed__))
  48210. +
  48211. +typedef struct {
  48212. + uByte bmRequestType;
  48213. + uByte bRequest;
  48214. + uWord wValue;
  48215. + uWord wIndex;
  48216. + uWord wLength;
  48217. +} UPACKED usb_device_request_t;
  48218. +
  48219. +#define UT_GET_DIR(a) ((a) & 0x80)
  48220. +#define UT_WRITE 0x00
  48221. +#define UT_READ 0x80
  48222. +
  48223. +#define UT_GET_TYPE(a) ((a) & 0x60)
  48224. +#define UT_STANDARD 0x00
  48225. +#define UT_CLASS 0x20
  48226. +#define UT_VENDOR 0x40
  48227. +
  48228. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  48229. +#define UT_DEVICE 0x00
  48230. +#define UT_INTERFACE 0x01
  48231. +#define UT_ENDPOINT 0x02
  48232. +#define UT_OTHER 0x03
  48233. +
  48234. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  48235. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  48236. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  48237. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  48238. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  48239. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  48240. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  48241. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  48242. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  48243. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  48244. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  48245. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  48246. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  48247. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  48248. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  48249. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  48250. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  48251. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  48252. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  48253. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  48254. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  48255. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  48256. +
  48257. +/* Requests */
  48258. +#define UR_GET_STATUS 0x00
  48259. +#define USTAT_STANDARD_STATUS 0x00
  48260. +#define WUSTAT_WUSB_FEATURE 0x01
  48261. +#define WUSTAT_CHANNEL_INFO 0x02
  48262. +#define WUSTAT_RECEIVED_DATA 0x03
  48263. +#define WUSTAT_MAS_AVAILABILITY 0x04
  48264. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  48265. +#define UR_CLEAR_FEATURE 0x01
  48266. +#define UR_SET_FEATURE 0x03
  48267. +#define UR_SET_AND_TEST_FEATURE 0x0c
  48268. +#define UR_SET_ADDRESS 0x05
  48269. +#define UR_GET_DESCRIPTOR 0x06
  48270. +#define UDESC_DEVICE 0x01
  48271. +#define UDESC_CONFIG 0x02
  48272. +#define UDESC_STRING 0x03
  48273. +#define UDESC_INTERFACE 0x04
  48274. +#define UDESC_ENDPOINT 0x05
  48275. +#define UDESC_SS_USB_COMPANION 0x30
  48276. +#define UDESC_DEVICE_QUALIFIER 0x06
  48277. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  48278. +#define UDESC_INTERFACE_POWER 0x08
  48279. +#define UDESC_OTG 0x09
  48280. +#define WUDESC_SECURITY 0x0c
  48281. +#define WUDESC_KEY 0x0d
  48282. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  48283. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  48284. +#define WUD_KEY_TYPE_ASSOC 0x01
  48285. +#define WUD_KEY_TYPE_GTK 0x02
  48286. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  48287. +#define WUD_KEY_ORIGIN_HOST 0x00
  48288. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  48289. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  48290. +#define WUDESC_BOS 0x0f
  48291. +#define WUDESC_DEVICE_CAPABILITY 0x10
  48292. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  48293. +#define UDESC_BOS 0x0f
  48294. +#define UDESC_DEVICE_CAPABILITY 0x10
  48295. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  48296. +#define UDESC_CS_CONFIG 0x22
  48297. +#define UDESC_CS_STRING 0x23
  48298. +#define UDESC_CS_INTERFACE 0x24
  48299. +#define UDESC_CS_ENDPOINT 0x25
  48300. +#define UDESC_HUB 0x29
  48301. +#define UR_SET_DESCRIPTOR 0x07
  48302. +#define UR_GET_CONFIG 0x08
  48303. +#define UR_SET_CONFIG 0x09
  48304. +#define UR_GET_INTERFACE 0x0a
  48305. +#define UR_SET_INTERFACE 0x0b
  48306. +#define UR_SYNCH_FRAME 0x0c
  48307. +#define WUR_SET_ENCRYPTION 0x0d
  48308. +#define WUR_GET_ENCRYPTION 0x0e
  48309. +#define WUR_SET_HANDSHAKE 0x0f
  48310. +#define WUR_GET_HANDSHAKE 0x10
  48311. +#define WUR_SET_CONNECTION 0x11
  48312. +#define WUR_SET_SECURITY_DATA 0x12
  48313. +#define WUR_GET_SECURITY_DATA 0x13
  48314. +#define WUR_SET_WUSB_DATA 0x14
  48315. +#define WUDATA_DRPIE_INFO 0x01
  48316. +#define WUDATA_TRANSMIT_DATA 0x02
  48317. +#define WUDATA_TRANSMIT_PARAMS 0x03
  48318. +#define WUDATA_RECEIVE_PARAMS 0x04
  48319. +#define WUDATA_TRANSMIT_POWER 0x05
  48320. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  48321. +#define WUR_LOOPBACK_DATA_READ 0x16
  48322. +#define WUR_SET_INTERFACE_DS 0x17
  48323. +
  48324. +/* Feature numbers */
  48325. +#define UF_ENDPOINT_HALT 0
  48326. +#define UF_DEVICE_REMOTE_WAKEUP 1
  48327. +#define UF_TEST_MODE 2
  48328. +#define UF_DEVICE_B_HNP_ENABLE 3
  48329. +#define UF_DEVICE_A_HNP_SUPPORT 4
  48330. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  48331. +#define WUF_WUSB 3
  48332. +#define WUF_TX_DRPIE 0x0
  48333. +#define WUF_DEV_XMIT_PACKET 0x1
  48334. +#define WUF_COUNT_PACKETS 0x2
  48335. +#define WUF_CAPTURE_PACKETS 0x3
  48336. +#define UF_FUNCTION_SUSPEND 0
  48337. +#define UF_U1_ENABLE 48
  48338. +#define UF_U2_ENABLE 49
  48339. +#define UF_LTM_ENABLE 50
  48340. +
  48341. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  48342. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  48343. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  48344. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  48345. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  48346. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  48347. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  48348. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  48349. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  48350. +
  48351. +#ifdef _MSC_VER
  48352. +#include <pshpack1.h>
  48353. +#endif
  48354. +
  48355. +typedef struct {
  48356. + uByte bLength;
  48357. + uByte bDescriptorType;
  48358. + uByte bDescriptorSubtype;
  48359. +} UPACKED usb_descriptor_t;
  48360. +
  48361. +typedef struct {
  48362. + uByte bLength;
  48363. + uByte bDescriptorType;
  48364. +} UPACKED usb_descriptor_header_t;
  48365. +
  48366. +typedef struct {
  48367. + uByte bLength;
  48368. + uByte bDescriptorType;
  48369. + uWord bcdUSB;
  48370. +#define UD_USB_2_0 0x0200
  48371. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  48372. + uByte bDeviceClass;
  48373. + uByte bDeviceSubClass;
  48374. + uByte bDeviceProtocol;
  48375. + uByte bMaxPacketSize;
  48376. + /* The fields below are not part of the initial descriptor. */
  48377. + uWord idVendor;
  48378. + uWord idProduct;
  48379. + uWord bcdDevice;
  48380. + uByte iManufacturer;
  48381. + uByte iProduct;
  48382. + uByte iSerialNumber;
  48383. + uByte bNumConfigurations;
  48384. +} UPACKED usb_device_descriptor_t;
  48385. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  48386. +
  48387. +typedef struct {
  48388. + uByte bLength;
  48389. + uByte bDescriptorType;
  48390. + uWord wTotalLength;
  48391. + uByte bNumInterface;
  48392. + uByte bConfigurationValue;
  48393. + uByte iConfiguration;
  48394. +#define UC_ATT_ONE (1 << 7) /* must be set */
  48395. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  48396. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  48397. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  48398. + uByte bmAttributes;
  48399. +#define UC_BUS_POWERED 0x80
  48400. +#define UC_SELF_POWERED 0x40
  48401. +#define UC_REMOTE_WAKEUP 0x20
  48402. + uByte bMaxPower; /* max current in 2 mA units */
  48403. +#define UC_POWER_FACTOR 2
  48404. +} UPACKED usb_config_descriptor_t;
  48405. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  48406. +
  48407. +typedef struct {
  48408. + uByte bLength;
  48409. + uByte bDescriptorType;
  48410. + uByte bInterfaceNumber;
  48411. + uByte bAlternateSetting;
  48412. + uByte bNumEndpoints;
  48413. + uByte bInterfaceClass;
  48414. + uByte bInterfaceSubClass;
  48415. + uByte bInterfaceProtocol;
  48416. + uByte iInterface;
  48417. +} UPACKED usb_interface_descriptor_t;
  48418. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  48419. +
  48420. +typedef struct {
  48421. + uByte bLength;
  48422. + uByte bDescriptorType;
  48423. + uByte bEndpointAddress;
  48424. +#define UE_GET_DIR(a) ((a) & 0x80)
  48425. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  48426. +#define UE_DIR_IN 0x80
  48427. +#define UE_DIR_OUT 0x00
  48428. +#define UE_ADDR 0x0f
  48429. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  48430. + uByte bmAttributes;
  48431. +#define UE_XFERTYPE 0x03
  48432. +#define UE_CONTROL 0x00
  48433. +#define UE_ISOCHRONOUS 0x01
  48434. +#define UE_BULK 0x02
  48435. +#define UE_INTERRUPT 0x03
  48436. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  48437. +#define UE_ISO_TYPE 0x0c
  48438. +#define UE_ISO_ASYNC 0x04
  48439. +#define UE_ISO_ADAPT 0x08
  48440. +#define UE_ISO_SYNC 0x0c
  48441. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  48442. + uWord wMaxPacketSize;
  48443. + uByte bInterval;
  48444. +} UPACKED usb_endpoint_descriptor_t;
  48445. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  48446. +
  48447. +typedef struct ss_endpoint_companion_descriptor {
  48448. + uByte bLength;
  48449. + uByte bDescriptorType;
  48450. + uByte bMaxBurst;
  48451. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  48452. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  48453. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  48454. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  48455. + uByte bmAttributes;
  48456. + uWord wBytesPerInterval;
  48457. +} UPACKED ss_endpoint_companion_descriptor_t;
  48458. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  48459. +
  48460. +typedef struct {
  48461. + uByte bLength;
  48462. + uByte bDescriptorType;
  48463. + uWord bString[127];
  48464. +} UPACKED usb_string_descriptor_t;
  48465. +#define USB_MAX_STRING_LEN 128
  48466. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  48467. +
  48468. +/* Hub specific request */
  48469. +#define UR_GET_BUS_STATE 0x02
  48470. +#define UR_CLEAR_TT_BUFFER 0x08
  48471. +#define UR_RESET_TT 0x09
  48472. +#define UR_GET_TT_STATE 0x0a
  48473. +#define UR_STOP_TT 0x0b
  48474. +
  48475. +/* Hub features */
  48476. +#define UHF_C_HUB_LOCAL_POWER 0
  48477. +#define UHF_C_HUB_OVER_CURRENT 1
  48478. +#define UHF_PORT_CONNECTION 0
  48479. +#define UHF_PORT_ENABLE 1
  48480. +#define UHF_PORT_SUSPEND 2
  48481. +#define UHF_PORT_OVER_CURRENT 3
  48482. +#define UHF_PORT_RESET 4
  48483. +#define UHF_PORT_L1 5
  48484. +#define UHF_PORT_POWER 8
  48485. +#define UHF_PORT_LOW_SPEED 9
  48486. +#define UHF_PORT_HIGH_SPEED 10
  48487. +#define UHF_C_PORT_CONNECTION 16
  48488. +#define UHF_C_PORT_ENABLE 17
  48489. +#define UHF_C_PORT_SUSPEND 18
  48490. +#define UHF_C_PORT_OVER_CURRENT 19
  48491. +#define UHF_C_PORT_RESET 20
  48492. +#define UHF_C_PORT_L1 23
  48493. +#define UHF_PORT_TEST 21
  48494. +#define UHF_PORT_INDICATOR 22
  48495. +
  48496. +typedef struct {
  48497. + uByte bDescLength;
  48498. + uByte bDescriptorType;
  48499. + uByte bNbrPorts;
  48500. + uWord wHubCharacteristics;
  48501. +#define UHD_PWR 0x0003
  48502. +#define UHD_PWR_GANGED 0x0000
  48503. +#define UHD_PWR_INDIVIDUAL 0x0001
  48504. +#define UHD_PWR_NO_SWITCH 0x0002
  48505. +#define UHD_COMPOUND 0x0004
  48506. +#define UHD_OC 0x0018
  48507. +#define UHD_OC_GLOBAL 0x0000
  48508. +#define UHD_OC_INDIVIDUAL 0x0008
  48509. +#define UHD_OC_NONE 0x0010
  48510. +#define UHD_TT_THINK 0x0060
  48511. +#define UHD_TT_THINK_8 0x0000
  48512. +#define UHD_TT_THINK_16 0x0020
  48513. +#define UHD_TT_THINK_24 0x0040
  48514. +#define UHD_TT_THINK_32 0x0060
  48515. +#define UHD_PORT_IND 0x0080
  48516. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  48517. +#define UHD_PWRON_FACTOR 2
  48518. + uByte bHubContrCurrent;
  48519. + uByte DeviceRemovable[32]; /* max 255 ports */
  48520. +#define UHD_NOT_REMOV(desc, i) \
  48521. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  48522. + /* deprecated */ uByte PortPowerCtrlMask[1];
  48523. +} UPACKED usb_hub_descriptor_t;
  48524. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  48525. +
  48526. +typedef struct {
  48527. + uByte bLength;
  48528. + uByte bDescriptorType;
  48529. + uWord bcdUSB;
  48530. + uByte bDeviceClass;
  48531. + uByte bDeviceSubClass;
  48532. + uByte bDeviceProtocol;
  48533. + uByte bMaxPacketSize0;
  48534. + uByte bNumConfigurations;
  48535. + uByte bReserved;
  48536. +} UPACKED usb_device_qualifier_t;
  48537. +#define USB_DEVICE_QUALIFIER_SIZE 10
  48538. +
  48539. +typedef struct {
  48540. + uByte bLength;
  48541. + uByte bDescriptorType;
  48542. + uByte bmAttributes;
  48543. +#define UOTG_SRP 0x01
  48544. +#define UOTG_HNP 0x02
  48545. +} UPACKED usb_otg_descriptor_t;
  48546. +
  48547. +/* OTG feature selectors */
  48548. +#define UOTG_B_HNP_ENABLE 3
  48549. +#define UOTG_A_HNP_SUPPORT 4
  48550. +#define UOTG_A_ALT_HNP_SUPPORT 5
  48551. +
  48552. +typedef struct {
  48553. + uWord wStatus;
  48554. +/* Device status flags */
  48555. +#define UDS_SELF_POWERED 0x0001
  48556. +#define UDS_REMOTE_WAKEUP 0x0002
  48557. +/* Endpoint status flags */
  48558. +#define UES_HALT 0x0001
  48559. +} UPACKED usb_status_t;
  48560. +
  48561. +typedef struct {
  48562. + uWord wHubStatus;
  48563. +#define UHS_LOCAL_POWER 0x0001
  48564. +#define UHS_OVER_CURRENT 0x0002
  48565. + uWord wHubChange;
  48566. +} UPACKED usb_hub_status_t;
  48567. +
  48568. +typedef struct {
  48569. + uWord wPortStatus;
  48570. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  48571. +#define UPS_PORT_ENABLED 0x0002
  48572. +#define UPS_SUSPEND 0x0004
  48573. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  48574. +#define UPS_RESET 0x0010
  48575. +#define UPS_PORT_POWER 0x0100
  48576. +#define UPS_LOW_SPEED 0x0200
  48577. +#define UPS_HIGH_SPEED 0x0400
  48578. +#define UPS_PORT_TEST 0x0800
  48579. +#define UPS_PORT_INDICATOR 0x1000
  48580. + uWord wPortChange;
  48581. +#define UPS_C_CONNECT_STATUS 0x0001
  48582. +#define UPS_C_PORT_ENABLED 0x0002
  48583. +#define UPS_C_SUSPEND 0x0004
  48584. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  48585. +#define UPS_C_PORT_RESET 0x0010
  48586. +} UPACKED usb_port_status_t;
  48587. +
  48588. +#ifdef _MSC_VER
  48589. +#include <poppack.h>
  48590. +#endif
  48591. +
  48592. +/* Device class codes */
  48593. +#define UDCLASS_IN_INTERFACE 0x00
  48594. +#define UDCLASS_COMM 0x02
  48595. +#define UDCLASS_HUB 0x09
  48596. +#define UDSUBCLASS_HUB 0x00
  48597. +#define UDPROTO_FSHUB 0x00
  48598. +#define UDPROTO_HSHUBSTT 0x01
  48599. +#define UDPROTO_HSHUBMTT 0x02
  48600. +#define UDCLASS_DIAGNOSTIC 0xdc
  48601. +#define UDCLASS_WIRELESS 0xe0
  48602. +#define UDSUBCLASS_RF 0x01
  48603. +#define UDPROTO_BLUETOOTH 0x01
  48604. +#define UDCLASS_VENDOR 0xff
  48605. +
  48606. +/* Interface class codes */
  48607. +#define UICLASS_UNSPEC 0x00
  48608. +
  48609. +#define UICLASS_AUDIO 0x01
  48610. +#define UISUBCLASS_AUDIOCONTROL 1
  48611. +#define UISUBCLASS_AUDIOSTREAM 2
  48612. +#define UISUBCLASS_MIDISTREAM 3
  48613. +
  48614. +#define UICLASS_CDC 0x02 /* communication */
  48615. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  48616. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  48617. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  48618. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  48619. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  48620. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  48621. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  48622. +#define UIPROTO_CDC_AT 1
  48623. +
  48624. +#define UICLASS_HID 0x03
  48625. +#define UISUBCLASS_BOOT 1
  48626. +#define UIPROTO_BOOT_KEYBOARD 1
  48627. +
  48628. +#define UICLASS_PHYSICAL 0x05
  48629. +
  48630. +#define UICLASS_IMAGE 0x06
  48631. +
  48632. +#define UICLASS_PRINTER 0x07
  48633. +#define UISUBCLASS_PRINTER 1
  48634. +#define UIPROTO_PRINTER_UNI 1
  48635. +#define UIPROTO_PRINTER_BI 2
  48636. +#define UIPROTO_PRINTER_1284 3
  48637. +
  48638. +#define UICLASS_MASS 0x08
  48639. +#define UISUBCLASS_RBC 1
  48640. +#define UISUBCLASS_SFF8020I 2
  48641. +#define UISUBCLASS_QIC157 3
  48642. +#define UISUBCLASS_UFI 4
  48643. +#define UISUBCLASS_SFF8070I 5
  48644. +#define UISUBCLASS_SCSI 6
  48645. +#define UIPROTO_MASS_CBI_I 0
  48646. +#define UIPROTO_MASS_CBI 1
  48647. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  48648. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  48649. +
  48650. +#define UICLASS_HUB 0x09
  48651. +#define UISUBCLASS_HUB 0
  48652. +#define UIPROTO_FSHUB 0
  48653. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  48654. +#define UIPROTO_HSHUBMTT 1
  48655. +
  48656. +#define UICLASS_CDC_DATA 0x0a
  48657. +#define UISUBCLASS_DATA 0
  48658. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  48659. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  48660. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  48661. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  48662. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  48663. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  48664. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  48665. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  48666. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  48667. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  48668. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  48669. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  48670. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  48671. +
  48672. +#define UICLASS_SMARTCARD 0x0b
  48673. +
  48674. +/*#define UICLASS_FIRM_UPD 0x0c*/
  48675. +
  48676. +#define UICLASS_SECURITY 0x0d
  48677. +
  48678. +#define UICLASS_DIAGNOSTIC 0xdc
  48679. +
  48680. +#define UICLASS_WIRELESS 0xe0
  48681. +#define UISUBCLASS_RF 0x01
  48682. +#define UIPROTO_BLUETOOTH 0x01
  48683. +
  48684. +#define UICLASS_APPL_SPEC 0xfe
  48685. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  48686. +#define UISUBCLASS_IRDA 2
  48687. +#define UIPROTO_IRDA 0
  48688. +
  48689. +#define UICLASS_VENDOR 0xff
  48690. +
  48691. +#define USB_HUB_MAX_DEPTH 5
  48692. +
  48693. +/*
  48694. + * Minimum time a device needs to be powered down to go through
  48695. + * a power cycle. XXX Are these time in the spec?
  48696. + */
  48697. +#define USB_POWER_DOWN_TIME 200 /* ms */
  48698. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  48699. +
  48700. +#if 0
  48701. +/* These are the values from the spec. */
  48702. +#define USB_PORT_RESET_DELAY 10 /* ms */
  48703. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  48704. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  48705. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  48706. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  48707. +#define USB_RESUME_DELAY (20*5) /* ms */
  48708. +#define USB_RESUME_WAIT 10 /* ms */
  48709. +#define USB_RESUME_RECOVERY 10 /* ms */
  48710. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  48711. +#else
  48712. +/* Allow for marginal (i.e. non-conforming) devices. */
  48713. +#define USB_PORT_RESET_DELAY 50 /* ms */
  48714. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  48715. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  48716. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  48717. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  48718. +#define USB_RESUME_DELAY (50*5) /* ms */
  48719. +#define USB_RESUME_WAIT 50 /* ms */
  48720. +#define USB_RESUME_RECOVERY 50 /* ms */
  48721. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  48722. +#endif
  48723. +
  48724. +#define USB_MIN_POWER 100 /* mA */
  48725. +#define USB_MAX_POWER 500 /* mA */
  48726. +
  48727. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  48728. +
  48729. +#define USB_UNCONFIG_NO 0
  48730. +#define USB_UNCONFIG_INDEX (-1)
  48731. +
  48732. +/*** ioctl() related stuff ***/
  48733. +
  48734. +struct usb_ctl_request {
  48735. + int ucr_addr;
  48736. + usb_device_request_t ucr_request;
  48737. + void *ucr_data;
  48738. + int ucr_flags;
  48739. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  48740. + int ucr_actlen; /* actual length transferred */
  48741. +};
  48742. +
  48743. +struct usb_alt_interface {
  48744. + int uai_config_index;
  48745. + int uai_interface_index;
  48746. + int uai_alt_no;
  48747. +};
  48748. +
  48749. +#define USB_CURRENT_CONFIG_INDEX (-1)
  48750. +#define USB_CURRENT_ALT_INDEX (-1)
  48751. +
  48752. +struct usb_config_desc {
  48753. + int ucd_config_index;
  48754. + usb_config_descriptor_t ucd_desc;
  48755. +};
  48756. +
  48757. +struct usb_interface_desc {
  48758. + int uid_config_index;
  48759. + int uid_interface_index;
  48760. + int uid_alt_index;
  48761. + usb_interface_descriptor_t uid_desc;
  48762. +};
  48763. +
  48764. +struct usb_endpoint_desc {
  48765. + int ued_config_index;
  48766. + int ued_interface_index;
  48767. + int ued_alt_index;
  48768. + int ued_endpoint_index;
  48769. + usb_endpoint_descriptor_t ued_desc;
  48770. +};
  48771. +
  48772. +struct usb_full_desc {
  48773. + int ufd_config_index;
  48774. + u_int ufd_size;
  48775. + u_char *ufd_data;
  48776. +};
  48777. +
  48778. +struct usb_string_desc {
  48779. + int usd_string_index;
  48780. + int usd_language_id;
  48781. + usb_string_descriptor_t usd_desc;
  48782. +};
  48783. +
  48784. +struct usb_ctl_report_desc {
  48785. + int ucrd_size;
  48786. + u_char ucrd_data[1024]; /* filled data size will vary */
  48787. +};
  48788. +
  48789. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  48790. +
  48791. +#define USB_MAX_DEVNAMES 4
  48792. +#define USB_MAX_DEVNAMELEN 16
  48793. +struct usb_device_info {
  48794. + u_int8_t udi_bus;
  48795. + u_int8_t udi_addr; /* device address */
  48796. + usb_event_cookie_t udi_cookie;
  48797. + char udi_product[USB_MAX_STRING_LEN];
  48798. + char udi_vendor[USB_MAX_STRING_LEN];
  48799. + char udi_release[8];
  48800. + u_int16_t udi_productNo;
  48801. + u_int16_t udi_vendorNo;
  48802. + u_int16_t udi_releaseNo;
  48803. + u_int8_t udi_class;
  48804. + u_int8_t udi_subclass;
  48805. + u_int8_t udi_protocol;
  48806. + u_int8_t udi_config;
  48807. + u_int8_t udi_speed;
  48808. +#define USB_SPEED_UNKNOWN 0
  48809. +#define USB_SPEED_LOW 1
  48810. +#define USB_SPEED_FULL 2
  48811. +#define USB_SPEED_HIGH 3
  48812. +#define USB_SPEED_VARIABLE 4
  48813. +#define USB_SPEED_SUPER 5
  48814. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  48815. + int udi_nports;
  48816. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  48817. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  48818. +#define USB_PORT_ENABLED 0xff
  48819. +#define USB_PORT_SUSPENDED 0xfe
  48820. +#define USB_PORT_POWERED 0xfd
  48821. +#define USB_PORT_DISABLED 0xfc
  48822. +};
  48823. +
  48824. +struct usb_ctl_report {
  48825. + int ucr_report;
  48826. + u_char ucr_data[1024]; /* filled data size will vary */
  48827. +};
  48828. +
  48829. +struct usb_device_stats {
  48830. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  48831. +};
  48832. +
  48833. +#define WUSB_MIN_IE 0x80
  48834. +#define WUSB_WCTA_IE 0x80
  48835. +#define WUSB_WCONNECTACK_IE 0x81
  48836. +#define WUSB_WHOSTINFO_IE 0x82
  48837. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  48838. +#define WUHI_CA_RECONN 0x00
  48839. +#define WUHI_CA_LIMITED 0x01
  48840. +#define WUHI_CA_ALL 0x03
  48841. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  48842. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  48843. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  48844. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  48845. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  48846. +#define WUSB_WWORK_IE 0x87
  48847. +#define WUSB_WCHANNEL_STOP_IE 0x88
  48848. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  48849. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  48850. +#define WUSB_WRESETDEVICE_IE 0x8B
  48851. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  48852. +#define WUSB_MAX_IE 0x8C
  48853. +
  48854. +/* Device Notification Types */
  48855. +
  48856. +#define WUSB_DN_MIN 0x01
  48857. +#define WUSB_DN_CONNECT 0x01
  48858. +# define WUSB_DA_OLDCONN 0x00
  48859. +# define WUSB_DA_NEWCONN 0x01
  48860. +# define WUSB_DA_SELF_BEACON 0x02
  48861. +# define WUSB_DA_DIR_BEACON 0x04
  48862. +# define WUSB_DA_NO_BEACON 0x06
  48863. +#define WUSB_DN_DISCONNECT 0x02
  48864. +#define WUSB_DN_EPRDY 0x03
  48865. +#define WUSB_DN_MASAVAILCHANGED 0x04
  48866. +#define WUSB_DN_REMOTEWAKEUP 0x05
  48867. +#define WUSB_DN_SLEEP 0x06
  48868. +#define WUSB_DN_ALIVE 0x07
  48869. +#define WUSB_DN_MAX 0x07
  48870. +
  48871. +#ifdef _MSC_VER
  48872. +#include <pshpack1.h>
  48873. +#endif
  48874. +
  48875. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  48876. +typedef struct wusb_hndshk_data {
  48877. + uByte bMessageNumber;
  48878. + uByte bStatus;
  48879. + uByte tTKID[3];
  48880. + uByte bReserved;
  48881. + uByte CDID[16];
  48882. + uByte Nonce[16];
  48883. + uByte MIC[8];
  48884. +} UPACKED wusb_hndshk_data_t;
  48885. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  48886. +
  48887. +/* WUSB Connection Context */
  48888. +typedef struct wusb_conn_context {
  48889. + uByte CHID [16];
  48890. + uByte CDID [16];
  48891. + uByte CK [16];
  48892. +} UPACKED wusb_conn_context_t;
  48893. +
  48894. +/* WUSB Security Descriptor */
  48895. +typedef struct wusb_security_desc {
  48896. + uByte bLength;
  48897. + uByte bDescriptorType;
  48898. + uWord wTotalLength;
  48899. + uByte bNumEncryptionTypes;
  48900. +} UPACKED wusb_security_desc_t;
  48901. +
  48902. +/* WUSB Encryption Type Descriptor */
  48903. +typedef struct wusb_encrypt_type_desc {
  48904. + uByte bLength;
  48905. + uByte bDescriptorType;
  48906. +
  48907. + uByte bEncryptionType;
  48908. +#define WUETD_UNSECURE 0
  48909. +#define WUETD_WIRED 1
  48910. +#define WUETD_CCM_1 2
  48911. +#define WUETD_RSA_1 3
  48912. +
  48913. + uByte bEncryptionValue;
  48914. + uByte bAuthKeyIndex;
  48915. +} UPACKED wusb_encrypt_type_desc_t;
  48916. +
  48917. +/* WUSB Key Descriptor */
  48918. +typedef struct wusb_key_desc {
  48919. + uByte bLength;
  48920. + uByte bDescriptorType;
  48921. + uByte tTKID[3];
  48922. + uByte bReserved;
  48923. + uByte KeyData[1]; /* variable length */
  48924. +} UPACKED wusb_key_desc_t;
  48925. +
  48926. +/* WUSB BOS Descriptor (Binary device Object Store) */
  48927. +typedef struct wusb_bos_desc {
  48928. + uByte bLength;
  48929. + uByte bDescriptorType;
  48930. + uWord wTotalLength;
  48931. + uByte bNumDeviceCaps;
  48932. +} UPACKED wusb_bos_desc_t;
  48933. +
  48934. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  48935. +typedef struct usb_dev_cap_20_ext_desc {
  48936. + uByte bLength;
  48937. + uByte bDescriptorType;
  48938. + uByte bDevCapabilityType;
  48939. +#define USB_20_EXT_LPM 0x02
  48940. + uDWord bmAttributes;
  48941. +} UPACKED usb_dev_cap_20_ext_desc_t;
  48942. +
  48943. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  48944. +typedef struct usb_dev_cap_ss_usb {
  48945. + uByte bLength;
  48946. + uByte bDescriptorType;
  48947. + uByte bDevCapabilityType;
  48948. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  48949. + uByte bmAttributes;
  48950. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  48951. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  48952. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  48953. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  48954. + uWord wSpeedsSupported;
  48955. + uByte bFunctionalitySupport;
  48956. + uByte bU1DevExitLat;
  48957. + uWord wU2DevExitLat;
  48958. +} UPACKED usb_dev_cap_ss_usb_t;
  48959. +
  48960. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  48961. +typedef struct usb_dev_cap_container_id {
  48962. + uByte bLength;
  48963. + uByte bDescriptorType;
  48964. + uByte bDevCapabilityType;
  48965. + uByte bReserved;
  48966. + uByte containerID[16];
  48967. +} UPACKED usb_dev_cap_container_id_t;
  48968. +
  48969. +/* Device Capability Type Codes */
  48970. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  48971. +
  48972. +/* Device Capability Descriptor */
  48973. +typedef struct wusb_dev_cap_desc {
  48974. + uByte bLength;
  48975. + uByte bDescriptorType;
  48976. + uByte bDevCapabilityType;
  48977. + uByte caps[1]; /* Variable length */
  48978. +} UPACKED wusb_dev_cap_desc_t;
  48979. +
  48980. +/* Device Capability Descriptor */
  48981. +typedef struct wusb_dev_cap_uwb_desc {
  48982. + uByte bLength;
  48983. + uByte bDescriptorType;
  48984. + uByte bDevCapabilityType;
  48985. + uByte bmAttributes;
  48986. + uWord wPHYRates; /* Bitmap */
  48987. + uByte bmTFITXPowerInfo;
  48988. + uByte bmFFITXPowerInfo;
  48989. + uWord bmBandGroup;
  48990. + uByte bReserved;
  48991. +} UPACKED wusb_dev_cap_uwb_desc_t;
  48992. +
  48993. +/* Wireless USB Endpoint Companion Descriptor */
  48994. +typedef struct wusb_endpoint_companion_desc {
  48995. + uByte bLength;
  48996. + uByte bDescriptorType;
  48997. + uByte bMaxBurst;
  48998. + uByte bMaxSequence;
  48999. + uWord wMaxStreamDelay;
  49000. + uWord wOverTheAirPacketSize;
  49001. + uByte bOverTheAirInterval;
  49002. + uByte bmCompAttributes;
  49003. +} UPACKED wusb_endpoint_companion_desc_t;
  49004. +
  49005. +/* Wireless USB Numeric Association M1 Data Structure */
  49006. +typedef struct wusb_m1_data {
  49007. + uByte version;
  49008. + uWord langId;
  49009. + uByte deviceFriendlyNameLength;
  49010. + uByte sha_256_m3[32];
  49011. + uByte deviceFriendlyName[256];
  49012. +} UPACKED wusb_m1_data_t;
  49013. +
  49014. +typedef struct wusb_m2_data {
  49015. + uByte version;
  49016. + uWord langId;
  49017. + uByte hostFriendlyNameLength;
  49018. + uByte pkh[384];
  49019. + uByte hostFriendlyName[256];
  49020. +} UPACKED wusb_m2_data_t;
  49021. +
  49022. +typedef struct wusb_m3_data {
  49023. + uByte pkd[384];
  49024. + uByte nd;
  49025. +} UPACKED wusb_m3_data_t;
  49026. +
  49027. +typedef struct wusb_m4_data {
  49028. + uDWord _attributeTypeIdAndLength_1;
  49029. + uWord associationTypeId;
  49030. +
  49031. + uDWord _attributeTypeIdAndLength_2;
  49032. + uWord associationSubTypeId;
  49033. +
  49034. + uDWord _attributeTypeIdAndLength_3;
  49035. + uDWord length;
  49036. +
  49037. + uDWord _attributeTypeIdAndLength_4;
  49038. + uDWord associationStatus;
  49039. +
  49040. + uDWord _attributeTypeIdAndLength_5;
  49041. + uByte chid[16];
  49042. +
  49043. + uDWord _attributeTypeIdAndLength_6;
  49044. + uByte cdid[16];
  49045. +
  49046. + uDWord _attributeTypeIdAndLength_7;
  49047. + uByte bandGroups[2];
  49048. +} UPACKED wusb_m4_data_t;
  49049. +
  49050. +#ifdef _MSC_VER
  49051. +#include <poppack.h>
  49052. +#endif
  49053. +
  49054. +#ifdef __cplusplus
  49055. +}
  49056. +#endif
  49057. +
  49058. +#endif /* _USB_H_ */
  49059. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/doc/doxygen.cfg linux-raspberry-pi/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  49060. --- linux-3.12.13/drivers/usb/host/dwc_otg/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  49061. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/doc/doxygen.cfg 2014-03-11 17:51:27.000000000 +0100
  49062. @@ -0,0 +1,224 @@
  49063. +# Doxyfile 1.3.9.1
  49064. +
  49065. +#---------------------------------------------------------------------------
  49066. +# Project related configuration options
  49067. +#---------------------------------------------------------------------------
  49068. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  49069. +PROJECT_NUMBER = v3.00a
  49070. +OUTPUT_DIRECTORY = ./doc/
  49071. +CREATE_SUBDIRS = NO
  49072. +OUTPUT_LANGUAGE = English
  49073. +BRIEF_MEMBER_DESC = YES
  49074. +REPEAT_BRIEF = YES
  49075. +ABBREVIATE_BRIEF = "The $name class" \
  49076. + "The $name widget" \
  49077. + "The $name file" \
  49078. + is \
  49079. + provides \
  49080. + specifies \
  49081. + contains \
  49082. + represents \
  49083. + a \
  49084. + an \
  49085. + the
  49086. +ALWAYS_DETAILED_SEC = NO
  49087. +INLINE_INHERITED_MEMB = NO
  49088. +FULL_PATH_NAMES = NO
  49089. +STRIP_FROM_PATH =
  49090. +STRIP_FROM_INC_PATH =
  49091. +SHORT_NAMES = NO
  49092. +JAVADOC_AUTOBRIEF = YES
  49093. +MULTILINE_CPP_IS_BRIEF = NO
  49094. +INHERIT_DOCS = YES
  49095. +DISTRIBUTE_GROUP_DOC = NO
  49096. +TAB_SIZE = 8
  49097. +ALIASES =
  49098. +OPTIMIZE_OUTPUT_FOR_C = YES
  49099. +OPTIMIZE_OUTPUT_JAVA = NO
  49100. +SUBGROUPING = YES
  49101. +#---------------------------------------------------------------------------
  49102. +# Build related configuration options
  49103. +#---------------------------------------------------------------------------
  49104. +EXTRACT_ALL = NO
  49105. +EXTRACT_PRIVATE = YES
  49106. +EXTRACT_STATIC = YES
  49107. +EXTRACT_LOCAL_CLASSES = YES
  49108. +EXTRACT_LOCAL_METHODS = NO
  49109. +HIDE_UNDOC_MEMBERS = NO
  49110. +HIDE_UNDOC_CLASSES = NO
  49111. +HIDE_FRIEND_COMPOUNDS = NO
  49112. +HIDE_IN_BODY_DOCS = NO
  49113. +INTERNAL_DOCS = NO
  49114. +CASE_SENSE_NAMES = NO
  49115. +HIDE_SCOPE_NAMES = NO
  49116. +SHOW_INCLUDE_FILES = YES
  49117. +INLINE_INFO = YES
  49118. +SORT_MEMBER_DOCS = NO
  49119. +SORT_BRIEF_DOCS = NO
  49120. +SORT_BY_SCOPE_NAME = NO
  49121. +GENERATE_TODOLIST = YES
  49122. +GENERATE_TESTLIST = YES
  49123. +GENERATE_BUGLIST = YES
  49124. +GENERATE_DEPRECATEDLIST= YES
  49125. +ENABLED_SECTIONS =
  49126. +MAX_INITIALIZER_LINES = 30
  49127. +SHOW_USED_FILES = YES
  49128. +SHOW_DIRECTORIES = YES
  49129. +#---------------------------------------------------------------------------
  49130. +# configuration options related to warning and progress messages
  49131. +#---------------------------------------------------------------------------
  49132. +QUIET = YES
  49133. +WARNINGS = YES
  49134. +WARN_IF_UNDOCUMENTED = NO
  49135. +WARN_IF_DOC_ERROR = YES
  49136. +WARN_FORMAT = "$file:$line: $text"
  49137. +WARN_LOGFILE =
  49138. +#---------------------------------------------------------------------------
  49139. +# configuration options related to the input files
  49140. +#---------------------------------------------------------------------------
  49141. +INPUT = .
  49142. +FILE_PATTERNS = *.c \
  49143. + *.h \
  49144. + ./linux/*.c \
  49145. + ./linux/*.h
  49146. +RECURSIVE = NO
  49147. +EXCLUDE = ./test/ \
  49148. + ./dwc_otg/.AppleDouble/
  49149. +EXCLUDE_SYMLINKS = YES
  49150. +EXCLUDE_PATTERNS = *.mod.*
  49151. +EXAMPLE_PATH =
  49152. +EXAMPLE_PATTERNS = *
  49153. +EXAMPLE_RECURSIVE = NO
  49154. +IMAGE_PATH =
  49155. +INPUT_FILTER =
  49156. +FILTER_PATTERNS =
  49157. +FILTER_SOURCE_FILES = NO
  49158. +#---------------------------------------------------------------------------
  49159. +# configuration options related to source browsing
  49160. +#---------------------------------------------------------------------------
  49161. +SOURCE_BROWSER = YES
  49162. +INLINE_SOURCES = NO
  49163. +STRIP_CODE_COMMENTS = YES
  49164. +REFERENCED_BY_RELATION = NO
  49165. +REFERENCES_RELATION = NO
  49166. +VERBATIM_HEADERS = NO
  49167. +#---------------------------------------------------------------------------
  49168. +# configuration options related to the alphabetical class index
  49169. +#---------------------------------------------------------------------------
  49170. +ALPHABETICAL_INDEX = NO
  49171. +COLS_IN_ALPHA_INDEX = 5
  49172. +IGNORE_PREFIX =
  49173. +#---------------------------------------------------------------------------
  49174. +# configuration options related to the HTML output
  49175. +#---------------------------------------------------------------------------
  49176. +GENERATE_HTML = YES
  49177. +HTML_OUTPUT = html
  49178. +HTML_FILE_EXTENSION = .html
  49179. +HTML_HEADER =
  49180. +HTML_FOOTER =
  49181. +HTML_STYLESHEET =
  49182. +HTML_ALIGN_MEMBERS = YES
  49183. +GENERATE_HTMLHELP = NO
  49184. +CHM_FILE =
  49185. +HHC_LOCATION =
  49186. +GENERATE_CHI = NO
  49187. +BINARY_TOC = NO
  49188. +TOC_EXPAND = NO
  49189. +DISABLE_INDEX = NO
  49190. +ENUM_VALUES_PER_LINE = 4
  49191. +GENERATE_TREEVIEW = YES
  49192. +TREEVIEW_WIDTH = 250
  49193. +#---------------------------------------------------------------------------
  49194. +# configuration options related to the LaTeX output
  49195. +#---------------------------------------------------------------------------
  49196. +GENERATE_LATEX = NO
  49197. +LATEX_OUTPUT = latex
  49198. +LATEX_CMD_NAME = latex
  49199. +MAKEINDEX_CMD_NAME = makeindex
  49200. +COMPACT_LATEX = NO
  49201. +PAPER_TYPE = a4wide
  49202. +EXTRA_PACKAGES =
  49203. +LATEX_HEADER =
  49204. +PDF_HYPERLINKS = NO
  49205. +USE_PDFLATEX = NO
  49206. +LATEX_BATCHMODE = NO
  49207. +LATEX_HIDE_INDICES = NO
  49208. +#---------------------------------------------------------------------------
  49209. +# configuration options related to the RTF output
  49210. +#---------------------------------------------------------------------------
  49211. +GENERATE_RTF = NO
  49212. +RTF_OUTPUT = rtf
  49213. +COMPACT_RTF = NO
  49214. +RTF_HYPERLINKS = NO
  49215. +RTF_STYLESHEET_FILE =
  49216. +RTF_EXTENSIONS_FILE =
  49217. +#---------------------------------------------------------------------------
  49218. +# configuration options related to the man page output
  49219. +#---------------------------------------------------------------------------
  49220. +GENERATE_MAN = NO
  49221. +MAN_OUTPUT = man
  49222. +MAN_EXTENSION = .3
  49223. +MAN_LINKS = NO
  49224. +#---------------------------------------------------------------------------
  49225. +# configuration options related to the XML output
  49226. +#---------------------------------------------------------------------------
  49227. +GENERATE_XML = NO
  49228. +XML_OUTPUT = xml
  49229. +XML_SCHEMA =
  49230. +XML_DTD =
  49231. +XML_PROGRAMLISTING = YES
  49232. +#---------------------------------------------------------------------------
  49233. +# configuration options for the AutoGen Definitions output
  49234. +#---------------------------------------------------------------------------
  49235. +GENERATE_AUTOGEN_DEF = NO
  49236. +#---------------------------------------------------------------------------
  49237. +# configuration options related to the Perl module output
  49238. +#---------------------------------------------------------------------------
  49239. +GENERATE_PERLMOD = NO
  49240. +PERLMOD_LATEX = NO
  49241. +PERLMOD_PRETTY = YES
  49242. +PERLMOD_MAKEVAR_PREFIX =
  49243. +#---------------------------------------------------------------------------
  49244. +# Configuration options related to the preprocessor
  49245. +#---------------------------------------------------------------------------
  49246. +ENABLE_PREPROCESSING = YES
  49247. +MACRO_EXPANSION = YES
  49248. +EXPAND_ONLY_PREDEF = YES
  49249. +SEARCH_INCLUDES = YES
  49250. +INCLUDE_PATH =
  49251. +INCLUDE_FILE_PATTERNS =
  49252. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  49253. +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
  49254. +SKIP_FUNCTION_MACROS = NO
  49255. +#---------------------------------------------------------------------------
  49256. +# Configuration::additions related to external references
  49257. +#---------------------------------------------------------------------------
  49258. +TAGFILES =
  49259. +GENERATE_TAGFILE =
  49260. +ALLEXTERNALS = NO
  49261. +EXTERNAL_GROUPS = YES
  49262. +PERL_PATH = /usr/bin/perl
  49263. +#---------------------------------------------------------------------------
  49264. +# Configuration options related to the dot tool
  49265. +#---------------------------------------------------------------------------
  49266. +CLASS_DIAGRAMS = YES
  49267. +HIDE_UNDOC_RELATIONS = YES
  49268. +HAVE_DOT = NO
  49269. +CLASS_GRAPH = YES
  49270. +COLLABORATION_GRAPH = YES
  49271. +UML_LOOK = NO
  49272. +TEMPLATE_RELATIONS = NO
  49273. +INCLUDE_GRAPH = YES
  49274. +INCLUDED_BY_GRAPH = YES
  49275. +CALL_GRAPH = NO
  49276. +GRAPHICAL_HIERARCHY = YES
  49277. +DOT_IMAGE_FORMAT = png
  49278. +DOT_PATH =
  49279. +DOTFILE_DIRS =
  49280. +MAX_DOT_GRAPH_DEPTH = 1000
  49281. +GENERATE_LEGEND = YES
  49282. +DOT_CLEANUP = YES
  49283. +#---------------------------------------------------------------------------
  49284. +# Configuration::additions related to the search engine
  49285. +#---------------------------------------------------------------------------
  49286. +SEARCHENGINE = NO
  49287. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dummy_audio.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dummy_audio.c
  49288. --- linux-3.12.13/drivers/usb/host/dwc_otg/dummy_audio.c 1970-01-01 01:00:00.000000000 +0100
  49289. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dummy_audio.c 2014-03-11 17:51:27.000000000 +0100
  49290. @@ -0,0 +1,1575 @@
  49291. +/*
  49292. + * zero.c -- Gadget Zero, for USB development
  49293. + *
  49294. + * Copyright (C) 2003-2004 David Brownell
  49295. + * All rights reserved.
  49296. + *
  49297. + * Redistribution and use in source and binary forms, with or without
  49298. + * modification, are permitted provided that the following conditions
  49299. + * are met:
  49300. + * 1. Redistributions of source code must retain the above copyright
  49301. + * notice, this list of conditions, and the following disclaimer,
  49302. + * without modification.
  49303. + * 2. Redistributions in binary form must reproduce the above copyright
  49304. + * notice, this list of conditions and the following disclaimer in the
  49305. + * documentation and/or other materials provided with the distribution.
  49306. + * 3. The names of the above-listed copyright holders may not be used
  49307. + * to endorse or promote products derived from this software without
  49308. + * specific prior written permission.
  49309. + *
  49310. + * ALTERNATIVELY, this software may be distributed under the terms of the
  49311. + * GNU General Public License ("GPL") as published by the Free Software
  49312. + * Foundation, either version 2 of that License or (at your option) any
  49313. + * later version.
  49314. + *
  49315. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  49316. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  49317. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  49318. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49319. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  49320. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  49321. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  49322. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  49323. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  49324. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  49325. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49326. + */
  49327. +
  49328. +
  49329. +/*
  49330. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  49331. + * can write a hardware-agnostic gadget driver running inside a USB device.
  49332. + *
  49333. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  49334. + * affect most of the driver.
  49335. + *
  49336. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  49337. + * functional test of your device-side usb stack, or with "usb-skeleton".
  49338. + *
  49339. + * It supports two similar configurations. One sinks whatever the usb host
  49340. + * writes, and in return sources zeroes. The other loops whatever the host
  49341. + * writes back, so the host can read it. Module options include:
  49342. + *
  49343. + * buflen=N default N=4096, buffer size used
  49344. + * qlen=N default N=32, how many buffers in the loopback queue
  49345. + * loopdefault default false, list loopback config first
  49346. + *
  49347. + * Many drivers will only have one configuration, letting them be much
  49348. + * simpler if they also don't support high speed operation (like this
  49349. + * driver does).
  49350. + */
  49351. +
  49352. +#include <linux/config.h>
  49353. +#include <linux/module.h>
  49354. +#include <linux/kernel.h>
  49355. +#include <linux/delay.h>
  49356. +#include <linux/ioport.h>
  49357. +#include <linux/sched.h>
  49358. +#include <linux/slab.h>
  49359. +#include <linux/smp_lock.h>
  49360. +#include <linux/errno.h>
  49361. +#include <linux/init.h>
  49362. +#include <linux/timer.h>
  49363. +#include <linux/list.h>
  49364. +#include <linux/interrupt.h>
  49365. +#include <linux/uts.h>
  49366. +#include <linux/version.h>
  49367. +#include <linux/device.h>
  49368. +#include <linux/moduleparam.h>
  49369. +#include <linux/proc_fs.h>
  49370. +
  49371. +#include <asm/byteorder.h>
  49372. +#include <asm/io.h>
  49373. +#include <asm/irq.h>
  49374. +#include <asm/system.h>
  49375. +#include <asm/unaligned.h>
  49376. +
  49377. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  49378. +# include <linux/usb/ch9.h>
  49379. +#else
  49380. +# include <linux/usb_ch9.h>
  49381. +#endif
  49382. +
  49383. +#include <linux/usb_gadget.h>
  49384. +
  49385. +
  49386. +/*-------------------------------------------------------------------------*/
  49387. +/*-------------------------------------------------------------------------*/
  49388. +
  49389. +
  49390. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  49391. +{
  49392. + int count = 0;
  49393. + u8 c;
  49394. + u16 uchar;
  49395. +
  49396. + /* this insists on correct encodings, though not minimal ones.
  49397. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  49398. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  49399. + */
  49400. + while (len != 0 && (c = (u8) *s++) != 0) {
  49401. + if (unlikely(c & 0x80)) {
  49402. + // 2-byte sequence:
  49403. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  49404. + if ((c & 0xe0) == 0xc0) {
  49405. + uchar = (c & 0x1f) << 6;
  49406. +
  49407. + c = (u8) *s++;
  49408. + if ((c & 0xc0) != 0xc0)
  49409. + goto fail;
  49410. + c &= 0x3f;
  49411. + uchar |= c;
  49412. +
  49413. + // 3-byte sequence (most CJKV characters):
  49414. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  49415. + } else if ((c & 0xf0) == 0xe0) {
  49416. + uchar = (c & 0x0f) << 12;
  49417. +
  49418. + c = (u8) *s++;
  49419. + if ((c & 0xc0) != 0xc0)
  49420. + goto fail;
  49421. + c &= 0x3f;
  49422. + uchar |= c << 6;
  49423. +
  49424. + c = (u8) *s++;
  49425. + if ((c & 0xc0) != 0xc0)
  49426. + goto fail;
  49427. + c &= 0x3f;
  49428. + uchar |= c;
  49429. +
  49430. + /* no bogus surrogates */
  49431. + if (0xd800 <= uchar && uchar <= 0xdfff)
  49432. + goto fail;
  49433. +
  49434. + // 4-byte sequence (surrogate pairs, currently rare):
  49435. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  49436. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  49437. + // (uuuuu = wwww + 1)
  49438. + // FIXME accept the surrogate code points (only)
  49439. +
  49440. + } else
  49441. + goto fail;
  49442. + } else
  49443. + uchar = c;
  49444. + put_unaligned (cpu_to_le16 (uchar), cp++);
  49445. + count++;
  49446. + len--;
  49447. + }
  49448. + return count;
  49449. +fail:
  49450. + return -1;
  49451. +}
  49452. +
  49453. +
  49454. +/**
  49455. + * usb_gadget_get_string - fill out a string descriptor
  49456. + * @table: of c strings encoded using UTF-8
  49457. + * @id: string id, from low byte of wValue in get string descriptor
  49458. + * @buf: at least 256 bytes
  49459. + *
  49460. + * Finds the UTF-8 string matching the ID, and converts it into a
  49461. + * string descriptor in utf16-le.
  49462. + * Returns length of descriptor (always even) or negative errno
  49463. + *
  49464. + * If your driver needs stings in multiple languages, you'll probably
  49465. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  49466. + * using this routine after choosing which set of UTF-8 strings to use.
  49467. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  49468. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  49469. + * characters (which are also widely used in C strings).
  49470. + */
  49471. +int
  49472. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  49473. +{
  49474. + struct usb_string *s;
  49475. + int len;
  49476. +
  49477. + /* descriptor 0 has the language id */
  49478. + if (id == 0) {
  49479. + buf [0] = 4;
  49480. + buf [1] = USB_DT_STRING;
  49481. + buf [2] = (u8) table->language;
  49482. + buf [3] = (u8) (table->language >> 8);
  49483. + return 4;
  49484. + }
  49485. + for (s = table->strings; s && s->s; s++)
  49486. + if (s->id == id)
  49487. + break;
  49488. +
  49489. + /* unrecognized: stall. */
  49490. + if (!s || !s->s)
  49491. + return -EINVAL;
  49492. +
  49493. + /* string descriptors have length, tag, then UTF16-LE text */
  49494. + len = min ((size_t) 126, strlen (s->s));
  49495. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  49496. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  49497. + if (len < 0)
  49498. + return -EINVAL;
  49499. + buf [0] = (len + 1) * 2;
  49500. + buf [1] = USB_DT_STRING;
  49501. + return buf [0];
  49502. +}
  49503. +
  49504. +
  49505. +/*-------------------------------------------------------------------------*/
  49506. +/*-------------------------------------------------------------------------*/
  49507. +
  49508. +
  49509. +/**
  49510. + * usb_descriptor_fillbuf - fill buffer with descriptors
  49511. + * @buf: Buffer to be filled
  49512. + * @buflen: Size of buf
  49513. + * @src: Array of descriptor pointers, terminated by null pointer.
  49514. + *
  49515. + * Copies descriptors into the buffer, returning the length or a
  49516. + * negative error code if they can't all be copied. Useful when
  49517. + * assembling descriptors for an associated set of interfaces used
  49518. + * as part of configuring a composite device; or in other cases where
  49519. + * sets of descriptors need to be marshaled.
  49520. + */
  49521. +int
  49522. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  49523. + const struct usb_descriptor_header **src)
  49524. +{
  49525. + u8 *dest = buf;
  49526. +
  49527. + if (!src)
  49528. + return -EINVAL;
  49529. +
  49530. + /* fill buffer from src[] until null descriptor ptr */
  49531. + for (; 0 != *src; src++) {
  49532. + unsigned len = (*src)->bLength;
  49533. +
  49534. + if (len > buflen)
  49535. + return -EINVAL;
  49536. + memcpy(dest, *src, len);
  49537. + buflen -= len;
  49538. + dest += len;
  49539. + }
  49540. + return dest - (u8 *)buf;
  49541. +}
  49542. +
  49543. +
  49544. +/**
  49545. + * usb_gadget_config_buf - builts a complete configuration descriptor
  49546. + * @config: Header for the descriptor, including characteristics such
  49547. + * as power requirements and number of interfaces.
  49548. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  49549. + * endpoint, etc) defining all functions in this device configuration.
  49550. + * @buf: Buffer for the resulting configuration descriptor.
  49551. + * @length: Length of buffer. If this is not big enough to hold the
  49552. + * entire configuration descriptor, an error code will be returned.
  49553. + *
  49554. + * This copies descriptors into the response buffer, building a descriptor
  49555. + * for that configuration. It returns the buffer length or a negative
  49556. + * status code. The config.wTotalLength field is set to match the length
  49557. + * of the result, but other descriptor fields (including power usage and
  49558. + * interface count) must be set by the caller.
  49559. + *
  49560. + * Gadget drivers could use this when constructing a config descriptor
  49561. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  49562. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  49563. + */
  49564. +int usb_gadget_config_buf(
  49565. + const struct usb_config_descriptor *config,
  49566. + void *buf,
  49567. + unsigned length,
  49568. + const struct usb_descriptor_header **desc
  49569. +)
  49570. +{
  49571. + struct usb_config_descriptor *cp = buf;
  49572. + int len;
  49573. +
  49574. + /* config descriptor first */
  49575. + if (length < USB_DT_CONFIG_SIZE || !desc)
  49576. + return -EINVAL;
  49577. + *cp = *config;
  49578. +
  49579. + /* then interface/endpoint/class/vendor/... */
  49580. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  49581. + length - USB_DT_CONFIG_SIZE, desc);
  49582. + if (len < 0)
  49583. + return len;
  49584. + len += USB_DT_CONFIG_SIZE;
  49585. + if (len > 0xffff)
  49586. + return -EINVAL;
  49587. +
  49588. + /* patch up the config descriptor */
  49589. + cp->bLength = USB_DT_CONFIG_SIZE;
  49590. + cp->bDescriptorType = USB_DT_CONFIG;
  49591. + cp->wTotalLength = cpu_to_le16(len);
  49592. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  49593. + return len;
  49594. +}
  49595. +
  49596. +/*-------------------------------------------------------------------------*/
  49597. +/*-------------------------------------------------------------------------*/
  49598. +
  49599. +
  49600. +#define RBUF_LEN (1024*1024)
  49601. +static int rbuf_start;
  49602. +static int rbuf_len;
  49603. +static __u8 rbuf[RBUF_LEN];
  49604. +
  49605. +/*-------------------------------------------------------------------------*/
  49606. +
  49607. +#define DRIVER_VERSION "St Patrick's Day 2004"
  49608. +
  49609. +static const char shortname [] = "zero";
  49610. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  49611. +
  49612. +static const char source_sink [] = "source and sink data";
  49613. +static const char loopback [] = "loop input to output";
  49614. +
  49615. +/*-------------------------------------------------------------------------*/
  49616. +
  49617. +/*
  49618. + * driver assumes self-powered hardware, and
  49619. + * has no way for users to trigger remote wakeup.
  49620. + *
  49621. + * this version autoconfigures as much as possible,
  49622. + * which is reasonable for most "bulk-only" drivers.
  49623. + */
  49624. +static const char *EP_IN_NAME; /* source */
  49625. +static const char *EP_OUT_NAME; /* sink */
  49626. +
  49627. +/*-------------------------------------------------------------------------*/
  49628. +
  49629. +/* big enough to hold our biggest descriptor */
  49630. +#define USB_BUFSIZ 512
  49631. +
  49632. +struct zero_dev {
  49633. + spinlock_t lock;
  49634. + struct usb_gadget *gadget;
  49635. + struct usb_request *req; /* for control responses */
  49636. +
  49637. + /* when configured, we have one of two configs:
  49638. + * - source data (in to host) and sink it (out from host)
  49639. + * - or loop it back (out from host back in to host)
  49640. + */
  49641. + u8 config;
  49642. + struct usb_ep *in_ep, *out_ep;
  49643. +
  49644. + /* autoresume timer */
  49645. + struct timer_list resume;
  49646. +};
  49647. +
  49648. +#define xprintk(d,level,fmt,args...) \
  49649. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  49650. +
  49651. +#ifdef DEBUG
  49652. +#define DBG(dev,fmt,args...) \
  49653. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  49654. +#else
  49655. +#define DBG(dev,fmt,args...) \
  49656. + do { } while (0)
  49657. +#endif /* DEBUG */
  49658. +
  49659. +#ifdef VERBOSE
  49660. +#define VDBG DBG
  49661. +#else
  49662. +#define VDBG(dev,fmt,args...) \
  49663. + do { } while (0)
  49664. +#endif /* VERBOSE */
  49665. +
  49666. +#define ERROR(dev,fmt,args...) \
  49667. + xprintk(dev , KERN_ERR , fmt , ## args)
  49668. +#define WARN(dev,fmt,args...) \
  49669. + xprintk(dev , KERN_WARNING , fmt , ## args)
  49670. +#define INFO(dev,fmt,args...) \
  49671. + xprintk(dev , KERN_INFO , fmt , ## args)
  49672. +
  49673. +/*-------------------------------------------------------------------------*/
  49674. +
  49675. +static unsigned buflen = 4096;
  49676. +static unsigned qlen = 32;
  49677. +static unsigned pattern = 0;
  49678. +
  49679. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  49680. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  49681. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  49682. +
  49683. +/*
  49684. + * if it's nonzero, autoresume says how many seconds to wait
  49685. + * before trying to wake up the host after suspend.
  49686. + */
  49687. +static unsigned autoresume = 0;
  49688. +module_param (autoresume, uint, 0);
  49689. +
  49690. +/*
  49691. + * Normally the "loopback" configuration is second (index 1) so
  49692. + * it's not the default. Here's where to change that order, to
  49693. + * work better with hosts where config changes are problematic.
  49694. + * Or controllers (like superh) that only support one config.
  49695. + */
  49696. +static int loopdefault = 0;
  49697. +
  49698. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  49699. +
  49700. +/*-------------------------------------------------------------------------*/
  49701. +
  49702. +/* Thanks to NetChip Technologies for donating this product ID.
  49703. + *
  49704. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  49705. + * Instead: allocate your own, using normal USB-IF procedures.
  49706. + */
  49707. +#ifndef CONFIG_USB_ZERO_HNPTEST
  49708. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  49709. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  49710. +#else
  49711. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  49712. +#define DRIVER_PRODUCT_NUM 0xbadd
  49713. +#endif
  49714. +
  49715. +/*-------------------------------------------------------------------------*/
  49716. +
  49717. +/*
  49718. + * DESCRIPTORS ... most are static, but strings and (full)
  49719. + * configuration descriptors are built on demand.
  49720. + */
  49721. +
  49722. +/*
  49723. +#define STRING_MANUFACTURER 25
  49724. +#define STRING_PRODUCT 42
  49725. +#define STRING_SERIAL 101
  49726. +*/
  49727. +#define STRING_MANUFACTURER 1
  49728. +#define STRING_PRODUCT 2
  49729. +#define STRING_SERIAL 3
  49730. +
  49731. +#define STRING_SOURCE_SINK 250
  49732. +#define STRING_LOOPBACK 251
  49733. +
  49734. +/*
  49735. + * This device advertises two configurations; these numbers work
  49736. + * on a pxa250 as well as more flexible hardware.
  49737. + */
  49738. +#define CONFIG_SOURCE_SINK 3
  49739. +#define CONFIG_LOOPBACK 2
  49740. +
  49741. +/*
  49742. +static struct usb_device_descriptor
  49743. +device_desc = {
  49744. + .bLength = sizeof device_desc,
  49745. + .bDescriptorType = USB_DT_DEVICE,
  49746. +
  49747. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  49748. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  49749. +
  49750. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  49751. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  49752. + .iManufacturer = STRING_MANUFACTURER,
  49753. + .iProduct = STRING_PRODUCT,
  49754. + .iSerialNumber = STRING_SERIAL,
  49755. + .bNumConfigurations = 2,
  49756. +};
  49757. +*/
  49758. +static struct usb_device_descriptor
  49759. +device_desc = {
  49760. + .bLength = sizeof device_desc,
  49761. + .bDescriptorType = USB_DT_DEVICE,
  49762. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  49763. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  49764. + .bDeviceSubClass = 0,
  49765. + .bDeviceProtocol = 0,
  49766. + .bMaxPacketSize0 = 64,
  49767. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  49768. + .idVendor = __constant_cpu_to_le16 (0x0499),
  49769. + .idProduct = __constant_cpu_to_le16 (0x3002),
  49770. + .iManufacturer = STRING_MANUFACTURER,
  49771. + .iProduct = STRING_PRODUCT,
  49772. + .iSerialNumber = STRING_SERIAL,
  49773. + .bNumConfigurations = 1,
  49774. +};
  49775. +
  49776. +static struct usb_config_descriptor
  49777. +z_config = {
  49778. + .bLength = sizeof z_config,
  49779. + .bDescriptorType = USB_DT_CONFIG,
  49780. +
  49781. + /* compute wTotalLength on the fly */
  49782. + .bNumInterfaces = 2,
  49783. + .bConfigurationValue = 1,
  49784. + .iConfiguration = 0,
  49785. + .bmAttributes = 0x40,
  49786. + .bMaxPower = 0, /* self-powered */
  49787. +};
  49788. +
  49789. +
  49790. +static struct usb_otg_descriptor
  49791. +otg_descriptor = {
  49792. + .bLength = sizeof otg_descriptor,
  49793. + .bDescriptorType = USB_DT_OTG,
  49794. +
  49795. + .bmAttributes = USB_OTG_SRP,
  49796. +};
  49797. +
  49798. +/* one interface in each configuration */
  49799. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  49800. +
  49801. +/*
  49802. + * usb 2.0 devices need to expose both high speed and full speed
  49803. + * descriptors, unless they only run at full speed.
  49804. + *
  49805. + * that means alternate endpoint descriptors (bigger packets)
  49806. + * and a "device qualifier" ... plus more construction options
  49807. + * for the config descriptor.
  49808. + */
  49809. +
  49810. +static struct usb_qualifier_descriptor
  49811. +dev_qualifier = {
  49812. + .bLength = sizeof dev_qualifier,
  49813. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  49814. +
  49815. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  49816. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  49817. +
  49818. + .bNumConfigurations = 2,
  49819. +};
  49820. +
  49821. +
  49822. +struct usb_cs_as_general_descriptor {
  49823. + __u8 bLength;
  49824. + __u8 bDescriptorType;
  49825. +
  49826. + __u8 bDescriptorSubType;
  49827. + __u8 bTerminalLink;
  49828. + __u8 bDelay;
  49829. + __u16 wFormatTag;
  49830. +} __attribute__ ((packed));
  49831. +
  49832. +struct usb_cs_as_format_descriptor {
  49833. + __u8 bLength;
  49834. + __u8 bDescriptorType;
  49835. +
  49836. + __u8 bDescriptorSubType;
  49837. + __u8 bFormatType;
  49838. + __u8 bNrChannels;
  49839. + __u8 bSubframeSize;
  49840. + __u8 bBitResolution;
  49841. + __u8 bSamfreqType;
  49842. + __u8 tLowerSamFreq[3];
  49843. + __u8 tUpperSamFreq[3];
  49844. +} __attribute__ ((packed));
  49845. +
  49846. +static const struct usb_interface_descriptor
  49847. +z_audio_control_if_desc = {
  49848. + .bLength = sizeof z_audio_control_if_desc,
  49849. + .bDescriptorType = USB_DT_INTERFACE,
  49850. + .bInterfaceNumber = 0,
  49851. + .bAlternateSetting = 0,
  49852. + .bNumEndpoints = 0,
  49853. + .bInterfaceClass = USB_CLASS_AUDIO,
  49854. + .bInterfaceSubClass = 0x1,
  49855. + .bInterfaceProtocol = 0,
  49856. + .iInterface = 0,
  49857. +};
  49858. +
  49859. +static const struct usb_interface_descriptor
  49860. +z_audio_if_desc = {
  49861. + .bLength = sizeof z_audio_if_desc,
  49862. + .bDescriptorType = USB_DT_INTERFACE,
  49863. + .bInterfaceNumber = 1,
  49864. + .bAlternateSetting = 0,
  49865. + .bNumEndpoints = 0,
  49866. + .bInterfaceClass = USB_CLASS_AUDIO,
  49867. + .bInterfaceSubClass = 0x2,
  49868. + .bInterfaceProtocol = 0,
  49869. + .iInterface = 0,
  49870. +};
  49871. +
  49872. +static const struct usb_interface_descriptor
  49873. +z_audio_if_desc2 = {
  49874. + .bLength = sizeof z_audio_if_desc,
  49875. + .bDescriptorType = USB_DT_INTERFACE,
  49876. + .bInterfaceNumber = 1,
  49877. + .bAlternateSetting = 1,
  49878. + .bNumEndpoints = 1,
  49879. + .bInterfaceClass = USB_CLASS_AUDIO,
  49880. + .bInterfaceSubClass = 0x2,
  49881. + .bInterfaceProtocol = 0,
  49882. + .iInterface = 0,
  49883. +};
  49884. +
  49885. +static const struct usb_cs_as_general_descriptor
  49886. +z_audio_cs_as_if_desc = {
  49887. + .bLength = 7,
  49888. + .bDescriptorType = 0x24,
  49889. +
  49890. + .bDescriptorSubType = 0x01,
  49891. + .bTerminalLink = 0x01,
  49892. + .bDelay = 0x0,
  49893. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  49894. +};
  49895. +
  49896. +
  49897. +static const struct usb_cs_as_format_descriptor
  49898. +z_audio_cs_as_format_desc = {
  49899. + .bLength = 0xe,
  49900. + .bDescriptorType = 0x24,
  49901. +
  49902. + .bDescriptorSubType = 2,
  49903. + .bFormatType = 1,
  49904. + .bNrChannels = 1,
  49905. + .bSubframeSize = 1,
  49906. + .bBitResolution = 8,
  49907. + .bSamfreqType = 0,
  49908. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  49909. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  49910. +};
  49911. +
  49912. +static const struct usb_endpoint_descriptor
  49913. +z_iso_ep = {
  49914. + .bLength = 0x09,
  49915. + .bDescriptorType = 0x05,
  49916. + .bEndpointAddress = 0x04,
  49917. + .bmAttributes = 0x09,
  49918. + .wMaxPacketSize = 0x0038,
  49919. + .bInterval = 0x01,
  49920. + .bRefresh = 0x00,
  49921. + .bSynchAddress = 0x00,
  49922. +};
  49923. +
  49924. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49925. +
  49926. +// 9 bytes
  49927. +static char z_ac_interface_header_desc[] =
  49928. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  49929. +
  49930. +// 12 bytes
  49931. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  49932. + 0x03, 0x00, 0x00, 0x00};
  49933. +// 13 bytes
  49934. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  49935. + 0x02, 0x00, 0x02, 0x00, 0x00};
  49936. +// 9 bytes
  49937. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  49938. + 0x00};
  49939. +
  49940. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  49941. + 0x00};
  49942. +
  49943. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49944. +
  49945. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  49946. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49947. +
  49948. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  49949. + 0x00};
  49950. +
  49951. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49952. +
  49953. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  49954. + 0x00};
  49955. +
  49956. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49957. +
  49958. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  49959. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49960. +
  49961. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  49962. + 0x00};
  49963. +
  49964. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49965. +
  49966. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  49967. + 0x00};
  49968. +
  49969. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49970. +
  49971. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  49972. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49973. +
  49974. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  49975. + 0x00};
  49976. +
  49977. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49978. +
  49979. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  49980. + 0x00};
  49981. +
  49982. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49983. +
  49984. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  49985. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49986. +
  49987. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  49988. + 0x00};
  49989. +
  49990. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49991. +
  49992. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  49993. + 0x00};
  49994. +
  49995. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49996. +
  49997. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  49998. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49999. +
  50000. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  50001. + 0x00};
  50002. +
  50003. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  50004. +
  50005. +
  50006. +
  50007. +static const struct usb_descriptor_header *z_function [] = {
  50008. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  50009. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  50010. + (struct usb_descriptor_header *) &z_0,
  50011. + (struct usb_descriptor_header *) &z_1,
  50012. + (struct usb_descriptor_header *) &z_2,
  50013. + (struct usb_descriptor_header *) &z_audio_if_desc,
  50014. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  50015. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  50016. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  50017. + (struct usb_descriptor_header *) &z_iso_ep,
  50018. + (struct usb_descriptor_header *) &z_iso_ep2,
  50019. + (struct usb_descriptor_header *) &za_0,
  50020. + (struct usb_descriptor_header *) &za_1,
  50021. + (struct usb_descriptor_header *) &za_2,
  50022. + (struct usb_descriptor_header *) &za_3,
  50023. + (struct usb_descriptor_header *) &za_4,
  50024. + (struct usb_descriptor_header *) &za_5,
  50025. + (struct usb_descriptor_header *) &za_6,
  50026. + (struct usb_descriptor_header *) &za_7,
  50027. + (struct usb_descriptor_header *) &za_8,
  50028. + (struct usb_descriptor_header *) &za_9,
  50029. + (struct usb_descriptor_header *) &za_10,
  50030. + (struct usb_descriptor_header *) &za_11,
  50031. + (struct usb_descriptor_header *) &za_12,
  50032. + (struct usb_descriptor_header *) &za_13,
  50033. + (struct usb_descriptor_header *) &za_14,
  50034. + (struct usb_descriptor_header *) &za_15,
  50035. + (struct usb_descriptor_header *) &za_16,
  50036. + (struct usb_descriptor_header *) &za_17,
  50037. + (struct usb_descriptor_header *) &za_18,
  50038. + (struct usb_descriptor_header *) &za_19,
  50039. + (struct usb_descriptor_header *) &za_20,
  50040. + (struct usb_descriptor_header *) &za_21,
  50041. + (struct usb_descriptor_header *) &za_22,
  50042. + (struct usb_descriptor_header *) &za_23,
  50043. + (struct usb_descriptor_header *) &za_24,
  50044. + NULL,
  50045. +};
  50046. +
  50047. +/* maxpacket and other transfer characteristics vary by speed. */
  50048. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  50049. +
  50050. +#else
  50051. +
  50052. +/* if there's no high speed support, maxpacket doesn't change. */
  50053. +#define ep_desc(g,hs,fs) fs
  50054. +
  50055. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  50056. +
  50057. +static char manufacturer [40];
  50058. +//static char serial [40];
  50059. +static char serial [] = "Ser 00 em";
  50060. +
  50061. +/* static strings, in UTF-8 */
  50062. +static struct usb_string strings [] = {
  50063. + { STRING_MANUFACTURER, manufacturer, },
  50064. + { STRING_PRODUCT, longname, },
  50065. + { STRING_SERIAL, serial, },
  50066. + { STRING_LOOPBACK, loopback, },
  50067. + { STRING_SOURCE_SINK, source_sink, },
  50068. + { } /* end of list */
  50069. +};
  50070. +
  50071. +static struct usb_gadget_strings stringtab = {
  50072. + .language = 0x0409, /* en-us */
  50073. + .strings = strings,
  50074. +};
  50075. +
  50076. +/*
  50077. + * config descriptors are also handcrafted. these must agree with code
  50078. + * that sets configurations, and with code managing interfaces and their
  50079. + * altsettings. other complexity may come from:
  50080. + *
  50081. + * - high speed support, including "other speed config" rules
  50082. + * - multiple configurations
  50083. + * - interfaces with alternate settings
  50084. + * - embedded class or vendor-specific descriptors
  50085. + *
  50086. + * this handles high speed, and has a second config that could as easily
  50087. + * have been an alternate interface setting (on most hardware).
  50088. + *
  50089. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  50090. + * should include an altsetting to test interrupt transfers, including
  50091. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  50092. + * device?)
  50093. + */
  50094. +static int
  50095. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  50096. +{
  50097. + int len;
  50098. + const struct usb_descriptor_header **function;
  50099. +
  50100. + function = z_function;
  50101. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  50102. + if (len < 0)
  50103. + return len;
  50104. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  50105. + return len;
  50106. +}
  50107. +
  50108. +/*-------------------------------------------------------------------------*/
  50109. +
  50110. +static struct usb_request *
  50111. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  50112. +{
  50113. + struct usb_request *req;
  50114. +
  50115. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  50116. + if (req) {
  50117. + req->length = length;
  50118. + req->buf = usb_ep_alloc_buffer (ep, length,
  50119. + &req->dma, GFP_ATOMIC);
  50120. + if (!req->buf) {
  50121. + usb_ep_free_request (ep, req);
  50122. + req = NULL;
  50123. + }
  50124. + }
  50125. + return req;
  50126. +}
  50127. +
  50128. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  50129. +{
  50130. + if (req->buf)
  50131. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  50132. + usb_ep_free_request (ep, req);
  50133. +}
  50134. +
  50135. +/*-------------------------------------------------------------------------*/
  50136. +
  50137. +/* optionally require specific source/sink data patterns */
  50138. +
  50139. +static int
  50140. +check_read_data (
  50141. + struct zero_dev *dev,
  50142. + struct usb_ep *ep,
  50143. + struct usb_request *req
  50144. +)
  50145. +{
  50146. + unsigned i;
  50147. + u8 *buf = req->buf;
  50148. +
  50149. + for (i = 0; i < req->actual; i++, buf++) {
  50150. + switch (pattern) {
  50151. + /* all-zeroes has no synchronization issues */
  50152. + case 0:
  50153. + if (*buf == 0)
  50154. + continue;
  50155. + break;
  50156. + /* mod63 stays in sync with short-terminated transfers,
  50157. + * or otherwise when host and gadget agree on how large
  50158. + * each usb transfer request should be. resync is done
  50159. + * with set_interface or set_config.
  50160. + */
  50161. + case 1:
  50162. + if (*buf == (u8)(i % 63))
  50163. + continue;
  50164. + break;
  50165. + }
  50166. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  50167. + usb_ep_set_halt (ep);
  50168. + return -EINVAL;
  50169. + }
  50170. + return 0;
  50171. +}
  50172. +
  50173. +/*-------------------------------------------------------------------------*/
  50174. +
  50175. +static void zero_reset_config (struct zero_dev *dev)
  50176. +{
  50177. + if (dev->config == 0)
  50178. + return;
  50179. +
  50180. + DBG (dev, "reset config\n");
  50181. +
  50182. + /* just disable endpoints, forcing completion of pending i/o.
  50183. + * all our completion handlers free their requests in this case.
  50184. + */
  50185. + if (dev->in_ep) {
  50186. + usb_ep_disable (dev->in_ep);
  50187. + dev->in_ep = NULL;
  50188. + }
  50189. + if (dev->out_ep) {
  50190. + usb_ep_disable (dev->out_ep);
  50191. + dev->out_ep = NULL;
  50192. + }
  50193. + dev->config = 0;
  50194. + del_timer (&dev->resume);
  50195. +}
  50196. +
  50197. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  50198. +
  50199. +static void
  50200. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  50201. +{
  50202. + struct zero_dev *dev = ep->driver_data;
  50203. + int status = req->status;
  50204. + int i, j;
  50205. +
  50206. + switch (status) {
  50207. +
  50208. + case 0: /* normal completion? */
  50209. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  50210. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  50211. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  50212. + rbuf[j] = ((__u8*)req->buf)[i];
  50213. + j++;
  50214. + if (j >= RBUF_LEN) j=0;
  50215. + }
  50216. + rbuf_start = j;
  50217. + //printk ("\n\n");
  50218. +
  50219. + if (rbuf_len < RBUF_LEN) {
  50220. + rbuf_len += req->actual;
  50221. + if (rbuf_len > RBUF_LEN) {
  50222. + rbuf_len = RBUF_LEN;
  50223. + }
  50224. + }
  50225. +
  50226. + break;
  50227. +
  50228. + /* this endpoint is normally active while we're configured */
  50229. + case -ECONNABORTED: /* hardware forced ep reset */
  50230. + case -ECONNRESET: /* request dequeued */
  50231. + case -ESHUTDOWN: /* disconnect from host */
  50232. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  50233. + req->actual, req->length);
  50234. + if (ep == dev->out_ep)
  50235. + check_read_data (dev, ep, req);
  50236. + free_ep_req (ep, req);
  50237. + return;
  50238. +
  50239. + case -EOVERFLOW: /* buffer overrun on read means that
  50240. + * we didn't provide a big enough
  50241. + * buffer.
  50242. + */
  50243. + default:
  50244. +#if 1
  50245. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  50246. + status, req->actual, req->length);
  50247. +#endif
  50248. + case -EREMOTEIO: /* short read */
  50249. + break;
  50250. + }
  50251. +
  50252. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  50253. + if (status) {
  50254. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  50255. + ep->name, req->length, status);
  50256. + usb_ep_set_halt (ep);
  50257. + /* FIXME recover later ... somehow */
  50258. + }
  50259. +}
  50260. +
  50261. +static struct usb_request *
  50262. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  50263. +{
  50264. + struct usb_request *req;
  50265. + int status;
  50266. +
  50267. + req = alloc_ep_req (ep, 512);
  50268. + if (!req)
  50269. + return NULL;
  50270. +
  50271. + req->complete = zero_isoc_complete;
  50272. +
  50273. + status = usb_ep_queue (ep, req, gfp_flags);
  50274. + if (status) {
  50275. + struct zero_dev *dev = ep->driver_data;
  50276. +
  50277. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  50278. + free_ep_req (ep, req);
  50279. + req = NULL;
  50280. + }
  50281. +
  50282. + return req;
  50283. +}
  50284. +
  50285. +/* change our operational config. this code must agree with the code
  50286. + * that returns config descriptors, and altsetting code.
  50287. + *
  50288. + * it's also responsible for power management interactions. some
  50289. + * configurations might not work with our current power sources.
  50290. + *
  50291. + * note that some device controller hardware will constrain what this
  50292. + * code can do, perhaps by disallowing more than one configuration or
  50293. + * by limiting configuration choices (like the pxa2xx).
  50294. + */
  50295. +static int
  50296. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  50297. +{
  50298. + int result = 0;
  50299. + struct usb_gadget *gadget = dev->gadget;
  50300. + const struct usb_endpoint_descriptor *d;
  50301. + struct usb_ep *ep;
  50302. +
  50303. + if (number == dev->config)
  50304. + return 0;
  50305. +
  50306. + zero_reset_config (dev);
  50307. +
  50308. + gadget_for_each_ep (ep, gadget) {
  50309. +
  50310. + if (strcmp (ep->name, "ep4") == 0) {
  50311. +
  50312. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  50313. + result = usb_ep_enable (ep, d);
  50314. +
  50315. + if (result == 0) {
  50316. + ep->driver_data = dev;
  50317. + dev->in_ep = ep;
  50318. +
  50319. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  50320. +
  50321. + dev->in_ep = ep;
  50322. + continue;
  50323. + }
  50324. +
  50325. + usb_ep_disable (ep);
  50326. + result = -EIO;
  50327. + }
  50328. + }
  50329. +
  50330. + }
  50331. +
  50332. + dev->config = number;
  50333. + return result;
  50334. +}
  50335. +
  50336. +/*-------------------------------------------------------------------------*/
  50337. +
  50338. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  50339. +{
  50340. + if (req->status || req->actual != req->length)
  50341. + DBG ((struct zero_dev *) ep->driver_data,
  50342. + "setup complete --> %d, %d/%d\n",
  50343. + req->status, req->actual, req->length);
  50344. +}
  50345. +
  50346. +/*
  50347. + * The setup() callback implements all the ep0 functionality that's
  50348. + * not handled lower down, in hardware or the hardware driver (like
  50349. + * device and endpoint feature flags, and their status). It's all
  50350. + * housekeeping for the gadget function we're implementing. Most of
  50351. + * the work is in config-specific setup.
  50352. + */
  50353. +static int
  50354. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  50355. +{
  50356. + struct zero_dev *dev = get_gadget_data (gadget);
  50357. + struct usb_request *req = dev->req;
  50358. + int value = -EOPNOTSUPP;
  50359. +
  50360. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  50361. + * but config change events will reconfigure hardware.
  50362. + */
  50363. + req->zero = 0;
  50364. + switch (ctrl->bRequest) {
  50365. +
  50366. + case USB_REQ_GET_DESCRIPTOR:
  50367. +
  50368. + switch (ctrl->wValue >> 8) {
  50369. +
  50370. + case USB_DT_DEVICE:
  50371. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  50372. + memcpy (req->buf, &device_desc, value);
  50373. + break;
  50374. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50375. + case USB_DT_DEVICE_QUALIFIER:
  50376. + if (!gadget->is_dualspeed)
  50377. + break;
  50378. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  50379. + memcpy (req->buf, &dev_qualifier, value);
  50380. + break;
  50381. +
  50382. + case USB_DT_OTHER_SPEED_CONFIG:
  50383. + if (!gadget->is_dualspeed)
  50384. + break;
  50385. + // FALLTHROUGH
  50386. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  50387. + case USB_DT_CONFIG:
  50388. + value = config_buf (gadget, req->buf,
  50389. + ctrl->wValue >> 8,
  50390. + ctrl->wValue & 0xff);
  50391. + if (value >= 0)
  50392. + value = min (ctrl->wLength, (u16) value);
  50393. + break;
  50394. +
  50395. + case USB_DT_STRING:
  50396. + /* wIndex == language code.
  50397. + * this driver only handles one language, you can
  50398. + * add string tables for other languages, using
  50399. + * any UTF-8 characters
  50400. + */
  50401. + value = usb_gadget_get_string (&stringtab,
  50402. + ctrl->wValue & 0xff, req->buf);
  50403. + if (value >= 0) {
  50404. + value = min (ctrl->wLength, (u16) value);
  50405. + }
  50406. + break;
  50407. + }
  50408. + break;
  50409. +
  50410. + /* currently two configs, two speeds */
  50411. + case USB_REQ_SET_CONFIGURATION:
  50412. + if (ctrl->bRequestType != 0)
  50413. + goto unknown;
  50414. +
  50415. + spin_lock (&dev->lock);
  50416. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  50417. + spin_unlock (&dev->lock);
  50418. + break;
  50419. + case USB_REQ_GET_CONFIGURATION:
  50420. + if (ctrl->bRequestType != USB_DIR_IN)
  50421. + goto unknown;
  50422. + *(u8 *)req->buf = dev->config;
  50423. + value = min (ctrl->wLength, (u16) 1);
  50424. + break;
  50425. +
  50426. + /* until we add altsetting support, or other interfaces,
  50427. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  50428. + * and already killed pending endpoint I/O.
  50429. + */
  50430. + case USB_REQ_SET_INTERFACE:
  50431. +
  50432. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  50433. + goto unknown;
  50434. + spin_lock (&dev->lock);
  50435. + if (dev->config) {
  50436. + u8 config = dev->config;
  50437. +
  50438. + /* resets interface configuration, forgets about
  50439. + * previous transaction state (queued bufs, etc)
  50440. + * and re-inits endpoint state (toggle etc)
  50441. + * no response queued, just zero status == success.
  50442. + * if we had more than one interface we couldn't
  50443. + * use this "reset the config" shortcut.
  50444. + */
  50445. + zero_reset_config (dev);
  50446. + zero_set_config (dev, config, GFP_ATOMIC);
  50447. + value = 0;
  50448. + }
  50449. + spin_unlock (&dev->lock);
  50450. + break;
  50451. + case USB_REQ_GET_INTERFACE:
  50452. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  50453. + value = ctrl->wLength;
  50454. + break;
  50455. + }
  50456. + else {
  50457. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  50458. + goto unknown;
  50459. + if (!dev->config)
  50460. + break;
  50461. + if (ctrl->wIndex != 0) {
  50462. + value = -EDOM;
  50463. + break;
  50464. + }
  50465. + *(u8 *)req->buf = 0;
  50466. + value = min (ctrl->wLength, (u16) 1);
  50467. + }
  50468. + break;
  50469. +
  50470. + /*
  50471. + * These are the same vendor-specific requests supported by
  50472. + * Intel's USB 2.0 compliance test devices. We exceed that
  50473. + * device spec by allowing multiple-packet requests.
  50474. + */
  50475. + case 0x5b: /* control WRITE test -- fill the buffer */
  50476. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  50477. + goto unknown;
  50478. + if (ctrl->wValue || ctrl->wIndex)
  50479. + break;
  50480. + /* just read that many bytes into the buffer */
  50481. + if (ctrl->wLength > USB_BUFSIZ)
  50482. + break;
  50483. + value = ctrl->wLength;
  50484. + break;
  50485. + case 0x5c: /* control READ test -- return the buffer */
  50486. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  50487. + goto unknown;
  50488. + if (ctrl->wValue || ctrl->wIndex)
  50489. + break;
  50490. + /* expect those bytes are still in the buffer; send back */
  50491. + if (ctrl->wLength > USB_BUFSIZ
  50492. + || ctrl->wLength != req->length)
  50493. + break;
  50494. + value = ctrl->wLength;
  50495. + break;
  50496. +
  50497. + case 0x01: // SET_CUR
  50498. + case 0x02:
  50499. + case 0x03:
  50500. + case 0x04:
  50501. + case 0x05:
  50502. + value = ctrl->wLength;
  50503. + break;
  50504. + case 0x81:
  50505. + switch (ctrl->wValue) {
  50506. + case 0x0201:
  50507. + case 0x0202:
  50508. + ((u8*)req->buf)[0] = 0x00;
  50509. + ((u8*)req->buf)[1] = 0xe3;
  50510. + break;
  50511. + case 0x0300:
  50512. + case 0x0500:
  50513. + ((u8*)req->buf)[0] = 0x00;
  50514. + break;
  50515. + }
  50516. + //((u8*)req->buf)[0] = 0x81;
  50517. + //((u8*)req->buf)[1] = 0x81;
  50518. + value = ctrl->wLength;
  50519. + break;
  50520. + case 0x82:
  50521. + switch (ctrl->wValue) {
  50522. + case 0x0201:
  50523. + case 0x0202:
  50524. + ((u8*)req->buf)[0] = 0x00;
  50525. + ((u8*)req->buf)[1] = 0xc3;
  50526. + break;
  50527. + case 0x0300:
  50528. + case 0x0500:
  50529. + ((u8*)req->buf)[0] = 0x00;
  50530. + break;
  50531. + }
  50532. + //((u8*)req->buf)[0] = 0x82;
  50533. + //((u8*)req->buf)[1] = 0x82;
  50534. + value = ctrl->wLength;
  50535. + break;
  50536. + case 0x83:
  50537. + switch (ctrl->wValue) {
  50538. + case 0x0201:
  50539. + case 0x0202:
  50540. + ((u8*)req->buf)[0] = 0x00;
  50541. + ((u8*)req->buf)[1] = 0x00;
  50542. + break;
  50543. + case 0x0300:
  50544. + ((u8*)req->buf)[0] = 0x60;
  50545. + break;
  50546. + case 0x0500:
  50547. + ((u8*)req->buf)[0] = 0x18;
  50548. + break;
  50549. + }
  50550. + //((u8*)req->buf)[0] = 0x83;
  50551. + //((u8*)req->buf)[1] = 0x83;
  50552. + value = ctrl->wLength;
  50553. + break;
  50554. + case 0x84:
  50555. + switch (ctrl->wValue) {
  50556. + case 0x0201:
  50557. + case 0x0202:
  50558. + ((u8*)req->buf)[0] = 0x00;
  50559. + ((u8*)req->buf)[1] = 0x01;
  50560. + break;
  50561. + case 0x0300:
  50562. + case 0x0500:
  50563. + ((u8*)req->buf)[0] = 0x08;
  50564. + break;
  50565. + }
  50566. + //((u8*)req->buf)[0] = 0x84;
  50567. + //((u8*)req->buf)[1] = 0x84;
  50568. + value = ctrl->wLength;
  50569. + break;
  50570. + case 0x85:
  50571. + ((u8*)req->buf)[0] = 0x85;
  50572. + ((u8*)req->buf)[1] = 0x85;
  50573. + value = ctrl->wLength;
  50574. + break;
  50575. +
  50576. +
  50577. + default:
  50578. +unknown:
  50579. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  50580. + ctrl->bRequestType, ctrl->bRequest,
  50581. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  50582. + }
  50583. +
  50584. + /* respond with data transfer before status phase? */
  50585. + if (value >= 0) {
  50586. + req->length = value;
  50587. + req->zero = value < ctrl->wLength
  50588. + && (value % gadget->ep0->maxpacket) == 0;
  50589. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  50590. + if (value < 0) {
  50591. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  50592. + req->status = 0;
  50593. + zero_setup_complete (gadget->ep0, req);
  50594. + }
  50595. + }
  50596. +
  50597. + /* device either stalls (value < 0) or reports success */
  50598. + return value;
  50599. +}
  50600. +
  50601. +static void
  50602. +zero_disconnect (struct usb_gadget *gadget)
  50603. +{
  50604. + struct zero_dev *dev = get_gadget_data (gadget);
  50605. + unsigned long flags;
  50606. +
  50607. + spin_lock_irqsave (&dev->lock, flags);
  50608. + zero_reset_config (dev);
  50609. +
  50610. + /* a more significant application might have some non-usb
  50611. + * activities to quiesce here, saving resources like power
  50612. + * or pushing the notification up a network stack.
  50613. + */
  50614. + spin_unlock_irqrestore (&dev->lock, flags);
  50615. +
  50616. + /* next we may get setup() calls to enumerate new connections;
  50617. + * or an unbind() during shutdown (including removing module).
  50618. + */
  50619. +}
  50620. +
  50621. +static void
  50622. +zero_autoresume (unsigned long _dev)
  50623. +{
  50624. + struct zero_dev *dev = (struct zero_dev *) _dev;
  50625. + int status;
  50626. +
  50627. + /* normally the host would be woken up for something
  50628. + * more significant than just a timer firing...
  50629. + */
  50630. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  50631. + status = usb_gadget_wakeup (dev->gadget);
  50632. + DBG (dev, "wakeup --> %d\n", status);
  50633. + }
  50634. +}
  50635. +
  50636. +/*-------------------------------------------------------------------------*/
  50637. +
  50638. +static void
  50639. +zero_unbind (struct usb_gadget *gadget)
  50640. +{
  50641. + struct zero_dev *dev = get_gadget_data (gadget);
  50642. +
  50643. + DBG (dev, "unbind\n");
  50644. +
  50645. + /* we've already been disconnected ... no i/o is active */
  50646. + if (dev->req)
  50647. + free_ep_req (gadget->ep0, dev->req);
  50648. + del_timer_sync (&dev->resume);
  50649. + kfree (dev);
  50650. + set_gadget_data (gadget, NULL);
  50651. +}
  50652. +
  50653. +static int
  50654. +zero_bind (struct usb_gadget *gadget)
  50655. +{
  50656. + struct zero_dev *dev;
  50657. + //struct usb_ep *ep;
  50658. +
  50659. + printk("binding\n");
  50660. + /*
  50661. + * DRIVER POLICY CHOICE: you may want to do this differently.
  50662. + * One thing to avoid is reusing a bcdDevice revision code
  50663. + * with different host-visible configurations or behavior
  50664. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  50665. + */
  50666. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  50667. +
  50668. +
  50669. + /* ok, we made sense of the hardware ... */
  50670. + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  50671. + if (!dev)
  50672. + return -ENOMEM;
  50673. + memset (dev, 0, sizeof *dev);
  50674. + spin_lock_init (&dev->lock);
  50675. + dev->gadget = gadget;
  50676. + set_gadget_data (gadget, dev);
  50677. +
  50678. + /* preallocate control response and buffer */
  50679. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  50680. + if (!dev->req)
  50681. + goto enomem;
  50682. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  50683. + &dev->req->dma, GFP_KERNEL);
  50684. + if (!dev->req->buf)
  50685. + goto enomem;
  50686. +
  50687. + dev->req->complete = zero_setup_complete;
  50688. +
  50689. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  50690. +
  50691. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50692. + /* assume ep0 uses the same value for both speeds ... */
  50693. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  50694. +
  50695. + /* and that all endpoints are dual-speed */
  50696. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  50697. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  50698. +#endif
  50699. +
  50700. + usb_gadget_set_selfpowered (gadget);
  50701. +
  50702. + init_timer (&dev->resume);
  50703. + dev->resume.function = zero_autoresume;
  50704. + dev->resume.data = (unsigned long) dev;
  50705. +
  50706. + gadget->ep0->driver_data = dev;
  50707. +
  50708. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  50709. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  50710. + EP_OUT_NAME, EP_IN_NAME);
  50711. +
  50712. + snprintf (manufacturer, sizeof manufacturer,
  50713. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  50714. + gadget->name);
  50715. +
  50716. + return 0;
  50717. +
  50718. +enomem:
  50719. + zero_unbind (gadget);
  50720. + return -ENOMEM;
  50721. +}
  50722. +
  50723. +/*-------------------------------------------------------------------------*/
  50724. +
  50725. +static void
  50726. +zero_suspend (struct usb_gadget *gadget)
  50727. +{
  50728. + struct zero_dev *dev = get_gadget_data (gadget);
  50729. +
  50730. + if (gadget->speed == USB_SPEED_UNKNOWN)
  50731. + return;
  50732. +
  50733. + if (autoresume) {
  50734. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  50735. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  50736. + } else
  50737. + DBG (dev, "suspend\n");
  50738. +}
  50739. +
  50740. +static void
  50741. +zero_resume (struct usb_gadget *gadget)
  50742. +{
  50743. + struct zero_dev *dev = get_gadget_data (gadget);
  50744. +
  50745. + DBG (dev, "resume\n");
  50746. + del_timer (&dev->resume);
  50747. +}
  50748. +
  50749. +
  50750. +/*-------------------------------------------------------------------------*/
  50751. +
  50752. +static struct usb_gadget_driver zero_driver = {
  50753. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50754. + .speed = USB_SPEED_HIGH,
  50755. +#else
  50756. + .speed = USB_SPEED_FULL,
  50757. +#endif
  50758. + .function = (char *) longname,
  50759. + .bind = zero_bind,
  50760. + .unbind = zero_unbind,
  50761. +
  50762. + .setup = zero_setup,
  50763. + .disconnect = zero_disconnect,
  50764. +
  50765. + .suspend = zero_suspend,
  50766. + .resume = zero_resume,
  50767. +
  50768. + .driver = {
  50769. + .name = (char *) shortname,
  50770. + // .shutdown = ...
  50771. + // .suspend = ...
  50772. + // .resume = ...
  50773. + },
  50774. +};
  50775. +
  50776. +MODULE_AUTHOR ("David Brownell");
  50777. +MODULE_LICENSE ("Dual BSD/GPL");
  50778. +
  50779. +static struct proc_dir_entry *pdir, *pfile;
  50780. +
  50781. +static int isoc_read_data (char *page, char **start,
  50782. + off_t off, int count,
  50783. + int *eof, void *data)
  50784. +{
  50785. + int i;
  50786. + static int c = 0;
  50787. + static int done = 0;
  50788. + static int s = 0;
  50789. +
  50790. +/*
  50791. + printk ("\ncount: %d\n", count);
  50792. + printk ("rbuf_start: %d\n", rbuf_start);
  50793. + printk ("rbuf_len: %d\n", rbuf_len);
  50794. + printk ("off: %d\n", off);
  50795. + printk ("start: %p\n\n", *start);
  50796. +*/
  50797. + if (done) {
  50798. + c = 0;
  50799. + done = 0;
  50800. + *eof = 1;
  50801. + return 0;
  50802. + }
  50803. +
  50804. + if (c == 0) {
  50805. + if (rbuf_len == RBUF_LEN)
  50806. + s = rbuf_start;
  50807. + else s = 0;
  50808. + }
  50809. +
  50810. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  50811. + page[i] = rbuf[(c+s) % RBUF_LEN];
  50812. + }
  50813. + *start = page;
  50814. +
  50815. + if (c >= rbuf_len) {
  50816. + *eof = 1;
  50817. + done = 1;
  50818. + }
  50819. +
  50820. +
  50821. + return i;
  50822. +}
  50823. +
  50824. +static int __init init (void)
  50825. +{
  50826. +
  50827. + int retval = 0;
  50828. +
  50829. + pdir = proc_mkdir("isoc_test", NULL);
  50830. + if(pdir == NULL) {
  50831. + retval = -ENOMEM;
  50832. + printk("Error creating dir\n");
  50833. + goto done;
  50834. + }
  50835. + pdir->owner = THIS_MODULE;
  50836. +
  50837. + pfile = create_proc_read_entry("isoc_data",
  50838. + 0444, pdir,
  50839. + isoc_read_data,
  50840. + NULL);
  50841. + if (pfile == NULL) {
  50842. + retval = -ENOMEM;
  50843. + printk("Error creating file\n");
  50844. + goto no_file;
  50845. + }
  50846. + pfile->owner = THIS_MODULE;
  50847. +
  50848. + return usb_gadget_register_driver (&zero_driver);
  50849. +
  50850. + no_file:
  50851. + remove_proc_entry("isoc_data", NULL);
  50852. + done:
  50853. + return retval;
  50854. +}
  50855. +module_init (init);
  50856. +
  50857. +static void __exit cleanup (void)
  50858. +{
  50859. +
  50860. + usb_gadget_unregister_driver (&zero_driver);
  50861. +
  50862. + remove_proc_entry("isoc_data", pdir);
  50863. + remove_proc_entry("isoc_test", NULL);
  50864. +}
  50865. +module_exit (cleanup);
  50866. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_cfi_common.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  50867. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_cfi_common.h 1970-01-01 01:00:00.000000000 +0100
  50868. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_cfi_common.h 2014-03-11 17:51:27.000000000 +0100
  50869. @@ -0,0 +1,142 @@
  50870. +/* ==========================================================================
  50871. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50872. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50873. + * otherwise expressly agreed to in writing between Synopsys and you.
  50874. + *
  50875. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50876. + * any End User Software License Agreement or Agreement for Licensed Product
  50877. + * with Synopsys or any supplement thereto. You are permitted to use and
  50878. + * redistribute this Software in source and binary forms, with or without
  50879. + * modification, provided that redistributions of source code must retain this
  50880. + * notice. You may not view, use, disclose, copy or distribute this file or
  50881. + * any information contained herein except pursuant to this license grant from
  50882. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50883. + * below, then you are not authorized to use the Software.
  50884. + *
  50885. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50886. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50887. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50888. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50889. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50890. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50891. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50892. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50893. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50894. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50895. + * DAMAGE.
  50896. + * ========================================================================== */
  50897. +
  50898. +#if !defined(__DWC_CFI_COMMON_H__)
  50899. +#define __DWC_CFI_COMMON_H__
  50900. +
  50901. +//#include <linux/types.h>
  50902. +
  50903. +/**
  50904. + * @file
  50905. + *
  50906. + * This file contains the CFI specific common constants, interfaces
  50907. + * (functions and macros) and structures for Linux. No PCD specific
  50908. + * data structure or definition is to be included in this file.
  50909. + *
  50910. + */
  50911. +
  50912. +/** This is a request for all Core Features */
  50913. +#define VEN_CORE_GET_FEATURES 0xB1
  50914. +
  50915. +/** This is a request to get the value of a specific Core Feature */
  50916. +#define VEN_CORE_GET_FEATURE 0xB2
  50917. +
  50918. +/** This command allows the host to set the value of a specific Core Feature */
  50919. +#define VEN_CORE_SET_FEATURE 0xB3
  50920. +
  50921. +/** This command allows the host to set the default values of
  50922. + * either all or any specific Core Feature
  50923. + */
  50924. +#define VEN_CORE_RESET_FEATURES 0xB4
  50925. +
  50926. +/** This command forces the PCD to write the deferred values of a Core Features */
  50927. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  50928. +
  50929. +/** This request reads a DWORD value from a register at the specified offset */
  50930. +#define VEN_CORE_READ_REGISTER 0xB6
  50931. +
  50932. +/** This request writes a DWORD value into a register at the specified offset */
  50933. +#define VEN_CORE_WRITE_REGISTER 0xB7
  50934. +
  50935. +/** This structure is the header of the Core Features dataset returned to
  50936. + * the Host
  50937. + */
  50938. +struct cfi_all_features_header {
  50939. +/** The features header structure length is */
  50940. +#define CFI_ALL_FEATURES_HDR_LEN 8
  50941. + /**
  50942. + * The total length of the features dataset returned to the Host
  50943. + */
  50944. + uint16_t wTotalLen;
  50945. +
  50946. + /**
  50947. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  50948. + * This field identifies the version of the CFI Specification with which
  50949. + * the device is compliant.
  50950. + */
  50951. + uint16_t wVersion;
  50952. +
  50953. + /** The ID of the Core */
  50954. + uint16_t wCoreID;
  50955. +#define CFI_CORE_ID_UDC 1
  50956. +#define CFI_CORE_ID_OTG 2
  50957. +#define CFI_CORE_ID_WUDEV 3
  50958. +
  50959. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  50960. + uint16_t wNumFeatures;
  50961. +} UPACKED;
  50962. +
  50963. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  50964. +
  50965. +/** This structure is a header of the Core Feature descriptor dataset returned to
  50966. + * the Host after the VEN_CORE_GET_FEATURES request
  50967. + */
  50968. +struct cfi_feature_desc_header {
  50969. +#define CFI_FEATURE_DESC_HDR_LEN 8
  50970. +
  50971. + /** The feature ID */
  50972. + uint16_t wFeatureID;
  50973. +
  50974. + /** Length of this feature descriptor in bytes - including the
  50975. + * length of the feature name string
  50976. + */
  50977. + uint16_t wLength;
  50978. +
  50979. + /** The data length of this feature in bytes */
  50980. + uint16_t wDataLength;
  50981. +
  50982. + /**
  50983. + * Attributes of this features
  50984. + * D0: Access rights
  50985. + * 0 - Read/Write
  50986. + * 1 - Read only
  50987. + */
  50988. + uint8_t bmAttributes;
  50989. +#define CFI_FEATURE_ATTR_RO 1
  50990. +#define CFI_FEATURE_ATTR_RW 0
  50991. +
  50992. + /** Length of the feature name in bytes */
  50993. + uint8_t bNameLen;
  50994. +
  50995. + /** The feature name buffer */
  50996. + //uint8_t *name;
  50997. +} UPACKED;
  50998. +
  50999. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  51000. +
  51001. +/**
  51002. + * This structure describes a NULL terminated string referenced by its id field.
  51003. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  51004. + */
  51005. +struct cfi_string {
  51006. + uint16_t id;
  51007. + const uint8_t *s;
  51008. +};
  51009. +typedef struct cfi_string cfi_string_t;
  51010. +
  51011. +#endif
  51012. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_adp.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  51013. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_adp.c 1970-01-01 01:00:00.000000000 +0100
  51014. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.c 2014-03-11 17:51:27.000000000 +0100
  51015. @@ -0,0 +1,854 @@
  51016. +/* ==========================================================================
  51017. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  51018. + * $Revision: #12 $
  51019. + * $Date: 2011/10/26 $
  51020. + * $Change: 1873028 $
  51021. + *
  51022. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51023. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51024. + * otherwise expressly agreed to in writing between Synopsys and you.
  51025. + *
  51026. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51027. + * any End User Software License Agreement or Agreement for Licensed Product
  51028. + * with Synopsys or any supplement thereto. You are permitted to use and
  51029. + * redistribute this Software in source and binary forms, with or without
  51030. + * modification, provided that redistributions of source code must retain this
  51031. + * notice. You may not view, use, disclose, copy or distribute this file or
  51032. + * any information contained herein except pursuant to this license grant from
  51033. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51034. + * below, then you are not authorized to use the Software.
  51035. + *
  51036. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51037. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51038. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51039. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51040. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51041. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51042. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51043. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51044. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51045. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51046. + * DAMAGE.
  51047. + * ========================================================================== */
  51048. +
  51049. +#include "dwc_os.h"
  51050. +#include "dwc_otg_regs.h"
  51051. +#include "dwc_otg_cil.h"
  51052. +#include "dwc_otg_adp.h"
  51053. +
  51054. +/** @file
  51055. + *
  51056. + * This file contains the most of the Attach Detect Protocol implementation for
  51057. + * the driver to support OTG Rev2.0.
  51058. + *
  51059. + */
  51060. +
  51061. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  51062. +{
  51063. + adpctl_data_t adpctl;
  51064. +
  51065. + adpctl.d32 = value;
  51066. + adpctl.b.ar = 0x2;
  51067. +
  51068. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  51069. +
  51070. + while (adpctl.b.ar) {
  51071. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  51072. + }
  51073. +
  51074. +}
  51075. +
  51076. +/**
  51077. + * Function is called to read ADP registers
  51078. + */
  51079. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  51080. +{
  51081. + adpctl_data_t adpctl;
  51082. +
  51083. + adpctl.d32 = 0;
  51084. + adpctl.b.ar = 0x1;
  51085. +
  51086. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  51087. +
  51088. + while (adpctl.b.ar) {
  51089. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  51090. + }
  51091. +
  51092. + return adpctl.d32;
  51093. +}
  51094. +
  51095. +/**
  51096. + * Function is called to read ADPCTL register and filter Write-clear bits
  51097. + */
  51098. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  51099. +{
  51100. + adpctl_data_t adpctl;
  51101. +
  51102. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51103. + adpctl.b.adp_tmout_int = 0;
  51104. + adpctl.b.adp_prb_int = 0;
  51105. + adpctl.b.adp_tmout_int = 0;
  51106. +
  51107. + return adpctl.d32;
  51108. +}
  51109. +
  51110. +/**
  51111. + * Function is called to write ADP registers
  51112. + */
  51113. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  51114. + uint32_t set)
  51115. +{
  51116. + dwc_otg_adp_write_reg(core_if,
  51117. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  51118. +}
  51119. +
  51120. +static void adp_sense_timeout(void *ptr)
  51121. +{
  51122. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  51123. + core_if->adp.sense_timer_started = 0;
  51124. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  51125. + if (core_if->adp_enable) {
  51126. + dwc_otg_adp_sense_stop(core_if);
  51127. + dwc_otg_adp_probe_start(core_if);
  51128. + }
  51129. +}
  51130. +
  51131. +/**
  51132. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  51133. + */
  51134. +static void adp_vbuson_timeout(void *ptr)
  51135. +{
  51136. + gpwrdn_data_t gpwrdn;
  51137. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  51138. + hprt0_data_t hprt0 = {.d32 = 0 };
  51139. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  51140. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  51141. + if (core_if) {
  51142. + core_if->adp.vbuson_timer_started = 0;
  51143. + /* Turn off vbus */
  51144. + hprt0.b.prtpwr = 1;
  51145. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  51146. + gpwrdn.d32 = 0;
  51147. +
  51148. + /* Power off the core */
  51149. + if (core_if->power_down == 2) {
  51150. + /* Enable Wakeup Logic */
  51151. +// gpwrdn.b.wkupactiv = 1;
  51152. + gpwrdn.b.pmuactv = 0;
  51153. + gpwrdn.b.pwrdnrstn = 1;
  51154. + gpwrdn.b.pwrdnclmp = 1;
  51155. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  51156. + gpwrdn.d32);
  51157. +
  51158. + /* Suspend the Phy Clock */
  51159. + pcgcctl.b.stoppclk = 1;
  51160. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  51161. +
  51162. + /* Switch on VDD */
  51163. +// gpwrdn.b.wkupactiv = 1;
  51164. + gpwrdn.b.pmuactv = 1;
  51165. + gpwrdn.b.pwrdnrstn = 1;
  51166. + gpwrdn.b.pwrdnclmp = 1;
  51167. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  51168. + gpwrdn.d32);
  51169. + } else {
  51170. + /* Enable Power Down Logic */
  51171. + gpwrdn.b.pmuintsel = 1;
  51172. + gpwrdn.b.pmuactv = 1;
  51173. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51174. + }
  51175. +
  51176. + /* Power off the core */
  51177. + if (core_if->power_down == 2) {
  51178. + gpwrdn.d32 = 0;
  51179. + gpwrdn.b.pwrdnswtch = 1;
  51180. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  51181. + gpwrdn.d32, 0);
  51182. + }
  51183. +
  51184. + /* Unmask SRP detected interrupt from Power Down Logic */
  51185. + gpwrdn.d32 = 0;
  51186. + gpwrdn.b.srp_det_msk = 1;
  51187. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51188. +
  51189. + dwc_otg_adp_probe_start(core_if);
  51190. + dwc_otg_dump_global_registers(core_if);
  51191. + dwc_otg_dump_host_registers(core_if);
  51192. + }
  51193. +
  51194. +}
  51195. +
  51196. +/**
  51197. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  51198. + * not asserted within 1.1 seconds.
  51199. + *
  51200. + * @param core_if the pointer to core_if strucure.
  51201. + */
  51202. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  51203. +{
  51204. + core_if->adp.vbuson_timer_started = 1;
  51205. + if (core_if->adp.vbuson_timer)
  51206. + {
  51207. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  51208. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  51209. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  51210. + } else {
  51211. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  51212. + }
  51213. +}
  51214. +
  51215. +#if 0
  51216. +/**
  51217. + * Masks all DWC OTG core interrupts
  51218. + *
  51219. + */
  51220. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  51221. +{
  51222. + int i;
  51223. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  51224. +
  51225. + /* Mask Host Interrupts */
  51226. +
  51227. + /* Clear and disable HCINTs */
  51228. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  51229. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  51230. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  51231. +
  51232. + }
  51233. +
  51234. + /* Clear and disable HAINT */
  51235. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  51236. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  51237. +
  51238. + /* Mask Device Interrupts */
  51239. + if (!core_if->multiproc_int_enable) {
  51240. + /* Clear and disable IN Endpoint interrupts */
  51241. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  51242. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  51243. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  51244. + diepint, 0xFFFFFFFF);
  51245. + }
  51246. +
  51247. + /* Clear and disable OUT Endpoint interrupts */
  51248. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  51249. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  51250. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  51251. + doepint, 0xFFFFFFFF);
  51252. + }
  51253. +
  51254. + /* Clear and disable DAINT */
  51255. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  51256. + 0xFFFFFFFF);
  51257. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  51258. + } else {
  51259. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  51260. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  51261. + diepeachintmsk[i], 0);
  51262. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  51263. + diepint, 0xFFFFFFFF);
  51264. + }
  51265. +
  51266. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  51267. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  51268. + doepeachintmsk[i], 0);
  51269. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  51270. + doepint, 0xFFFFFFFF);
  51271. + }
  51272. +
  51273. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  51274. + 0);
  51275. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  51276. + 0xFFFFFFFF);
  51277. +
  51278. + }
  51279. +
  51280. + /* Disable interrupts */
  51281. + ahbcfg.b.glblintrmsk = 1;
  51282. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  51283. +
  51284. + /* Disable all interrupts. */
  51285. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  51286. +
  51287. + /* Clear any pending interrupts */
  51288. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  51289. +
  51290. + /* Clear any pending OTG Interrupts */
  51291. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  51292. +}
  51293. +
  51294. +/**
  51295. + * Unmask Port Connection Detected interrupt
  51296. + *
  51297. + */
  51298. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  51299. +{
  51300. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  51301. +
  51302. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  51303. +}
  51304. +#endif
  51305. +
  51306. +/**
  51307. + * Starts the ADP Probing
  51308. + *
  51309. + * @param core_if the pointer to core_if structure.
  51310. + */
  51311. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  51312. +{
  51313. +
  51314. + adpctl_data_t adpctl = {.d32 = 0};
  51315. + gpwrdn_data_t gpwrdn;
  51316. +#if 0
  51317. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  51318. + .b.adp_sns_int = 1, b.adp_tmout_int};
  51319. +#endif
  51320. + dwc_otg_disable_global_interrupts(core_if);
  51321. + DWC_PRINTF("ADP Probe Start\n");
  51322. + core_if->adp.probe_enabled = 1;
  51323. +
  51324. + adpctl.b.adpres = 1;
  51325. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51326. +
  51327. + while (adpctl.b.adpres) {
  51328. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51329. + }
  51330. +
  51331. + adpctl.d32 = 0;
  51332. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51333. +
  51334. + /* In Host mode unmask SRP detected interrupt */
  51335. + gpwrdn.d32 = 0;
  51336. + gpwrdn.b.sts_chngint_msk = 1;
  51337. + if (!gpwrdn.b.idsts) {
  51338. + gpwrdn.b.srp_det_msk = 1;
  51339. + }
  51340. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51341. +
  51342. + adpctl.b.adp_tmout_int_msk = 1;
  51343. + adpctl.b.adp_prb_int_msk = 1;
  51344. + adpctl.b.prb_dschg = 1;
  51345. + adpctl.b.prb_delta = 1;
  51346. + adpctl.b.prb_per = 1;
  51347. + adpctl.b.adpen = 1;
  51348. + adpctl.b.enaprb = 1;
  51349. +
  51350. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51351. + DWC_PRINTF("ADP Probe Finish\n");
  51352. + return 0;
  51353. +}
  51354. +
  51355. +/**
  51356. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  51357. + * within 3 seconds.
  51358. + *
  51359. + * @param core_if the pointer to core_if strucure.
  51360. + */
  51361. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  51362. +{
  51363. + core_if->adp.sense_timer_started = 1;
  51364. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  51365. +}
  51366. +
  51367. +/**
  51368. + * Starts the ADP Sense
  51369. + *
  51370. + * @param core_if the pointer to core_if strucure.
  51371. + */
  51372. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  51373. +{
  51374. + adpctl_data_t adpctl;
  51375. +
  51376. + DWC_PRINTF("ADP Sense Start\n");
  51377. +
  51378. + /* Unmask ADP sense interrupt and mask all other from the core */
  51379. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  51380. + adpctl.b.adp_sns_int_msk = 1;
  51381. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51382. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  51383. +
  51384. + /* Set ADP reset bit*/
  51385. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  51386. + adpctl.b.adpres = 1;
  51387. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51388. +
  51389. + while (adpctl.b.adpres) {
  51390. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51391. + }
  51392. +
  51393. + adpctl.b.adpres = 0;
  51394. + adpctl.b.adpen = 1;
  51395. + adpctl.b.enasns = 1;
  51396. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51397. +
  51398. + dwc_otg_adp_sense_timer_start(core_if);
  51399. +
  51400. + return 0;
  51401. +}
  51402. +
  51403. +/**
  51404. + * Stops the ADP Probing
  51405. + *
  51406. + * @param core_if the pointer to core_if strucure.
  51407. + */
  51408. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  51409. +{
  51410. +
  51411. + adpctl_data_t adpctl;
  51412. + DWC_PRINTF("Stop ADP probe\n");
  51413. + core_if->adp.probe_enabled = 0;
  51414. + core_if->adp.probe_counter = 0;
  51415. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51416. +
  51417. + adpctl.b.adpen = 0;
  51418. + adpctl.b.adp_prb_int = 1;
  51419. + adpctl.b.adp_tmout_int = 1;
  51420. + adpctl.b.adp_sns_int = 1;
  51421. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51422. +
  51423. + return 0;
  51424. +}
  51425. +
  51426. +/**
  51427. + * Stops the ADP Sensing
  51428. + *
  51429. + * @param core_if the pointer to core_if strucure.
  51430. + */
  51431. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  51432. +{
  51433. + adpctl_data_t adpctl;
  51434. +
  51435. + core_if->adp.sense_enabled = 0;
  51436. +
  51437. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  51438. + adpctl.b.enasns = 0;
  51439. + adpctl.b.adp_sns_int = 1;
  51440. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51441. +
  51442. + return 0;
  51443. +}
  51444. +
  51445. +/**
  51446. + * Called to turn on the VBUS after initial ADP probe in host mode.
  51447. + * If port power was already enabled in cil_hcd_start function then
  51448. + * only schedule a timer.
  51449. + *
  51450. + * @param core_if the pointer to core_if structure.
  51451. + */
  51452. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  51453. +{
  51454. + hprt0_data_t hprt0 = {.d32 = 0 };
  51455. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  51456. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  51457. +
  51458. + if (hprt0.b.prtpwr == 0) {
  51459. + hprt0.b.prtpwr = 1;
  51460. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  51461. + }
  51462. +
  51463. + dwc_otg_adp_vbuson_timer_start(core_if);
  51464. +}
  51465. +
  51466. +/**
  51467. + * Called right after driver is loaded
  51468. + * to perform initial actions for ADP
  51469. + *
  51470. + * @param core_if the pointer to core_if structure.
  51471. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  51472. + */
  51473. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  51474. +{
  51475. + gpwrdn_data_t gpwrdn;
  51476. +
  51477. + DWC_PRINTF("ADP Initial Start\n");
  51478. + core_if->adp.adp_started = 1;
  51479. +
  51480. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  51481. + dwc_otg_disable_global_interrupts(core_if);
  51482. + if (is_host) {
  51483. + DWC_PRINTF("HOST MODE\n");
  51484. + /* Enable Power Down Logic Interrupt*/
  51485. + gpwrdn.d32 = 0;
  51486. + gpwrdn.b.pmuintsel = 1;
  51487. + gpwrdn.b.pmuactv = 1;
  51488. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51489. + /* Initialize first ADP probe to obtain Ramp Time value */
  51490. + core_if->adp.initial_probe = 1;
  51491. + dwc_otg_adp_probe_start(core_if);
  51492. + } else {
  51493. + gotgctl_data_t gotgctl;
  51494. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  51495. + DWC_PRINTF("DEVICE MODE\n");
  51496. + if (gotgctl.b.bsesvld == 0) {
  51497. + /* Enable Power Down Logic Interrupt*/
  51498. + gpwrdn.d32 = 0;
  51499. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  51500. + gpwrdn.b.pmuintsel = 1;
  51501. + gpwrdn.b.pmuactv = 1;
  51502. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51503. + core_if->adp.initial_probe = 1;
  51504. + dwc_otg_adp_probe_start(core_if);
  51505. + } else {
  51506. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  51507. + core_if->op_state = B_PERIPHERAL;
  51508. + dwc_otg_core_init(core_if);
  51509. + dwc_otg_enable_global_interrupts(core_if);
  51510. + cil_pcd_start(core_if);
  51511. + dwc_otg_dump_global_registers(core_if);
  51512. + dwc_otg_dump_dev_registers(core_if);
  51513. + }
  51514. + }
  51515. +}
  51516. +
  51517. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  51518. +{
  51519. + core_if->adp.adp_started = 0;
  51520. + core_if->adp.initial_probe = 0;
  51521. + core_if->adp.probe_timer_values[0] = -1;
  51522. + core_if->adp.probe_timer_values[1] = -1;
  51523. + core_if->adp.probe_enabled = 0;
  51524. + core_if->adp.sense_enabled = 0;
  51525. + core_if->adp.sense_timer_started = 0;
  51526. + core_if->adp.vbuson_timer_started = 0;
  51527. + core_if->adp.probe_counter = 0;
  51528. + core_if->adp.gpwrdn = 0;
  51529. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  51530. + /* Initialize timers */
  51531. + core_if->adp.sense_timer =
  51532. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  51533. + core_if->adp.vbuson_timer =
  51534. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  51535. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  51536. + {
  51537. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  51538. + }
  51539. +}
  51540. +
  51541. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  51542. +{
  51543. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  51544. + gpwrdn.b.pmuintsel = 1;
  51545. + gpwrdn.b.pmuactv = 1;
  51546. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51547. +
  51548. + if (core_if->adp.probe_enabled)
  51549. + dwc_otg_adp_probe_stop(core_if);
  51550. + if (core_if->adp.sense_enabled)
  51551. + dwc_otg_adp_sense_stop(core_if);
  51552. + if (core_if->adp.sense_timer_started)
  51553. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  51554. + if (core_if->adp.vbuson_timer_started)
  51555. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  51556. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  51557. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  51558. +}
  51559. +
  51560. +/////////////////////////////////////////////////////////////////////
  51561. +////////////// ADP Interrupt Handlers ///////////////////////////////
  51562. +/////////////////////////////////////////////////////////////////////
  51563. +/**
  51564. + * This function sets Ramp Timer values
  51565. + */
  51566. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  51567. +{
  51568. + if (core_if->adp.probe_timer_values[0] == -1) {
  51569. + core_if->adp.probe_timer_values[0] = val;
  51570. + core_if->adp.probe_timer_values[1] = -1;
  51571. + return 1;
  51572. + } else {
  51573. + core_if->adp.probe_timer_values[1] =
  51574. + core_if->adp.probe_timer_values[0];
  51575. + core_if->adp.probe_timer_values[0] = val;
  51576. + return 0;
  51577. + }
  51578. +}
  51579. +
  51580. +/**
  51581. + * This function compares Ramp Timer values
  51582. + */
  51583. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  51584. +{
  51585. + uint32_t diff;
  51586. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  51587. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  51588. + else
  51589. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  51590. + if(diff < 2) {
  51591. + return 0;
  51592. + } else {
  51593. + return 1;
  51594. + }
  51595. +}
  51596. +
  51597. +/**
  51598. + * This function handles ADP Probe Interrupts
  51599. + */
  51600. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  51601. + uint32_t val)
  51602. +{
  51603. + adpctl_data_t adpctl = {.d32 = 0 };
  51604. + gpwrdn_data_t gpwrdn, temp;
  51605. + adpctl.d32 = val;
  51606. +
  51607. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51608. + core_if->adp.probe_counter++;
  51609. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51610. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  51611. + DWC_PRINTF("RTIM value is 0\n");
  51612. + goto exit;
  51613. + }
  51614. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  51615. + core_if->adp.initial_probe) {
  51616. + core_if->adp.initial_probe = 0;
  51617. + dwc_otg_adp_probe_stop(core_if);
  51618. + gpwrdn.d32 = 0;
  51619. + gpwrdn.b.pmuactv = 1;
  51620. + gpwrdn.b.pmuintsel = 1;
  51621. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51622. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  51623. +
  51624. + /* check which value is for device mode and which for Host mode */
  51625. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  51626. + /*
  51627. + * Turn on VBUS after initial ADP probe.
  51628. + */
  51629. + core_if->op_state = A_HOST;
  51630. + dwc_otg_enable_global_interrupts(core_if);
  51631. + DWC_SPINUNLOCK(core_if->lock);
  51632. + cil_hcd_start(core_if);
  51633. + dwc_otg_adp_turnon_vbus(core_if);
  51634. + DWC_SPINLOCK(core_if->lock);
  51635. + } else {
  51636. + /*
  51637. + * Initiate SRP after initial ADP probe.
  51638. + */
  51639. + dwc_otg_enable_global_interrupts(core_if);
  51640. + dwc_otg_initiate_srp(core_if);
  51641. + }
  51642. + } else if (core_if->adp.probe_counter > 2){
  51643. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51644. + if (compare_timer_values(core_if)) {
  51645. + DWC_PRINTF("Difference in timer values !!! \n");
  51646. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  51647. + dwc_otg_adp_probe_stop(core_if);
  51648. +
  51649. + /* Power on the core */
  51650. + if (core_if->power_down == 2) {
  51651. + gpwrdn.b.pwrdnswtch = 1;
  51652. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51653. + gpwrdn, 0, gpwrdn.d32);
  51654. + }
  51655. +
  51656. + /* check which value is for device mode and which for Host mode */
  51657. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  51658. + /* Disable Interrupt from Power Down Logic */
  51659. + gpwrdn.d32 = 0;
  51660. + gpwrdn.b.pmuintsel = 1;
  51661. + gpwrdn.b.pmuactv = 1;
  51662. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51663. + gpwrdn, gpwrdn.d32, 0);
  51664. +
  51665. + /*
  51666. + * Initialize the Core for Host mode.
  51667. + */
  51668. + core_if->op_state = A_HOST;
  51669. + dwc_otg_core_init(core_if);
  51670. + dwc_otg_enable_global_interrupts(core_if);
  51671. + cil_hcd_start(core_if);
  51672. + } else {
  51673. + gotgctl_data_t gotgctl;
  51674. + /* Mask SRP detected interrupt from Power Down Logic */
  51675. + gpwrdn.d32 = 0;
  51676. + gpwrdn.b.srp_det_msk = 1;
  51677. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51678. + gpwrdn, gpwrdn.d32, 0);
  51679. +
  51680. + /* Disable Power Down Logic */
  51681. + gpwrdn.d32 = 0;
  51682. + gpwrdn.b.pmuintsel = 1;
  51683. + gpwrdn.b.pmuactv = 1;
  51684. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51685. + gpwrdn, gpwrdn.d32, 0);
  51686. +
  51687. + /*
  51688. + * Initialize the Core for Device mode.
  51689. + */
  51690. + core_if->op_state = B_PERIPHERAL;
  51691. + dwc_otg_core_init(core_if);
  51692. + dwc_otg_enable_global_interrupts(core_if);
  51693. + cil_pcd_start(core_if);
  51694. +
  51695. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  51696. + if (!gotgctl.b.bsesvld) {
  51697. + dwc_otg_initiate_srp(core_if);
  51698. + }
  51699. + }
  51700. + }
  51701. + if (core_if->power_down == 2) {
  51702. + if (gpwrdn.b.bsessvld) {
  51703. + /* Mask SRP detected interrupt from Power Down Logic */
  51704. + gpwrdn.d32 = 0;
  51705. + gpwrdn.b.srp_det_msk = 1;
  51706. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51707. +
  51708. + /* Disable Power Down Logic */
  51709. + gpwrdn.d32 = 0;
  51710. + gpwrdn.b.pmuactv = 1;
  51711. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51712. +
  51713. + /*
  51714. + * Initialize the Core for Device mode.
  51715. + */
  51716. + core_if->op_state = B_PERIPHERAL;
  51717. + dwc_otg_core_init(core_if);
  51718. + dwc_otg_enable_global_interrupts(core_if);
  51719. + cil_pcd_start(core_if);
  51720. + }
  51721. + }
  51722. + }
  51723. +exit:
  51724. + /* Clear interrupt */
  51725. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51726. + adpctl.b.adp_prb_int = 1;
  51727. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51728. +
  51729. + return 0;
  51730. +}
  51731. +
  51732. +/**
  51733. + * This function hadles ADP Sense Interrupt
  51734. + */
  51735. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  51736. +{
  51737. + adpctl_data_t adpctl;
  51738. + /* Stop ADP Sense timer */
  51739. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  51740. +
  51741. + /* Restart ADP Sense timer */
  51742. + dwc_otg_adp_sense_timer_start(core_if);
  51743. +
  51744. + /* Clear interrupt */
  51745. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51746. + adpctl.b.adp_sns_int = 1;
  51747. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51748. +
  51749. + return 0;
  51750. +}
  51751. +
  51752. +/**
  51753. + * This function handles ADP Probe Interrupts
  51754. + */
  51755. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  51756. + uint32_t val)
  51757. +{
  51758. + adpctl_data_t adpctl = {.d32 = 0 };
  51759. + adpctl.d32 = val;
  51760. + set_timer_value(core_if, adpctl.b.rtim);
  51761. +
  51762. + /* Clear interrupt */
  51763. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51764. + adpctl.b.adp_tmout_int = 1;
  51765. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51766. +
  51767. + return 0;
  51768. +}
  51769. +
  51770. +/**
  51771. + * ADP Interrupt handler.
  51772. + *
  51773. + */
  51774. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  51775. +{
  51776. + int retval = 0;
  51777. + adpctl_data_t adpctl = {.d32 = 0};
  51778. +
  51779. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51780. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  51781. +
  51782. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  51783. + DWC_PRINTF("ADP Sense interrupt\n");
  51784. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  51785. + }
  51786. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  51787. + DWC_PRINTF("ADP timeout interrupt\n");
  51788. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  51789. + }
  51790. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  51791. + DWC_PRINTF("ADP Probe interrupt\n");
  51792. + adpctl.b.adp_prb_int = 1;
  51793. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  51794. + }
  51795. +
  51796. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  51797. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51798. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  51799. +
  51800. + return retval;
  51801. +}
  51802. +
  51803. +/**
  51804. + *
  51805. + * @param core_if Programming view of DWC_otg controller.
  51806. + */
  51807. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  51808. +{
  51809. +
  51810. +#ifndef DWC_HOST_ONLY
  51811. + hprt0_data_t hprt0;
  51812. + gpwrdn_data_t gpwrdn;
  51813. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  51814. +
  51815. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51816. + /* check which value is for device mode and which for Host mode */
  51817. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  51818. + DWC_PRINTF("SRP: Host mode\n");
  51819. +
  51820. + if (core_if->adp_enable) {
  51821. + dwc_otg_adp_probe_stop(core_if);
  51822. +
  51823. + /* Power on the core */
  51824. + if (core_if->power_down == 2) {
  51825. + gpwrdn.b.pwrdnswtch = 1;
  51826. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51827. + gpwrdn, 0, gpwrdn.d32);
  51828. + }
  51829. +
  51830. + core_if->op_state = A_HOST;
  51831. + dwc_otg_core_init(core_if);
  51832. + dwc_otg_enable_global_interrupts(core_if);
  51833. + cil_hcd_start(core_if);
  51834. + }
  51835. +
  51836. + /* Turn on the port power bit. */
  51837. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  51838. + hprt0.b.prtpwr = 1;
  51839. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  51840. +
  51841. + /* Start the Connection timer. So a message can be displayed
  51842. + * if connect does not occur within 10 seconds. */
  51843. + cil_hcd_session_start(core_if);
  51844. + } else {
  51845. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  51846. + if (core_if->adp_enable) {
  51847. + dwc_otg_adp_probe_stop(core_if);
  51848. +
  51849. + /* Power on the core */
  51850. + if (core_if->power_down == 2) {
  51851. + gpwrdn.b.pwrdnswtch = 1;
  51852. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51853. + gpwrdn, 0, gpwrdn.d32);
  51854. + }
  51855. +
  51856. + gpwrdn.d32 = 0;
  51857. + gpwrdn.b.pmuactv = 0;
  51858. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  51859. + gpwrdn.d32);
  51860. +
  51861. + core_if->op_state = B_PERIPHERAL;
  51862. + dwc_otg_core_init(core_if);
  51863. + dwc_otg_enable_global_interrupts(core_if);
  51864. + cil_pcd_start(core_if);
  51865. + }
  51866. + }
  51867. +#endif
  51868. + return 1;
  51869. +}
  51870. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_adp.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  51871. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_adp.h 1970-01-01 01:00:00.000000000 +0100
  51872. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.h 2014-03-11 17:51:27.000000000 +0100
  51873. @@ -0,0 +1,80 @@
  51874. +/* ==========================================================================
  51875. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  51876. + * $Revision: #7 $
  51877. + * $Date: 2011/10/24 $
  51878. + * $Change: 1871159 $
  51879. + *
  51880. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51881. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51882. + * otherwise expressly agreed to in writing between Synopsys and you.
  51883. + *
  51884. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51885. + * any End User Software License Agreement or Agreement for Licensed Product
  51886. + * with Synopsys or any supplement thereto. You are permitted to use and
  51887. + * redistribute this Software in source and binary forms, with or without
  51888. + * modification, provided that redistributions of source code must retain this
  51889. + * notice. You may not view, use, disclose, copy or distribute this file or
  51890. + * any information contained herein except pursuant to this license grant from
  51891. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51892. + * below, then you are not authorized to use the Software.
  51893. + *
  51894. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51895. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51896. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51897. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51898. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51899. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51900. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51901. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51902. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51903. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51904. + * DAMAGE.
  51905. + * ========================================================================== */
  51906. +
  51907. +#ifndef __DWC_OTG_ADP_H__
  51908. +#define __DWC_OTG_ADP_H__
  51909. +
  51910. +/**
  51911. + * @file
  51912. + *
  51913. + * This file contains the Attach Detect Protocol interfaces and defines
  51914. + * (functions) and structures for Linux.
  51915. + *
  51916. + */
  51917. +
  51918. +#define DWC_OTG_ADP_UNATTACHED 0
  51919. +#define DWC_OTG_ADP_ATTACHED 1
  51920. +#define DWC_OTG_ADP_UNKOWN 2
  51921. +
  51922. +typedef struct dwc_otg_adp {
  51923. + uint32_t adp_started;
  51924. + uint32_t initial_probe;
  51925. + int32_t probe_timer_values[2];
  51926. + uint32_t probe_enabled;
  51927. + uint32_t sense_enabled;
  51928. + dwc_timer_t *sense_timer;
  51929. + uint32_t sense_timer_started;
  51930. + dwc_timer_t *vbuson_timer;
  51931. + uint32_t vbuson_timer_started;
  51932. + uint32_t attached;
  51933. + uint32_t probe_counter;
  51934. + uint32_t gpwrdn;
  51935. +} dwc_otg_adp_t;
  51936. +
  51937. +/**
  51938. + * Attach Detect Protocol functions
  51939. + */
  51940. +
  51941. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  51942. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  51943. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  51944. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  51945. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  51946. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  51947. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  51948. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  51949. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  51950. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  51951. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  51952. +
  51953. +#endif //__DWC_OTG_ADP_H__
  51954. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_attr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  51955. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_attr.c 1970-01-01 01:00:00.000000000 +0100
  51956. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.c 2014-03-11 17:51:27.000000000 +0100
  51957. @@ -0,0 +1,1210 @@
  51958. +/* ==========================================================================
  51959. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  51960. + * $Revision: #44 $
  51961. + * $Date: 2010/11/29 $
  51962. + * $Change: 1636033 $
  51963. + *
  51964. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51965. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51966. + * otherwise expressly agreed to in writing between Synopsys and you.
  51967. + *
  51968. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51969. + * any End User Software License Agreement or Agreement for Licensed Product
  51970. + * with Synopsys or any supplement thereto. You are permitted to use and
  51971. + * redistribute this Software in source and binary forms, with or without
  51972. + * modification, provided that redistributions of source code must retain this
  51973. + * notice. You may not view, use, disclose, copy or distribute this file or
  51974. + * any information contained herein except pursuant to this license grant from
  51975. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51976. + * below, then you are not authorized to use the Software.
  51977. + *
  51978. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51979. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51980. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51981. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51982. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51983. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51984. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51985. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51986. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51987. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51988. + * DAMAGE.
  51989. + * ========================================================================== */
  51990. +
  51991. +/** @file
  51992. + *
  51993. + * The diagnostic interface will provide access to the controller for
  51994. + * bringing up the hardware and testing. The Linux driver attributes
  51995. + * feature will be used to provide the Linux Diagnostic
  51996. + * Interface. These attributes are accessed through sysfs.
  51997. + */
  51998. +
  51999. +/** @page "Linux Module Attributes"
  52000. + *
  52001. + * The Linux module attributes feature is used to provide the Linux
  52002. + * Diagnostic Interface. These attributes are accessed through sysfs.
  52003. + * The diagnostic interface will provide access to the controller for
  52004. + * bringing up the hardware and testing.
  52005. +
  52006. + The following table shows the attributes.
  52007. + <table>
  52008. + <tr>
  52009. + <td><b> Name</b></td>
  52010. + <td><b> Description</b></td>
  52011. + <td><b> Access</b></td>
  52012. + </tr>
  52013. +
  52014. + <tr>
  52015. + <td> mode </td>
  52016. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  52017. + <td> Read</td>
  52018. + </tr>
  52019. +
  52020. + <tr>
  52021. + <td> hnpcapable </td>
  52022. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  52023. + Read returns the current value.</td>
  52024. + <td> Read/Write</td>
  52025. + </tr>
  52026. +
  52027. + <tr>
  52028. + <td> srpcapable </td>
  52029. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  52030. + Read returns the current value.</td>
  52031. + <td> Read/Write</td>
  52032. + </tr>
  52033. +
  52034. + <tr>
  52035. + <td> hsic_connect </td>
  52036. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  52037. + Read returns the current value.</td>
  52038. + <td> Read/Write</td>
  52039. + </tr>
  52040. +
  52041. + <tr>
  52042. + <td> inv_sel_hsic </td>
  52043. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  52044. + Read returns the current value.</td>
  52045. + <td> Read/Write</td>
  52046. + </tr>
  52047. +
  52048. + <tr>
  52049. + <td> hnp </td>
  52050. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  52051. + <td> Read/Write</td>
  52052. + </tr>
  52053. +
  52054. + <tr>
  52055. + <td> srp </td>
  52056. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  52057. + <td> Read/Write</td>
  52058. + </tr>
  52059. +
  52060. + <tr>
  52061. + <td> buspower </td>
  52062. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  52063. + <td> Read/Write</td>
  52064. + </tr>
  52065. +
  52066. + <tr>
  52067. + <td> bussuspend </td>
  52068. + <td> Suspends the USB bus.</td>
  52069. + <td> Read/Write</td>
  52070. + </tr>
  52071. +
  52072. + <tr>
  52073. + <td> busconnected </td>
  52074. + <td> Gets the connection status of the bus</td>
  52075. + <td> Read</td>
  52076. + </tr>
  52077. +
  52078. + <tr>
  52079. + <td> gotgctl </td>
  52080. + <td> Gets or sets the Core Control Status Register.</td>
  52081. + <td> Read/Write</td>
  52082. + </tr>
  52083. +
  52084. + <tr>
  52085. + <td> gusbcfg </td>
  52086. + <td> Gets or sets the Core USB Configuration Register</td>
  52087. + <td> Read/Write</td>
  52088. + </tr>
  52089. +
  52090. + <tr>
  52091. + <td> grxfsiz </td>
  52092. + <td> Gets or sets the Receive FIFO Size Register</td>
  52093. + <td> Read/Write</td>
  52094. + </tr>
  52095. +
  52096. + <tr>
  52097. + <td> gnptxfsiz </td>
  52098. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  52099. + <td> Read/Write</td>
  52100. + </tr>
  52101. +
  52102. + <tr>
  52103. + <td> gpvndctl </td>
  52104. + <td> Gets or sets the PHY Vendor Control Register</td>
  52105. + <td> Read/Write</td>
  52106. + </tr>
  52107. +
  52108. + <tr>
  52109. + <td> ggpio </td>
  52110. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  52111. + or sets the upper 16 bits.</td>
  52112. + <td> Read/Write</td>
  52113. + </tr>
  52114. +
  52115. + <tr>
  52116. + <td> guid </td>
  52117. + <td> Gets or sets the value of the User ID Register</td>
  52118. + <td> Read/Write</td>
  52119. + </tr>
  52120. +
  52121. + <tr>
  52122. + <td> gsnpsid </td>
  52123. + <td> Gets the value of the Synopsys ID Regester</td>
  52124. + <td> Read</td>
  52125. + </tr>
  52126. +
  52127. + <tr>
  52128. + <td> devspeed </td>
  52129. + <td> Gets or sets the device speed setting in the DCFG register</td>
  52130. + <td> Read/Write</td>
  52131. + </tr>
  52132. +
  52133. + <tr>
  52134. + <td> enumspeed </td>
  52135. + <td> Gets the device enumeration Speed.</td>
  52136. + <td> Read</td>
  52137. + </tr>
  52138. +
  52139. + <tr>
  52140. + <td> hptxfsiz </td>
  52141. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  52142. + <td> Read</td>
  52143. + </tr>
  52144. +
  52145. + <tr>
  52146. + <td> hprt0 </td>
  52147. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  52148. + <td> Read/Write</td>
  52149. + </tr>
  52150. +
  52151. + <tr>
  52152. + <td> regoffset </td>
  52153. + <td> Sets the register offset for the next Register Access</td>
  52154. + <td> Read/Write</td>
  52155. + </tr>
  52156. +
  52157. + <tr>
  52158. + <td> regvalue </td>
  52159. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  52160. + <td> Read/Write</td>
  52161. + </tr>
  52162. +
  52163. + <tr>
  52164. + <td> remote_wakeup </td>
  52165. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  52166. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  52167. + Wakeup signalling bit in the Device Control Register is set for 1
  52168. + milli-second.</td>
  52169. + <td> Read/Write</td>
  52170. + </tr>
  52171. +
  52172. + <tr>
  52173. + <td> rem_wakeup_pwrdn </td>
  52174. + <td> On read, shows the status core - hibernated or not. On write, initiates
  52175. + a remote wakeup of the device from Hibernation. </td>
  52176. + <td> Read/Write</td>
  52177. + </tr>
  52178. +
  52179. + <tr>
  52180. + <td> mode_ch_tim_en </td>
  52181. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  52182. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  52183. + after Suspend or LPM. </td>
  52184. + <td> Read/Write</td>
  52185. + </tr>
  52186. +
  52187. + <tr>
  52188. + <td> fr_interval </td>
  52189. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  52190. + reload HFIR register during runtime. The application can write a value to this
  52191. + register only after the Port Enable bit of the Host Port Control and Status
  52192. + register (HPRT.PrtEnaPort) has been set </td>
  52193. + <td> Read/Write</td>
  52194. + </tr>
  52195. +
  52196. + <tr>
  52197. + <td> disconnect_us </td>
  52198. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  52199. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  52200. + <td> Read/Write</td>
  52201. + </tr>
  52202. +
  52203. + <tr>
  52204. + <td> regdump </td>
  52205. + <td> Dumps the contents of core registers.</td>
  52206. + <td> Read</td>
  52207. + </tr>
  52208. +
  52209. + <tr>
  52210. + <td> spramdump </td>
  52211. + <td> Dumps the contents of core registers.</td>
  52212. + <td> Read</td>
  52213. + </tr>
  52214. +
  52215. + <tr>
  52216. + <td> hcddump </td>
  52217. + <td> Dumps the current HCD state.</td>
  52218. + <td> Read</td>
  52219. + </tr>
  52220. +
  52221. + <tr>
  52222. + <td> hcd_frrem </td>
  52223. + <td> Shows the average value of the Frame Remaining
  52224. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  52225. + occurs. This can be used to determine the average interrupt latency. Also
  52226. + shows the average Frame Remaining value for start_transfer and the "a" and
  52227. + "b" sample points. The "a" and "b" sample points may be used during debugging
  52228. + bto determine how long it takes to execute a section of the HCD code.</td>
  52229. + <td> Read</td>
  52230. + </tr>
  52231. +
  52232. + <tr>
  52233. + <td> rd_reg_test </td>
  52234. + <td> Displays the time required to read the GNPTXFSIZ register many times
  52235. + (the output shows the number of times the register is read).
  52236. + <td> Read</td>
  52237. + </tr>
  52238. +
  52239. + <tr>
  52240. + <td> wr_reg_test </td>
  52241. + <td> Displays the time required to write the GNPTXFSIZ register many times
  52242. + (the output shows the number of times the register is written).
  52243. + <td> Read</td>
  52244. + </tr>
  52245. +
  52246. + <tr>
  52247. + <td> lpm_response </td>
  52248. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  52249. + <td> Write</td>
  52250. + </tr>
  52251. +
  52252. + <tr>
  52253. + <td> sleep_status </td>
  52254. + <td> Shows sleep status of device.
  52255. + <td> Read</td>
  52256. + </tr>
  52257. +
  52258. + </table>
  52259. +
  52260. + Example usage:
  52261. + To get the current mode:
  52262. + cat /sys/devices/lm0/mode
  52263. +
  52264. + To power down the USB:
  52265. + echo 0 > /sys/devices/lm0/buspower
  52266. + */
  52267. +
  52268. +#include "dwc_otg_os_dep.h"
  52269. +#include "dwc_os.h"
  52270. +#include "dwc_otg_driver.h"
  52271. +#include "dwc_otg_attr.h"
  52272. +#include "dwc_otg_core_if.h"
  52273. +#include "dwc_otg_pcd_if.h"
  52274. +#include "dwc_otg_hcd_if.h"
  52275. +
  52276. +/*
  52277. + * MACROs for defining sysfs attribute
  52278. + */
  52279. +#ifdef LM_INTERFACE
  52280. +
  52281. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52282. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52283. +{ \
  52284. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52285. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52286. + uint32_t val; \
  52287. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52288. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  52289. +}
  52290. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52291. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52292. + const char *buf, size_t count) \
  52293. +{ \
  52294. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52295. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52296. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  52297. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  52298. + return count; \
  52299. +}
  52300. +
  52301. +#elif defined(PCI_INTERFACE)
  52302. +
  52303. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52304. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52305. +{ \
  52306. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52307. + uint32_t val; \
  52308. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52309. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  52310. +}
  52311. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52312. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52313. + const char *buf, size_t count) \
  52314. +{ \
  52315. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52316. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  52317. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  52318. + return count; \
  52319. +}
  52320. +
  52321. +#elif defined(PLATFORM_INTERFACE)
  52322. +
  52323. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52324. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52325. +{ \
  52326. + struct platform_device *platform_dev = \
  52327. + container_of(_dev, struct platform_device, dev); \
  52328. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52329. + uint32_t val; \
  52330. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  52331. + __func__, _dev, platform_dev, otg_dev); \
  52332. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52333. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  52334. +}
  52335. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52336. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52337. + const char *buf, size_t count) \
  52338. +{ \
  52339. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  52340. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52341. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  52342. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  52343. + return count; \
  52344. +}
  52345. +#endif
  52346. +
  52347. +/*
  52348. + * MACROs for defining sysfs attribute for 32-bit registers
  52349. + */
  52350. +#ifdef LM_INTERFACE
  52351. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52352. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52353. +{ \
  52354. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52355. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52356. + uint32_t val; \
  52357. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52358. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  52359. +}
  52360. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  52361. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52362. + const char *buf, size_t count) \
  52363. +{ \
  52364. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52365. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52366. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  52367. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  52368. + return count; \
  52369. +}
  52370. +#elif defined(PCI_INTERFACE)
  52371. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52372. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52373. +{ \
  52374. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52375. + uint32_t val; \
  52376. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52377. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  52378. +}
  52379. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  52380. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52381. + const char *buf, size_t count) \
  52382. +{ \
  52383. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52384. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  52385. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  52386. + return count; \
  52387. +}
  52388. +
  52389. +#elif defined(PLATFORM_INTERFACE)
  52390. +#include "dwc_otg_dbg.h"
  52391. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52392. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52393. +{ \
  52394. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  52395. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52396. + uint32_t val; \
  52397. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  52398. + __func__, _dev, platform_dev, otg_dev); \
  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. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  52407. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52408. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  52409. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  52410. + return count; \
  52411. +}
  52412. +
  52413. +#endif
  52414. +
  52415. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  52416. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52417. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52418. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  52419. +
  52420. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  52421. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52422. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  52423. +
  52424. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  52425. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52426. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  52427. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  52428. +
  52429. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  52430. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52431. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  52432. +
  52433. +/** @name Functions for Show/Store of Attributes */
  52434. +/**@{*/
  52435. +
  52436. +/**
  52437. + * Helper function returning the otg_device structure of the given device
  52438. + */
  52439. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  52440. +{
  52441. + dwc_otg_device_t *otg_dev;
  52442. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  52443. + return otg_dev;
  52444. +}
  52445. +
  52446. +/**
  52447. + * Show the register offset of the Register Access.
  52448. + */
  52449. +static ssize_t regoffset_show(struct device *_dev,
  52450. + struct device_attribute *attr, char *buf)
  52451. +{
  52452. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52453. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  52454. + otg_dev->os_dep.reg_offset);
  52455. +}
  52456. +
  52457. +/**
  52458. + * Set the register offset for the next Register Access Read/Write
  52459. + */
  52460. +static ssize_t regoffset_store(struct device *_dev,
  52461. + struct device_attribute *attr,
  52462. + const char *buf, size_t count)
  52463. +{
  52464. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52465. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  52466. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  52467. + if (offset < SZ_256K) {
  52468. +#elif defined(PCI_INTERFACE)
  52469. + if (offset < 0x00040000) {
  52470. +#endif
  52471. + otg_dev->os_dep.reg_offset = offset;
  52472. + } else {
  52473. + dev_err(_dev, "invalid offset\n");
  52474. + }
  52475. +
  52476. + return count;
  52477. +}
  52478. +
  52479. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  52480. +
  52481. +/**
  52482. + * Show the value of the register at the offset in the reg_offset
  52483. + * attribute.
  52484. + */
  52485. +static ssize_t regvalue_show(struct device *_dev,
  52486. + struct device_attribute *attr, char *buf)
  52487. +{
  52488. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52489. + uint32_t val;
  52490. + volatile uint32_t *addr;
  52491. +
  52492. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  52493. + /* Calculate the address */
  52494. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  52495. + (uint8_t *) otg_dev->os_dep.base);
  52496. + val = DWC_READ_REG32(addr);
  52497. + return snprintf(buf,
  52498. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  52499. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  52500. + val);
  52501. + } else {
  52502. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  52503. + return sprintf(buf, "invalid offset\n");
  52504. + }
  52505. +}
  52506. +
  52507. +/**
  52508. + * Store the value in the register at the offset in the reg_offset
  52509. + * attribute.
  52510. + *
  52511. + */
  52512. +static ssize_t regvalue_store(struct device *_dev,
  52513. + struct device_attribute *attr,
  52514. + const char *buf, size_t count)
  52515. +{
  52516. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52517. + volatile uint32_t *addr;
  52518. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52519. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  52520. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  52521. + /* Calculate the address */
  52522. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  52523. + (uint8_t *) otg_dev->os_dep.base);
  52524. + DWC_WRITE_REG32(addr, val);
  52525. + } else {
  52526. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  52527. + otg_dev->os_dep.reg_offset);
  52528. + }
  52529. + return count;
  52530. +}
  52531. +
  52532. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  52533. +
  52534. +/*
  52535. + * Attributes
  52536. + */
  52537. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  52538. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  52539. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  52540. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  52541. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  52542. +
  52543. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  52544. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  52545. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  52546. +
  52547. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  52548. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  52549. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  52550. + "GUSBCFG");
  52551. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  52552. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  52553. + "GRXFSIZ");
  52554. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  52555. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  52556. + "GNPTXFSIZ");
  52557. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  52558. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  52559. + "GPVNDCTL");
  52560. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  52561. + &(otg_dev->core_if->core_global_regs->ggpio),
  52562. + "GGPIO");
  52563. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  52564. + "GUID");
  52565. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  52566. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  52567. + "GSNPSID");
  52568. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  52569. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  52570. +
  52571. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  52572. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  52573. + "HPTXFSIZ");
  52574. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  52575. +
  52576. +/**
  52577. + * @todo Add code to initiate the HNP.
  52578. + */
  52579. +/**
  52580. + * Show the HNP status bit
  52581. + */
  52582. +static ssize_t hnp_show(struct device *_dev,
  52583. + struct device_attribute *attr, char *buf)
  52584. +{
  52585. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52586. + return sprintf(buf, "HstNegScs = 0x%x\n",
  52587. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  52588. +}
  52589. +
  52590. +/**
  52591. + * Set the HNP Request bit
  52592. + */
  52593. +static ssize_t hnp_store(struct device *_dev,
  52594. + struct device_attribute *attr,
  52595. + const char *buf, size_t count)
  52596. +{
  52597. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52598. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52599. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  52600. + return count;
  52601. +}
  52602. +
  52603. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  52604. +
  52605. +/**
  52606. + * @todo Add code to initiate the SRP.
  52607. + */
  52608. +/**
  52609. + * Show the SRP status bit
  52610. + */
  52611. +static ssize_t srp_show(struct device *_dev,
  52612. + struct device_attribute *attr, char *buf)
  52613. +{
  52614. +#ifndef DWC_HOST_ONLY
  52615. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52616. + return sprintf(buf, "SesReqScs = 0x%x\n",
  52617. + dwc_otg_get_srpstatus(otg_dev->core_if));
  52618. +#else
  52619. + return sprintf(buf, "Host Only Mode!\n");
  52620. +#endif
  52621. +}
  52622. +
  52623. +/**
  52624. + * Set the SRP Request bit
  52625. + */
  52626. +static ssize_t srp_store(struct device *_dev,
  52627. + struct device_attribute *attr,
  52628. + const char *buf, size_t count)
  52629. +{
  52630. +#ifndef DWC_HOST_ONLY
  52631. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52632. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  52633. +#endif
  52634. + return count;
  52635. +}
  52636. +
  52637. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  52638. +
  52639. +/**
  52640. + * @todo Need to do more for power on/off?
  52641. + */
  52642. +/**
  52643. + * Show the Bus Power status
  52644. + */
  52645. +static ssize_t buspower_show(struct device *_dev,
  52646. + struct device_attribute *attr, char *buf)
  52647. +{
  52648. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52649. + return sprintf(buf, "Bus Power = 0x%x\n",
  52650. + dwc_otg_get_prtpower(otg_dev->core_if));
  52651. +}
  52652. +
  52653. +/**
  52654. + * Set the Bus Power status
  52655. + */
  52656. +static ssize_t buspower_store(struct device *_dev,
  52657. + struct device_attribute *attr,
  52658. + const char *buf, size_t count)
  52659. +{
  52660. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52661. + uint32_t on = simple_strtoul(buf, NULL, 16);
  52662. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  52663. + return count;
  52664. +}
  52665. +
  52666. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  52667. +
  52668. +/**
  52669. + * @todo Need to do more for suspend?
  52670. + */
  52671. +/**
  52672. + * Show the Bus Suspend status
  52673. + */
  52674. +static ssize_t bussuspend_show(struct device *_dev,
  52675. + struct device_attribute *attr, char *buf)
  52676. +{
  52677. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52678. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  52679. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  52680. +}
  52681. +
  52682. +/**
  52683. + * Set the Bus Suspend status
  52684. + */
  52685. +static ssize_t bussuspend_store(struct device *_dev,
  52686. + struct device_attribute *attr,
  52687. + const char *buf, size_t count)
  52688. +{
  52689. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52690. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52691. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  52692. + return count;
  52693. +}
  52694. +
  52695. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  52696. +
  52697. +/**
  52698. + * Show the Mode Change Ready Timer status
  52699. + */
  52700. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  52701. + struct device_attribute *attr, char *buf)
  52702. +{
  52703. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52704. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  52705. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  52706. +}
  52707. +
  52708. +/**
  52709. + * Set the Mode Change Ready Timer status
  52710. + */
  52711. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  52712. + struct device_attribute *attr,
  52713. + const char *buf, size_t count)
  52714. +{
  52715. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52716. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52717. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  52718. + return count;
  52719. +}
  52720. +
  52721. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  52722. +
  52723. +/**
  52724. + * Show the value of HFIR Frame Interval bitfield
  52725. + */
  52726. +static ssize_t fr_interval_show(struct device *_dev,
  52727. + struct device_attribute *attr, char *buf)
  52728. +{
  52729. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52730. + return sprintf(buf, "Frame Interval = 0x%x\n",
  52731. + dwc_otg_get_fr_interval(otg_dev->core_if));
  52732. +}
  52733. +
  52734. +/**
  52735. + * Set the HFIR Frame Interval value
  52736. + */
  52737. +static ssize_t fr_interval_store(struct device *_dev,
  52738. + struct device_attribute *attr,
  52739. + const char *buf, size_t count)
  52740. +{
  52741. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52742. + uint32_t in = simple_strtoul(buf, NULL, 10);
  52743. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  52744. + return count;
  52745. +}
  52746. +
  52747. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  52748. +
  52749. +/**
  52750. + * Show the status of Remote Wakeup.
  52751. + */
  52752. +static ssize_t remote_wakeup_show(struct device *_dev,
  52753. + struct device_attribute *attr, char *buf)
  52754. +{
  52755. +#ifndef DWC_HOST_ONLY
  52756. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52757. +
  52758. + return sprintf(buf,
  52759. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  52760. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  52761. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  52762. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  52763. +#else
  52764. + return sprintf(buf, "Host Only Mode!\n");
  52765. +#endif /* DWC_HOST_ONLY */
  52766. +}
  52767. +
  52768. +/**
  52769. + * Initiate a remote wakeup of the host. The Device control register
  52770. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  52771. + * flag is set.
  52772. + *
  52773. + */
  52774. +static ssize_t remote_wakeup_store(struct device *_dev,
  52775. + struct device_attribute *attr,
  52776. + const char *buf, size_t count)
  52777. +{
  52778. +#ifndef DWC_HOST_ONLY
  52779. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52780. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52781. +
  52782. + if (val & 1) {
  52783. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  52784. + } else {
  52785. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  52786. + }
  52787. +#endif /* DWC_HOST_ONLY */
  52788. + return count;
  52789. +}
  52790. +
  52791. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  52792. + remote_wakeup_store);
  52793. +
  52794. +/**
  52795. + * Show the whether core is hibernated or not.
  52796. + */
  52797. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  52798. + struct device_attribute *attr, char *buf)
  52799. +{
  52800. +#ifndef DWC_HOST_ONLY
  52801. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52802. +
  52803. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  52804. + DWC_PRINTF("Core is in hibernation\n");
  52805. + } else {
  52806. + DWC_PRINTF("Core is not in hibernation\n");
  52807. + }
  52808. +#endif /* DWC_HOST_ONLY */
  52809. + return 0;
  52810. +}
  52811. +
  52812. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  52813. + int rem_wakeup, int reset);
  52814. +
  52815. +/**
  52816. + * Initiate a remote wakeup of the device to exit from hibernation.
  52817. + */
  52818. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  52819. + struct device_attribute *attr,
  52820. + const char *buf, size_t count)
  52821. +{
  52822. +#ifndef DWC_HOST_ONLY
  52823. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52824. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  52825. +#endif
  52826. + return count;
  52827. +}
  52828. +
  52829. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  52830. + rem_wakeup_pwrdn_store);
  52831. +
  52832. +static ssize_t disconnect_us(struct device *_dev,
  52833. + struct device_attribute *attr,
  52834. + const char *buf, size_t count)
  52835. +{
  52836. +
  52837. +#ifndef DWC_HOST_ONLY
  52838. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52839. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52840. + DWC_PRINTF("The Passed value is %04x\n", val);
  52841. +
  52842. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  52843. +
  52844. +#endif /* DWC_HOST_ONLY */
  52845. + return count;
  52846. +}
  52847. +
  52848. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  52849. +
  52850. +/**
  52851. + * Dump global registers and either host or device registers (depending on the
  52852. + * current mode of the core).
  52853. + */
  52854. +static ssize_t regdump_show(struct device *_dev,
  52855. + struct device_attribute *attr, char *buf)
  52856. +{
  52857. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52858. +
  52859. + dwc_otg_dump_global_registers(otg_dev->core_if);
  52860. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  52861. + dwc_otg_dump_host_registers(otg_dev->core_if);
  52862. + } else {
  52863. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  52864. +
  52865. + }
  52866. + return sprintf(buf, "Register Dump\n");
  52867. +}
  52868. +
  52869. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  52870. +
  52871. +/**
  52872. + * Dump global registers and either host or device registers (depending on the
  52873. + * current mode of the core).
  52874. + */
  52875. +static ssize_t spramdump_show(struct device *_dev,
  52876. + struct device_attribute *attr, char *buf)
  52877. +{
  52878. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52879. +
  52880. + //dwc_otg_dump_spram(otg_dev->core_if);
  52881. +
  52882. + return sprintf(buf, "SPRAM Dump\n");
  52883. +}
  52884. +
  52885. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  52886. +
  52887. +/**
  52888. + * Dump the current hcd state.
  52889. + */
  52890. +static ssize_t hcddump_show(struct device *_dev,
  52891. + struct device_attribute *attr, char *buf)
  52892. +{
  52893. +#ifndef DWC_DEVICE_ONLY
  52894. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52895. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  52896. +#endif /* DWC_DEVICE_ONLY */
  52897. + return sprintf(buf, "HCD Dump\n");
  52898. +}
  52899. +
  52900. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  52901. +
  52902. +/**
  52903. + * Dump the average frame remaining at SOF. This can be used to
  52904. + * determine average interrupt latency. Frame remaining is also shown for
  52905. + * start transfer and two additional sample points.
  52906. + */
  52907. +static ssize_t hcd_frrem_show(struct device *_dev,
  52908. + struct device_attribute *attr, char *buf)
  52909. +{
  52910. +#ifndef DWC_DEVICE_ONLY
  52911. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52912. +
  52913. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  52914. +#endif /* DWC_DEVICE_ONLY */
  52915. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  52916. +}
  52917. +
  52918. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  52919. +
  52920. +/**
  52921. + * Displays the time required to read the GNPTXFSIZ register many times (the
  52922. + * output shows the number of times the register is read).
  52923. + */
  52924. +#define RW_REG_COUNT 10000000
  52925. +#define MSEC_PER_JIFFIE 1000/HZ
  52926. +static ssize_t rd_reg_test_show(struct device *_dev,
  52927. + struct device_attribute *attr, char *buf)
  52928. +{
  52929. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52930. + int i;
  52931. + int time;
  52932. + int start_jiffies;
  52933. +
  52934. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  52935. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  52936. + start_jiffies = jiffies;
  52937. + for (i = 0; i < RW_REG_COUNT; i++) {
  52938. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  52939. + }
  52940. + time = jiffies - start_jiffies;
  52941. + return sprintf(buf,
  52942. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  52943. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  52944. +}
  52945. +
  52946. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  52947. +
  52948. +/**
  52949. + * Displays the time required to write the GNPTXFSIZ register many times (the
  52950. + * output shows the number of times the register is written).
  52951. + */
  52952. +static ssize_t wr_reg_test_show(struct device *_dev,
  52953. + struct device_attribute *attr, char *buf)
  52954. +{
  52955. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52956. + uint32_t reg_val;
  52957. + int i;
  52958. + int time;
  52959. + int start_jiffies;
  52960. +
  52961. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  52962. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  52963. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  52964. + start_jiffies = jiffies;
  52965. + for (i = 0; i < RW_REG_COUNT; i++) {
  52966. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  52967. + }
  52968. + time = jiffies - start_jiffies;
  52969. + return sprintf(buf,
  52970. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  52971. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  52972. +}
  52973. +
  52974. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  52975. +
  52976. +#ifdef CONFIG_USB_DWC_OTG_LPM
  52977. +
  52978. +/**
  52979. +* Show the lpm_response attribute.
  52980. +*/
  52981. +static ssize_t lpmresp_show(struct device *_dev,
  52982. + struct device_attribute *attr, char *buf)
  52983. +{
  52984. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52985. +
  52986. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  52987. + return sprintf(buf, "** LPM is DISABLED **\n");
  52988. +
  52989. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  52990. + return sprintf(buf, "** Current mode is not device mode\n");
  52991. + }
  52992. + return sprintf(buf, "lpm_response = %d\n",
  52993. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  52994. +}
  52995. +
  52996. +/**
  52997. +* Store the lpm_response attribute.
  52998. +*/
  52999. +static ssize_t lpmresp_store(struct device *_dev,
  53000. + struct device_attribute *attr,
  53001. + const char *buf, size_t count)
  53002. +{
  53003. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  53004. + uint32_t val = simple_strtoul(buf, NULL, 16);
  53005. +
  53006. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  53007. + return 0;
  53008. + }
  53009. +
  53010. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  53011. + return 0;
  53012. + }
  53013. +
  53014. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  53015. + return count;
  53016. +}
  53017. +
  53018. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  53019. +
  53020. +/**
  53021. +* Show the sleep_status attribute.
  53022. +*/
  53023. +static ssize_t sleepstatus_show(struct device *_dev,
  53024. + struct device_attribute *attr, char *buf)
  53025. +{
  53026. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  53027. + return sprintf(buf, "Sleep Status = %d\n",
  53028. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  53029. +}
  53030. +
  53031. +/**
  53032. + * Store the sleep_status attribure.
  53033. + */
  53034. +static ssize_t sleepstatus_store(struct device *_dev,
  53035. + struct device_attribute *attr,
  53036. + const char *buf, size_t count)
  53037. +{
  53038. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  53039. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  53040. +
  53041. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  53042. + if (dwc_otg_is_host_mode(core_if)) {
  53043. +
  53044. + DWC_PRINTF("Host initiated resume\n");
  53045. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  53046. + }
  53047. + }
  53048. +
  53049. + return count;
  53050. +}
  53051. +
  53052. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  53053. + sleepstatus_store);
  53054. +
  53055. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  53056. +
  53057. +/**@}*/
  53058. +
  53059. +/**
  53060. + * Create the device files
  53061. + */
  53062. +void dwc_otg_attr_create(
  53063. +#ifdef LM_INTERFACE
  53064. + struct lm_device *dev
  53065. +#elif defined(PCI_INTERFACE)
  53066. + struct pci_dev *dev
  53067. +#elif defined(PLATFORM_INTERFACE)
  53068. + struct platform_device *dev
  53069. +#endif
  53070. + )
  53071. +{
  53072. + int error;
  53073. +
  53074. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  53075. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  53076. + error = device_create_file(&dev->dev, &dev_attr_mode);
  53077. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  53078. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  53079. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  53080. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  53081. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  53082. + error = device_create_file(&dev->dev, &dev_attr_srp);
  53083. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  53084. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  53085. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  53086. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  53087. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  53088. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  53089. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  53090. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  53091. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  53092. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  53093. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  53094. + error = device_create_file(&dev->dev, &dev_attr_guid);
  53095. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  53096. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  53097. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  53098. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  53099. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  53100. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  53101. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  53102. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  53103. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  53104. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  53105. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  53106. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  53107. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  53108. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  53109. +#ifdef CONFIG_USB_DWC_OTG_LPM
  53110. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  53111. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  53112. +#endif
  53113. +}
  53114. +
  53115. +/**
  53116. + * Remove the device files
  53117. + */
  53118. +void dwc_otg_attr_remove(
  53119. +#ifdef LM_INTERFACE
  53120. + struct lm_device *dev
  53121. +#elif defined(PCI_INTERFACE)
  53122. + struct pci_dev *dev
  53123. +#elif defined(PLATFORM_INTERFACE)
  53124. + struct platform_device *dev
  53125. +#endif
  53126. + )
  53127. +{
  53128. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  53129. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  53130. + device_remove_file(&dev->dev, &dev_attr_mode);
  53131. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  53132. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  53133. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  53134. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  53135. + device_remove_file(&dev->dev, &dev_attr_hnp);
  53136. + device_remove_file(&dev->dev, &dev_attr_srp);
  53137. + device_remove_file(&dev->dev, &dev_attr_buspower);
  53138. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  53139. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  53140. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  53141. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  53142. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  53143. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  53144. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  53145. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  53146. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  53147. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  53148. + device_remove_file(&dev->dev, &dev_attr_guid);
  53149. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  53150. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  53151. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  53152. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  53153. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  53154. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  53155. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  53156. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  53157. + device_remove_file(&dev->dev, &dev_attr_regdump);
  53158. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  53159. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  53160. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  53161. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  53162. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  53163. +#ifdef CONFIG_USB_DWC_OTG_LPM
  53164. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  53165. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  53166. +#endif
  53167. +}
  53168. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_attr.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  53169. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_attr.h 1970-01-01 01:00:00.000000000 +0100
  53170. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.h 2014-03-11 17:51:27.000000000 +0100
  53171. @@ -0,0 +1,89 @@
  53172. +/* ==========================================================================
  53173. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  53174. + * $Revision: #13 $
  53175. + * $Date: 2010/06/21 $
  53176. + * $Change: 1532021 $
  53177. + *
  53178. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  53179. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  53180. + * otherwise expressly agreed to in writing between Synopsys and you.
  53181. + *
  53182. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  53183. + * any End User Software License Agreement or Agreement for Licensed Product
  53184. + * with Synopsys or any supplement thereto. You are permitted to use and
  53185. + * redistribute this Software in source and binary forms, with or without
  53186. + * modification, provided that redistributions of source code must retain this
  53187. + * notice. You may not view, use, disclose, copy or distribute this file or
  53188. + * any information contained herein except pursuant to this license grant from
  53189. + * Synopsys. If you do not agree with this notice, including the disclaimer
  53190. + * below, then you are not authorized to use the Software.
  53191. + *
  53192. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  53193. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53194. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53195. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  53196. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53197. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  53198. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  53199. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53200. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53201. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  53202. + * DAMAGE.
  53203. + * ========================================================================== */
  53204. +
  53205. +#if !defined(__DWC_OTG_ATTR_H__)
  53206. +#define __DWC_OTG_ATTR_H__
  53207. +
  53208. +/** @file
  53209. + * This file contains the interface to the Linux device attributes.
  53210. + */
  53211. +extern struct device_attribute dev_attr_regoffset;
  53212. +extern struct device_attribute dev_attr_regvalue;
  53213. +
  53214. +extern struct device_attribute dev_attr_mode;
  53215. +extern struct device_attribute dev_attr_hnpcapable;
  53216. +extern struct device_attribute dev_attr_srpcapable;
  53217. +extern struct device_attribute dev_attr_hnp;
  53218. +extern struct device_attribute dev_attr_srp;
  53219. +extern struct device_attribute dev_attr_buspower;
  53220. +extern struct device_attribute dev_attr_bussuspend;
  53221. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  53222. +extern struct device_attribute dev_attr_fr_interval;
  53223. +extern struct device_attribute dev_attr_busconnected;
  53224. +extern struct device_attribute dev_attr_gotgctl;
  53225. +extern struct device_attribute dev_attr_gusbcfg;
  53226. +extern struct device_attribute dev_attr_grxfsiz;
  53227. +extern struct device_attribute dev_attr_gnptxfsiz;
  53228. +extern struct device_attribute dev_attr_gpvndctl;
  53229. +extern struct device_attribute dev_attr_ggpio;
  53230. +extern struct device_attribute dev_attr_guid;
  53231. +extern struct device_attribute dev_attr_gsnpsid;
  53232. +extern struct device_attribute dev_attr_devspeed;
  53233. +extern struct device_attribute dev_attr_enumspeed;
  53234. +extern struct device_attribute dev_attr_hptxfsiz;
  53235. +extern struct device_attribute dev_attr_hprt0;
  53236. +#ifdef CONFIG_USB_DWC_OTG_LPM
  53237. +extern struct device_attribute dev_attr_lpm_response;
  53238. +extern struct device_attribute devi_attr_sleep_status;
  53239. +#endif
  53240. +
  53241. +void dwc_otg_attr_create(
  53242. +#ifdef LM_INTERFACE
  53243. + struct lm_device *dev
  53244. +#elif defined(PCI_INTERFACE)
  53245. + struct pci_dev *dev
  53246. +#elif defined(PLATFORM_INTERFACE)
  53247. + struct platform_device *dev
  53248. +#endif
  53249. + );
  53250. +
  53251. +void dwc_otg_attr_remove(
  53252. +#ifdef LM_INTERFACE
  53253. + struct lm_device *dev
  53254. +#elif defined(PCI_INTERFACE)
  53255. + struct pci_dev *dev
  53256. +#elif defined(PLATFORM_INTERFACE)
  53257. + struct platform_device *dev
  53258. +#endif
  53259. + );
  53260. +#endif
  53261. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cfi.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  53262. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 1970-01-01 01:00:00.000000000 +0100
  53263. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 2014-03-11 17:51:27.000000000 +0100
  53264. @@ -0,0 +1,1876 @@
  53265. +/* ==========================================================================
  53266. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  53267. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  53268. + * otherwise expressly agreed to in writing between Synopsys and you.
  53269. + *
  53270. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  53271. + * any End User Software License Agreement or Agreement for Licensed Product
  53272. + * with Synopsys or any supplement thereto. You are permitted to use and
  53273. + * redistribute this Software in source and binary forms, with or without
  53274. + * modification, provided that redistributions of source code must retain this
  53275. + * notice. You may not view, use, disclose, copy or distribute this file or
  53276. + * any information contained herein except pursuant to this license grant from
  53277. + * Synopsys. If you do not agree with this notice, including the disclaimer
  53278. + * below, then you are not authorized to use the Software.
  53279. + *
  53280. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  53281. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53282. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53283. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  53284. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53285. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  53286. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  53287. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53288. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53289. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  53290. + * DAMAGE.
  53291. + * ========================================================================== */
  53292. +
  53293. +/** @file
  53294. + *
  53295. + * This file contains the most of the CFI(Core Feature Interface)
  53296. + * implementation for the OTG.
  53297. + */
  53298. +
  53299. +#ifdef DWC_UTE_CFI
  53300. +
  53301. +#include "dwc_otg_pcd.h"
  53302. +#include "dwc_otg_cfi.h"
  53303. +
  53304. +/** This definition should actually migrate to the Portability Library */
  53305. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  53306. +
  53307. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  53308. +
  53309. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  53310. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  53311. + struct dwc_otg_pcd *pcd,
  53312. + struct cfi_usb_ctrlrequest *ctrl_req);
  53313. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  53314. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  53315. + struct cfi_usb_ctrlrequest *req);
  53316. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  53317. + struct cfi_usb_ctrlrequest *req);
  53318. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  53319. + struct cfi_usb_ctrlrequest *req);
  53320. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  53321. + struct cfi_usb_ctrlrequest *req);
  53322. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  53323. +
  53324. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  53325. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  53326. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  53327. +
  53328. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  53329. +
  53330. +/** This is the header of the all features descriptor */
  53331. +static cfi_all_features_header_t all_props_desc_header = {
  53332. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  53333. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  53334. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  53335. +};
  53336. +
  53337. +/** This is an array of statically allocated feature descriptors */
  53338. +static cfi_feature_desc_header_t prop_descs[] = {
  53339. +
  53340. + /* FT_ID_DMA_MODE */
  53341. + {
  53342. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  53343. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53344. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  53345. + },
  53346. +
  53347. + /* FT_ID_DMA_BUFFER_SETUP */
  53348. + {
  53349. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  53350. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53351. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53352. + },
  53353. +
  53354. + /* FT_ID_DMA_BUFF_ALIGN */
  53355. + {
  53356. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  53357. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53358. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53359. + },
  53360. +
  53361. + /* FT_ID_DMA_CONCAT_SETUP */
  53362. + {
  53363. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  53364. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53365. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53366. + },
  53367. +
  53368. + /* FT_ID_DMA_CIRCULAR */
  53369. + {
  53370. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  53371. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53372. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53373. + },
  53374. +
  53375. + /* FT_ID_THRESHOLD_SETUP */
  53376. + {
  53377. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  53378. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53379. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53380. + },
  53381. +
  53382. + /* FT_ID_DFIFO_DEPTH */
  53383. + {
  53384. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  53385. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  53386. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53387. + },
  53388. +
  53389. + /* FT_ID_TX_FIFO_DEPTH */
  53390. + {
  53391. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  53392. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53393. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53394. + },
  53395. +
  53396. + /* FT_ID_RX_FIFO_DEPTH */
  53397. + {
  53398. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  53399. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53400. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53401. + }
  53402. +};
  53403. +
  53404. +/** The table of feature names */
  53405. +cfi_string_t prop_name_table[] = {
  53406. + {FT_ID_DMA_MODE, "dma_mode"},
  53407. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  53408. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  53409. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  53410. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  53411. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  53412. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  53413. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  53414. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  53415. + {}
  53416. +};
  53417. +
  53418. +/************************************************************************/
  53419. +
  53420. +/**
  53421. + * Returns the name of the feature by its ID
  53422. + * or NULL if no featute ID matches.
  53423. + *
  53424. + */
  53425. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  53426. +{
  53427. + cfi_string_t *pstr;
  53428. + *len = 0;
  53429. +
  53430. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  53431. + if (pstr->id == prop_id) {
  53432. + *len = DWC_STRLEN(pstr->s);
  53433. + return pstr->s;
  53434. + }
  53435. + }
  53436. + return NULL;
  53437. +}
  53438. +
  53439. +/**
  53440. + * This function handles all CFI specific control requests.
  53441. + *
  53442. + * Return a negative value to stall the DCE.
  53443. + */
  53444. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  53445. +{
  53446. + int retval = 0;
  53447. + dwc_otg_pcd_ep_t *ep = NULL;
  53448. + cfiobject_t *cfi = pcd->cfi;
  53449. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  53450. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  53451. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  53452. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  53453. + uint32_t regaddr = 0;
  53454. + uint32_t regval = 0;
  53455. +
  53456. + /* Save this Control Request in the CFI object.
  53457. + * The data field will be assigned in the data stage completion CB function.
  53458. + */
  53459. + cfi->ctrl_req = *ctrl;
  53460. + cfi->ctrl_req.data = NULL;
  53461. +
  53462. + cfi->need_gadget_att = 0;
  53463. + cfi->need_status_in_complete = 0;
  53464. +
  53465. + switch (ctrl->bRequest) {
  53466. + case VEN_CORE_GET_FEATURES:
  53467. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  53468. + if (retval >= 0) {
  53469. + //dump_msg(cfi->buf_in.buf, retval);
  53470. + ep = &pcd->ep0;
  53471. +
  53472. + retval = min((uint16_t) retval, wLen);
  53473. + /* Transfer this buffer to the host through the EP0-IN EP */
  53474. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  53475. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  53476. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  53477. + ep->dwc_ep.xfer_len = retval;
  53478. + ep->dwc_ep.xfer_count = 0;
  53479. + ep->dwc_ep.sent_zlp = 0;
  53480. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53481. +
  53482. + pcd->ep0_pending = 1;
  53483. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53484. + }
  53485. + retval = 0;
  53486. + break;
  53487. +
  53488. + case VEN_CORE_GET_FEATURE:
  53489. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  53490. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  53491. + pcd, ctrl);
  53492. + if (retval >= 0) {
  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. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  53509. + dump_msg(cfi->buf_in.buf, retval);
  53510. + break;
  53511. +
  53512. + case VEN_CORE_SET_FEATURE:
  53513. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  53514. + /* Set up an XFER to get the data stage of the control request,
  53515. + * which is the new value of the feature to be modified.
  53516. + */
  53517. + ep = &pcd->ep0;
  53518. + ep->dwc_ep.is_in = 0;
  53519. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  53520. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  53521. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  53522. + ep->dwc_ep.xfer_len = wLen;
  53523. + ep->dwc_ep.xfer_count = 0;
  53524. + ep->dwc_ep.sent_zlp = 0;
  53525. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53526. +
  53527. + pcd->ep0_pending = 1;
  53528. + /* Read the control write's data stage */
  53529. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53530. + retval = 0;
  53531. + break;
  53532. +
  53533. + case VEN_CORE_RESET_FEATURES:
  53534. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  53535. + cfi->need_gadget_att = 1;
  53536. + cfi->need_status_in_complete = 1;
  53537. + retval = cfi_preproc_reset(pcd, ctrl);
  53538. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  53539. + break;
  53540. +
  53541. + case VEN_CORE_ACTIVATE_FEATURES:
  53542. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  53543. + break;
  53544. +
  53545. + case VEN_CORE_READ_REGISTER:
  53546. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  53547. + /* wValue optionally contains the HI WORD of the register offset and
  53548. + * wIndex contains the LOW WORD of the register offset
  53549. + */
  53550. + if (wValue == 0) {
  53551. + /* @TODO - MAS - fix the access to the base field */
  53552. + regaddr = 0;
  53553. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  53554. + //GET_CORE_IF(pcd)->co
  53555. + regaddr |= wIndex;
  53556. + } else {
  53557. + regaddr = (wValue << 16) | wIndex;
  53558. + }
  53559. +
  53560. + /* Read a 32-bit value of the memory at the regaddr */
  53561. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  53562. +
  53563. + ep = &pcd->ep0;
  53564. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  53565. + ep->dwc_ep.is_in = 1;
  53566. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  53567. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  53568. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  53569. + ep->dwc_ep.xfer_len = wLen;
  53570. + ep->dwc_ep.xfer_count = 0;
  53571. + ep->dwc_ep.sent_zlp = 0;
  53572. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53573. +
  53574. + pcd->ep0_pending = 1;
  53575. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53576. + cfi->need_gadget_att = 0;
  53577. + retval = 0;
  53578. + break;
  53579. +
  53580. + case VEN_CORE_WRITE_REGISTER:
  53581. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  53582. + /* Set up an XFER to get the data stage of the control request,
  53583. + * which is the new value of the register to be modified.
  53584. + */
  53585. + ep = &pcd->ep0;
  53586. + ep->dwc_ep.is_in = 0;
  53587. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  53588. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  53589. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  53590. + ep->dwc_ep.xfer_len = wLen;
  53591. + ep->dwc_ep.xfer_count = 0;
  53592. + ep->dwc_ep.sent_zlp = 0;
  53593. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53594. +
  53595. + pcd->ep0_pending = 1;
  53596. + /* Read the control write's data stage */
  53597. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53598. + retval = 0;
  53599. + break;
  53600. +
  53601. + default:
  53602. + retval = -DWC_E_NOT_SUPPORTED;
  53603. + break;
  53604. + }
  53605. +
  53606. + return retval;
  53607. +}
  53608. +
  53609. +/**
  53610. + * This function prepares the core features descriptors and copies its
  53611. + * raw representation into the buffer <buf>.
  53612. + *
  53613. + * The buffer structure is as follows:
  53614. + * all_features_header (8 bytes)
  53615. + * features_#1 (8 bytes + feature name string length)
  53616. + * features_#2 (8 bytes + feature name string length)
  53617. + * .....
  53618. + * features_#n - where n=the total count of feature descriptors
  53619. + */
  53620. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  53621. +{
  53622. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  53623. + cfi_feature_desc_header_t *prop;
  53624. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  53625. + cfi_all_features_header_t *tmp;
  53626. + uint8_t *tmpbuf = buf;
  53627. + const uint8_t *pname = NULL;
  53628. + int i, j, namelen = 0, totlen;
  53629. +
  53630. + /* Prepare and copy the core features into the buffer */
  53631. + CFI_INFO("%s:\n", __func__);
  53632. +
  53633. + tmp = (cfi_all_features_header_t *) tmpbuf;
  53634. + *tmp = *all_props_hdr;
  53635. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  53636. +
  53637. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  53638. + for (i = 0; i < j; i++, prop_hdr++) {
  53639. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  53640. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  53641. + *prop = *prop_hdr;
  53642. +
  53643. + prop->bNameLen = namelen;
  53644. + prop->wLength =
  53645. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  53646. + namelen);
  53647. +
  53648. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  53649. + dwc_memcpy(tmpbuf, pname, namelen);
  53650. + tmpbuf += namelen;
  53651. + }
  53652. +
  53653. + totlen = tmpbuf - buf;
  53654. +
  53655. + if (totlen > 0) {
  53656. + tmp = (cfi_all_features_header_t *) buf;
  53657. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  53658. + }
  53659. +
  53660. + return totlen;
  53661. +}
  53662. +
  53663. +/**
  53664. + * This function releases all the dynamic memory in the CFI object.
  53665. + */
  53666. +static void cfi_release(cfiobject_t * cfiobj)
  53667. +{
  53668. + cfi_ep_t *cfiep;
  53669. + dwc_list_link_t *tmp;
  53670. +
  53671. + CFI_INFO("%s\n", __func__);
  53672. +
  53673. + if (cfiobj->buf_in.buf) {
  53674. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  53675. + cfiobj->buf_in.addr);
  53676. + cfiobj->buf_in.buf = NULL;
  53677. + }
  53678. +
  53679. + if (cfiobj->buf_out.buf) {
  53680. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  53681. + cfiobj->buf_out.addr);
  53682. + cfiobj->buf_out.buf = NULL;
  53683. + }
  53684. +
  53685. + /* Free the Buffer Setup values for each EP */
  53686. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  53687. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  53688. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  53689. + cfi_free_ep_bs_dyn_data(cfiep);
  53690. + }
  53691. +}
  53692. +
  53693. +/**
  53694. + * This function frees the dynamically allocated EP buffer setup data.
  53695. + */
  53696. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  53697. +{
  53698. + if (cfiep->bm_sg) {
  53699. + DWC_FREE(cfiep->bm_sg);
  53700. + cfiep->bm_sg = NULL;
  53701. + }
  53702. +
  53703. + if (cfiep->bm_align) {
  53704. + DWC_FREE(cfiep->bm_align);
  53705. + cfiep->bm_align = NULL;
  53706. + }
  53707. +
  53708. + if (cfiep->bm_concat) {
  53709. + if (NULL != cfiep->bm_concat->wTxBytes) {
  53710. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  53711. + cfiep->bm_concat->wTxBytes = NULL;
  53712. + }
  53713. + DWC_FREE(cfiep->bm_concat);
  53714. + cfiep->bm_concat = NULL;
  53715. + }
  53716. +}
  53717. +
  53718. +/**
  53719. + * This function initializes the default values of the features
  53720. + * for a specific endpoint and should be called only once when
  53721. + * the EP is enabled first time.
  53722. + */
  53723. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  53724. +{
  53725. + int retval = 0;
  53726. +
  53727. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  53728. + if (NULL == cfiep->bm_sg) {
  53729. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  53730. + return -DWC_E_NO_MEMORY;
  53731. + }
  53732. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  53733. +
  53734. + /* For the Concatenation feature's default value we do not allocate
  53735. + * memory for the wTxBytes field - it will be done in the set_feature_value
  53736. + * request handler.
  53737. + */
  53738. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  53739. + if (NULL == cfiep->bm_concat) {
  53740. + CFI_INFO
  53741. + ("Failed to allocate memory for CONCATENATION feature value\n");
  53742. + DWC_FREE(cfiep->bm_sg);
  53743. + return -DWC_E_NO_MEMORY;
  53744. + }
  53745. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  53746. +
  53747. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  53748. + if (NULL == cfiep->bm_align) {
  53749. + CFI_INFO
  53750. + ("Failed to allocate memory for Alignment feature value\n");
  53751. + DWC_FREE(cfiep->bm_sg);
  53752. + DWC_FREE(cfiep->bm_concat);
  53753. + return -DWC_E_NO_MEMORY;
  53754. + }
  53755. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  53756. +
  53757. + return retval;
  53758. +}
  53759. +
  53760. +/**
  53761. + * The callback function that notifies the CFI on the activation of
  53762. + * an endpoint in the PCD. The following steps are done in this function:
  53763. + *
  53764. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  53765. + * active endpoint)
  53766. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  53767. + * Set the Buffer Mode to standard
  53768. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  53769. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  53770. + */
  53771. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  53772. + struct dwc_otg_pcd_ep *ep)
  53773. +{
  53774. + cfi_ep_t *cfiep;
  53775. + int retval = -DWC_E_NOT_SUPPORTED;
  53776. +
  53777. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  53778. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  53779. + /* MAS - Check whether this endpoint already is in the list */
  53780. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  53781. +
  53782. + if (NULL == cfiep) {
  53783. + /* Allocate a cfi_ep_t object */
  53784. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  53785. + if (NULL == cfiep) {
  53786. + CFI_INFO
  53787. + ("Unable to allocate memory for <cfiep> in function %s\n",
  53788. + __func__);
  53789. + return -DWC_E_NO_MEMORY;
  53790. + }
  53791. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  53792. +
  53793. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  53794. + cfiep->ep = ep;
  53795. +
  53796. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  53797. + ep->dwc_ep.descs =
  53798. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  53799. + sizeof(dwc_otg_dma_desc_t),
  53800. + &ep->dwc_ep.descs_dma_addr);
  53801. +
  53802. + if (NULL == ep->dwc_ep.descs) {
  53803. + DWC_FREE(cfiep);
  53804. + return -DWC_E_NO_MEMORY;
  53805. + }
  53806. +
  53807. + DWC_LIST_INIT(&cfiep->lh);
  53808. +
  53809. + /* Set the buffer mode to BM_STANDARD. It will be modified
  53810. + * when building descriptors for a specific buffer mode */
  53811. + ep->dwc_ep.buff_mode = BM_STANDARD;
  53812. +
  53813. + /* Create and initialize the default values for this EP's Buffer modes */
  53814. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  53815. + return retval;
  53816. +
  53817. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  53818. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  53819. + retval = 0;
  53820. + } else { /* The sought EP already is in the list */
  53821. + CFI_INFO("%s: The sought EP already is in the list\n",
  53822. + __func__);
  53823. + }
  53824. +
  53825. + return retval;
  53826. +}
  53827. +
  53828. +/**
  53829. + * This function is called when the data stage of a 3-stage Control Write request
  53830. + * is complete.
  53831. + *
  53832. + */
  53833. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  53834. + struct dwc_otg_pcd *pcd)
  53835. +{
  53836. + uint32_t addr, reg_value;
  53837. + uint16_t wIndex, wValue;
  53838. + uint8_t bRequest;
  53839. + uint8_t *buf = cfi->buf_out.buf;
  53840. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  53841. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  53842. + int retval = -DWC_E_NOT_SUPPORTED;
  53843. +
  53844. + CFI_INFO("%s\n", __func__);
  53845. +
  53846. + bRequest = ctrl_req->bRequest;
  53847. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  53848. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  53849. +
  53850. + /*
  53851. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  53852. + * The request should be already saved in the command stage by now.
  53853. + */
  53854. + ctrl_req->data = cfi->buf_out.buf;
  53855. + cfi->need_status_in_complete = 0;
  53856. + cfi->need_gadget_att = 0;
  53857. +
  53858. + switch (bRequest) {
  53859. + case VEN_CORE_WRITE_REGISTER:
  53860. + /* The buffer contains raw data of the new value for the register */
  53861. + reg_value = *((uint32_t *) buf);
  53862. + if (wValue == 0) {
  53863. + addr = 0;
  53864. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  53865. + addr += wIndex;
  53866. + } else {
  53867. + addr = (wValue << 16) | wIndex;
  53868. + }
  53869. +
  53870. + //writel(reg_value, addr);
  53871. +
  53872. + retval = 0;
  53873. + cfi->need_status_in_complete = 1;
  53874. + break;
  53875. +
  53876. + case VEN_CORE_SET_FEATURE:
  53877. + /* The buffer contains raw data of the new value of the feature */
  53878. + retval = cfi_set_feature_value(pcd);
  53879. + if (retval < 0)
  53880. + return retval;
  53881. +
  53882. + cfi->need_status_in_complete = 1;
  53883. + break;
  53884. +
  53885. + default:
  53886. + break;
  53887. + }
  53888. +
  53889. + return retval;
  53890. +}
  53891. +
  53892. +/**
  53893. + * This function builds the DMA descriptors for the SG buffer mode.
  53894. + */
  53895. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53896. + dwc_otg_pcd_request_t * req)
  53897. +{
  53898. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53899. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  53900. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53901. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  53902. + dma_addr_t buff_addr = req->dma;
  53903. + int i;
  53904. + uint32_t txsize, off;
  53905. +
  53906. + txsize = sgval->wSize;
  53907. + off = sgval->bOffset;
  53908. +
  53909. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  53910. +// __func__, cfiep->ep->ep.name, txsize, off);
  53911. +
  53912. + for (i = 0; i < sgval->bCount; i++) {
  53913. + desc->status.b.bs = BS_HOST_BUSY;
  53914. + desc->buf = buff_addr;
  53915. + desc->status.b.l = 0;
  53916. + desc->status.b.ioc = 0;
  53917. + desc->status.b.sp = 0;
  53918. + desc->status.b.bytes = txsize;
  53919. + desc->status.b.bs = BS_HOST_READY;
  53920. +
  53921. + /* Set the next address of the buffer */
  53922. + buff_addr += txsize + off;
  53923. + desc_last = desc;
  53924. + desc++;
  53925. + }
  53926. +
  53927. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  53928. + desc_last->status.b.l = 1;
  53929. + desc_last->status.b.ioc = 1;
  53930. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  53931. + /* Save the last DMA descriptor pointer */
  53932. + cfiep->dma_desc_last = desc_last;
  53933. + cfiep->desc_count = sgval->bCount;
  53934. +}
  53935. +
  53936. +/**
  53937. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  53938. + */
  53939. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53940. + dwc_otg_pcd_request_t * req)
  53941. +{
  53942. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53943. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  53944. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53945. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  53946. + dma_addr_t buff_addr = req->dma;
  53947. + int i;
  53948. + uint16_t *txsize;
  53949. +
  53950. + txsize = concatval->wTxBytes;
  53951. +
  53952. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  53953. + desc->buf = buff_addr;
  53954. + desc->status.b.bs = BS_HOST_BUSY;
  53955. + desc->status.b.l = 0;
  53956. + desc->status.b.ioc = 0;
  53957. + desc->status.b.sp = 0;
  53958. + desc->status.b.bytes = *txsize;
  53959. + desc->status.b.bs = BS_HOST_READY;
  53960. +
  53961. + txsize++;
  53962. + /* Set the next address of the buffer */
  53963. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  53964. + desc_last = desc;
  53965. + desc++;
  53966. + }
  53967. +
  53968. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  53969. + desc_last->status.b.l = 1;
  53970. + desc_last->status.b.ioc = 1;
  53971. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  53972. + cfiep->dma_desc_last = desc_last;
  53973. + cfiep->desc_count = concatval->hdr.bDescCount;
  53974. +}
  53975. +
  53976. +/**
  53977. + * This function builds the DMA descriptors for the Circular buffer mode
  53978. + */
  53979. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53980. + dwc_otg_pcd_request_t * req)
  53981. +{
  53982. + /* @todo: MAS - add implementation when this feature needs to be tested */
  53983. +}
  53984. +
  53985. +/**
  53986. + * This function builds the DMA descriptors for the Alignment buffer mode
  53987. + */
  53988. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53989. + dwc_otg_pcd_request_t * req)
  53990. +{
  53991. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53992. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  53993. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53994. + dma_addr_t buff_addr = req->dma;
  53995. +
  53996. + desc->status.b.bs = BS_HOST_BUSY;
  53997. + desc->status.b.l = 1;
  53998. + desc->status.b.ioc = 1;
  53999. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  54000. + desc->status.b.bytes = req->length;
  54001. + /* Adjust the buffer alignment */
  54002. + desc->buf = (buff_addr + alignval->bAlign);
  54003. + desc->status.b.bs = BS_HOST_READY;
  54004. + cfiep->dma_desc_last = desc;
  54005. + cfiep->desc_count = 1;
  54006. +}
  54007. +
  54008. +/**
  54009. + * This function builds the DMA descriptors chain for different modes of the
  54010. + * buffer setup of an endpoint.
  54011. + */
  54012. +static void cfi_build_descriptors(struct cfiobject *cfi,
  54013. + struct dwc_otg_pcd *pcd,
  54014. + struct dwc_otg_pcd_ep *ep,
  54015. + dwc_otg_pcd_request_t * req)
  54016. +{
  54017. + cfi_ep_t *cfiep;
  54018. +
  54019. + /* Get the cfiep by the dwc_otg_pcd_ep */
  54020. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  54021. + if (NULL == cfiep) {
  54022. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  54023. + __func__);
  54024. + return;
  54025. + }
  54026. +
  54027. + cfiep->xfer_len = req->length;
  54028. +
  54029. + /* Iterate through all the DMA descriptors */
  54030. + switch (cfiep->ep->dwc_ep.buff_mode) {
  54031. + case BM_SG:
  54032. + cfi_build_sg_descs(cfi, cfiep, req);
  54033. + break;
  54034. +
  54035. + case BM_CONCAT:
  54036. + cfi_build_concat_descs(cfi, cfiep, req);
  54037. + break;
  54038. +
  54039. + case BM_CIRCULAR:
  54040. + cfi_build_circ_descs(cfi, cfiep, req);
  54041. + break;
  54042. +
  54043. + case BM_ALIGN:
  54044. + cfi_build_align_descs(cfi, cfiep, req);
  54045. + break;
  54046. +
  54047. + default:
  54048. + break;
  54049. + }
  54050. +}
  54051. +
  54052. +/**
  54053. + * Allocate DMA buffer for different Buffer modes.
  54054. + */
  54055. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  54056. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  54057. + unsigned size, gfp_t flags)
  54058. +{
  54059. + return DWC_DMA_ALLOC(size, dma);
  54060. +}
  54061. +
  54062. +/**
  54063. + * This function initializes the CFI object.
  54064. + */
  54065. +int init_cfi(cfiobject_t * cfiobj)
  54066. +{
  54067. + CFI_INFO("%s\n", __func__);
  54068. +
  54069. + /* Allocate a buffer for IN XFERs */
  54070. + cfiobj->buf_in.buf =
  54071. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  54072. + if (NULL == cfiobj->buf_in.buf) {
  54073. + CFI_INFO("Unable to allocate buffer for INs\n");
  54074. + return -DWC_E_NO_MEMORY;
  54075. + }
  54076. +
  54077. + /* Allocate a buffer for OUT XFERs */
  54078. + cfiobj->buf_out.buf =
  54079. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  54080. + if (NULL == cfiobj->buf_out.buf) {
  54081. + CFI_INFO("Unable to allocate buffer for OUT\n");
  54082. + return -DWC_E_NO_MEMORY;
  54083. + }
  54084. +
  54085. + /* Initialize the callback function pointers */
  54086. + cfiobj->ops.release = cfi_release;
  54087. + cfiobj->ops.ep_enable = cfi_ep_enable;
  54088. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  54089. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  54090. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  54091. +
  54092. + /* Initialize the list of active endpoints in the CFI object */
  54093. + DWC_LIST_INIT(&cfiobj->active_eps);
  54094. +
  54095. + return 0;
  54096. +}
  54097. +
  54098. +/**
  54099. + * This function reads the required feature's current value into the buffer
  54100. + *
  54101. + * @retval: Returns negative as error, or the data length of the feature
  54102. + */
  54103. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  54104. + struct dwc_otg_pcd *pcd,
  54105. + struct cfi_usb_ctrlrequest *ctrl_req)
  54106. +{
  54107. + int retval = -DWC_E_NOT_SUPPORTED;
  54108. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  54109. + uint16_t dfifo, rxfifo, txfifo;
  54110. +
  54111. + switch (ctrl_req->wIndex) {
  54112. + /* Whether the DDMA is enabled or not */
  54113. + case FT_ID_DMA_MODE:
  54114. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  54115. + retval = 1;
  54116. + break;
  54117. +
  54118. + case FT_ID_DMA_BUFFER_SETUP:
  54119. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  54120. + break;
  54121. +
  54122. + case FT_ID_DMA_BUFF_ALIGN:
  54123. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  54124. + break;
  54125. +
  54126. + case FT_ID_DMA_CONCAT_SETUP:
  54127. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  54128. + break;
  54129. +
  54130. + case FT_ID_DMA_CIRCULAR:
  54131. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  54132. + break;
  54133. +
  54134. + case FT_ID_THRESHOLD_SETUP:
  54135. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  54136. + break;
  54137. +
  54138. + case FT_ID_DFIFO_DEPTH:
  54139. + dfifo = get_dfifo_size(coreif);
  54140. + *((uint16_t *) buf) = dfifo;
  54141. + retval = sizeof(uint16_t);
  54142. + break;
  54143. +
  54144. + case FT_ID_TX_FIFO_DEPTH:
  54145. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  54146. + if (retval >= 0) {
  54147. + txfifo = retval;
  54148. + *((uint16_t *) buf) = txfifo;
  54149. + retval = sizeof(uint16_t);
  54150. + }
  54151. + break;
  54152. +
  54153. + case FT_ID_RX_FIFO_DEPTH:
  54154. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  54155. + if (retval >= 0) {
  54156. + rxfifo = retval;
  54157. + *((uint16_t *) buf) = rxfifo;
  54158. + retval = sizeof(uint16_t);
  54159. + }
  54160. + break;
  54161. + }
  54162. +
  54163. + return retval;
  54164. +}
  54165. +
  54166. +/**
  54167. + * This function resets the SG for the specified EP to its default value
  54168. + */
  54169. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  54170. +{
  54171. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  54172. + return 0;
  54173. +}
  54174. +
  54175. +/**
  54176. + * This function resets the Alignment for the specified EP to its default value
  54177. + */
  54178. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  54179. +{
  54180. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  54181. + return 0;
  54182. +}
  54183. +
  54184. +/**
  54185. + * This function resets the Concatenation for the specified EP to its default value
  54186. + * This function will also set the value of the wTxBytes field to NULL after
  54187. + * freeing the memory previously allocated for this field.
  54188. + */
  54189. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  54190. +{
  54191. + /* First we need to free the wTxBytes field */
  54192. + if (cfiep->bm_concat->wTxBytes) {
  54193. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  54194. + cfiep->bm_concat->wTxBytes = NULL;
  54195. + }
  54196. +
  54197. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  54198. + return 0;
  54199. +}
  54200. +
  54201. +/**
  54202. + * This function resets all the buffer setups of the specified endpoint
  54203. + */
  54204. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  54205. +{
  54206. + cfi_reset_sg_val(cfiep);
  54207. + cfi_reset_align_val(cfiep);
  54208. + cfi_reset_concat_val(cfiep);
  54209. + return 0;
  54210. +}
  54211. +
  54212. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  54213. + uint8_t rx_rst, uint8_t tx_rst)
  54214. +{
  54215. + int retval = -DWC_E_INVALID;
  54216. + uint16_t tx_siz[15];
  54217. + uint16_t rx_siz = 0;
  54218. + dwc_otg_pcd_ep_t *ep = NULL;
  54219. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54220. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54221. +
  54222. + if (rx_rst) {
  54223. + rx_siz = params->dev_rx_fifo_size;
  54224. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  54225. + }
  54226. +
  54227. + if (tx_rst) {
  54228. + if (ep_addr == 0) {
  54229. + int i;
  54230. +
  54231. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54232. + tx_siz[i] =
  54233. + core_if->core_params->dev_tx_fifo_size[i];
  54234. + core_if->core_params->dev_tx_fifo_size[i] =
  54235. + core_if->init_txfsiz[i];
  54236. + }
  54237. + } else {
  54238. +
  54239. + ep = get_ep_by_addr(pcd, ep_addr);
  54240. +
  54241. + if (NULL == ep) {
  54242. + CFI_INFO
  54243. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  54244. + __func__, ep_addr);
  54245. + return -DWC_E_INVALID;
  54246. + }
  54247. +
  54248. + tx_siz[0] =
  54249. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  54250. + 1];
  54251. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  54252. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  54253. + dwc_ep.tx_fifo_num -
  54254. + 1];
  54255. + }
  54256. + }
  54257. +
  54258. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54259. + retval = 0;
  54260. + } else {
  54261. + CFI_INFO
  54262. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  54263. + __func__);
  54264. + if (rx_rst) {
  54265. + params->dev_rx_fifo_size = rx_siz;
  54266. + }
  54267. +
  54268. + if (tx_rst) {
  54269. + if (ep_addr == 0) {
  54270. + int i;
  54271. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  54272. + i++) {
  54273. + core_if->
  54274. + core_params->dev_tx_fifo_size[i] =
  54275. + tx_siz[i];
  54276. + }
  54277. + } else {
  54278. + params->dev_tx_fifo_size[ep->
  54279. + dwc_ep.tx_fifo_num -
  54280. + 1] = tx_siz[0];
  54281. + }
  54282. + }
  54283. + retval = -DWC_E_INVALID;
  54284. + }
  54285. + return retval;
  54286. +}
  54287. +
  54288. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  54289. +{
  54290. + int retval = 0;
  54291. + cfi_ep_t *cfiep;
  54292. + cfiobject_t *cfi = pcd->cfi;
  54293. + dwc_list_link_t *tmp;
  54294. +
  54295. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  54296. + if (retval < 0) {
  54297. + return retval;
  54298. + }
  54299. +
  54300. + /* If the EP address is known then reset the features for only that EP */
  54301. + if (addr) {
  54302. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54303. + if (NULL == cfiep) {
  54304. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54305. + __func__, addr);
  54306. + return -DWC_E_INVALID;
  54307. + }
  54308. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  54309. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  54310. + }
  54311. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54312. + else {
  54313. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54314. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54315. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54316. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54317. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  54318. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  54319. + if (retval < 0) {
  54320. + CFI_INFO
  54321. + ("%s: Error resetting the feature Reset All\n",
  54322. + __func__);
  54323. + return retval;
  54324. + }
  54325. + }
  54326. + }
  54327. + return retval;
  54328. +}
  54329. +
  54330. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  54331. + uint8_t addr)
  54332. +{
  54333. + int retval = 0;
  54334. + cfi_ep_t *cfiep;
  54335. + cfiobject_t *cfi = pcd->cfi;
  54336. + dwc_list_link_t *tmp;
  54337. +
  54338. + /* If the EP address is known then reset the features for only that EP */
  54339. + if (addr) {
  54340. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54341. + if (NULL == cfiep) {
  54342. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54343. + __func__, addr);
  54344. + return -DWC_E_INVALID;
  54345. + }
  54346. + retval = cfi_reset_sg_val(cfiep);
  54347. + }
  54348. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54349. + else {
  54350. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54351. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54352. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54353. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54354. + retval = cfi_reset_sg_val(cfiep);
  54355. + if (retval < 0) {
  54356. + CFI_INFO
  54357. + ("%s: Error resetting the feature Buffer Setup\n",
  54358. + __func__);
  54359. + return retval;
  54360. + }
  54361. + }
  54362. + }
  54363. + return retval;
  54364. +}
  54365. +
  54366. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  54367. +{
  54368. + int retval = 0;
  54369. + cfi_ep_t *cfiep;
  54370. + cfiobject_t *cfi = pcd->cfi;
  54371. + dwc_list_link_t *tmp;
  54372. +
  54373. + /* If the EP address is known then reset the features for only that EP */
  54374. + if (addr) {
  54375. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54376. + if (NULL == cfiep) {
  54377. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54378. + __func__, addr);
  54379. + return -DWC_E_INVALID;
  54380. + }
  54381. + retval = cfi_reset_concat_val(cfiep);
  54382. + }
  54383. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54384. + else {
  54385. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54386. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54387. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54388. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54389. + retval = cfi_reset_concat_val(cfiep);
  54390. + if (retval < 0) {
  54391. + CFI_INFO
  54392. + ("%s: Error resetting the feature Concatenation Value\n",
  54393. + __func__);
  54394. + return retval;
  54395. + }
  54396. + }
  54397. + }
  54398. + return retval;
  54399. +}
  54400. +
  54401. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  54402. +{
  54403. + int retval = 0;
  54404. + cfi_ep_t *cfiep;
  54405. + cfiobject_t *cfi = pcd->cfi;
  54406. + dwc_list_link_t *tmp;
  54407. +
  54408. + /* If the EP address is known then reset the features for only that EP */
  54409. + if (addr) {
  54410. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54411. + if (NULL == cfiep) {
  54412. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54413. + __func__, addr);
  54414. + return -DWC_E_INVALID;
  54415. + }
  54416. + retval = cfi_reset_align_val(cfiep);
  54417. + }
  54418. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54419. + else {
  54420. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54421. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54422. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54423. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54424. + retval = cfi_reset_align_val(cfiep);
  54425. + if (retval < 0) {
  54426. + CFI_INFO
  54427. + ("%s: Error resetting the feature Aliignment Value\n",
  54428. + __func__);
  54429. + return retval;
  54430. + }
  54431. + }
  54432. + }
  54433. + return retval;
  54434. +
  54435. +}
  54436. +
  54437. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  54438. + struct cfi_usb_ctrlrequest *req)
  54439. +{
  54440. + int retval = 0;
  54441. +
  54442. + switch (req->wIndex) {
  54443. + case 0:
  54444. + /* Reset all features */
  54445. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  54446. + break;
  54447. +
  54448. + case FT_ID_DMA_BUFFER_SETUP:
  54449. + /* Reset the SG buffer setup */
  54450. + retval =
  54451. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  54452. + break;
  54453. +
  54454. + case FT_ID_DMA_CONCAT_SETUP:
  54455. + /* Reset the Concatenation buffer setup */
  54456. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  54457. + break;
  54458. +
  54459. + case FT_ID_DMA_BUFF_ALIGN:
  54460. + /* Reset the Alignment buffer setup */
  54461. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  54462. + break;
  54463. +
  54464. + case FT_ID_TX_FIFO_DEPTH:
  54465. + retval =
  54466. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  54467. + pcd->cfi->need_gadget_att = 0;
  54468. + break;
  54469. +
  54470. + case FT_ID_RX_FIFO_DEPTH:
  54471. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  54472. + pcd->cfi->need_gadget_att = 0;
  54473. + break;
  54474. + default:
  54475. + break;
  54476. + }
  54477. + return retval;
  54478. +}
  54479. +
  54480. +/**
  54481. + * This function sets a new value for the SG buffer setup.
  54482. + */
  54483. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  54484. +{
  54485. + uint8_t inaddr, outaddr;
  54486. + cfi_ep_t *epin, *epout;
  54487. + ddma_sg_buffer_setup_t *psgval;
  54488. + uint32_t desccount, size;
  54489. +
  54490. + CFI_INFO("%s\n", __func__);
  54491. +
  54492. + psgval = (ddma_sg_buffer_setup_t *) buf;
  54493. + desccount = (uint32_t) psgval->bCount;
  54494. + size = (uint32_t) psgval->wSize;
  54495. +
  54496. + /* Check the DMA descriptor count */
  54497. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  54498. + CFI_INFO
  54499. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  54500. + __func__, MAX_DMA_DESCS_PER_EP);
  54501. + return -DWC_E_INVALID;
  54502. + }
  54503. +
  54504. + /* Check the DMA descriptor count */
  54505. +
  54506. + if (size == 0) {
  54507. +
  54508. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  54509. + __func__);
  54510. +
  54511. + return -DWC_E_INVALID;
  54512. +
  54513. + }
  54514. +
  54515. + inaddr = psgval->bInEndpointAddress;
  54516. + outaddr = psgval->bOutEndpointAddress;
  54517. +
  54518. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  54519. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  54520. +
  54521. + if (NULL == epin || NULL == epout) {
  54522. + CFI_INFO
  54523. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  54524. + __func__, inaddr, outaddr);
  54525. + return -DWC_E_INVALID;
  54526. + }
  54527. +
  54528. + epin->ep->dwc_ep.buff_mode = BM_SG;
  54529. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  54530. +
  54531. + epout->ep->dwc_ep.buff_mode = BM_SG;
  54532. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  54533. +
  54534. + return 0;
  54535. +}
  54536. +
  54537. +/**
  54538. + * This function sets a new value for the buffer Alignment setup.
  54539. + */
  54540. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  54541. +{
  54542. + cfi_ep_t *ep;
  54543. + uint8_t addr;
  54544. + ddma_align_buffer_setup_t *palignval;
  54545. +
  54546. + palignval = (ddma_align_buffer_setup_t *) buf;
  54547. + addr = palignval->bEndpointAddress;
  54548. +
  54549. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54550. +
  54551. + if (NULL == ep) {
  54552. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54553. + __func__, addr);
  54554. + return -DWC_E_INVALID;
  54555. + }
  54556. +
  54557. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  54558. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  54559. +
  54560. + return 0;
  54561. +}
  54562. +
  54563. +/**
  54564. + * This function sets a new value for the Concatenation buffer setup.
  54565. + */
  54566. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  54567. +{
  54568. + uint8_t addr;
  54569. + cfi_ep_t *ep;
  54570. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  54571. + uint16_t *pVals;
  54572. + uint32_t desccount;
  54573. + int i;
  54574. + uint16_t mps;
  54575. +
  54576. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  54577. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  54578. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  54579. +
  54580. + /* Check the DMA descriptor count */
  54581. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  54582. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  54583. + __func__, MAX_DMA_DESCS_PER_EP);
  54584. + return -DWC_E_INVALID;
  54585. + }
  54586. +
  54587. + addr = pConcatValHdr->bEndpointAddress;
  54588. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54589. + if (NULL == ep) {
  54590. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54591. + __func__, addr);
  54592. + return -DWC_E_INVALID;
  54593. + }
  54594. +
  54595. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  54596. +
  54597. +#if 0
  54598. + for (i = 0; i < desccount; i++) {
  54599. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  54600. + }
  54601. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  54602. +#endif
  54603. +
  54604. + /* Check the wTxSizes to be less than or equal to the mps */
  54605. + for (i = 0; i < desccount; i++) {
  54606. + if (pVals[i] > mps) {
  54607. + CFI_INFO
  54608. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  54609. + __func__, i, pVals[i]);
  54610. + return -DWC_E_INVALID;
  54611. + }
  54612. + }
  54613. +
  54614. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  54615. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  54616. +
  54617. + /* Free the previously allocated storage for the wTxBytes */
  54618. + if (ep->bm_concat->wTxBytes) {
  54619. + DWC_FREE(ep->bm_concat->wTxBytes);
  54620. + }
  54621. +
  54622. + /* Allocate a new storage for the wTxBytes field */
  54623. + ep->bm_concat->wTxBytes =
  54624. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  54625. + if (NULL == ep->bm_concat->wTxBytes) {
  54626. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  54627. + return -DWC_E_NO_MEMORY;
  54628. + }
  54629. +
  54630. + /* Copy the new values into the wTxBytes filed */
  54631. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  54632. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  54633. +
  54634. + return 0;
  54635. +}
  54636. +
  54637. +/**
  54638. + * This function calculates the total of all FIFO sizes
  54639. + *
  54640. + * @param core_if Programming view of DWC_otg controller
  54641. + *
  54642. + * @return The total of data FIFO sizes.
  54643. + *
  54644. + */
  54645. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  54646. +{
  54647. + dwc_otg_core_params_t *params = core_if->core_params;
  54648. + uint16_t dfifo_total = 0;
  54649. + int i;
  54650. +
  54651. + /* The shared RxFIFO size */
  54652. + dfifo_total =
  54653. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  54654. +
  54655. + /* Add up each TxFIFO size to the total */
  54656. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54657. + dfifo_total += params->dev_tx_fifo_size[i];
  54658. + }
  54659. +
  54660. + return dfifo_total;
  54661. +}
  54662. +
  54663. +/**
  54664. + * This function returns Rx FIFO size
  54665. + *
  54666. + * @param core_if Programming view of DWC_otg controller
  54667. + *
  54668. + * @return The total of data FIFO sizes.
  54669. + *
  54670. + */
  54671. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  54672. +{
  54673. + switch (wValue >> 8) {
  54674. + case 0:
  54675. + return (core_if->pwron_rxfsiz <
  54676. + 32768) ? core_if->pwron_rxfsiz : 32768;
  54677. + break;
  54678. + case 1:
  54679. + return core_if->core_params->dev_rx_fifo_size;
  54680. + break;
  54681. + default:
  54682. + return -DWC_E_INVALID;
  54683. + break;
  54684. + }
  54685. +}
  54686. +
  54687. +/**
  54688. + * This function returns Tx FIFO size for IN EP
  54689. + *
  54690. + * @param core_if Programming view of DWC_otg controller
  54691. + *
  54692. + * @return The total of data FIFO sizes.
  54693. + *
  54694. + */
  54695. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  54696. +{
  54697. + dwc_otg_pcd_ep_t *ep;
  54698. +
  54699. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  54700. +
  54701. + if (NULL == ep) {
  54702. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54703. + __func__, wValue & 0xff);
  54704. + return -DWC_E_INVALID;
  54705. + }
  54706. +
  54707. + if (!ep->dwc_ep.is_in) {
  54708. + CFI_INFO
  54709. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  54710. + __func__, wValue & 0xff);
  54711. + return -DWC_E_INVALID;
  54712. + }
  54713. +
  54714. + switch (wValue >> 8) {
  54715. + case 0:
  54716. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  54717. + [ep->dwc_ep.tx_fifo_num - 1] <
  54718. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  54719. + dwc_ep.tx_fifo_num
  54720. + - 1] : 32768;
  54721. + break;
  54722. + case 1:
  54723. + return GET_CORE_IF(pcd)->core_params->
  54724. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  54725. + break;
  54726. + default:
  54727. + return -DWC_E_INVALID;
  54728. + break;
  54729. + }
  54730. +}
  54731. +
  54732. +/**
  54733. + * This function checks if the submitted combination of
  54734. + * device mode FIFO sizes is possible or not.
  54735. + *
  54736. + * @param core_if Programming view of DWC_otg controller
  54737. + *
  54738. + * @return 1 if possible, 0 otherwise.
  54739. + *
  54740. + */
  54741. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  54742. +{
  54743. + uint16_t dfifo_actual = 0;
  54744. + dwc_otg_core_params_t *params = core_if->core_params;
  54745. + uint16_t start_addr = 0;
  54746. + int i;
  54747. +
  54748. + dfifo_actual =
  54749. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  54750. +
  54751. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54752. + dfifo_actual += params->dev_tx_fifo_size[i];
  54753. + }
  54754. +
  54755. + if (dfifo_actual > core_if->total_fifo_size) {
  54756. + return 0;
  54757. + }
  54758. +
  54759. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  54760. + return 0;
  54761. +
  54762. + if (params->dev_nperio_tx_fifo_size > 32768
  54763. + || params->dev_nperio_tx_fifo_size < 16)
  54764. + return 0;
  54765. +
  54766. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54767. +
  54768. + if (params->dev_tx_fifo_size[i] > 768
  54769. + || params->dev_tx_fifo_size[i] < 4)
  54770. + return 0;
  54771. + }
  54772. +
  54773. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  54774. + return 0;
  54775. + start_addr = params->dev_rx_fifo_size;
  54776. +
  54777. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  54778. + return 0;
  54779. + start_addr += params->dev_nperio_tx_fifo_size;
  54780. +
  54781. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54782. +
  54783. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  54784. + return 0;
  54785. + start_addr += params->dev_tx_fifo_size[i];
  54786. + }
  54787. +
  54788. + return 1;
  54789. +}
  54790. +
  54791. +/**
  54792. + * This function resizes Device mode FIFOs
  54793. + *
  54794. + * @param core_if Programming view of DWC_otg controller
  54795. + *
  54796. + * @return 1 if successful, 0 otherwise
  54797. + *
  54798. + */
  54799. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  54800. +{
  54801. + int i = 0;
  54802. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  54803. + dwc_otg_core_params_t *params = core_if->core_params;
  54804. + uint32_t rx_fifo_size;
  54805. + fifosize_data_t nptxfifosize;
  54806. + fifosize_data_t txfifosize[15];
  54807. +
  54808. + uint32_t rx_fsz_bak;
  54809. + uint32_t nptxfsz_bak;
  54810. + uint32_t txfsz_bak[15];
  54811. +
  54812. + uint16_t start_address;
  54813. + uint8_t retval = 1;
  54814. +
  54815. + if (!check_fifo_sizes(core_if)) {
  54816. + return 0;
  54817. + }
  54818. +
  54819. + /* Configure data FIFO sizes */
  54820. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  54821. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  54822. + rx_fifo_size = params->dev_rx_fifo_size;
  54823. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  54824. +
  54825. + /*
  54826. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  54827. + * Indexes of the FIFO size module parameters in the
  54828. + * dev_tx_fifo_size array and the FIFO size registers in
  54829. + * the dtxfsiz array run from 0 to 14.
  54830. + */
  54831. +
  54832. + /* Non-periodic Tx FIFO */
  54833. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  54834. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  54835. + start_address = params->dev_rx_fifo_size;
  54836. + nptxfifosize.b.startaddr = start_address;
  54837. +
  54838. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  54839. +
  54840. + start_address += nptxfifosize.b.depth;
  54841. +
  54842. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54843. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  54844. +
  54845. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  54846. + txfifosize[i].b.startaddr = start_address;
  54847. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  54848. + txfifosize[i].d32);
  54849. +
  54850. + start_address += txfifosize[i].b.depth;
  54851. + }
  54852. +
  54853. + /** Check if register values are set correctly */
  54854. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  54855. + retval = 0;
  54856. + }
  54857. +
  54858. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  54859. + retval = 0;
  54860. + }
  54861. +
  54862. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54863. + if (txfifosize[i].d32 !=
  54864. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  54865. + retval = 0;
  54866. + }
  54867. + }
  54868. +
  54869. + /** If register values are not set correctly, reset old values */
  54870. + if (retval == 0) {
  54871. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  54872. +
  54873. + /* Non-periodic Tx FIFO */
  54874. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  54875. +
  54876. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54877. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  54878. + txfsz_bak[i]);
  54879. + }
  54880. + }
  54881. + } else {
  54882. + return 0;
  54883. + }
  54884. +
  54885. + /* Flush the FIFOs */
  54886. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  54887. + dwc_otg_flush_rx_fifo(core_if);
  54888. +
  54889. + return retval;
  54890. +}
  54891. +
  54892. +/**
  54893. + * This function sets a new value for the buffer Alignment setup.
  54894. + */
  54895. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  54896. +{
  54897. + int retval;
  54898. + uint32_t fsiz;
  54899. + uint16_t size;
  54900. + uint16_t ep_addr;
  54901. + dwc_otg_pcd_ep_t *ep;
  54902. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54903. + tx_fifo_size_setup_t *ptxfifoval;
  54904. +
  54905. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  54906. + ep_addr = ptxfifoval->bEndpointAddress;
  54907. + size = ptxfifoval->wDepth;
  54908. +
  54909. + ep = get_ep_by_addr(pcd, ep_addr);
  54910. +
  54911. + CFI_INFO
  54912. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  54913. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  54914. +
  54915. + if (NULL == ep) {
  54916. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54917. + __func__, ep_addr);
  54918. + return -DWC_E_INVALID;
  54919. + }
  54920. +
  54921. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  54922. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  54923. +
  54924. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54925. + retval = 0;
  54926. + } else {
  54927. + CFI_INFO
  54928. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  54929. + __func__, ep_addr);
  54930. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  54931. + retval = -DWC_E_INVALID;
  54932. + }
  54933. +
  54934. + return retval;
  54935. +}
  54936. +
  54937. +/**
  54938. + * This function sets a new value for the buffer Alignment setup.
  54939. + */
  54940. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  54941. +{
  54942. + int retval;
  54943. + uint32_t fsiz;
  54944. + uint16_t size;
  54945. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54946. + rx_fifo_size_setup_t *prxfifoval;
  54947. +
  54948. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  54949. + size = prxfifoval->wDepth;
  54950. +
  54951. + fsiz = params->dev_rx_fifo_size;
  54952. + params->dev_rx_fifo_size = size;
  54953. +
  54954. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54955. + retval = 0;
  54956. + } else {
  54957. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  54958. + __func__);
  54959. + params->dev_rx_fifo_size = fsiz;
  54960. + retval = -DWC_E_INVALID;
  54961. + }
  54962. +
  54963. + return retval;
  54964. +}
  54965. +
  54966. +/**
  54967. + * This function reads the SG of an EP's buffer setup into the buffer buf
  54968. + */
  54969. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  54970. + struct cfi_usb_ctrlrequest *req)
  54971. +{
  54972. + int retval = -DWC_E_INVALID;
  54973. + uint8_t addr;
  54974. + cfi_ep_t *ep;
  54975. +
  54976. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  54977. + addr = req->wValue & 0xFF;
  54978. + if (addr == 0) /* The address should be non-zero */
  54979. + return retval;
  54980. +
  54981. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54982. + if (NULL == ep) {
  54983. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  54984. + __func__, addr);
  54985. + return retval;
  54986. + }
  54987. +
  54988. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  54989. + retval = BS_SG_VAL_DESC_LEN;
  54990. + return retval;
  54991. +}
  54992. +
  54993. +/**
  54994. + * This function reads the Concatenation value of an EP's buffer mode into
  54995. + * the buffer buf
  54996. + */
  54997. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  54998. + struct cfi_usb_ctrlrequest *req)
  54999. +{
  55000. + int retval = -DWC_E_INVALID;
  55001. + uint8_t addr;
  55002. + cfi_ep_t *ep;
  55003. + uint8_t desc_count;
  55004. +
  55005. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  55006. + addr = req->wValue & 0xFF;
  55007. + if (addr == 0) /* The address should be non-zero */
  55008. + return retval;
  55009. +
  55010. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  55011. + if (NULL == ep) {
  55012. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  55013. + __func__, addr);
  55014. + return retval;
  55015. + }
  55016. +
  55017. + /* Copy the header to the buffer */
  55018. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  55019. + /* Advance the buffer pointer by the header size */
  55020. + buf += BS_CONCAT_VAL_HDR_LEN;
  55021. +
  55022. + desc_count = ep->bm_concat->hdr.bDescCount;
  55023. + /* Copy alll the wTxBytes to the buffer */
  55024. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  55025. +
  55026. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  55027. + return retval;
  55028. +}
  55029. +
  55030. +/**
  55031. + * This function reads the buffer Alignment value of an EP's buffer mode into
  55032. + * the buffer buf
  55033. + *
  55034. + * @return The total number of bytes copied to the buffer or negative error code.
  55035. + */
  55036. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  55037. + struct cfi_usb_ctrlrequest *req)
  55038. +{
  55039. + int retval = -DWC_E_INVALID;
  55040. + uint8_t addr;
  55041. + cfi_ep_t *ep;
  55042. +
  55043. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  55044. + addr = req->wValue & 0xFF;
  55045. + if (addr == 0) /* The address should be non-zero */
  55046. + return retval;
  55047. +
  55048. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  55049. + if (NULL == ep) {
  55050. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  55051. + __func__, addr);
  55052. + return retval;
  55053. + }
  55054. +
  55055. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  55056. + retval = BS_ALIGN_VAL_HDR_LEN;
  55057. +
  55058. + return retval;
  55059. +}
  55060. +
  55061. +/**
  55062. + * This function sets a new value for the specified feature
  55063. + *
  55064. + * @param pcd A pointer to the PCD object
  55065. + *
  55066. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  55067. + */
  55068. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  55069. +{
  55070. + int retval = -DWC_E_NOT_SUPPORTED;
  55071. + uint16_t wIndex, wValue;
  55072. + uint8_t bRequest;
  55073. + struct dwc_otg_core_if *coreif;
  55074. + cfiobject_t *cfi = pcd->cfi;
  55075. + struct cfi_usb_ctrlrequest *ctrl_req;
  55076. + uint8_t *buf;
  55077. + ctrl_req = &cfi->ctrl_req;
  55078. +
  55079. + buf = pcd->cfi->ctrl_req.data;
  55080. +
  55081. + coreif = GET_CORE_IF(pcd);
  55082. + bRequest = ctrl_req->bRequest;
  55083. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  55084. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  55085. +
  55086. + /* See which feature is to be modified */
  55087. + switch (wIndex) {
  55088. + case FT_ID_DMA_BUFFER_SETUP:
  55089. + /* Modify the feature */
  55090. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  55091. + return retval;
  55092. +
  55093. + /* And send this request to the gadget */
  55094. + cfi->need_gadget_att = 1;
  55095. + break;
  55096. +
  55097. + case FT_ID_DMA_BUFF_ALIGN:
  55098. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  55099. + return retval;
  55100. + cfi->need_gadget_att = 1;
  55101. + break;
  55102. +
  55103. + case FT_ID_DMA_CONCAT_SETUP:
  55104. + /* Modify the feature */
  55105. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  55106. + return retval;
  55107. + cfi->need_gadget_att = 1;
  55108. + break;
  55109. +
  55110. + case FT_ID_DMA_CIRCULAR:
  55111. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  55112. + break;
  55113. +
  55114. + case FT_ID_THRESHOLD_SETUP:
  55115. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  55116. + break;
  55117. +
  55118. + case FT_ID_DFIFO_DEPTH:
  55119. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  55120. + break;
  55121. +
  55122. + case FT_ID_TX_FIFO_DEPTH:
  55123. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  55124. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  55125. + return retval;
  55126. + cfi->need_gadget_att = 0;
  55127. + break;
  55128. +
  55129. + case FT_ID_RX_FIFO_DEPTH:
  55130. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  55131. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  55132. + return retval;
  55133. + cfi->need_gadget_att = 0;
  55134. + break;
  55135. + }
  55136. +
  55137. + return retval;
  55138. +}
  55139. +
  55140. +#endif //DWC_UTE_CFI
  55141. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cfi.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  55142. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 1970-01-01 01:00:00.000000000 +0100
  55143. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 2014-03-11 17:51:27.000000000 +0100
  55144. @@ -0,0 +1,320 @@
  55145. +/* ==========================================================================
  55146. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  55147. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  55148. + * otherwise expressly agreed to in writing between Synopsys and you.
  55149. + *
  55150. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  55151. + * any End User Software License Agreement or Agreement for Licensed Product
  55152. + * with Synopsys or any supplement thereto. You are permitted to use and
  55153. + * redistribute this Software in source and binary forms, with or without
  55154. + * modification, provided that redistributions of source code must retain this
  55155. + * notice. You may not view, use, disclose, copy or distribute this file or
  55156. + * any information contained herein except pursuant to this license grant from
  55157. + * Synopsys. If you do not agree with this notice, including the disclaimer
  55158. + * below, then you are not authorized to use the Software.
  55159. + *
  55160. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  55161. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55162. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55163. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  55164. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  55165. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  55166. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  55167. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55168. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55169. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  55170. + * DAMAGE.
  55171. + * ========================================================================== */
  55172. +
  55173. +#if !defined(__DWC_OTG_CFI_H__)
  55174. +#define __DWC_OTG_CFI_H__
  55175. +
  55176. +#include "dwc_otg_pcd.h"
  55177. +#include "dwc_cfi_common.h"
  55178. +
  55179. +/**
  55180. + * @file
  55181. + * This file contains the CFI related OTG PCD specific common constants,
  55182. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  55183. + * optional interface for internal testing purposes that a DUT may implement to
  55184. + * support testing of configurable features.
  55185. + *
  55186. + */
  55187. +
  55188. +struct dwc_otg_pcd;
  55189. +struct dwc_otg_pcd_ep;
  55190. +
  55191. +/** OTG CFI Features (properties) ID constants */
  55192. +/** This is a request for all Core Features */
  55193. +#define FT_ID_DMA_MODE 0x0001
  55194. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  55195. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  55196. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  55197. +#define FT_ID_DMA_CIRCULAR 0x0005
  55198. +#define FT_ID_THRESHOLD_SETUP 0x0006
  55199. +#define FT_ID_DFIFO_DEPTH 0x0007
  55200. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  55201. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  55202. +
  55203. +/**********************************************************/
  55204. +#define CFI_INFO_DEF
  55205. +
  55206. +#ifdef CFI_INFO_DEF
  55207. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  55208. +#else
  55209. +#define CFI_INFO(fmt...)
  55210. +#endif
  55211. +
  55212. +#define min(x,y) ({ \
  55213. + x < y ? x : y; })
  55214. +
  55215. +#define max(x,y) ({ \
  55216. + x > y ? x : y; })
  55217. +
  55218. +/**
  55219. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  55220. + * also used for setting up a buffer for Circular DDMA.
  55221. + */
  55222. +struct _ddma_sg_buffer_setup {
  55223. +#define BS_SG_VAL_DESC_LEN 6
  55224. + /* The OUT EP address */
  55225. + uint8_t bOutEndpointAddress;
  55226. + /* The IN EP address */
  55227. + uint8_t bInEndpointAddress;
  55228. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  55229. + uint8_t bOffset;
  55230. + /* The number of transfer segments (a DMA descriptors per each segment) */
  55231. + uint8_t bCount;
  55232. + /* Size (in byte) of each transfer segment */
  55233. + uint16_t wSize;
  55234. +} __attribute__ ((packed));
  55235. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  55236. +
  55237. +/** Descriptor DMA Concatenation Buffer setup structure */
  55238. +struct _ddma_concat_buffer_setup_hdr {
  55239. +#define BS_CONCAT_VAL_HDR_LEN 4
  55240. + /* The endpoint for which the buffer is to be set up */
  55241. + uint8_t bEndpointAddress;
  55242. + /* The count of descriptors to be used */
  55243. + uint8_t bDescCount;
  55244. + /* The total size of the transfer */
  55245. + uint16_t wSize;
  55246. +} __attribute__ ((packed));
  55247. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  55248. +
  55249. +/** Descriptor DMA Concatenation Buffer setup structure */
  55250. +struct _ddma_concat_buffer_setup {
  55251. + /* The SG header */
  55252. + ddma_concat_buffer_setup_hdr_t hdr;
  55253. +
  55254. + /* The XFER sizes pointer (allocated dynamically) */
  55255. + uint16_t *wTxBytes;
  55256. +} __attribute__ ((packed));
  55257. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  55258. +
  55259. +/** Descriptor DMA Alignment Buffer setup structure */
  55260. +struct _ddma_align_buffer_setup {
  55261. +#define BS_ALIGN_VAL_HDR_LEN 2
  55262. + uint8_t bEndpointAddress;
  55263. + uint8_t bAlign;
  55264. +} __attribute__ ((packed));
  55265. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  55266. +
  55267. +/** Transmit FIFO Size setup structure */
  55268. +struct _tx_fifo_size_setup {
  55269. + uint8_t bEndpointAddress;
  55270. + uint16_t wDepth;
  55271. +} __attribute__ ((packed));
  55272. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  55273. +
  55274. +/** Transmit FIFO Size setup structure */
  55275. +struct _rx_fifo_size_setup {
  55276. + uint16_t wDepth;
  55277. +} __attribute__ ((packed));
  55278. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  55279. +
  55280. +/**
  55281. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  55282. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  55283. + * to the data returned in the data stage of a 3-stage Control Write requests.
  55284. + */
  55285. +struct cfi_usb_ctrlrequest {
  55286. + uint8_t bRequestType;
  55287. + uint8_t bRequest;
  55288. + uint16_t wValue;
  55289. + uint16_t wIndex;
  55290. + uint16_t wLength;
  55291. + uint8_t *data;
  55292. +} UPACKED;
  55293. +
  55294. +/*---------------------------------------------------------------------------*/
  55295. +
  55296. +/**
  55297. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  55298. + * This structure is used to store the buffer setup data for any
  55299. + * enabled endpoint in the PCD.
  55300. + */
  55301. +struct cfi_ep {
  55302. + /* Entry for the list container */
  55303. + dwc_list_link_t lh;
  55304. + /* Pointer to the active PCD endpoint structure */
  55305. + struct dwc_otg_pcd_ep *ep;
  55306. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  55307. + struct dwc_otg_dma_desc *dma_desc_last;
  55308. + /* The SG feature value */
  55309. + ddma_sg_buffer_setup_t *bm_sg;
  55310. + /* The Circular feature value */
  55311. + ddma_sg_buffer_setup_t *bm_circ;
  55312. + /* The Concatenation feature value */
  55313. + ddma_concat_buffer_setup_t *bm_concat;
  55314. + /* The Alignment feature value */
  55315. + ddma_align_buffer_setup_t *bm_align;
  55316. + /* XFER length */
  55317. + uint32_t xfer_len;
  55318. + /*
  55319. + * Count of DMA descriptors currently used.
  55320. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  55321. + * defined in the dwc_otg_cil.h
  55322. + */
  55323. + uint32_t desc_count;
  55324. +};
  55325. +typedef struct cfi_ep cfi_ep_t;
  55326. +
  55327. +typedef struct cfi_dma_buff {
  55328. +#define CFI_IN_BUF_LEN 1024
  55329. +#define CFI_OUT_BUF_LEN 1024
  55330. + dma_addr_t addr;
  55331. + uint8_t *buf;
  55332. +} cfi_dma_buff_t;
  55333. +
  55334. +struct cfiobject;
  55335. +
  55336. +/**
  55337. + * This is the interface for the CFI operations.
  55338. + *
  55339. + * @param ep_enable Called when any endpoint is enabled and activated.
  55340. + * @param release Called when the CFI object is released and it needs to correctly
  55341. + * deallocate the dynamic memory
  55342. + * @param ctrl_write_complete Called when the data stage of the request is complete
  55343. + */
  55344. +typedef struct cfi_ops {
  55345. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  55346. + struct dwc_otg_pcd_ep * ep);
  55347. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  55348. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  55349. + unsigned size, gfp_t flags);
  55350. + void (*release) (struct cfiobject * cfi);
  55351. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  55352. + struct dwc_otg_pcd * pcd);
  55353. + void (*build_descriptors) (struct cfiobject * cfi,
  55354. + struct dwc_otg_pcd * pcd,
  55355. + struct dwc_otg_pcd_ep * ep,
  55356. + dwc_otg_pcd_request_t * req);
  55357. +} cfi_ops_t;
  55358. +
  55359. +struct cfiobject {
  55360. + cfi_ops_t ops;
  55361. + struct dwc_otg_pcd *pcd;
  55362. + struct usb_gadget *gadget;
  55363. +
  55364. + /* Buffers used to send/receive CFI-related request data */
  55365. + cfi_dma_buff_t buf_in;
  55366. + cfi_dma_buff_t buf_out;
  55367. +
  55368. + /* CFI specific Control request wrapper */
  55369. + struct cfi_usb_ctrlrequest ctrl_req;
  55370. +
  55371. + /* The list of active EP's in the PCD of type cfi_ep_t */
  55372. + dwc_list_link_t active_eps;
  55373. +
  55374. + /* This flag shall control the propagation of a specific request
  55375. + * to the gadget's processing routines.
  55376. + * 0 - no gadget handling
  55377. + * 1 - the gadget needs to know about this request (w/o completing a status
  55378. + * phase - just return a 0 to the _setup callback)
  55379. + */
  55380. + uint8_t need_gadget_att;
  55381. +
  55382. + /* Flag indicating whether the status IN phase needs to be
  55383. + * completed by the PCD
  55384. + */
  55385. + uint8_t need_status_in_complete;
  55386. +};
  55387. +typedef struct cfiobject cfiobject_t;
  55388. +
  55389. +#define DUMP_MSG
  55390. +
  55391. +#if defined(DUMP_MSG)
  55392. +static inline void dump_msg(const u8 * buf, unsigned int length)
  55393. +{
  55394. + unsigned int start, num, i;
  55395. + char line[52], *p;
  55396. +
  55397. + if (length >= 512)
  55398. + return;
  55399. +
  55400. + start = 0;
  55401. + while (length > 0) {
  55402. + num = min(length, 16u);
  55403. + p = line;
  55404. + for (i = 0; i < num; ++i) {
  55405. + if (i == 8)
  55406. + *p++ = ' ';
  55407. + DWC_SPRINTF(p, " %02x", buf[i]);
  55408. + p += 3;
  55409. + }
  55410. + *p = 0;
  55411. + DWC_DEBUG("%6x: %s\n", start, line);
  55412. + buf += num;
  55413. + start += num;
  55414. + length -= num;
  55415. + }
  55416. +}
  55417. +#else
  55418. +static inline void dump_msg(const u8 * buf, unsigned int length)
  55419. +{
  55420. +}
  55421. +#endif
  55422. +
  55423. +/**
  55424. + * This function returns a pointer to cfi_ep_t object with the addr address.
  55425. + */
  55426. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  55427. + uint8_t addr)
  55428. +{
  55429. + struct cfi_ep *pcfiep;
  55430. + dwc_list_link_t *tmp;
  55431. +
  55432. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  55433. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  55434. +
  55435. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  55436. + return pcfiep;
  55437. + }
  55438. + }
  55439. +
  55440. + return NULL;
  55441. +}
  55442. +
  55443. +/**
  55444. + * This function returns a pointer to cfi_ep_t object that matches
  55445. + * the dwc_otg_pcd_ep object.
  55446. + */
  55447. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  55448. + struct dwc_otg_pcd_ep *ep)
  55449. +{
  55450. + struct cfi_ep *pcfiep = NULL;
  55451. + dwc_list_link_t *tmp;
  55452. +
  55453. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  55454. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  55455. + if (pcfiep->ep == ep) {
  55456. + return pcfiep;
  55457. + }
  55458. + }
  55459. + return NULL;
  55460. +}
  55461. +
  55462. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  55463. +
  55464. +#endif /* (__DWC_OTG_CFI_H__) */
  55465. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cil.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  55466. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cil.c 1970-01-01 01:00:00.000000000 +0100
  55467. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.c 2014-03-11 17:51:27.000000000 +0100
  55468. @@ -0,0 +1,7151 @@
  55469. +/* ==========================================================================
  55470. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  55471. + * $Revision: #191 $
  55472. + * $Date: 2012/08/10 $
  55473. + * $Change: 2047372 $
  55474. + *
  55475. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  55476. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  55477. + * otherwise expressly agreed to in writing between Synopsys and you.
  55478. + *
  55479. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  55480. + * any End User Software License Agreement or Agreement for Licensed Product
  55481. + * with Synopsys or any supplement thereto. You are permitted to use and
  55482. + * redistribute this Software in source and binary forms, with or without
  55483. + * modification, provided that redistributions of source code must retain this
  55484. + * notice. You may not view, use, disclose, copy or distribute this file or
  55485. + * any information contained herein except pursuant to this license grant from
  55486. + * Synopsys. If you do not agree with this notice, including the disclaimer
  55487. + * below, then you are not authorized to use the Software.
  55488. + *
  55489. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  55490. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55491. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55492. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  55493. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  55494. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  55495. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  55496. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55497. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55498. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  55499. + * DAMAGE.
  55500. + * ========================================================================== */
  55501. +
  55502. +/** @file
  55503. + *
  55504. + * The Core Interface Layer provides basic services for accessing and
  55505. + * managing the DWC_otg hardware. These services are used by both the
  55506. + * Host Controller Driver and the Peripheral Controller Driver.
  55507. + *
  55508. + * The CIL manages the memory map for the core so that the HCD and PCD
  55509. + * don't have to do this separately. It also handles basic tasks like
  55510. + * reading/writing the registers and data FIFOs in the controller.
  55511. + * Some of the data access functions provide encapsulation of several
  55512. + * operations required to perform a task, such as writing multiple
  55513. + * registers to start a transfer. Finally, the CIL performs basic
  55514. + * services that are not specific to either the host or device modes
  55515. + * of operation. These services include management of the OTG Host
  55516. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  55517. + * Diagnostic API is also provided to allow testing of the controller
  55518. + * hardware.
  55519. + *
  55520. + * The Core Interface Layer has the following requirements:
  55521. + * - Provides basic controller operations.
  55522. + * - Minimal use of OS services.
  55523. + * - The OS services used will be abstracted by using inline functions
  55524. + * or macros.
  55525. + *
  55526. + */
  55527. +
  55528. +#include "dwc_os.h"
  55529. +#include "dwc_otg_regs.h"
  55530. +#include "dwc_otg_cil.h"
  55531. +
  55532. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  55533. +
  55534. +/**
  55535. + * This function is called to initialize the DWC_otg CSR data
  55536. + * structures. The register addresses in the device and host
  55537. + * structures are initialized from the base address supplied by the
  55538. + * caller. The calling function must make the OS calls to get the
  55539. + * base address of the DWC_otg controller registers. The core_params
  55540. + * argument holds the parameters that specify how the core should be
  55541. + * configured.
  55542. + *
  55543. + * @param reg_base_addr Base address of DWC_otg core registers
  55544. + *
  55545. + */
  55546. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  55547. +{
  55548. + dwc_otg_core_if_t *core_if = 0;
  55549. + dwc_otg_dev_if_t *dev_if = 0;
  55550. + dwc_otg_host_if_t *host_if = 0;
  55551. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  55552. + int i = 0;
  55553. +
  55554. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  55555. +
  55556. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  55557. +
  55558. + if (core_if == NULL) {
  55559. + DWC_DEBUGPL(DBG_CIL,
  55560. + "Allocation of dwc_otg_core_if_t failed\n");
  55561. + return 0;
  55562. + }
  55563. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  55564. +
  55565. + /*
  55566. + * Allocate the Device Mode structures.
  55567. + */
  55568. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  55569. +
  55570. + if (dev_if == NULL) {
  55571. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  55572. + DWC_FREE(core_if);
  55573. + return 0;
  55574. + }
  55575. +
  55576. + dev_if->dev_global_regs =
  55577. + (dwc_otg_device_global_regs_t *) (reg_base +
  55578. + DWC_DEV_GLOBAL_REG_OFFSET);
  55579. +
  55580. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55581. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  55582. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  55583. + (i * DWC_EP_REG_OFFSET));
  55584. +
  55585. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  55586. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  55587. + (i * DWC_EP_REG_OFFSET));
  55588. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  55589. + i, &dev_if->in_ep_regs[i]->diepctl);
  55590. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  55591. + i, &dev_if->out_ep_regs[i]->doepctl);
  55592. + }
  55593. +
  55594. + dev_if->speed = 0; // unknown
  55595. +
  55596. + core_if->dev_if = dev_if;
  55597. +
  55598. + /*
  55599. + * Allocate the Host Mode structures.
  55600. + */
  55601. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  55602. +
  55603. + if (host_if == NULL) {
  55604. + DWC_DEBUGPL(DBG_CIL,
  55605. + "Allocation of dwc_otg_host_if_t failed\n");
  55606. + DWC_FREE(dev_if);
  55607. + DWC_FREE(core_if);
  55608. + return 0;
  55609. + }
  55610. +
  55611. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  55612. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  55613. +
  55614. + host_if->hprt0 =
  55615. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  55616. +
  55617. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55618. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  55619. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  55620. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  55621. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  55622. + i, &host_if->hc_regs[i]->hcchar);
  55623. + }
  55624. +
  55625. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  55626. + core_if->host_if = host_if;
  55627. +
  55628. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55629. + core_if->data_fifo[i] =
  55630. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  55631. + (i * DWC_OTG_DATA_FIFO_SIZE));
  55632. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  55633. + i, (unsigned long)core_if->data_fifo[i]);
  55634. + }
  55635. +
  55636. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  55637. +
  55638. + /* Initiate lx_state to L3 disconnected state */
  55639. + core_if->lx_state = DWC_OTG_L3;
  55640. + /*
  55641. + * Store the contents of the hardware configuration registers here for
  55642. + * easy access later.
  55643. + */
  55644. + core_if->hwcfg1.d32 =
  55645. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  55646. + core_if->hwcfg2.d32 =
  55647. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  55648. + core_if->hwcfg3.d32 =
  55649. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  55650. + core_if->hwcfg4.d32 =
  55651. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  55652. +
  55653. + /* Force host mode to get HPTXFSIZ exact power on value */
  55654. + {
  55655. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  55656. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  55657. + gusbcfg.b.force_host_mode = 1;
  55658. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  55659. + dwc_mdelay(100);
  55660. + core_if->hptxfsiz.d32 =
  55661. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  55662. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  55663. + gusbcfg.b.force_host_mode = 0;
  55664. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  55665. + dwc_mdelay(100);
  55666. + }
  55667. +
  55668. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  55669. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  55670. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  55671. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  55672. +
  55673. + core_if->hcfg.d32 =
  55674. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  55675. + core_if->dcfg.d32 =
  55676. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  55677. +
  55678. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  55679. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  55680. +
  55681. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  55682. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  55683. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  55684. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  55685. + core_if->hwcfg2.b.num_host_chan);
  55686. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  55687. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  55688. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  55689. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  55690. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  55691. + core_if->hwcfg2.b.dev_token_q_depth);
  55692. +
  55693. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  55694. + core_if->hwcfg3.b.dfifo_depth);
  55695. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  55696. + core_if->hwcfg3.b.xfer_size_cntr_width);
  55697. +
  55698. + /*
  55699. + * Set the SRP sucess bit for FS-I2c
  55700. + */
  55701. + core_if->srp_success = 0;
  55702. + core_if->srp_timer_started = 0;
  55703. +
  55704. + /*
  55705. + * Create new workqueue and init works
  55706. + */
  55707. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  55708. + if (core_if->wq_otg == 0) {
  55709. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  55710. + DWC_FREE(host_if);
  55711. + DWC_FREE(dev_if);
  55712. + DWC_FREE(core_if);
  55713. + return 0;
  55714. + }
  55715. +
  55716. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  55717. +
  55718. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  55719. + (core_if->snpsid >> 12 & 0xF),
  55720. + (core_if->snpsid >> 8 & 0xF),
  55721. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  55722. +
  55723. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  55724. + w_wakeup_detected, core_if);
  55725. + if (core_if->wkp_timer == 0) {
  55726. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  55727. + DWC_FREE(host_if);
  55728. + DWC_FREE(dev_if);
  55729. + DWC_WORKQ_FREE(core_if->wq_otg);
  55730. + DWC_FREE(core_if);
  55731. + return 0;
  55732. + }
  55733. +
  55734. + if (dwc_otg_setup_params(core_if)) {
  55735. + DWC_WARN("Error while setting core params\n");
  55736. + }
  55737. +
  55738. + core_if->hibernation_suspend = 0;
  55739. +
  55740. + /** ADP initialization */
  55741. + dwc_otg_adp_init(core_if);
  55742. +
  55743. + return core_if;
  55744. +}
  55745. +
  55746. +/**
  55747. + * This function frees the structures allocated by dwc_otg_cil_init().
  55748. + *
  55749. + * @param core_if The core interface pointer returned from
  55750. + * dwc_otg_cil_init().
  55751. + *
  55752. + */
  55753. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  55754. +{
  55755. + dctl_data_t dctl = {.d32 = 0 };
  55756. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  55757. +
  55758. + /* Disable all interrupts */
  55759. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  55760. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  55761. +
  55762. + dctl.b.sftdiscon = 1;
  55763. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  55764. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  55765. + dctl.d32);
  55766. + }
  55767. +
  55768. + if (core_if->wq_otg) {
  55769. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  55770. + DWC_WORKQ_FREE(core_if->wq_otg);
  55771. + }
  55772. + if (core_if->dev_if) {
  55773. + DWC_FREE(core_if->dev_if);
  55774. + }
  55775. + if (core_if->host_if) {
  55776. + DWC_FREE(core_if->host_if);
  55777. + }
  55778. +
  55779. + /** Remove ADP Stuff */
  55780. + dwc_otg_adp_remove(core_if);
  55781. + if (core_if->core_params) {
  55782. + DWC_FREE(core_if->core_params);
  55783. + }
  55784. + if (core_if->wkp_timer) {
  55785. + DWC_TIMER_FREE(core_if->wkp_timer);
  55786. + }
  55787. + if (core_if->srp_timer) {
  55788. + DWC_TIMER_FREE(core_if->srp_timer);
  55789. + }
  55790. + DWC_FREE(core_if);
  55791. +}
  55792. +
  55793. +/**
  55794. + * This function enables the controller's Global Interrupt in the AHB Config
  55795. + * register.
  55796. + *
  55797. + * @param core_if Programming view of DWC_otg controller.
  55798. + */
  55799. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  55800. +{
  55801. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  55802. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  55803. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  55804. +}
  55805. +
  55806. +/**
  55807. + * This function disables the controller's Global Interrupt in the AHB Config
  55808. + * register.
  55809. + *
  55810. + * @param core_if Programming view of DWC_otg controller.
  55811. + */
  55812. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  55813. +{
  55814. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  55815. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  55816. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  55817. +}
  55818. +
  55819. +/**
  55820. + * This function initializes the commmon interrupts, used in both
  55821. + * device and host modes.
  55822. + *
  55823. + * @param core_if Programming view of the DWC_otg controller
  55824. + *
  55825. + */
  55826. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  55827. +{
  55828. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  55829. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55830. +
  55831. + /* Clear any pending OTG Interrupts */
  55832. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  55833. +
  55834. + /* Clear any pending interrupts */
  55835. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  55836. +
  55837. + /*
  55838. + * Enable the interrupts in the GINTMSK.
  55839. + */
  55840. + intr_mask.b.modemismatch = 1;
  55841. + intr_mask.b.otgintr = 1;
  55842. +
  55843. + if (!core_if->dma_enable) {
  55844. + intr_mask.b.rxstsqlvl = 1;
  55845. + }
  55846. +
  55847. + intr_mask.b.conidstschng = 1;
  55848. + intr_mask.b.wkupintr = 1;
  55849. + intr_mask.b.disconnect = 0;
  55850. + intr_mask.b.usbsuspend = 1;
  55851. + intr_mask.b.sessreqintr = 1;
  55852. +#ifdef CONFIG_USB_DWC_OTG_LPM
  55853. + if (core_if->core_params->lpm_enable) {
  55854. + intr_mask.b.lpmtranrcvd = 1;
  55855. + }
  55856. +#endif
  55857. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  55858. +}
  55859. +
  55860. +/*
  55861. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  55862. + * Hibernation. This function is for exiting from Device mode hibernation by
  55863. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  55864. + * @param core_if Programming view of DWC_otg controller.
  55865. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  55866. + * @param reset - indicates whether resume is initiated by Reset.
  55867. + */
  55868. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  55869. + int rem_wakeup, int reset)
  55870. +{
  55871. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  55872. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  55873. + dctl_data_t dctl = {.d32 = 0 };
  55874. +
  55875. + int timeout = 2000;
  55876. +
  55877. + if (!core_if->hibernation_suspend) {
  55878. + DWC_PRINTF("Already exited from Hibernation\n");
  55879. + return 1;
  55880. + }
  55881. +
  55882. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  55883. + /* Switch-on voltage to the core */
  55884. + gpwrdn.b.pwrdnswtch = 1;
  55885. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55886. + dwc_udelay(10);
  55887. +
  55888. + /* Reset core */
  55889. + gpwrdn.d32 = 0;
  55890. + gpwrdn.b.pwrdnrstn = 1;
  55891. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55892. + dwc_udelay(10);
  55893. +
  55894. + /* Assert Restore signal */
  55895. + gpwrdn.d32 = 0;
  55896. + gpwrdn.b.restore = 1;
  55897. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55898. + dwc_udelay(10);
  55899. +
  55900. + /* Disable power clamps */
  55901. + gpwrdn.d32 = 0;
  55902. + gpwrdn.b.pwrdnclmp = 1;
  55903. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55904. +
  55905. + if (rem_wakeup) {
  55906. + dwc_udelay(70);
  55907. + }
  55908. +
  55909. + /* Deassert Reset core */
  55910. + gpwrdn.d32 = 0;
  55911. + gpwrdn.b.pwrdnrstn = 1;
  55912. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55913. + dwc_udelay(10);
  55914. +
  55915. + /* Disable PMU interrupt */
  55916. + gpwrdn.d32 = 0;
  55917. + gpwrdn.b.pmuintsel = 1;
  55918. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55919. +
  55920. + /* Mask interrupts from gpwrdn */
  55921. + gpwrdn.d32 = 0;
  55922. + gpwrdn.b.connect_det_msk = 1;
  55923. + gpwrdn.b.srp_det_msk = 1;
  55924. + gpwrdn.b.disconn_det_msk = 1;
  55925. + gpwrdn.b.rst_det_msk = 1;
  55926. + gpwrdn.b.lnstchng_msk = 1;
  55927. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55928. +
  55929. + /* Indicates that we are going out from hibernation */
  55930. + core_if->hibernation_suspend = 0;
  55931. +
  55932. + /*
  55933. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  55934. + * indicates restore from remote_wakeup
  55935. + */
  55936. + restore_essential_regs(core_if, rem_wakeup, 0);
  55937. +
  55938. + /*
  55939. + * Wait a little for seeing new value of variable hibernation_suspend if
  55940. + * Restore done interrupt received before polling
  55941. + */
  55942. + dwc_udelay(10);
  55943. +
  55944. + if (core_if->hibernation_suspend == 0) {
  55945. + /*
  55946. + * Wait For Restore_done Interrupt. This mechanism of polling the
  55947. + * interrupt is introduced to avoid any possible race conditions
  55948. + */
  55949. + do {
  55950. + gintsts_data_t gintsts;
  55951. + gintsts.d32 =
  55952. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  55953. + if (gintsts.b.restoredone) {
  55954. + gintsts.d32 = 0;
  55955. + gintsts.b.restoredone = 1;
  55956. + DWC_WRITE_REG32(&core_if->core_global_regs->
  55957. + gintsts, gintsts.d32);
  55958. + DWC_PRINTF("Restore Done Interrupt seen\n");
  55959. + break;
  55960. + }
  55961. + dwc_udelay(10);
  55962. + } while (--timeout);
  55963. + if (!timeout) {
  55964. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  55965. + }
  55966. + }
  55967. + /* Clear all pending interupts */
  55968. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55969. +
  55970. + /* De-assert Restore */
  55971. + gpwrdn.d32 = 0;
  55972. + gpwrdn.b.restore = 1;
  55973. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55974. + dwc_udelay(10);
  55975. +
  55976. + if (!rem_wakeup) {
  55977. + pcgcctl.d32 = 0;
  55978. + pcgcctl.b.rstpdwnmodule = 1;
  55979. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  55980. + }
  55981. +
  55982. + /* Restore GUSBCFG and DCFG */
  55983. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  55984. + core_if->gr_backup->gusbcfg_local);
  55985. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  55986. + core_if->dr_backup->dcfg);
  55987. +
  55988. + /* De-assert Wakeup Logic */
  55989. + gpwrdn.d32 = 0;
  55990. + gpwrdn.b.pmuactv = 1;
  55991. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55992. + dwc_udelay(10);
  55993. +
  55994. + if (!rem_wakeup) {
  55995. + /* Set Device programming done bit */
  55996. + dctl.b.pwronprgdone = 1;
  55997. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  55998. + } else {
  55999. + /* Start Remote Wakeup Signaling */
  56000. + dctl.d32 = core_if->dr_backup->dctl;
  56001. + dctl.b.rmtwkupsig = 1;
  56002. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  56003. + }
  56004. +
  56005. + dwc_mdelay(2);
  56006. + /* Clear all pending interupts */
  56007. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56008. +
  56009. + /* Restore global registers */
  56010. + dwc_otg_restore_global_regs(core_if);
  56011. + /* Restore device global registers */
  56012. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  56013. +
  56014. + if (rem_wakeup) {
  56015. + dwc_mdelay(7);
  56016. + dctl.d32 = 0;
  56017. + dctl.b.rmtwkupsig = 1;
  56018. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  56019. + }
  56020. +
  56021. + core_if->hibernation_suspend = 0;
  56022. + /* The core will be in ON STATE */
  56023. + core_if->lx_state = DWC_OTG_L0;
  56024. + DWC_PRINTF("Hibernation recovery completes here\n");
  56025. +
  56026. + return 1;
  56027. +}
  56028. +
  56029. +/*
  56030. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  56031. + * Hibernation. This function is for exiting from Host mode hibernation by
  56032. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  56033. + * @param core_if Programming view of DWC_otg controller.
  56034. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  56035. + * @param reset - indicates whether resume is initiated by Reset.
  56036. + */
  56037. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  56038. + int rem_wakeup, int reset)
  56039. +{
  56040. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  56041. + hprt0_data_t hprt0 = {.d32 = 0 };
  56042. +
  56043. + int timeout = 2000;
  56044. +
  56045. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  56046. + /* Switch-on voltage to the core */
  56047. + gpwrdn.b.pwrdnswtch = 1;
  56048. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56049. + dwc_udelay(10);
  56050. +
  56051. + /* Reset core */
  56052. + gpwrdn.d32 = 0;
  56053. + gpwrdn.b.pwrdnrstn = 1;
  56054. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56055. + dwc_udelay(10);
  56056. +
  56057. + /* Assert Restore signal */
  56058. + gpwrdn.d32 = 0;
  56059. + gpwrdn.b.restore = 1;
  56060. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  56061. + dwc_udelay(10);
  56062. +
  56063. + /* Disable power clamps */
  56064. + gpwrdn.d32 = 0;
  56065. + gpwrdn.b.pwrdnclmp = 1;
  56066. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56067. +
  56068. + if (!rem_wakeup) {
  56069. + dwc_udelay(50);
  56070. + }
  56071. +
  56072. + /* Deassert Reset core */
  56073. + gpwrdn.d32 = 0;
  56074. + gpwrdn.b.pwrdnrstn = 1;
  56075. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  56076. + dwc_udelay(10);
  56077. +
  56078. + /* Disable PMU interrupt */
  56079. + gpwrdn.d32 = 0;
  56080. + gpwrdn.b.pmuintsel = 1;
  56081. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56082. +
  56083. + gpwrdn.d32 = 0;
  56084. + gpwrdn.b.connect_det_msk = 1;
  56085. + gpwrdn.b.srp_det_msk = 1;
  56086. + gpwrdn.b.disconn_det_msk = 1;
  56087. + gpwrdn.b.rst_det_msk = 1;
  56088. + gpwrdn.b.lnstchng_msk = 1;
  56089. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56090. +
  56091. + /* Indicates that we are going out from hibernation */
  56092. + core_if->hibernation_suspend = 0;
  56093. +
  56094. + /* Set Restore Essential Regs bit in PCGCCTL register */
  56095. + restore_essential_regs(core_if, rem_wakeup, 1);
  56096. +
  56097. + /* Wait a little for seeing new value of variable hibernation_suspend if
  56098. + * Restore done interrupt received before polling */
  56099. + dwc_udelay(10);
  56100. +
  56101. + if (core_if->hibernation_suspend == 0) {
  56102. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  56103. + * interrupt is introduced to avoid any possible race conditions
  56104. + */
  56105. + do {
  56106. + gintsts_data_t gintsts;
  56107. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  56108. + if (gintsts.b.restoredone) {
  56109. + gintsts.d32 = 0;
  56110. + gintsts.b.restoredone = 1;
  56111. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  56112. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  56113. + break;
  56114. + }
  56115. + dwc_udelay(10);
  56116. + } while (--timeout);
  56117. + if (!timeout) {
  56118. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  56119. + }
  56120. + }
  56121. +
  56122. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  56123. + core_if->hibernation_suspend = 0;
  56124. +
  56125. + /* This step is not described in functional spec but if not wait for this
  56126. + * delay, mismatch interrupts occurred because just after restore core is
  56127. + * in Device mode(gintsts.curmode == 0) */
  56128. + dwc_mdelay(100);
  56129. +
  56130. + /* Clear all pending interrupts */
  56131. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56132. +
  56133. + /* De-assert Restore */
  56134. + gpwrdn.d32 = 0;
  56135. + gpwrdn.b.restore = 1;
  56136. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56137. + dwc_udelay(10);
  56138. +
  56139. + /* Restore GUSBCFG and HCFG */
  56140. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  56141. + core_if->gr_backup->gusbcfg_local);
  56142. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  56143. + core_if->hr_backup->hcfg_local);
  56144. +
  56145. + /* De-assert Wakeup Logic */
  56146. + gpwrdn.d32 = 0;
  56147. + gpwrdn.b.pmuactv = 1;
  56148. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56149. + dwc_udelay(10);
  56150. +
  56151. + /* Start the Resume operation by programming HPRT0 */
  56152. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  56153. + hprt0.b.prtpwr = 1;
  56154. + hprt0.b.prtena = 0;
  56155. + hprt0.b.prtsusp = 0;
  56156. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56157. +
  56158. + DWC_PRINTF("Resume Starts Now\n");
  56159. + if (!reset) { // Indicates it is Resume Operation
  56160. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  56161. + hprt0.b.prtres = 1;
  56162. + hprt0.b.prtpwr = 1;
  56163. + hprt0.b.prtena = 0;
  56164. + hprt0.b.prtsusp = 0;
  56165. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56166. +
  56167. + if (!rem_wakeup)
  56168. + hprt0.b.prtres = 0;
  56169. + /* Wait for Resume time and then program HPRT again */
  56170. + dwc_mdelay(100);
  56171. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56172. +
  56173. + } else { // Indicates it is Reset Operation
  56174. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  56175. + hprt0.b.prtrst = 1;
  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. + /* Wait for Reset time and then program HPRT again */
  56181. + dwc_mdelay(60);
  56182. + hprt0.b.prtrst = 0;
  56183. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56184. + }
  56185. + /* Clear all interrupt status */
  56186. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  56187. + hprt0.b.prtconndet = 1;
  56188. + hprt0.b.prtenchng = 1;
  56189. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56190. +
  56191. + /* Clear all pending interupts */
  56192. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56193. +
  56194. + /* Restore global registers */
  56195. + dwc_otg_restore_global_regs(core_if);
  56196. + /* Restore host global registers */
  56197. + dwc_otg_restore_host_regs(core_if, reset);
  56198. +
  56199. + /* The core will be in ON STATE */
  56200. + core_if->lx_state = DWC_OTG_L0;
  56201. + DWC_PRINTF("Hibernation recovery is complete here\n");
  56202. + return 0;
  56203. +}
  56204. +
  56205. +/** Saves some register values into system memory. */
  56206. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  56207. +{
  56208. + struct dwc_otg_global_regs_backup *gr;
  56209. + int i;
  56210. +
  56211. + gr = core_if->gr_backup;
  56212. + if (!gr) {
  56213. + gr = DWC_ALLOC(sizeof(*gr));
  56214. + if (!gr) {
  56215. + return -DWC_E_NO_MEMORY;
  56216. + }
  56217. + core_if->gr_backup = gr;
  56218. + }
  56219. +
  56220. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  56221. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  56222. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  56223. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  56224. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  56225. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  56226. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  56227. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56228. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  56229. +#endif
  56230. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  56231. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  56232. + gr->gdfifocfg_local =
  56233. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  56234. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  56235. + gr->dtxfsiz_local[i] =
  56236. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  56237. + }
  56238. +
  56239. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  56240. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  56241. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  56242. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  56243. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  56244. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  56245. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  56246. + gr->gnptxfsiz_local);
  56247. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  56248. + gr->hptxfsiz_local);
  56249. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56250. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  56251. +#endif
  56252. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  56253. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  56254. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  56255. +
  56256. + return 0;
  56257. +}
  56258. +
  56259. +/** Saves GINTMSK register before setting the msk bits. */
  56260. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  56261. +{
  56262. + struct dwc_otg_global_regs_backup *gr;
  56263. +
  56264. + gr = core_if->gr_backup;
  56265. + if (!gr) {
  56266. + gr = DWC_ALLOC(sizeof(*gr));
  56267. + if (!gr) {
  56268. + return -DWC_E_NO_MEMORY;
  56269. + }
  56270. + core_if->gr_backup = gr;
  56271. + }
  56272. +
  56273. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  56274. +
  56275. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  56276. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  56277. +
  56278. + return 0;
  56279. +}
  56280. +
  56281. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  56282. +{
  56283. + struct dwc_otg_dev_regs_backup *dr;
  56284. + int i;
  56285. +
  56286. + dr = core_if->dr_backup;
  56287. + if (!dr) {
  56288. + dr = DWC_ALLOC(sizeof(*dr));
  56289. + if (!dr) {
  56290. + return -DWC_E_NO_MEMORY;
  56291. + }
  56292. + core_if->dr_backup = dr;
  56293. + }
  56294. +
  56295. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  56296. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  56297. + dr->daintmsk =
  56298. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  56299. + dr->diepmsk =
  56300. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  56301. + dr->doepmsk =
  56302. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  56303. +
  56304. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  56305. + dr->diepctl[i] =
  56306. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  56307. + dr->dieptsiz[i] =
  56308. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  56309. + dr->diepdma[i] =
  56310. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  56311. + }
  56312. +
  56313. + DWC_DEBUGPL(DBG_ANY,
  56314. + "=============Backing Host registers==============\n");
  56315. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  56316. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  56317. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  56318. + dr->daintmsk);
  56319. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  56320. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  56321. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  56322. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  56323. + dr->diepctl[i]);
  56324. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  56325. + i, dr->dieptsiz[i]);
  56326. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  56327. + dr->diepdma[i]);
  56328. + }
  56329. +
  56330. + return 0;
  56331. +}
  56332. +
  56333. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  56334. +{
  56335. + struct dwc_otg_host_regs_backup *hr;
  56336. + int i;
  56337. +
  56338. + hr = core_if->hr_backup;
  56339. + if (!hr) {
  56340. + hr = DWC_ALLOC(sizeof(*hr));
  56341. + if (!hr) {
  56342. + return -DWC_E_NO_MEMORY;
  56343. + }
  56344. + core_if->hr_backup = hr;
  56345. + }
  56346. +
  56347. + hr->hcfg_local =
  56348. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  56349. + hr->haintmsk_local =
  56350. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  56351. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  56352. + hr->hcintmsk_local[i] =
  56353. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  56354. + }
  56355. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  56356. + hr->hfir_local =
  56357. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  56358. +
  56359. + DWC_DEBUGPL(DBG_ANY,
  56360. + "=============Backing Host registers===============\n");
  56361. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  56362. + hr->hcfg_local);
  56363. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  56364. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  56365. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  56366. + hr->hcintmsk_local[i]);
  56367. + }
  56368. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  56369. + hr->hprt0_local);
  56370. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  56371. + hr->hfir_local);
  56372. +
  56373. + return 0;
  56374. +}
  56375. +
  56376. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  56377. +{
  56378. + struct dwc_otg_global_regs_backup *gr;
  56379. + int i;
  56380. +
  56381. + gr = core_if->gr_backup;
  56382. + if (!gr) {
  56383. + return -DWC_E_INVALID;
  56384. + }
  56385. +
  56386. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  56387. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  56388. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  56389. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  56390. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  56391. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  56392. + gr->gnptxfsiz_local);
  56393. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  56394. + gr->hptxfsiz_local);
  56395. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  56396. + gr->gdfifocfg_local);
  56397. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  56398. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  56399. + gr->dtxfsiz_local[i]);
  56400. + }
  56401. +
  56402. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56403. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  56404. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  56405. + (gr->gahbcfg_local));
  56406. + return 0;
  56407. +}
  56408. +
  56409. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  56410. +{
  56411. + struct dwc_otg_dev_regs_backup *dr;
  56412. + int i;
  56413. +
  56414. + dr = core_if->dr_backup;
  56415. +
  56416. + if (!dr) {
  56417. + return -DWC_E_INVALID;
  56418. + }
  56419. +
  56420. + if (!rem_wakeup) {
  56421. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  56422. + dr->dctl);
  56423. + }
  56424. +
  56425. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  56426. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  56427. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  56428. +
  56429. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  56430. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  56431. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  56432. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  56433. + }
  56434. +
  56435. + return 0;
  56436. +}
  56437. +
  56438. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  56439. +{
  56440. + struct dwc_otg_host_regs_backup *hr;
  56441. + int i;
  56442. + hr = core_if->hr_backup;
  56443. +
  56444. + if (!hr) {
  56445. + return -DWC_E_INVALID;
  56446. + }
  56447. +
  56448. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  56449. + //if (!reset)
  56450. + //{
  56451. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  56452. + //}
  56453. +
  56454. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  56455. + hr->haintmsk_local);
  56456. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  56457. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  56458. + hr->hcintmsk_local[i]);
  56459. + }
  56460. +
  56461. + return 0;
  56462. +}
  56463. +
  56464. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  56465. +{
  56466. + struct dwc_otg_global_regs_backup *gr;
  56467. +
  56468. + gr = core_if->gr_backup;
  56469. +
  56470. + /* Restore values for LPM and I2C */
  56471. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56472. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  56473. +#endif
  56474. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  56475. +
  56476. + return 0;
  56477. +}
  56478. +
  56479. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  56480. +{
  56481. + struct dwc_otg_global_regs_backup *gr;
  56482. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  56483. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  56484. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  56485. + gintmsk_data_t gintmsk = {.d32 = 0 };
  56486. +
  56487. + /* Restore LPM and I2C registers */
  56488. + restore_lpm_i2c_regs(core_if);
  56489. +
  56490. + /* Set PCGCCTL to 0 */
  56491. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  56492. +
  56493. + gr = core_if->gr_backup;
  56494. + /* Load restore values for [31:14] bits */
  56495. + DWC_WRITE_REG32(core_if->pcgcctl,
  56496. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  56497. +
  56498. + /* Umnask global Interrupt in GAHBCFG and restore it */
  56499. + gahbcfg.d32 = gr->gahbcfg_local;
  56500. + gahbcfg.b.glblintrmsk = 1;
  56501. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  56502. +
  56503. + /* Clear all pending interupts */
  56504. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56505. +
  56506. + /* Unmask restore done interrupt */
  56507. + gintmsk.b.restoredone = 1;
  56508. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  56509. +
  56510. + /* Restore GUSBCFG and HCFG/DCFG */
  56511. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  56512. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  56513. +
  56514. + if (is_host) {
  56515. + hcfg_data_t hcfg = {.d32 = 0 };
  56516. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  56517. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  56518. + hcfg.d32);
  56519. +
  56520. + /* Load restore values for [31:14] bits */
  56521. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56522. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  56523. +
  56524. + if (rmode)
  56525. + pcgcctl.b.restoremode = 1;
  56526. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56527. + dwc_udelay(10);
  56528. +
  56529. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  56530. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  56531. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56532. + pcgcctl.b.ess_reg_restored = 1;
  56533. + if (rmode)
  56534. + pcgcctl.b.restoremode = 1;
  56535. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56536. + } else {
  56537. + dcfg_data_t dcfg = {.d32 = 0 };
  56538. + dcfg.d32 = core_if->dr_backup->dcfg;
  56539. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  56540. +
  56541. + /* Load restore values for [31:14] bits */
  56542. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56543. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  56544. + if (!rmode) {
  56545. + pcgcctl.d32 |= 0x208;
  56546. + }
  56547. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56548. + dwc_udelay(10);
  56549. +
  56550. + /* Load restore values for [31:14] bits */
  56551. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56552. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  56553. + pcgcctl.b.ess_reg_restored = 1;
  56554. + if (!rmode)
  56555. + pcgcctl.d32 |= 0x208;
  56556. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56557. + }
  56558. +
  56559. + return 0;
  56560. +}
  56561. +
  56562. +/**
  56563. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  56564. + * type.
  56565. + */
  56566. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  56567. +{
  56568. + uint32_t val;
  56569. + hcfg_data_t hcfg;
  56570. +
  56571. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  56572. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  56573. + (core_if->core_params->ulpi_fs_ls)) ||
  56574. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  56575. + /* Full speed PHY */
  56576. + val = DWC_HCFG_48_MHZ;
  56577. + } else {
  56578. + /* High speed PHY running at full speed or high speed */
  56579. + val = DWC_HCFG_30_60_MHZ;
  56580. + }
  56581. +
  56582. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  56583. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  56584. + hcfg.b.fslspclksel = val;
  56585. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  56586. +}
  56587. +
  56588. +/**
  56589. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  56590. + * and the enumeration speed of the device.
  56591. + */
  56592. +static void init_devspd(dwc_otg_core_if_t * core_if)
  56593. +{
  56594. + uint32_t val;
  56595. + dcfg_data_t dcfg;
  56596. +
  56597. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  56598. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  56599. + (core_if->core_params->ulpi_fs_ls)) ||
  56600. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  56601. + /* Full speed PHY */
  56602. + val = 0x3;
  56603. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  56604. + /* High speed PHY running at full speed */
  56605. + val = 0x1;
  56606. + } else {
  56607. + /* High speed PHY running at high speed */
  56608. + val = 0x0;
  56609. + }
  56610. +
  56611. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  56612. +
  56613. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  56614. + dcfg.b.devspd = val;
  56615. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  56616. +}
  56617. +
  56618. +/**
  56619. + * This function calculates the number of IN EPS
  56620. + * using GHWCFG1 and GHWCFG2 registers values
  56621. + *
  56622. + * @param core_if Programming view of the DWC_otg controller
  56623. + */
  56624. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  56625. +{
  56626. + uint32_t num_in_eps = 0;
  56627. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  56628. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  56629. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  56630. + int i;
  56631. +
  56632. + for (i = 0; i < num_eps; ++i) {
  56633. + if (!(hwcfg1 & 0x1))
  56634. + num_in_eps++;
  56635. +
  56636. + hwcfg1 >>= 2;
  56637. + }
  56638. +
  56639. + if (core_if->hwcfg4.b.ded_fifo_en) {
  56640. + num_in_eps =
  56641. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  56642. + }
  56643. +
  56644. + return num_in_eps;
  56645. +}
  56646. +
  56647. +/**
  56648. + * This function calculates the number of OUT EPS
  56649. + * using GHWCFG1 and GHWCFG2 registers values
  56650. + *
  56651. + * @param core_if Programming view of the DWC_otg controller
  56652. + */
  56653. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  56654. +{
  56655. + uint32_t num_out_eps = 0;
  56656. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  56657. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  56658. + int i;
  56659. +
  56660. + for (i = 0; i < num_eps; ++i) {
  56661. + if (!(hwcfg1 & 0x1))
  56662. + num_out_eps++;
  56663. +
  56664. + hwcfg1 >>= 2;
  56665. + }
  56666. + return num_out_eps;
  56667. +}
  56668. +
  56669. +/**
  56670. + * This function initializes the DWC_otg controller registers and
  56671. + * prepares the core for device mode or host mode operation.
  56672. + *
  56673. + * @param core_if Programming view of the DWC_otg controller
  56674. + *
  56675. + */
  56676. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  56677. +{
  56678. + int i = 0;
  56679. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56680. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  56681. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  56682. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  56683. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  56684. +
  56685. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  56686. + core_if, global_regs);
  56687. +
  56688. + /* Common Initialization */
  56689. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56690. +
  56691. + /* Program the ULPI External VBUS bit if needed */
  56692. + usbcfg.b.ulpi_ext_vbus_drv =
  56693. + (core_if->core_params->phy_ulpi_ext_vbus ==
  56694. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  56695. +
  56696. + /* Set external TS Dline pulsing */
  56697. + usbcfg.b.term_sel_dl_pulse =
  56698. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  56699. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56700. +
  56701. + /* Reset the Controller */
  56702. + dwc_otg_core_reset(core_if);
  56703. +
  56704. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  56705. + core_if->power_down = core_if->core_params->power_down;
  56706. + core_if->otg_sts = 0;
  56707. +
  56708. + /* Initialize parameters from Hardware configuration registers. */
  56709. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  56710. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  56711. +
  56712. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  56713. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  56714. +
  56715. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  56716. + dev_if->perio_tx_fifo_size[i] =
  56717. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  56718. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  56719. + i, dev_if->perio_tx_fifo_size[i]);
  56720. + }
  56721. +
  56722. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  56723. + dev_if->tx_fifo_size[i] =
  56724. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  56725. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  56726. + i, dev_if->tx_fifo_size[i]);
  56727. + }
  56728. +
  56729. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  56730. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  56731. + core_if->nperio_tx_fifo_size =
  56732. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  56733. +
  56734. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  56735. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  56736. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  56737. + core_if->nperio_tx_fifo_size);
  56738. +
  56739. + /* This programming sequence needs to happen in FS mode before any other
  56740. + * programming occurs */
  56741. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  56742. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  56743. + /* If FS mode with FS PHY */
  56744. +
  56745. + /* core_init() is now called on every switch so only call the
  56746. + * following for the first time through. */
  56747. + if (!core_if->phy_init_done) {
  56748. + core_if->phy_init_done = 1;
  56749. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  56750. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56751. + usbcfg.b.physel = 1;
  56752. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56753. +
  56754. + /* Reset after a PHY select */
  56755. + dwc_otg_core_reset(core_if);
  56756. + }
  56757. +
  56758. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  56759. + * do this on HNP Dev/Host mode switches (done in dev_init and
  56760. + * host_init). */
  56761. + if (dwc_otg_is_host_mode(core_if)) {
  56762. + init_fslspclksel(core_if);
  56763. + } else {
  56764. + init_devspd(core_if);
  56765. + }
  56766. +
  56767. + if (core_if->core_params->i2c_enable) {
  56768. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  56769. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  56770. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56771. + usbcfg.b.otgutmifssel = 1;
  56772. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56773. +
  56774. + /* Program GI2CCTL.I2CEn */
  56775. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  56776. + i2cctl.b.i2cdevaddr = 1;
  56777. + i2cctl.b.i2cen = 0;
  56778. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  56779. + i2cctl.b.i2cen = 1;
  56780. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  56781. + }
  56782. +
  56783. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  56784. + else {
  56785. + /* High speed PHY. */
  56786. + if (!core_if->phy_init_done) {
  56787. + core_if->phy_init_done = 1;
  56788. + /* HS PHY parameters. These parameters are preserved
  56789. + * during soft reset so only program the first time. Do
  56790. + * a soft reset immediately after setting phyif. */
  56791. +
  56792. + if (core_if->core_params->phy_type == 2) {
  56793. + /* ULPI interface */
  56794. + usbcfg.b.ulpi_utmi_sel = 1;
  56795. + usbcfg.b.phyif = 0;
  56796. + usbcfg.b.ddrsel =
  56797. + core_if->core_params->phy_ulpi_ddr;
  56798. + } else if (core_if->core_params->phy_type == 1) {
  56799. + /* UTMI+ interface */
  56800. + usbcfg.b.ulpi_utmi_sel = 0;
  56801. + if (core_if->core_params->phy_utmi_width == 16) {
  56802. + usbcfg.b.phyif = 1;
  56803. +
  56804. + } else {
  56805. + usbcfg.b.phyif = 0;
  56806. + }
  56807. + } else {
  56808. + DWC_ERROR("FS PHY TYPE\n");
  56809. + }
  56810. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56811. + /* Reset after setting the PHY parameters */
  56812. + dwc_otg_core_reset(core_if);
  56813. + }
  56814. + }
  56815. +
  56816. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  56817. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  56818. + (core_if->core_params->ulpi_fs_ls)) {
  56819. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  56820. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56821. + usbcfg.b.ulpi_fsls = 1;
  56822. + usbcfg.b.ulpi_clk_sus_m = 1;
  56823. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56824. + } else {
  56825. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56826. + usbcfg.b.ulpi_fsls = 0;
  56827. + usbcfg.b.ulpi_clk_sus_m = 0;
  56828. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56829. + }
  56830. +
  56831. + /* Program the GAHBCFG Register. */
  56832. + switch (core_if->hwcfg2.b.architecture) {
  56833. +
  56834. + case DWC_SLAVE_ONLY_ARCH:
  56835. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  56836. + ahbcfg.b.nptxfemplvl_txfemplvl =
  56837. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  56838. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  56839. + core_if->dma_enable = 0;
  56840. + core_if->dma_desc_enable = 0;
  56841. + break;
  56842. +
  56843. + case DWC_EXT_DMA_ARCH:
  56844. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  56845. + {
  56846. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  56847. + ahbcfg.b.hburstlen = 0;
  56848. + while (brst_sz > 1) {
  56849. + ahbcfg.b.hburstlen++;
  56850. + brst_sz >>= 1;
  56851. + }
  56852. + }
  56853. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  56854. + core_if->dma_desc_enable =
  56855. + (core_if->core_params->dma_desc_enable != 0);
  56856. + break;
  56857. +
  56858. + case DWC_INT_DMA_ARCH:
  56859. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  56860. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  56861. + Host mode ISOC in issue fix - vahrama */
  56862. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  56863. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  56864. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  56865. + core_if->dma_desc_enable =
  56866. + (core_if->core_params->dma_desc_enable != 0);
  56867. + break;
  56868. +
  56869. + }
  56870. + if (core_if->dma_enable) {
  56871. + if (core_if->dma_desc_enable) {
  56872. + DWC_PRINTF("Using Descriptor DMA mode\n");
  56873. + } else {
  56874. + DWC_PRINTF("Using Buffer DMA mode\n");
  56875. +
  56876. + }
  56877. + } else {
  56878. + DWC_PRINTF("Using Slave mode\n");
  56879. + core_if->dma_desc_enable = 0;
  56880. + }
  56881. +
  56882. + if (core_if->core_params->ahb_single) {
  56883. + ahbcfg.b.ahbsingle = 1;
  56884. + }
  56885. +
  56886. + ahbcfg.b.dmaenable = core_if->dma_enable;
  56887. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  56888. +
  56889. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  56890. +
  56891. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  56892. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  56893. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  56894. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  56895. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  56896. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  56897. +
  56898. + /*
  56899. + * Program the GUSBCFG register.
  56900. + */
  56901. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56902. +
  56903. + switch (core_if->hwcfg2.b.op_mode) {
  56904. + case DWC_MODE_HNP_SRP_CAPABLE:
  56905. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  56906. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  56907. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56908. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56909. + break;
  56910. +
  56911. + case DWC_MODE_SRP_ONLY_CAPABLE:
  56912. + usbcfg.b.hnpcap = 0;
  56913. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56914. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56915. + break;
  56916. +
  56917. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  56918. + usbcfg.b.hnpcap = 0;
  56919. + usbcfg.b.srpcap = 0;
  56920. + break;
  56921. +
  56922. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  56923. + usbcfg.b.hnpcap = 0;
  56924. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56925. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56926. + break;
  56927. +
  56928. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  56929. + usbcfg.b.hnpcap = 0;
  56930. + usbcfg.b.srpcap = 0;
  56931. + break;
  56932. +
  56933. + case DWC_MODE_SRP_CAPABLE_HOST:
  56934. + usbcfg.b.hnpcap = 0;
  56935. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56936. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56937. + break;
  56938. +
  56939. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  56940. + usbcfg.b.hnpcap = 0;
  56941. + usbcfg.b.srpcap = 0;
  56942. + break;
  56943. + }
  56944. +
  56945. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56946. +
  56947. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56948. + if (core_if->core_params->lpm_enable) {
  56949. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  56950. +
  56951. + /* To enable LPM support set lpm_cap_en bit */
  56952. + lpmcfg.b.lpm_cap_en = 1;
  56953. +
  56954. + /* Make AppL1Res ACK */
  56955. + lpmcfg.b.appl_resp = 1;
  56956. +
  56957. + /* Retry 3 times */
  56958. + lpmcfg.b.retry_count = 3;
  56959. +
  56960. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  56961. + 0, lpmcfg.d32);
  56962. +
  56963. + }
  56964. +#endif
  56965. + if (core_if->core_params->ic_usb_cap) {
  56966. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  56967. + gusbcfg.b.ic_usb_cap = 1;
  56968. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  56969. + 0, gusbcfg.d32);
  56970. + }
  56971. + {
  56972. + gotgctl_data_t gotgctl = {.d32 = 0 };
  56973. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  56974. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  56975. + gotgctl.d32);
  56976. + /* Set OTG version supported */
  56977. + core_if->otg_ver = core_if->core_params->otg_ver;
  56978. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  56979. + core_if->core_params->otg_ver, core_if->otg_ver);
  56980. + }
  56981. +
  56982. +
  56983. + /* Enable common interrupts */
  56984. + dwc_otg_enable_common_interrupts(core_if);
  56985. +
  56986. + /* Do device or host intialization based on mode during PCD
  56987. + * and HCD initialization */
  56988. + if (dwc_otg_is_host_mode(core_if)) {
  56989. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  56990. + core_if->op_state = A_HOST;
  56991. + } else {
  56992. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  56993. + core_if->op_state = B_PERIPHERAL;
  56994. +#ifdef DWC_DEVICE_ONLY
  56995. + dwc_otg_core_dev_init(core_if);
  56996. +#endif
  56997. + }
  56998. +}
  56999. +
  57000. +/**
  57001. + * This function enables the Device mode interrupts.
  57002. + *
  57003. + * @param core_if Programming view of DWC_otg controller
  57004. + */
  57005. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  57006. +{
  57007. + gintmsk_data_t intr_mask = {.d32 = 0 };
  57008. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57009. +
  57010. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  57011. +
  57012. + /* Disable all interrupts. */
  57013. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  57014. +
  57015. + /* Clear any pending interrupts */
  57016. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  57017. +
  57018. + /* Enable the common interrupts */
  57019. + dwc_otg_enable_common_interrupts(core_if);
  57020. +
  57021. + /* Enable interrupts */
  57022. + intr_mask.b.usbreset = 1;
  57023. + intr_mask.b.enumdone = 1;
  57024. + /* Disable Disconnect interrupt in Device mode */
  57025. + intr_mask.b.disconnect = 0;
  57026. +
  57027. + if (!core_if->multiproc_int_enable) {
  57028. + intr_mask.b.inepintr = 1;
  57029. + intr_mask.b.outepintr = 1;
  57030. + }
  57031. +
  57032. + intr_mask.b.erlysuspend = 1;
  57033. +
  57034. + if (core_if->en_multiple_tx_fifo == 0) {
  57035. + intr_mask.b.epmismatch = 1;
  57036. + }
  57037. +
  57038. + //intr_mask.b.incomplisoout = 1;
  57039. + intr_mask.b.incomplisoin = 1;
  57040. +
  57041. +/* Enable the ignore frame number for ISOC xfers - MAS */
  57042. +/* Disable to support high bandwith ISOC transfers - manukz */
  57043. +#if 0
  57044. +#ifdef DWC_UTE_PER_IO
  57045. + if (core_if->dma_enable) {
  57046. + if (core_if->dma_desc_enable) {
  57047. + dctl_data_t dctl1 = {.d32 = 0 };
  57048. + dctl1.b.ifrmnum = 1;
  57049. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  57050. + dctl, 0, dctl1.d32);
  57051. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  57052. + DWC_READ_REG32(&core_if->dev_if->
  57053. + dev_global_regs->dctl));
  57054. + }
  57055. + }
  57056. +#endif
  57057. +#endif
  57058. +#ifdef DWC_EN_ISOC
  57059. + if (core_if->dma_enable) {
  57060. + if (core_if->dma_desc_enable == 0) {
  57061. + if (core_if->pti_enh_enable) {
  57062. + dctl_data_t dctl = {.d32 = 0 };
  57063. + dctl.b.ifrmnum = 1;
  57064. + DWC_MODIFY_REG32(&core_if->
  57065. + dev_if->dev_global_regs->dctl,
  57066. + 0, dctl.d32);
  57067. + } else {
  57068. + intr_mask.b.incomplisoin = 1;
  57069. + intr_mask.b.incomplisoout = 1;
  57070. + }
  57071. + }
  57072. + } else {
  57073. + intr_mask.b.incomplisoin = 1;
  57074. + intr_mask.b.incomplisoout = 1;
  57075. + }
  57076. +#endif /* DWC_EN_ISOC */
  57077. +
  57078. + /** @todo NGS: Should this be a module parameter? */
  57079. +#ifdef USE_PERIODIC_EP
  57080. + intr_mask.b.isooutdrop = 1;
  57081. + intr_mask.b.eopframe = 1;
  57082. + intr_mask.b.incomplisoin = 1;
  57083. + intr_mask.b.incomplisoout = 1;
  57084. +#endif
  57085. +
  57086. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  57087. +
  57088. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  57089. + DWC_READ_REG32(&global_regs->gintmsk));
  57090. +}
  57091. +
  57092. +/**
  57093. + * This function initializes the DWC_otg controller registers for
  57094. + * device mode.
  57095. + *
  57096. + * @param core_if Programming view of DWC_otg controller
  57097. + *
  57098. + */
  57099. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  57100. +{
  57101. + int i;
  57102. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57103. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  57104. + dwc_otg_core_params_t *params = core_if->core_params;
  57105. + dcfg_data_t dcfg = {.d32 = 0 };
  57106. + depctl_data_t diepctl = {.d32 = 0 };
  57107. + grstctl_t resetctl = {.d32 = 0 };
  57108. + uint32_t rx_fifo_size;
  57109. + fifosize_data_t nptxfifosize;
  57110. + fifosize_data_t txfifosize;
  57111. + dthrctl_data_t dthrctl;
  57112. + fifosize_data_t ptxfifosize;
  57113. + uint16_t rxfsiz, nptxfsiz;
  57114. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  57115. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  57116. +
  57117. + /* Restart the Phy Clock */
  57118. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  57119. +
  57120. + /* Device configuration register */
  57121. + init_devspd(core_if);
  57122. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  57123. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  57124. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  57125. + /* Enable Device OUT NAK in case of DDMA mode*/
  57126. + if (core_if->core_params->dev_out_nak) {
  57127. + dcfg.b.endevoutnak = 1;
  57128. + }
  57129. +
  57130. + if (core_if->core_params->cont_on_bna) {
  57131. + dctl_data_t dctl = {.d32 = 0 };
  57132. + dctl.b.encontonbna = 1;
  57133. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57134. + }
  57135. +
  57136. +
  57137. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  57138. +
  57139. + /* Configure data FIFO sizes */
  57140. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  57141. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  57142. + core_if->total_fifo_size);
  57143. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  57144. + params->dev_rx_fifo_size);
  57145. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  57146. + params->dev_nperio_tx_fifo_size);
  57147. +
  57148. + /* Rx FIFO */
  57149. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  57150. + DWC_READ_REG32(&global_regs->grxfsiz));
  57151. +
  57152. +#ifdef DWC_UTE_CFI
  57153. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  57154. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  57155. +#endif
  57156. + rx_fifo_size = params->dev_rx_fifo_size;
  57157. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  57158. +
  57159. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  57160. + DWC_READ_REG32(&global_regs->grxfsiz));
  57161. +
  57162. + /** Set Periodic Tx FIFO Mask all bits 0 */
  57163. + core_if->p_tx_msk = 0;
  57164. +
  57165. + /** Set Tx FIFO Mask all bits 0 */
  57166. + core_if->tx_msk = 0;
  57167. +
  57168. + if (core_if->en_multiple_tx_fifo == 0) {
  57169. + /* Non-periodic Tx FIFO */
  57170. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  57171. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57172. +
  57173. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  57174. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  57175. +
  57176. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  57177. + nptxfifosize.d32);
  57178. +
  57179. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  57180. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57181. +
  57182. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  57183. + /*
  57184. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  57185. + * Indexes of the FIFO size module parameters in the
  57186. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  57187. + * the dptxfsiz array run from 0 to 14.
  57188. + */
  57189. + /** @todo Finish debug of this */
  57190. + ptxfifosize.b.startaddr =
  57191. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  57192. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  57193. + ptxfifosize.b.depth =
  57194. + params->dev_perio_tx_fifo_size[i];
  57195. + DWC_DEBUGPL(DBG_CIL,
  57196. + "initial dtxfsiz[%d]=%08x\n", i,
  57197. + DWC_READ_REG32(&global_regs->dtxfsiz
  57198. + [i]));
  57199. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  57200. + ptxfifosize.d32);
  57201. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  57202. + i,
  57203. + DWC_READ_REG32(&global_regs->dtxfsiz
  57204. + [i]));
  57205. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  57206. + }
  57207. + } else {
  57208. + /*
  57209. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  57210. + * Indexes of the FIFO size module parameters in the
  57211. + * dev_tx_fifo_size array and the FIFO size registers in
  57212. + * the dtxfsiz array run from 0 to 14.
  57213. + */
  57214. +
  57215. + /* Non-periodic Tx FIFO */
  57216. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  57217. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57218. +
  57219. +#ifdef DWC_UTE_CFI
  57220. + core_if->pwron_gnptxfsiz =
  57221. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  57222. + core_if->init_gnptxfsiz =
  57223. + params->dev_nperio_tx_fifo_size;
  57224. +#endif
  57225. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  57226. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  57227. +
  57228. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  57229. + nptxfifosize.d32);
  57230. +
  57231. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  57232. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57233. +
  57234. + txfifosize.b.startaddr =
  57235. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  57236. +
  57237. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  57238. +
  57239. + txfifosize.b.depth =
  57240. + params->dev_tx_fifo_size[i];
  57241. +
  57242. + DWC_DEBUGPL(DBG_CIL,
  57243. + "initial dtxfsiz[%d]=%08x\n",
  57244. + i,
  57245. + DWC_READ_REG32(&global_regs->dtxfsiz
  57246. + [i]));
  57247. +
  57248. +#ifdef DWC_UTE_CFI
  57249. + core_if->pwron_txfsiz[i] =
  57250. + (DWC_READ_REG32
  57251. + (&global_regs->dtxfsiz[i]) >> 16);
  57252. + core_if->init_txfsiz[i] =
  57253. + params->dev_tx_fifo_size[i];
  57254. +#endif
  57255. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  57256. + txfifosize.d32);
  57257. +
  57258. + DWC_DEBUGPL(DBG_CIL,
  57259. + "new dtxfsiz[%d]=%08x\n",
  57260. + i,
  57261. + DWC_READ_REG32(&global_regs->dtxfsiz
  57262. + [i]));
  57263. +
  57264. + txfifosize.b.startaddr += txfifosize.b.depth;
  57265. + }
  57266. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  57267. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  57268. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  57269. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  57270. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  57271. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  57272. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  57273. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  57274. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  57275. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  57276. + }
  57277. + }
  57278. +
  57279. + /* Flush the FIFOs */
  57280. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  57281. + dwc_otg_flush_rx_fifo(core_if);
  57282. +
  57283. + /* Flush the Learning Queue. */
  57284. + resetctl.b.intknqflsh = 1;
  57285. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  57286. +
  57287. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  57288. + core_if->start_predict = 0;
  57289. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  57290. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  57291. + }
  57292. + core_if->nextep_seq[0] = 0;
  57293. + core_if->first_in_nextep_seq = 0;
  57294. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  57295. + diepctl.b.nextep = 0;
  57296. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  57297. +
  57298. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  57299. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  57300. + dcfg.b.epmscnt = 2;
  57301. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  57302. +
  57303. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  57304. + __func__, core_if->first_in_nextep_seq);
  57305. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  57306. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  57307. + }
  57308. + DWC_DEBUGPL(DBG_CILV,"\n");
  57309. + }
  57310. +
  57311. + /* Clear all pending Device Interrupts */
  57312. + /** @todo - if the condition needed to be checked
  57313. + * or in any case all pending interrutps should be cleared?
  57314. + */
  57315. + if (core_if->multiproc_int_enable) {
  57316. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  57317. + DWC_WRITE_REG32(&dev_if->
  57318. + dev_global_regs->diepeachintmsk[i], 0);
  57319. + }
  57320. + }
  57321. +
  57322. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  57323. + DWC_WRITE_REG32(&dev_if->
  57324. + dev_global_regs->doepeachintmsk[i], 0);
  57325. + }
  57326. +
  57327. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  57328. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  57329. + } else {
  57330. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  57331. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  57332. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  57333. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  57334. + }
  57335. +
  57336. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  57337. + depctl_data_t depctl;
  57338. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  57339. + if (depctl.b.epena) {
  57340. + depctl.d32 = 0;
  57341. + depctl.b.epdis = 1;
  57342. + depctl.b.snak = 1;
  57343. + } else {
  57344. + depctl.d32 = 0;
  57345. + }
  57346. +
  57347. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  57348. +
  57349. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  57350. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  57351. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  57352. + }
  57353. +
  57354. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  57355. + depctl_data_t depctl;
  57356. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  57357. + if (depctl.b.epena) {
  57358. + dctl_data_t dctl = {.d32 = 0 };
  57359. + gintmsk_data_t gintsts = {.d32 = 0 };
  57360. + doepint_data_t doepint = {.d32 = 0 };
  57361. + dctl.b.sgoutnak = 1;
  57362. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57363. + do {
  57364. + dwc_udelay(10);
  57365. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  57366. + } while (!gintsts.b.goutnakeff);
  57367. + gintsts.d32 = 0;
  57368. + gintsts.b.goutnakeff = 1;
  57369. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  57370. +
  57371. + depctl.d32 = 0;
  57372. + depctl.b.epdis = 1;
  57373. + depctl.b.snak = 1;
  57374. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  57375. + do {
  57376. + dwc_udelay(10);
  57377. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  57378. + out_ep_regs[i]->doepint);
  57379. + } while (!doepint.b.epdisabled);
  57380. +
  57381. + doepint.b.epdisabled = 1;
  57382. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  57383. +
  57384. + dctl.d32 = 0;
  57385. + dctl.b.cgoutnak = 1;
  57386. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57387. + } else {
  57388. + depctl.d32 = 0;
  57389. + }
  57390. +
  57391. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  57392. +
  57393. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  57394. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  57395. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  57396. + }
  57397. +
  57398. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  57399. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  57400. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  57401. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  57402. +
  57403. + dev_if->rx_thr_length = params->rx_thr_length;
  57404. + dev_if->tx_thr_length = params->tx_thr_length;
  57405. +
  57406. + dev_if->setup_desc_index = 0;
  57407. +
  57408. + dthrctl.d32 = 0;
  57409. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  57410. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  57411. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  57412. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  57413. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  57414. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  57415. +
  57416. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  57417. + dthrctl.d32);
  57418. +
  57419. + DWC_DEBUGPL(DBG_CIL,
  57420. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  57421. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  57422. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  57423. + dthrctl.b.rx_thr_len);
  57424. +
  57425. + }
  57426. +
  57427. + dwc_otg_enable_device_interrupts(core_if);
  57428. +
  57429. + {
  57430. + diepmsk_data_t msk = {.d32 = 0 };
  57431. + msk.b.txfifoundrn = 1;
  57432. + if (core_if->multiproc_int_enable) {
  57433. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  57434. + diepeachintmsk[0], msk.d32, msk.d32);
  57435. + } else {
  57436. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  57437. + msk.d32, msk.d32);
  57438. + }
  57439. + }
  57440. +
  57441. + if (core_if->multiproc_int_enable) {
  57442. + /* Set NAK on Babble */
  57443. + dctl_data_t dctl = {.d32 = 0 };
  57444. + dctl.b.nakonbble = 1;
  57445. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57446. + }
  57447. +
  57448. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  57449. + dctl_data_t dctl = {.d32 = 0 };
  57450. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  57451. + dctl.b.sftdiscon = 0;
  57452. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  57453. + }
  57454. +}
  57455. +
  57456. +/**
  57457. + * This function enables the Host mode interrupts.
  57458. + *
  57459. + * @param core_if Programming view of DWC_otg controller
  57460. + */
  57461. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  57462. +{
  57463. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57464. + gintmsk_data_t intr_mask = {.d32 = 0 };
  57465. +
  57466. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  57467. +
  57468. + /* Disable all interrupts. */
  57469. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  57470. +
  57471. + /* Clear any pending interrupts. */
  57472. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  57473. +
  57474. + /* Enable the common interrupts */
  57475. + dwc_otg_enable_common_interrupts(core_if);
  57476. +
  57477. + /*
  57478. + * Enable host mode interrupts without disturbing common
  57479. + * interrupts.
  57480. + */
  57481. +
  57482. + intr_mask.b.disconnect = 1;
  57483. + intr_mask.b.portintr = 1;
  57484. + intr_mask.b.hcintr = 1;
  57485. +
  57486. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  57487. +}
  57488. +
  57489. +/**
  57490. + * This function disables the Host Mode interrupts.
  57491. + *
  57492. + * @param core_if Programming view of DWC_otg controller
  57493. + */
  57494. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  57495. +{
  57496. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57497. + gintmsk_data_t intr_mask = {.d32 = 0 };
  57498. +
  57499. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  57500. +
  57501. + /*
  57502. + * Disable host mode interrupts without disturbing common
  57503. + * interrupts.
  57504. + */
  57505. + intr_mask.b.sofintr = 1;
  57506. + intr_mask.b.portintr = 1;
  57507. + intr_mask.b.hcintr = 1;
  57508. + intr_mask.b.ptxfempty = 1;
  57509. + intr_mask.b.nptxfempty = 1;
  57510. +
  57511. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  57512. +}
  57513. +
  57514. +/**
  57515. + * This function initializes the DWC_otg controller registers for
  57516. + * host mode.
  57517. + *
  57518. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  57519. + * request queues. Host channels are reset to ensure that they are ready for
  57520. + * performing transfers.
  57521. + *
  57522. + * @param core_if Programming view of DWC_otg controller
  57523. + *
  57524. + */
  57525. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  57526. +{
  57527. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57528. + dwc_otg_host_if_t *host_if = core_if->host_if;
  57529. + dwc_otg_core_params_t *params = core_if->core_params;
  57530. + hprt0_data_t hprt0 = {.d32 = 0 };
  57531. + fifosize_data_t nptxfifosize;
  57532. + fifosize_data_t ptxfifosize;
  57533. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  57534. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  57535. + int i;
  57536. + hcchar_data_t hcchar;
  57537. + hcfg_data_t hcfg;
  57538. + hfir_data_t hfir;
  57539. + dwc_otg_hc_regs_t *hc_regs;
  57540. + int num_channels;
  57541. + gotgctl_data_t gotgctl = {.d32 = 0 };
  57542. +
  57543. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  57544. +
  57545. + /* Restart the Phy Clock */
  57546. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  57547. +
  57548. + /* Initialize Host Configuration Register */
  57549. + init_fslspclksel(core_if);
  57550. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  57551. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  57552. + hcfg.b.fslssupp = 1;
  57553. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  57554. +
  57555. + }
  57556. +
  57557. + /* This bit allows dynamic reloading of the HFIR register
  57558. + * during runtime. This bit needs to be programmed during
  57559. + * initial configuration and its value must not be changed
  57560. + * during runtime.*/
  57561. + if (core_if->core_params->reload_ctl == 1) {
  57562. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  57563. + hfir.b.hfirrldctrl = 1;
  57564. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  57565. + }
  57566. +
  57567. + if (core_if->core_params->dma_desc_enable) {
  57568. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  57569. + if (!
  57570. + (core_if->hwcfg4.b.desc_dma
  57571. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  57572. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  57573. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  57574. + || (op_mode ==
  57575. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  57576. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  57577. + || (op_mode ==
  57578. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  57579. +
  57580. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  57581. + "Either core version is below 2.90a or "
  57582. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  57583. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  57584. + "module parameter to 0.\n");
  57585. + return;
  57586. + }
  57587. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  57588. + hcfg.b.descdma = 1;
  57589. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  57590. + }
  57591. +
  57592. + /* Configure data FIFO sizes */
  57593. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  57594. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  57595. + core_if->total_fifo_size);
  57596. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  57597. + params->host_rx_fifo_size);
  57598. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  57599. + params->host_nperio_tx_fifo_size);
  57600. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  57601. + params->host_perio_tx_fifo_size);
  57602. +
  57603. + /* Rx FIFO */
  57604. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  57605. + DWC_READ_REG32(&global_regs->grxfsiz));
  57606. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  57607. + params->host_rx_fifo_size);
  57608. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  57609. + DWC_READ_REG32(&global_regs->grxfsiz));
  57610. +
  57611. + /* Non-periodic Tx FIFO */
  57612. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  57613. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57614. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  57615. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  57616. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  57617. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  57618. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57619. +
  57620. + /* Periodic Tx FIFO */
  57621. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  57622. + DWC_READ_REG32(&global_regs->hptxfsiz));
  57623. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  57624. + ptxfifosize.b.startaddr =
  57625. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  57626. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  57627. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  57628. + DWC_READ_REG32(&global_regs->hptxfsiz));
  57629. +
  57630. + if (core_if->en_multiple_tx_fifo
  57631. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  57632. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  57633. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  57634. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  57635. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  57636. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  57637. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  57638. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  57639. + }
  57640. + }
  57641. +
  57642. + /* TODO - check this */
  57643. + /* Clear Host Set HNP Enable in the OTG Control Register */
  57644. + gotgctl.b.hstsethnpen = 1;
  57645. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  57646. + /* Make sure the FIFOs are flushed. */
  57647. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  57648. + dwc_otg_flush_rx_fifo(core_if);
  57649. +
  57650. + /* Clear Host Set HNP Enable in the OTG Control Register */
  57651. + gotgctl.b.hstsethnpen = 1;
  57652. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  57653. +
  57654. + if (!core_if->core_params->dma_desc_enable) {
  57655. + /* Flush out any leftover queued requests. */
  57656. + num_channels = core_if->core_params->host_channels;
  57657. +
  57658. + for (i = 0; i < num_channels; i++) {
  57659. + hc_regs = core_if->host_if->hc_regs[i];
  57660. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57661. + hcchar.b.chen = 0;
  57662. + hcchar.b.chdis = 1;
  57663. + hcchar.b.epdir = 0;
  57664. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57665. + }
  57666. +
  57667. + /* Halt all channels to put them into a known state. */
  57668. + for (i = 0; i < num_channels; i++) {
  57669. + int count = 0;
  57670. + hc_regs = core_if->host_if->hc_regs[i];
  57671. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57672. + hcchar.b.chen = 1;
  57673. + hcchar.b.chdis = 1;
  57674. + hcchar.b.epdir = 0;
  57675. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57676. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  57677. + do {
  57678. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57679. + if (++count > 1000) {
  57680. + DWC_ERROR
  57681. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  57682. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  57683. + break;
  57684. + }
  57685. + dwc_udelay(1);
  57686. + } while (hcchar.b.chen);
  57687. + }
  57688. + }
  57689. +
  57690. + /* Turn on the vbus power. */
  57691. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  57692. + if (core_if->op_state == A_HOST) {
  57693. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  57694. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  57695. + if (hprt0.b.prtpwr == 0) {
  57696. + hprt0.b.prtpwr = 1;
  57697. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  57698. + }
  57699. + }
  57700. +
  57701. + dwc_otg_enable_host_interrupts(core_if);
  57702. +}
  57703. +
  57704. +/**
  57705. + * Prepares a host channel for transferring packets to/from a specific
  57706. + * endpoint. The HCCHARn register is set up with the characteristics specified
  57707. + * in _hc. Host channel interrupts that may need to be serviced while this
  57708. + * transfer is in progress are enabled.
  57709. + *
  57710. + * @param core_if Programming view of DWC_otg controller
  57711. + * @param hc Information needed to initialize the host channel
  57712. + */
  57713. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57714. +{
  57715. + uint32_t intr_enable;
  57716. + hcintmsk_data_t hc_intr_mask;
  57717. + gintmsk_data_t gintmsk = {.d32 = 0 };
  57718. + hcchar_data_t hcchar;
  57719. + hcsplt_data_t hcsplt;
  57720. +
  57721. + uint8_t hc_num = hc->hc_num;
  57722. + dwc_otg_host_if_t *host_if = core_if->host_if;
  57723. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  57724. +
  57725. + /* Clear old interrupt conditions for this host channel. */
  57726. + hc_intr_mask.d32 = 0xFFFFFFFF;
  57727. + hc_intr_mask.b.reserved14_31 = 0;
  57728. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  57729. +
  57730. + /* Enable channel interrupts required for this transfer. */
  57731. + hc_intr_mask.d32 = 0;
  57732. + hc_intr_mask.b.chhltd = 1;
  57733. + if (core_if->dma_enable) {
  57734. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  57735. + if (!core_if->dma_desc_enable)
  57736. + hc_intr_mask.b.ahberr = 1;
  57737. + else {
  57738. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  57739. + hc_intr_mask.b.xfercompl = 1;
  57740. + }
  57741. +
  57742. + if (hc->error_state && !hc->do_split &&
  57743. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  57744. + hc_intr_mask.b.ack = 1;
  57745. + if (hc->ep_is_in) {
  57746. + hc_intr_mask.b.datatglerr = 1;
  57747. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  57748. + hc_intr_mask.b.nak = 1;
  57749. + }
  57750. + }
  57751. + }
  57752. + } else {
  57753. + switch (hc->ep_type) {
  57754. + case DWC_OTG_EP_TYPE_CONTROL:
  57755. + case DWC_OTG_EP_TYPE_BULK:
  57756. + hc_intr_mask.b.xfercompl = 1;
  57757. + hc_intr_mask.b.stall = 1;
  57758. + hc_intr_mask.b.xacterr = 1;
  57759. + hc_intr_mask.b.datatglerr = 1;
  57760. + if (hc->ep_is_in) {
  57761. + hc_intr_mask.b.bblerr = 1;
  57762. + } else {
  57763. + hc_intr_mask.b.nak = 1;
  57764. + hc_intr_mask.b.nyet = 1;
  57765. + if (hc->do_ping) {
  57766. + hc_intr_mask.b.ack = 1;
  57767. + }
  57768. + }
  57769. +
  57770. + if (hc->do_split) {
  57771. + hc_intr_mask.b.nak = 1;
  57772. + if (hc->complete_split) {
  57773. + hc_intr_mask.b.nyet = 1;
  57774. + } else {
  57775. + hc_intr_mask.b.ack = 1;
  57776. + }
  57777. + }
  57778. +
  57779. + if (hc->error_state) {
  57780. + hc_intr_mask.b.ack = 1;
  57781. + }
  57782. + break;
  57783. + case DWC_OTG_EP_TYPE_INTR:
  57784. + hc_intr_mask.b.xfercompl = 1;
  57785. + hc_intr_mask.b.nak = 1;
  57786. + hc_intr_mask.b.stall = 1;
  57787. + hc_intr_mask.b.xacterr = 1;
  57788. + hc_intr_mask.b.datatglerr = 1;
  57789. + hc_intr_mask.b.frmovrun = 1;
  57790. +
  57791. + if (hc->ep_is_in) {
  57792. + hc_intr_mask.b.bblerr = 1;
  57793. + }
  57794. + if (hc->error_state) {
  57795. + hc_intr_mask.b.ack = 1;
  57796. + }
  57797. + if (hc->do_split) {
  57798. + if (hc->complete_split) {
  57799. + hc_intr_mask.b.nyet = 1;
  57800. + } else {
  57801. + hc_intr_mask.b.ack = 1;
  57802. + }
  57803. + }
  57804. + break;
  57805. + case DWC_OTG_EP_TYPE_ISOC:
  57806. + hc_intr_mask.b.xfercompl = 1;
  57807. + hc_intr_mask.b.frmovrun = 1;
  57808. + hc_intr_mask.b.ack = 1;
  57809. +
  57810. + if (hc->ep_is_in) {
  57811. + hc_intr_mask.b.xacterr = 1;
  57812. + hc_intr_mask.b.bblerr = 1;
  57813. + }
  57814. + break;
  57815. + }
  57816. + }
  57817. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  57818. +
  57819. + /* Enable the top level host channel interrupt. */
  57820. + intr_enable = (1 << hc_num);
  57821. + DWC_MODIFY_REG32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
  57822. +
  57823. + /* Make sure host channel interrupts are enabled. */
  57824. + gintmsk.b.hcintr = 1;
  57825. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  57826. +
  57827. + /*
  57828. + * Program the HCCHARn register with the endpoint characteristics for
  57829. + * the current transfer.
  57830. + */
  57831. + hcchar.d32 = 0;
  57832. + hcchar.b.devaddr = hc->dev_addr;
  57833. + hcchar.b.epnum = hc->ep_num;
  57834. + hcchar.b.epdir = hc->ep_is_in;
  57835. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  57836. + hcchar.b.eptype = hc->ep_type;
  57837. + hcchar.b.mps = hc->max_packet;
  57838. +
  57839. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  57840. +
  57841. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  57842. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  57843. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  57844. + "Max Pkt %d, Multi Cnt %d\n",
  57845. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  57846. + hcchar.b.mps, hcchar.b.multicnt);
  57847. +
  57848. + /*
  57849. + * Program the HCSPLIT register for SPLITs
  57850. + */
  57851. + hcsplt.d32 = 0;
  57852. + if (hc->do_split) {
  57853. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  57854. + hc->hc_num,
  57855. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  57856. + hcsplt.b.compsplt = hc->complete_split;
  57857. + hcsplt.b.xactpos = hc->xact_pos;
  57858. + hcsplt.b.hubaddr = hc->hub_addr;
  57859. + hcsplt.b.prtaddr = hc->port_addr;
  57860. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  57861. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  57862. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  57863. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  57864. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  57865. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  57866. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  57867. + }
  57868. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  57869. +
  57870. +}
  57871. +
  57872. +/**
  57873. + * Attempts to halt a host channel. This function should only be called in
  57874. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  57875. + * normal circumstances in DMA mode, the controller halts the channel when the
  57876. + * transfer is complete or a condition occurs that requires application
  57877. + * intervention.
  57878. + *
  57879. + * In slave mode, checks for a free request queue entry, then sets the Channel
  57880. + * Enable and Channel Disable bits of the Host Channel Characteristics
  57881. + * register of the specified channel to intiate the halt. If there is no free
  57882. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  57883. + * register to flush requests for this channel. In the latter case, sets a
  57884. + * flag to indicate that the host channel needs to be halted when a request
  57885. + * queue slot is open.
  57886. + *
  57887. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  57888. + * HCCHARn register. The controller ensures there is space in the request
  57889. + * queue before submitting the halt request.
  57890. + *
  57891. + * Some time may elapse before the core flushes any posted requests for this
  57892. + * host channel and halts. The Channel Halted interrupt handler completes the
  57893. + * deactivation of the host channel.
  57894. + *
  57895. + * @param core_if Controller register interface.
  57896. + * @param hc Host channel to halt.
  57897. + * @param halt_status Reason for halting the channel.
  57898. + */
  57899. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  57900. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  57901. +{
  57902. + gnptxsts_data_t nptxsts;
  57903. + hptxsts_data_t hptxsts;
  57904. + hcchar_data_t hcchar;
  57905. + dwc_otg_hc_regs_t *hc_regs;
  57906. + dwc_otg_core_global_regs_t *global_regs;
  57907. + dwc_otg_host_global_regs_t *host_global_regs;
  57908. +
  57909. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57910. + global_regs = core_if->core_global_regs;
  57911. + host_global_regs = core_if->host_if->host_global_regs;
  57912. +
  57913. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  57914. + "halt_status = %d\n", halt_status);
  57915. +
  57916. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  57917. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  57918. + /*
  57919. + * Disable all channel interrupts except Ch Halted. The QTD
  57920. + * and QH state associated with this transfer has been cleared
  57921. + * (in the case of URB_DEQUEUE), so the channel needs to be
  57922. + * shut down carefully to prevent crashes.
  57923. + */
  57924. + hcintmsk_data_t hcintmsk;
  57925. + hcintmsk.d32 = 0;
  57926. + hcintmsk.b.chhltd = 1;
  57927. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  57928. +
  57929. + /*
  57930. + * Make sure no other interrupts besides halt are currently
  57931. + * pending. Handling another interrupt could cause a crash due
  57932. + * to the QTD and QH state.
  57933. + */
  57934. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  57935. +
  57936. + /*
  57937. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  57938. + * even if the channel was already halted for some other
  57939. + * reason.
  57940. + */
  57941. + hc->halt_status = halt_status;
  57942. +
  57943. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57944. + if (hcchar.b.chen == 0) {
  57945. + /*
  57946. + * The channel is either already halted or it hasn't
  57947. + * started yet. In DMA mode, the transfer may halt if
  57948. + * it finishes normally or a condition occurs that
  57949. + * requires driver intervention. Don't want to halt
  57950. + * the channel again. In either Slave or DMA mode,
  57951. + * it's possible that the transfer has been assigned
  57952. + * to a channel, but not started yet when an URB is
  57953. + * dequeued. Don't want to halt a channel that hasn't
  57954. + * started yet.
  57955. + */
  57956. + return;
  57957. + }
  57958. + }
  57959. + if (hc->halt_pending) {
  57960. + /*
  57961. + * A halt has already been issued for this channel. This might
  57962. + * happen when a transfer is aborted by a higher level in
  57963. + * the stack.
  57964. + */
  57965. +#ifdef DEBUG
  57966. + DWC_PRINTF
  57967. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  57968. + __func__, hc->hc_num);
  57969. +
  57970. +#endif
  57971. + return;
  57972. + }
  57973. +
  57974. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57975. +
  57976. + /* No need to set the bit in DDMA for disabling the channel */
  57977. + //TODO check it everywhere channel is disabled
  57978. + if (!core_if->core_params->dma_desc_enable)
  57979. + hcchar.b.chen = 1;
  57980. + hcchar.b.chdis = 1;
  57981. +
  57982. + if (!core_if->dma_enable) {
  57983. + /* Check for space in the request queue to issue the halt. */
  57984. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  57985. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  57986. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  57987. + if (nptxsts.b.nptxqspcavail == 0) {
  57988. + hcchar.b.chen = 0;
  57989. + }
  57990. + } else {
  57991. + hptxsts.d32 =
  57992. + DWC_READ_REG32(&host_global_regs->hptxsts);
  57993. + if ((hptxsts.b.ptxqspcavail == 0)
  57994. + || (core_if->queuing_high_bandwidth)) {
  57995. + hcchar.b.chen = 0;
  57996. + }
  57997. + }
  57998. + }
  57999. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58000. +
  58001. + hc->halt_status = halt_status;
  58002. +
  58003. + if (hcchar.b.chen) {
  58004. + hc->halt_pending = 1;
  58005. + hc->halt_on_queue = 0;
  58006. + } else {
  58007. + hc->halt_on_queue = 1;
  58008. + }
  58009. +
  58010. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58011. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  58012. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  58013. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  58014. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  58015. +
  58016. + return;
  58017. +}
  58018. +
  58019. +/**
  58020. + * Clears the transfer state for a host channel. This function is normally
  58021. + * called after a transfer is done and the host channel is being released.
  58022. + *
  58023. + * @param core_if Programming view of DWC_otg controller.
  58024. + * @param hc Identifies the host channel to clean up.
  58025. + */
  58026. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58027. +{
  58028. + dwc_otg_hc_regs_t *hc_regs;
  58029. +
  58030. + hc->xfer_started = 0;
  58031. +
  58032. + /*
  58033. + * Clear channel interrupt enables and any unhandled channel interrupt
  58034. + * conditions.
  58035. + */
  58036. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58037. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  58038. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  58039. +#ifdef DEBUG
  58040. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  58041. +#endif
  58042. +}
  58043. +
  58044. +/**
  58045. + * Sets the channel property that indicates in which frame a periodic transfer
  58046. + * should occur. This is always set to the _next_ frame. This function has no
  58047. + * effect on non-periodic transfers.
  58048. + *
  58049. + * @param core_if Programming view of DWC_otg controller.
  58050. + * @param hc Identifies the host channel to set up and its properties.
  58051. + * @param hcchar Current value of the HCCHAR register for the specified host
  58052. + * channel.
  58053. + */
  58054. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  58055. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  58056. +{
  58057. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58058. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58059. + hfnum_data_t hfnum;
  58060. + hfnum.d32 =
  58061. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  58062. +
  58063. + /* 1 if _next_ frame is odd, 0 if it's even */
  58064. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  58065. +#ifdef DEBUG
  58066. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  58067. + && !hc->complete_split) {
  58068. + switch (hfnum.b.frnum & 0x7) {
  58069. + case 7:
  58070. + core_if->hfnum_7_samples++;
  58071. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  58072. + break;
  58073. + case 0:
  58074. + core_if->hfnum_0_samples++;
  58075. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  58076. + break;
  58077. + default:
  58078. + core_if->hfnum_other_samples++;
  58079. + core_if->hfnum_other_frrem_accum +=
  58080. + hfnum.b.frrem;
  58081. + break;
  58082. + }
  58083. + }
  58084. +#endif
  58085. + }
  58086. +}
  58087. +
  58088. +#ifdef DEBUG
  58089. +void hc_xfer_timeout(void *ptr)
  58090. +{
  58091. + hc_xfer_info_t *xfer_info = NULL;
  58092. + int hc_num = 0;
  58093. +
  58094. + if (ptr)
  58095. + xfer_info = (hc_xfer_info_t *) ptr;
  58096. +
  58097. + if (!xfer_info->hc) {
  58098. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  58099. + return;
  58100. + }
  58101. +
  58102. + hc_num = xfer_info->hc->hc_num;
  58103. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  58104. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  58105. + xfer_info->core_if->start_hcchar_val[hc_num]);
  58106. +}
  58107. +#endif
  58108. +
  58109. +void ep_xfer_timeout(void *ptr)
  58110. +{
  58111. + ep_xfer_info_t *xfer_info = NULL;
  58112. + int ep_num = 0;
  58113. + dctl_data_t dctl = {.d32 = 0 };
  58114. + gintsts_data_t gintsts = {.d32 = 0 };
  58115. + gintmsk_data_t gintmsk = {.d32 = 0 };
  58116. +
  58117. + if (ptr)
  58118. + xfer_info = (ep_xfer_info_t *) ptr;
  58119. +
  58120. + if (!xfer_info->ep) {
  58121. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  58122. + return;
  58123. + }
  58124. +
  58125. + ep_num = xfer_info->ep->num;
  58126. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  58127. + /* Put the sate to 2 as it was time outed */
  58128. + xfer_info->state = 2;
  58129. +
  58130. + dctl.d32 =
  58131. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  58132. + gintsts.d32 =
  58133. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  58134. + gintmsk.d32 =
  58135. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  58136. +
  58137. + if (!gintmsk.b.goutnakeff) {
  58138. + /* Unmask it */
  58139. + gintmsk.b.goutnakeff = 1;
  58140. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  58141. + gintmsk.d32);
  58142. +
  58143. + }
  58144. +
  58145. + if (!gintsts.b.goutnakeff) {
  58146. + dctl.b.sgoutnak = 1;
  58147. + }
  58148. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  58149. + dctl.d32);
  58150. +
  58151. +}
  58152. +
  58153. +void set_pid_isoc(dwc_hc_t * hc)
  58154. +{
  58155. + /* Set up the initial PID for the transfer. */
  58156. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  58157. + if (hc->ep_is_in) {
  58158. + if (hc->multi_count == 1) {
  58159. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  58160. + } else if (hc->multi_count == 2) {
  58161. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  58162. + } else {
  58163. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  58164. + }
  58165. + } else {
  58166. + if (hc->multi_count == 1) {
  58167. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  58168. + } else {
  58169. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  58170. + }
  58171. + }
  58172. + } else {
  58173. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  58174. + }
  58175. +}
  58176. +
  58177. +/**
  58178. + * This function does the setup for a data transfer for a host channel and
  58179. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  58180. + * Slave mode, the caller must ensure that there is sufficient space in the
  58181. + * request queue and Tx Data FIFO.
  58182. + *
  58183. + * For an OUT transfer in Slave mode, it loads a data packet into the
  58184. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  58185. + * the Host ISR.
  58186. + *
  58187. + * For an IN transfer in Slave mode, a data packet is requested. The data
  58188. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  58189. + * additional data packets are requested in the Host ISR.
  58190. + *
  58191. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  58192. + * register along with a packet count of 1 and the channel is enabled. This
  58193. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  58194. + * simply set to 0 since no data transfer occurs in this case.
  58195. + *
  58196. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  58197. + * all the information required to perform the subsequent data transfer. In
  58198. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  58199. + * controller performs the entire PING protocol, then starts the data
  58200. + * transfer.
  58201. + *
  58202. + * @param core_if Programming view of DWC_otg controller.
  58203. + * @param hc Information needed to initialize the host channel. The xfer_len
  58204. + * value may be reduced to accommodate the max widths of the XferSize and
  58205. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  58206. + * to reflect the final xfer_len value.
  58207. + */
  58208. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58209. +{
  58210. + hcchar_data_t hcchar;
  58211. + hctsiz_data_t hctsiz;
  58212. + uint16_t num_packets;
  58213. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  58214. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  58215. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58216. +
  58217. + hctsiz.d32 = 0;
  58218. +
  58219. + if (hc->do_ping) {
  58220. + if (!core_if->dma_enable) {
  58221. + dwc_otg_hc_do_ping(core_if, hc);
  58222. + hc->xfer_started = 1;
  58223. + return;
  58224. + } else {
  58225. + hctsiz.b.dopng = 1;
  58226. + }
  58227. + }
  58228. +
  58229. + if (hc->do_split) {
  58230. + num_packets = 1;
  58231. +
  58232. + if (hc->complete_split && !hc->ep_is_in) {
  58233. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  58234. + * core doesn't expect any data written to the FIFO */
  58235. + hc->xfer_len = 0;
  58236. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  58237. + hc->xfer_len = hc->max_packet;
  58238. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  58239. + hc->xfer_len = 188;
  58240. + }
  58241. +
  58242. + hctsiz.b.xfersize = hc->xfer_len;
  58243. + } else {
  58244. + /*
  58245. + * Ensure that the transfer length and packet count will fit
  58246. + * in the widths allocated for them in the HCTSIZn register.
  58247. + */
  58248. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58249. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58250. + /*
  58251. + * Make sure the transfer size is no larger than one
  58252. + * (micro)frame's worth of data. (A check was done
  58253. + * when the periodic transfer was accepted to ensure
  58254. + * that a (micro)frame's worth of data can be
  58255. + * programmed into a channel.)
  58256. + */
  58257. + uint32_t max_periodic_len =
  58258. + hc->multi_count * hc->max_packet;
  58259. + if (hc->xfer_len > max_periodic_len) {
  58260. + hc->xfer_len = max_periodic_len;
  58261. + } else {
  58262. + }
  58263. + } else if (hc->xfer_len > max_hc_xfer_size) {
  58264. + /* Make sure that xfer_len is a multiple of max packet size. */
  58265. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  58266. + }
  58267. +
  58268. + if (hc->xfer_len > 0) {
  58269. + num_packets =
  58270. + (hc->xfer_len + hc->max_packet -
  58271. + 1) / hc->max_packet;
  58272. + if (num_packets > max_hc_pkt_count) {
  58273. + num_packets = max_hc_pkt_count;
  58274. + hc->xfer_len = num_packets * hc->max_packet;
  58275. + }
  58276. + } else {
  58277. + /* Need 1 packet for transfer length of 0. */
  58278. + num_packets = 1;
  58279. + }
  58280. +
  58281. + if (hc->ep_is_in) {
  58282. + /* Always program an integral # of max packets for IN transfers. */
  58283. + hc->xfer_len = num_packets * hc->max_packet;
  58284. + }
  58285. +
  58286. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58287. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58288. + /*
  58289. + * Make sure that the multi_count field matches the
  58290. + * actual transfer length.
  58291. + */
  58292. + hc->multi_count = num_packets;
  58293. + }
  58294. +
  58295. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  58296. + set_pid_isoc(hc);
  58297. +
  58298. + hctsiz.b.xfersize = hc->xfer_len;
  58299. + }
  58300. +
  58301. + hc->start_pkt_count = num_packets;
  58302. + hctsiz.b.pktcnt = num_packets;
  58303. + hctsiz.b.pid = hc->data_pid_start;
  58304. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  58305. +
  58306. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58307. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  58308. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  58309. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  58310. +
  58311. + if (core_if->dma_enable) {
  58312. + dwc_dma_t dma_addr;
  58313. + if (hc->align_buff) {
  58314. + dma_addr = hc->align_buff;
  58315. + } else {
  58316. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  58317. + }
  58318. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  58319. + }
  58320. +
  58321. + /* Start the split */
  58322. + if (hc->do_split) {
  58323. + hcsplt_data_t hcsplt;
  58324. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  58325. + hcsplt.b.spltena = 1;
  58326. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  58327. + }
  58328. +
  58329. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58330. + hcchar.b.multicnt = hc->multi_count;
  58331. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  58332. +#ifdef DEBUG
  58333. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  58334. + if (hcchar.b.chdis) {
  58335. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  58336. + __func__, hc->hc_num, hcchar.d32);
  58337. + }
  58338. +#endif
  58339. +
  58340. + /* Set host channel enable after all other setup is complete. */
  58341. + hcchar.b.chen = 1;
  58342. + hcchar.b.chdis = 0;
  58343. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58344. +
  58345. + hc->xfer_started = 1;
  58346. + hc->requests++;
  58347. +
  58348. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  58349. + /* Load OUT packet into the appropriate Tx FIFO. */
  58350. + dwc_otg_hc_write_packet(core_if, hc);
  58351. + }
  58352. +#ifdef DEBUG
  58353. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  58354. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  58355. + hc->hc_num, core_if);//GRAYG
  58356. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  58357. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  58358. +
  58359. + /* Start a timer for this transfer. */
  58360. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  58361. + }
  58362. +#endif
  58363. +}
  58364. +
  58365. +/**
  58366. + * This function does the setup for a data transfer for a host channel
  58367. + * and starts the transfer in Descriptor DMA mode.
  58368. + *
  58369. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  58370. + * Sets PID and NTD values. For periodic transfers
  58371. + * initializes SCHED_INFO field with micro-frame bitmap.
  58372. + *
  58373. + * Initializes HCDMA register with descriptor list address and CTD value
  58374. + * then starts the transfer via enabling the channel.
  58375. + *
  58376. + * @param core_if Programming view of DWC_otg controller.
  58377. + * @param hc Information needed to initialize the host channel.
  58378. + */
  58379. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58380. +{
  58381. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58382. + hcchar_data_t hcchar;
  58383. + hctsiz_data_t hctsiz;
  58384. + hcdma_data_t hcdma;
  58385. +
  58386. + hctsiz.d32 = 0;
  58387. +
  58388. + if (hc->do_ping)
  58389. + hctsiz.b_ddma.dopng = 1;
  58390. +
  58391. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  58392. + set_pid_isoc(hc);
  58393. +
  58394. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  58395. + hctsiz.b_ddma.pid = hc->data_pid_start;
  58396. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  58397. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  58398. +
  58399. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58400. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  58401. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  58402. +
  58403. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  58404. +
  58405. + hcdma.d32 = 0;
  58406. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  58407. +
  58408. + /* Always start from first descriptor. */
  58409. + hcdma.b.ctd = 0;
  58410. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  58411. +
  58412. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58413. + hcchar.b.multicnt = hc->multi_count;
  58414. +
  58415. +#ifdef DEBUG
  58416. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  58417. + if (hcchar.b.chdis) {
  58418. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  58419. + __func__, hc->hc_num, hcchar.d32);
  58420. + }
  58421. +#endif
  58422. +
  58423. + /* Set host channel enable after all other setup is complete. */
  58424. + hcchar.b.chen = 1;
  58425. + hcchar.b.chdis = 0;
  58426. +
  58427. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58428. +
  58429. + hc->xfer_started = 1;
  58430. + hc->requests++;
  58431. +
  58432. +#ifdef DEBUG
  58433. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  58434. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  58435. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  58436. + hc->hc_num, core_if);//GRAYG
  58437. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  58438. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  58439. + /* Start a timer for this transfer. */
  58440. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  58441. + }
  58442. +#endif
  58443. +
  58444. +}
  58445. +
  58446. +/**
  58447. + * This function continues a data transfer that was started by previous call
  58448. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  58449. + * sufficient space in the request queue and Tx Data FIFO. This function
  58450. + * should only be called in Slave mode. In DMA mode, the controller acts
  58451. + * autonomously to complete transfers programmed to a host channel.
  58452. + *
  58453. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  58454. + * if there is any data remaining to be queued. For an IN transfer, another
  58455. + * data packet is always requested. For the SETUP phase of a control transfer,
  58456. + * this function does nothing.
  58457. + *
  58458. + * @return 1 if a new request is queued, 0 if no more requests are required
  58459. + * for this transfer.
  58460. + */
  58461. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58462. +{
  58463. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58464. +
  58465. + if (hc->do_split) {
  58466. + /* SPLITs always queue just once per channel */
  58467. + return 0;
  58468. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  58469. + /* SETUPs are queued only once since they can't be NAKed. */
  58470. + return 0;
  58471. + } else if (hc->ep_is_in) {
  58472. + /*
  58473. + * Always queue another request for other IN transfers. If
  58474. + * back-to-back INs are issued and NAKs are received for both,
  58475. + * the driver may still be processing the first NAK when the
  58476. + * second NAK is received. When the interrupt handler clears
  58477. + * the NAK interrupt for the first NAK, the second NAK will
  58478. + * not be seen. So we can't depend on the NAK interrupt
  58479. + * handler to requeue a NAKed request. Instead, IN requests
  58480. + * are issued each time this function is called. When the
  58481. + * transfer completes, the extra requests for the channel will
  58482. + * be flushed.
  58483. + */
  58484. + hcchar_data_t hcchar;
  58485. + dwc_otg_hc_regs_t *hc_regs =
  58486. + core_if->host_if->hc_regs[hc->hc_num];
  58487. +
  58488. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58489. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  58490. + hcchar.b.chen = 1;
  58491. + hcchar.b.chdis = 0;
  58492. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  58493. + hcchar.d32);
  58494. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58495. + hc->requests++;
  58496. + return 1;
  58497. + } else {
  58498. + /* OUT transfers. */
  58499. + if (hc->xfer_count < hc->xfer_len) {
  58500. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58501. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58502. + hcchar_data_t hcchar;
  58503. + dwc_otg_hc_regs_t *hc_regs;
  58504. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58505. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58506. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  58507. + }
  58508. +
  58509. + /* Load OUT packet into the appropriate Tx FIFO. */
  58510. + dwc_otg_hc_write_packet(core_if, hc);
  58511. + hc->requests++;
  58512. + return 1;
  58513. + } else {
  58514. + return 0;
  58515. + }
  58516. + }
  58517. +}
  58518. +
  58519. +/**
  58520. + * Starts a PING transfer. This function should only be called in Slave mode.
  58521. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  58522. + */
  58523. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58524. +{
  58525. + hcchar_data_t hcchar;
  58526. + hctsiz_data_t hctsiz;
  58527. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58528. +
  58529. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58530. +
  58531. + hctsiz.d32 = 0;
  58532. + hctsiz.b.dopng = 1;
  58533. + hctsiz.b.pktcnt = 1;
  58534. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  58535. +
  58536. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58537. + hcchar.b.chen = 1;
  58538. + hcchar.b.chdis = 0;
  58539. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58540. +}
  58541. +
  58542. +/*
  58543. + * This function writes a packet into the Tx FIFO associated with the Host
  58544. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  58545. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  58546. + * periodic Tx FIFO is written. This function should only be called in Slave
  58547. + * mode.
  58548. + *
  58549. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  58550. + * then number of bytes written to the Tx FIFO.
  58551. + */
  58552. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58553. +{
  58554. + uint32_t i;
  58555. + uint32_t remaining_count;
  58556. + uint32_t byte_count;
  58557. + uint32_t dword_count;
  58558. +
  58559. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  58560. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  58561. +
  58562. + remaining_count = hc->xfer_len - hc->xfer_count;
  58563. + if (remaining_count > hc->max_packet) {
  58564. + byte_count = hc->max_packet;
  58565. + } else {
  58566. + byte_count = remaining_count;
  58567. + }
  58568. +
  58569. + dword_count = (byte_count + 3) / 4;
  58570. +
  58571. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  58572. + /* xfer_buff is DWORD aligned. */
  58573. + for (i = 0; i < dword_count; i++, data_buff++) {
  58574. + DWC_WRITE_REG32(data_fifo, *data_buff);
  58575. + }
  58576. + } else {
  58577. + /* xfer_buff is not DWORD aligned. */
  58578. + for (i = 0; i < dword_count; i++, data_buff++) {
  58579. + uint32_t data;
  58580. + data =
  58581. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  58582. + 16 | data_buff[3] << 24);
  58583. + DWC_WRITE_REG32(data_fifo, data);
  58584. + }
  58585. + }
  58586. +
  58587. + hc->xfer_count += byte_count;
  58588. + hc->xfer_buff += byte_count;
  58589. +}
  58590. +
  58591. +/**
  58592. + * Gets the current USB frame number. This is the frame number from the last
  58593. + * SOF packet.
  58594. + */
  58595. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  58596. +{
  58597. + dsts_data_t dsts;
  58598. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  58599. +
  58600. + /* read current frame/microframe number from DSTS register */
  58601. + return dsts.b.soffn;
  58602. +}
  58603. +
  58604. +/**
  58605. + * Calculates and gets the frame Interval value of HFIR register according PHY
  58606. + * type and speed.The application can modify a value of HFIR register only after
  58607. + * the Port Enable bit of the Host Port Control and Status register
  58608. + * (HPRT.PrtEnaPort) has been set.
  58609. +*/
  58610. +
  58611. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  58612. +{
  58613. + gusbcfg_data_t usbcfg;
  58614. + hwcfg2_data_t hwcfg2;
  58615. + hprt0_data_t hprt0;
  58616. + int clock = 60; // default value
  58617. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  58618. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  58619. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  58620. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  58621. + clock = 60;
  58622. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  58623. + clock = 48;
  58624. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  58625. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  58626. + clock = 30;
  58627. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  58628. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  58629. + clock = 60;
  58630. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  58631. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  58632. + clock = 48;
  58633. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  58634. + clock = 48;
  58635. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  58636. + clock = 48;
  58637. + if (hprt0.b.prtspd == 0)
  58638. + /* High speed case */
  58639. + return 125 * clock;
  58640. + else
  58641. + /* FS/LS case */
  58642. + return 1000 * clock;
  58643. +}
  58644. +
  58645. +/**
  58646. + * This function reads a setup packet from the Rx FIFO into the destination
  58647. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  58648. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  58649. + *
  58650. + * @param core_if Programming view of DWC_otg controller.
  58651. + * @param dest Destination buffer for packet data.
  58652. + */
  58653. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  58654. +{
  58655. + device_grxsts_data_t status;
  58656. + /* Get the 8 bytes of a setup transaction data */
  58657. +
  58658. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  58659. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  58660. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  58661. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  58662. + status.d32 =
  58663. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  58664. + DWC_DEBUGPL(DBG_ANY,
  58665. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  58666. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  58667. + status.b.fn, status.b.fn);
  58668. + }
  58669. +}
  58670. +
  58671. +/**
  58672. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  58673. + * IN for transmitting packets. It is normally called when the
  58674. + * "Enumeration Done" interrupt occurs.
  58675. + *
  58676. + * @param core_if Programming view of DWC_otg controller.
  58677. + * @param ep The EP0 data.
  58678. + */
  58679. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58680. +{
  58681. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  58682. + dsts_data_t dsts;
  58683. + depctl_data_t diepctl;
  58684. + depctl_data_t doepctl;
  58685. + dctl_data_t dctl = {.d32 = 0 };
  58686. +
  58687. + ep->stp_rollover = 0;
  58688. + /* Read the Device Status and Endpoint 0 Control registers */
  58689. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  58690. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  58691. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  58692. +
  58693. + /* Set the MPS of the IN EP based on the enumeration speed */
  58694. + switch (dsts.b.enumspd) {
  58695. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  58696. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  58697. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  58698. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  58699. + break;
  58700. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  58701. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  58702. + break;
  58703. + }
  58704. +
  58705. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  58706. +
  58707. + /* Enable OUT EP for receive */
  58708. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  58709. + doepctl.b.epena = 1;
  58710. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  58711. + }
  58712. +#ifdef VERBOSE
  58713. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  58714. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  58715. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  58716. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  58717. +#endif
  58718. + dctl.b.cgnpinnak = 1;
  58719. +
  58720. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  58721. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  58722. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  58723. +
  58724. +}
  58725. +
  58726. +/**
  58727. + * This function activates an EP. The Device EP control register for
  58728. + * the EP is configured as defined in the ep structure. Note: This
  58729. + * function is not used for EP0.
  58730. + *
  58731. + * @param core_if Programming view of DWC_otg controller.
  58732. + * @param ep The EP to activate.
  58733. + */
  58734. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58735. +{
  58736. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  58737. + depctl_data_t depctl;
  58738. + volatile uint32_t *addr;
  58739. + daint_data_t daintmsk = {.d32 = 0 };
  58740. + dcfg_data_t dcfg;
  58741. + uint8_t i;
  58742. +
  58743. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  58744. + (ep->is_in ? "IN" : "OUT"));
  58745. +
  58746. +#ifdef DWC_UTE_PER_IO
  58747. + ep->xiso_frame_num = 0xFFFFFFFF;
  58748. + ep->xiso_active_xfers = 0;
  58749. + ep->xiso_queued_xfers = 0;
  58750. +#endif
  58751. + /* Read DEPCTLn register */
  58752. + if (ep->is_in == 1) {
  58753. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  58754. + daintmsk.ep.in = 1 << ep->num;
  58755. + } else {
  58756. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  58757. + daintmsk.ep.out = 1 << ep->num;
  58758. + }
  58759. +
  58760. + /* If the EP is already active don't change the EP Control
  58761. + * register. */
  58762. + depctl.d32 = DWC_READ_REG32(addr);
  58763. + if (!depctl.b.usbactep) {
  58764. + depctl.b.mps = ep->maxpacket;
  58765. + depctl.b.eptype = ep->type;
  58766. + depctl.b.txfnum = ep->tx_fifo_num;
  58767. +
  58768. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58769. + depctl.b.setd0pid = 1; // ???
  58770. + } else {
  58771. + depctl.b.setd0pid = 1;
  58772. + }
  58773. + depctl.b.usbactep = 1;
  58774. +
  58775. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  58776. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  58777. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  58778. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  58779. + break;
  58780. + }
  58781. + core_if->nextep_seq[i] = ep->num;
  58782. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  58783. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  58784. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  58785. + dcfg.b.epmscnt++;
  58786. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  58787. +
  58788. + DWC_DEBUGPL(DBG_PCDV,
  58789. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  58790. + __func__, core_if->first_in_nextep_seq);
  58791. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  58792. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  58793. + core_if->nextep_seq[i]);
  58794. + }
  58795. +
  58796. + }
  58797. +
  58798. +
  58799. + DWC_WRITE_REG32(addr, depctl.d32);
  58800. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  58801. + }
  58802. +
  58803. + /* Enable the Interrupt for this EP */
  58804. + if (core_if->multiproc_int_enable) {
  58805. + if (ep->is_in == 1) {
  58806. + diepmsk_data_t diepmsk = {.d32 = 0 };
  58807. + diepmsk.b.xfercompl = 1;
  58808. + diepmsk.b.timeout = 1;
  58809. + diepmsk.b.epdisabled = 1;
  58810. + diepmsk.b.ahberr = 1;
  58811. + diepmsk.b.intknepmis = 1;
  58812. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  58813. + diepmsk.b.intknepmis = 0;
  58814. + diepmsk.b.txfifoundrn = 1; //?????
  58815. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58816. + diepmsk.b.nak = 1;
  58817. + }
  58818. +
  58819. +
  58820. +
  58821. +/*
  58822. + if (core_if->dma_desc_enable) {
  58823. + diepmsk.b.bna = 1;
  58824. + }
  58825. +*/
  58826. +/*
  58827. + if (core_if->dma_enable) {
  58828. + doepmsk.b.nak = 1;
  58829. + }
  58830. +*/
  58831. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  58832. + diepeachintmsk[ep->num], diepmsk.d32);
  58833. +
  58834. + } else {
  58835. + doepmsk_data_t doepmsk = {.d32 = 0 };
  58836. + doepmsk.b.xfercompl = 1;
  58837. + doepmsk.b.ahberr = 1;
  58838. + doepmsk.b.epdisabled = 1;
  58839. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  58840. + doepmsk.b.outtknepdis = 1;
  58841. +
  58842. +/*
  58843. +
  58844. + if (core_if->dma_desc_enable) {
  58845. + doepmsk.b.bna = 1;
  58846. + }
  58847. +*/
  58848. +/*
  58849. + doepmsk.b.babble = 1;
  58850. + doepmsk.b.nyet = 1;
  58851. + doepmsk.b.nak = 1;
  58852. +*/
  58853. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  58854. + doepeachintmsk[ep->num], doepmsk.d32);
  58855. + }
  58856. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  58857. + 0, daintmsk.d32);
  58858. + } else {
  58859. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58860. + if (ep->is_in) {
  58861. + diepmsk_data_t diepmsk = {.d32 = 0 };
  58862. + diepmsk.b.nak = 1;
  58863. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  58864. + } else {
  58865. + doepmsk_data_t doepmsk = {.d32 = 0 };
  58866. + doepmsk.b.outtknepdis = 1;
  58867. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  58868. + }
  58869. + }
  58870. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  58871. + 0, daintmsk.d32);
  58872. + }
  58873. +
  58874. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  58875. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  58876. +
  58877. + ep->stall_clear_flag = 0;
  58878. +
  58879. + return;
  58880. +}
  58881. +
  58882. +/**
  58883. + * This function deactivates an EP. This is done by clearing the USB Active
  58884. + * EP bit in the Device EP control register. Note: This function is not used
  58885. + * for EP0. EP0 cannot be deactivated.
  58886. + *
  58887. + * @param core_if Programming view of DWC_otg controller.
  58888. + * @param ep The EP to deactivate.
  58889. + */
  58890. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58891. +{
  58892. + depctl_data_t depctl = {.d32 = 0 };
  58893. + volatile uint32_t *addr;
  58894. + daint_data_t daintmsk = {.d32 = 0 };
  58895. + dcfg_data_t dcfg;
  58896. + uint8_t i = 0;
  58897. +
  58898. +#ifdef DWC_UTE_PER_IO
  58899. + ep->xiso_frame_num = 0xFFFFFFFF;
  58900. + ep->xiso_active_xfers = 0;
  58901. + ep->xiso_queued_xfers = 0;
  58902. +#endif
  58903. +
  58904. + /* Read DEPCTLn register */
  58905. + if (ep->is_in == 1) {
  58906. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  58907. + daintmsk.ep.in = 1 << ep->num;
  58908. + } else {
  58909. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  58910. + daintmsk.ep.out = 1 << ep->num;
  58911. + }
  58912. +
  58913. + depctl.d32 = DWC_READ_REG32(addr);
  58914. +
  58915. + depctl.b.usbactep = 0;
  58916. +
  58917. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  58918. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  58919. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  58920. + if (core_if->nextep_seq[i] == ep->num)
  58921. + break;
  58922. + }
  58923. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  58924. + if (core_if->first_in_nextep_seq == ep->num)
  58925. + core_if->first_in_nextep_seq = i;
  58926. + core_if->nextep_seq[ep->num] = 0xff;
  58927. + depctl.b.nextep = 0;
  58928. + dcfg.d32 =
  58929. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  58930. + dcfg.b.epmscnt--;
  58931. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  58932. + dcfg.d32);
  58933. +
  58934. + DWC_DEBUGPL(DBG_PCDV,
  58935. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  58936. + __func__, core_if->first_in_nextep_seq);
  58937. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  58938. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  58939. + }
  58940. + }
  58941. +
  58942. + if (ep->is_in == 1)
  58943. + depctl.b.txfnum = 0;
  58944. +
  58945. + if (core_if->dma_desc_enable)
  58946. + depctl.b.epdis = 1;
  58947. +
  58948. + DWC_WRITE_REG32(addr, depctl.d32);
  58949. + depctl.d32 = DWC_READ_REG32(addr);
  58950. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  58951. + && depctl.b.epena) {
  58952. + depctl_data_t depctl = {.d32 = 0};
  58953. + if (ep->is_in) {
  58954. + diepint_data_t diepint = {.d32 = 0};
  58955. +
  58956. + depctl.b.snak = 1;
  58957. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58958. + diepctl, depctl.d32);
  58959. + do {
  58960. + dwc_udelay(10);
  58961. + diepint.d32 =
  58962. + DWC_READ_REG32(&core_if->
  58963. + dev_if->in_ep_regs[ep->num]->
  58964. + diepint);
  58965. + } while (!diepint.b.inepnakeff);
  58966. + diepint.b.inepnakeff = 1;
  58967. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58968. + diepint, diepint.d32);
  58969. + depctl.d32 = 0;
  58970. + depctl.b.epdis = 1;
  58971. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58972. + diepctl, depctl.d32);
  58973. + do {
  58974. + dwc_udelay(10);
  58975. + diepint.d32 =
  58976. + DWC_READ_REG32(&core_if->
  58977. + dev_if->in_ep_regs[ep->num]->
  58978. + diepint);
  58979. + } while (!diepint.b.epdisabled);
  58980. + diepint.b.epdisabled = 1;
  58981. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58982. + diepint, diepint.d32);
  58983. + } else {
  58984. + dctl_data_t dctl = {.d32 = 0};
  58985. + gintmsk_data_t gintsts = {.d32 = 0};
  58986. + doepint_data_t doepint = {.d32 = 0};
  58987. + dctl.b.sgoutnak = 1;
  58988. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  58989. + dctl, 0, dctl.d32);
  58990. + do {
  58991. + dwc_udelay(10);
  58992. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  58993. + } while (!gintsts.b.goutnakeff);
  58994. + gintsts.d32 = 0;
  58995. + gintsts.b.goutnakeff = 1;
  58996. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  58997. +
  58998. + depctl.d32 = 0;
  58999. + depctl.b.epdis = 1;
  59000. + depctl.b.snak = 1;
  59001. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  59002. + do
  59003. + {
  59004. + dwc_udelay(10);
  59005. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  59006. + out_ep_regs[ep->num]->doepint);
  59007. + } while (!doepint.b.epdisabled);
  59008. +
  59009. + doepint.b.epdisabled = 1;
  59010. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  59011. +
  59012. + dctl.d32 = 0;
  59013. + dctl.b.cgoutnak = 1;
  59014. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  59015. + }
  59016. + }
  59017. +
  59018. + /* Disable the Interrupt for this EP */
  59019. + if (core_if->multiproc_int_enable) {
  59020. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  59021. + daintmsk.d32, 0);
  59022. +
  59023. + if (ep->is_in == 1) {
  59024. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  59025. + diepeachintmsk[ep->num], 0);
  59026. + } else {
  59027. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  59028. + doepeachintmsk[ep->num], 0);
  59029. + }
  59030. + } else {
  59031. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  59032. + daintmsk.d32, 0);
  59033. + }
  59034. +
  59035. +}
  59036. +
  59037. +/**
  59038. + * This function initializes dma descriptor chain.
  59039. + *
  59040. + * @param core_if Programming view of DWC_otg controller.
  59041. + * @param ep The EP to start the transfer on.
  59042. + */
  59043. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59044. +{
  59045. + dwc_otg_dev_dma_desc_t *dma_desc;
  59046. + uint32_t offset;
  59047. + uint32_t xfer_est;
  59048. + int i;
  59049. + unsigned maxxfer_local, total_len;
  59050. +
  59051. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  59052. + (ep->maxpacket%4)) {
  59053. + maxxfer_local = ep->maxpacket;
  59054. + total_len = ep->xfer_len;
  59055. + } else {
  59056. + maxxfer_local = ep->maxxfer;
  59057. + total_len = ep->total_len;
  59058. + }
  59059. +
  59060. + ep->desc_cnt = (total_len / maxxfer_local) +
  59061. + ((total_len % maxxfer_local) ? 1 : 0);
  59062. +
  59063. + if (!ep->desc_cnt)
  59064. + ep->desc_cnt = 1;
  59065. +
  59066. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  59067. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  59068. +
  59069. + dma_desc = ep->desc_addr;
  59070. + if (maxxfer_local == ep->maxpacket) {
  59071. + if ((total_len % maxxfer_local) &&
  59072. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  59073. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  59074. + (total_len % maxxfer_local);
  59075. + } else
  59076. + xfer_est = ep->desc_cnt * maxxfer_local;
  59077. + } else
  59078. + xfer_est = total_len;
  59079. + offset = 0;
  59080. + for (i = 0; i < ep->desc_cnt; ++i) {
  59081. + /** DMA Descriptor Setup */
  59082. + if (xfer_est > maxxfer_local) {
  59083. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59084. + dma_desc->status.b.l = 0;
  59085. + dma_desc->status.b.ioc = 0;
  59086. + dma_desc->status.b.sp = 0;
  59087. + dma_desc->status.b.bytes = maxxfer_local;
  59088. + dma_desc->buf = ep->dma_addr + offset;
  59089. + dma_desc->status.b.sts = 0;
  59090. + dma_desc->status.b.bs = BS_HOST_READY;
  59091. +
  59092. + xfer_est -= maxxfer_local;
  59093. + offset += maxxfer_local;
  59094. + } else {
  59095. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59096. + dma_desc->status.b.l = 1;
  59097. + dma_desc->status.b.ioc = 1;
  59098. + if (ep->is_in) {
  59099. + dma_desc->status.b.sp =
  59100. + (xfer_est %
  59101. + ep->maxpacket) ? 1 : ((ep->
  59102. + sent_zlp) ? 1 : 0);
  59103. + dma_desc->status.b.bytes = xfer_est;
  59104. + } else {
  59105. + if (maxxfer_local == ep->maxpacket)
  59106. + dma_desc->status.b.bytes = xfer_est;
  59107. + else
  59108. + dma_desc->status.b.bytes =
  59109. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  59110. + }
  59111. +
  59112. + dma_desc->buf = ep->dma_addr + offset;
  59113. + dma_desc->status.b.sts = 0;
  59114. + dma_desc->status.b.bs = BS_HOST_READY;
  59115. + }
  59116. + dma_desc++;
  59117. + }
  59118. +}
  59119. +/**
  59120. + * This function is called when to write ISOC data into appropriate dedicated
  59121. + * periodic FIFO.
  59122. + */
  59123. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  59124. +{
  59125. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  59126. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  59127. + dtxfsts_data_t txstatus = {.d32 = 0 };
  59128. + uint32_t len = 0;
  59129. + int epnum = dwc_ep->num;
  59130. + int dwords;
  59131. +
  59132. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  59133. +
  59134. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  59135. +
  59136. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  59137. +
  59138. + if (len > dwc_ep->maxpacket) {
  59139. + len = dwc_ep->maxpacket;
  59140. + }
  59141. +
  59142. + dwords = (len + 3) / 4;
  59143. +
  59144. + /* While there is space in the queue and space in the FIFO and
  59145. + * More data to tranfer, Write packets to the Tx FIFO */
  59146. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  59147. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  59148. +
  59149. + while (txstatus.b.txfspcavail > dwords &&
  59150. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  59151. + /* Write the FIFO */
  59152. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  59153. +
  59154. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  59155. + if (len > dwc_ep->maxpacket) {
  59156. + len = dwc_ep->maxpacket;
  59157. + }
  59158. +
  59159. + dwords = (len + 3) / 4;
  59160. + txstatus.d32 =
  59161. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  59162. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  59163. + txstatus.d32);
  59164. + }
  59165. +
  59166. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  59167. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  59168. +
  59169. + return 1;
  59170. +}
  59171. +/**
  59172. + * This function does the setup for a data transfer for an EP and
  59173. + * starts the transfer. For an IN transfer, the packets will be
  59174. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  59175. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  59176. + *
  59177. + * @param core_if Programming view of DWC_otg controller.
  59178. + * @param ep The EP to start the transfer on.
  59179. + */
  59180. +
  59181. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59182. +{
  59183. + depctl_data_t depctl;
  59184. + deptsiz_data_t deptsiz;
  59185. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59186. +
  59187. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  59188. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  59189. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  59190. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  59191. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  59192. + ep->total_len);
  59193. + /* IN endpoint */
  59194. + if (ep->is_in == 1) {
  59195. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59196. + core_if->dev_if->in_ep_regs[ep->num];
  59197. +
  59198. + gnptxsts_data_t gtxstatus;
  59199. +
  59200. + gtxstatus.d32 =
  59201. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  59202. +
  59203. + if (core_if->en_multiple_tx_fifo == 0
  59204. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  59205. +#ifdef DEBUG
  59206. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  59207. +#endif
  59208. + return;
  59209. + }
  59210. +
  59211. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  59212. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  59213. +
  59214. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  59215. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  59216. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  59217. + else
  59218. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  59219. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  59220. +
  59221. +
  59222. + /* Zero Length Packet? */
  59223. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  59224. + deptsiz.b.xfersize = 0;
  59225. + deptsiz.b.pktcnt = 1;
  59226. + } else {
  59227. + /* Program the transfer size and packet count
  59228. + * as follows: xfersize = N * maxpacket +
  59229. + * short_packet pktcnt = N + (short_packet
  59230. + * exist ? 1 : 0)
  59231. + */
  59232. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  59233. + deptsiz.b.pktcnt =
  59234. + (ep->xfer_len - ep->xfer_count - 1 +
  59235. + ep->maxpacket) / ep->maxpacket;
  59236. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  59237. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  59238. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  59239. + }
  59240. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  59241. + deptsiz.b.mc = deptsiz.b.pktcnt;
  59242. + }
  59243. +
  59244. + /* Write the DMA register */
  59245. + if (core_if->dma_enable) {
  59246. + if (core_if->dma_desc_enable == 0) {
  59247. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  59248. + deptsiz.b.mc = 1;
  59249. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  59250. + deptsiz.d32);
  59251. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59252. + (uint32_t) ep->dma_addr);
  59253. + } else {
  59254. +#ifdef DWC_UTE_CFI
  59255. + /* The descriptor chain should be already initialized by now */
  59256. + if (ep->buff_mode != BM_STANDARD) {
  59257. + DWC_WRITE_REG32(&in_regs->diepdma,
  59258. + ep->descs_dma_addr);
  59259. + } else {
  59260. +#endif
  59261. + init_dma_desc_chain(core_if, ep);
  59262. + /** DIEPDMAn Register write */
  59263. + DWC_WRITE_REG32(&in_regs->diepdma,
  59264. + ep->dma_desc_addr);
  59265. +#ifdef DWC_UTE_CFI
  59266. + }
  59267. +#endif
  59268. + }
  59269. + } else {
  59270. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59271. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  59272. + /**
  59273. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  59274. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  59275. + * the data will be written into the fifo by the ISR.
  59276. + */
  59277. + if (core_if->en_multiple_tx_fifo == 0) {
  59278. + intr_mask.b.nptxfempty = 1;
  59279. + DWC_MODIFY_REG32
  59280. + (&core_if->core_global_regs->gintmsk,
  59281. + intr_mask.d32, intr_mask.d32);
  59282. + } else {
  59283. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59284. + if (ep->xfer_len > 0) {
  59285. + uint32_t fifoemptymsk = 0;
  59286. + fifoemptymsk = 1 << ep->num;
  59287. + DWC_MODIFY_REG32
  59288. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59289. + 0, fifoemptymsk);
  59290. +
  59291. + }
  59292. + }
  59293. + } else {
  59294. + write_isoc_tx_fifo(core_if, ep);
  59295. + }
  59296. + }
  59297. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59298. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59299. +
  59300. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  59301. + dsts_data_t dsts = {.d32 = 0};
  59302. + if (ep->bInterval == 1) {
  59303. + dsts.d32 =
  59304. + DWC_READ_REG32(&core_if->dev_if->
  59305. + dev_global_regs->dsts);
  59306. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  59307. + if (ep->frame_num > 0x3FFF) {
  59308. + ep->frm_overrun = 1;
  59309. + ep->frame_num &= 0x3FFF;
  59310. + } else
  59311. + ep->frm_overrun = 0;
  59312. + if (ep->frame_num & 0x1) {
  59313. + depctl.b.setd1pid = 1;
  59314. + } else {
  59315. + depctl.b.setd0pid = 1;
  59316. + }
  59317. + }
  59318. + }
  59319. + /* EP enable, IN data in FIFO */
  59320. + depctl.b.cnak = 1;
  59321. + depctl.b.epena = 1;
  59322. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59323. +
  59324. + } else {
  59325. + /* OUT endpoint */
  59326. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59327. + core_if->dev_if->out_ep_regs[ep->num];
  59328. +
  59329. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  59330. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  59331. +
  59332. + if (!core_if->dma_desc_enable) {
  59333. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  59334. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  59335. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  59336. + else
  59337. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  59338. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  59339. + }
  59340. +
  59341. + /* Program the transfer size and packet count as follows:
  59342. + *
  59343. + * pktcnt = N
  59344. + * xfersize = N * maxpacket
  59345. + */
  59346. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  59347. + /* Zero Length Packet */
  59348. + deptsiz.b.xfersize = ep->maxpacket;
  59349. + deptsiz.b.pktcnt = 1;
  59350. + } else {
  59351. + deptsiz.b.pktcnt =
  59352. + (ep->xfer_len - ep->xfer_count +
  59353. + (ep->maxpacket - 1)) / ep->maxpacket;
  59354. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  59355. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  59356. + }
  59357. + if (!core_if->dma_desc_enable) {
  59358. + ep->xfer_len =
  59359. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  59360. + }
  59361. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  59362. + }
  59363. +
  59364. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  59365. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  59366. +
  59367. + if (core_if->dma_enable) {
  59368. + if (!core_if->dma_desc_enable) {
  59369. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59370. + deptsiz.d32);
  59371. +
  59372. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59373. + (uint32_t) ep->dma_addr);
  59374. + } else {
  59375. +#ifdef DWC_UTE_CFI
  59376. + /* The descriptor chain should be already initialized by now */
  59377. + if (ep->buff_mode != BM_STANDARD) {
  59378. + DWC_WRITE_REG32(&out_regs->doepdma,
  59379. + ep->descs_dma_addr);
  59380. + } else {
  59381. +#endif
  59382. + /** This is used for interrupt out transfers*/
  59383. + if (!ep->xfer_len)
  59384. + ep->xfer_len = ep->total_len;
  59385. + init_dma_desc_chain(core_if, ep);
  59386. +
  59387. + if (core_if->core_params->dev_out_nak) {
  59388. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  59389. + deptsiz.b.pktcnt = (ep->total_len +
  59390. + (ep->maxpacket - 1)) / ep->maxpacket;
  59391. + deptsiz.b.xfersize = ep->total_len;
  59392. + /* Remember initial value of doeptsiz */
  59393. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  59394. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59395. + deptsiz.d32);
  59396. + }
  59397. + }
  59398. + /** DOEPDMAn Register write */
  59399. + DWC_WRITE_REG32(&out_regs->doepdma,
  59400. + ep->dma_desc_addr);
  59401. +#ifdef DWC_UTE_CFI
  59402. + }
  59403. +#endif
  59404. + }
  59405. + } else {
  59406. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59407. + }
  59408. +
  59409. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  59410. + dsts_data_t dsts = {.d32 = 0};
  59411. + if (ep->bInterval == 1) {
  59412. + dsts.d32 =
  59413. + DWC_READ_REG32(&core_if->dev_if->
  59414. + dev_global_regs->dsts);
  59415. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  59416. + if (ep->frame_num > 0x3FFF) {
  59417. + ep->frm_overrun = 1;
  59418. + ep->frame_num &= 0x3FFF;
  59419. + } else
  59420. + ep->frm_overrun = 0;
  59421. +
  59422. + if (ep->frame_num & 0x1) {
  59423. + depctl.b.setd1pid = 1;
  59424. + } else {
  59425. + depctl.b.setd0pid = 1;
  59426. + }
  59427. + }
  59428. + }
  59429. +
  59430. + /* EP enable */
  59431. + depctl.b.cnak = 1;
  59432. + depctl.b.epena = 1;
  59433. +
  59434. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  59435. +
  59436. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  59437. + DWC_READ_REG32(&out_regs->doepctl),
  59438. + DWC_READ_REG32(&out_regs->doeptsiz));
  59439. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  59440. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  59441. + daintmsk),
  59442. + DWC_READ_REG32(&core_if->core_global_regs->
  59443. + gintmsk));
  59444. +
  59445. + /* Timer is scheduling only for out bulk transfers for
  59446. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  59447. + * about received data payload in case of timeout
  59448. + */
  59449. + if (core_if->core_params->dev_out_nak) {
  59450. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  59451. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  59452. + core_if->ep_xfer_info[ep->num].ep = ep;
  59453. + core_if->ep_xfer_info[ep->num].state = 1;
  59454. +
  59455. + /* Start a timer for this transfer. */
  59456. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  59457. + }
  59458. + }
  59459. + }
  59460. +}
  59461. +
  59462. +/**
  59463. + * This function setup a zero length transfer in Buffer DMA and
  59464. + * Slave modes for usb requests with zero field set
  59465. + *
  59466. + * @param core_if Programming view of DWC_otg controller.
  59467. + * @param ep The EP to start the transfer on.
  59468. + *
  59469. + */
  59470. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59471. +{
  59472. +
  59473. + depctl_data_t depctl;
  59474. + deptsiz_data_t deptsiz;
  59475. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59476. +
  59477. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  59478. + DWC_PRINTF("zero length transfer is called\n");
  59479. +
  59480. + /* IN endpoint */
  59481. + if (ep->is_in == 1) {
  59482. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59483. + core_if->dev_if->in_ep_regs[ep->num];
  59484. +
  59485. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  59486. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  59487. +
  59488. + deptsiz.b.xfersize = 0;
  59489. + deptsiz.b.pktcnt = 1;
  59490. +
  59491. + /* Write the DMA register */
  59492. + if (core_if->dma_enable) {
  59493. + if (core_if->dma_desc_enable == 0) {
  59494. + deptsiz.b.mc = 1;
  59495. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  59496. + deptsiz.d32);
  59497. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59498. + (uint32_t) ep->dma_addr);
  59499. + }
  59500. + } else {
  59501. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59502. + /**
  59503. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  59504. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  59505. + * the data will be written into the fifo by the ISR.
  59506. + */
  59507. + if (core_if->en_multiple_tx_fifo == 0) {
  59508. + intr_mask.b.nptxfempty = 1;
  59509. + DWC_MODIFY_REG32(&core_if->
  59510. + core_global_regs->gintmsk,
  59511. + intr_mask.d32, intr_mask.d32);
  59512. + } else {
  59513. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59514. + if (ep->xfer_len > 0) {
  59515. + uint32_t fifoemptymsk = 0;
  59516. + fifoemptymsk = 1 << ep->num;
  59517. + DWC_MODIFY_REG32(&core_if->
  59518. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59519. + 0, fifoemptymsk);
  59520. + }
  59521. + }
  59522. + }
  59523. +
  59524. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59525. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59526. + /* EP enable, IN data in FIFO */
  59527. + depctl.b.cnak = 1;
  59528. + depctl.b.epena = 1;
  59529. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59530. +
  59531. + } else {
  59532. + /* OUT endpoint */
  59533. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59534. + core_if->dev_if->out_ep_regs[ep->num];
  59535. +
  59536. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  59537. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  59538. +
  59539. + /* Zero Length Packet */
  59540. + deptsiz.b.xfersize = ep->maxpacket;
  59541. + deptsiz.b.pktcnt = 1;
  59542. +
  59543. + if (core_if->dma_enable) {
  59544. + if (!core_if->dma_desc_enable) {
  59545. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59546. + deptsiz.d32);
  59547. +
  59548. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59549. + (uint32_t) ep->dma_addr);
  59550. + }
  59551. + } else {
  59552. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59553. + }
  59554. +
  59555. + /* EP enable */
  59556. + depctl.b.cnak = 1;
  59557. + depctl.b.epena = 1;
  59558. +
  59559. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  59560. +
  59561. + }
  59562. +}
  59563. +
  59564. +/**
  59565. + * This function does the setup for a data transfer for EP0 and starts
  59566. + * the transfer. For an IN transfer, the packets will be loaded into
  59567. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  59568. + * unloaded from the Rx FIFO in the ISR.
  59569. + *
  59570. + * @param core_if Programming view of DWC_otg controller.
  59571. + * @param ep The EP0 data.
  59572. + */
  59573. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59574. +{
  59575. + depctl_data_t depctl;
  59576. + deptsiz0_data_t deptsiz;
  59577. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59578. + dwc_otg_dev_dma_desc_t *dma_desc;
  59579. +
  59580. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  59581. + "xfer_buff=%p start_xfer_buff=%p \n",
  59582. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  59583. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  59584. +
  59585. + ep->total_len = ep->xfer_len;
  59586. +
  59587. + /* IN endpoint */
  59588. + if (ep->is_in == 1) {
  59589. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59590. + core_if->dev_if->in_ep_regs[0];
  59591. +
  59592. + gnptxsts_data_t gtxstatus;
  59593. +
  59594. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  59595. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  59596. + if (depctl.b.epena)
  59597. + return;
  59598. + }
  59599. +
  59600. + gtxstatus.d32 =
  59601. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  59602. +
  59603. + /* If dedicated FIFO every time flush fifo before enable ep*/
  59604. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  59605. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  59606. +
  59607. + if (core_if->en_multiple_tx_fifo == 0
  59608. + && gtxstatus.b.nptxqspcavail == 0
  59609. + && !core_if->dma_enable) {
  59610. +#ifdef DEBUG
  59611. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  59612. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  59613. + DWC_READ_REG32(&in_regs->diepctl));
  59614. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  59615. + deptsiz.d32,
  59616. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  59617. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  59618. + gtxstatus.d32);
  59619. +#endif
  59620. + return;
  59621. + }
  59622. +
  59623. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  59624. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  59625. +
  59626. + /* Zero Length Packet? */
  59627. + if (ep->xfer_len == 0) {
  59628. + deptsiz.b.xfersize = 0;
  59629. + deptsiz.b.pktcnt = 1;
  59630. + } else {
  59631. + /* Program the transfer size and packet count
  59632. + * as follows: xfersize = N * maxpacket +
  59633. + * short_packet pktcnt = N + (short_packet
  59634. + * exist ? 1 : 0)
  59635. + */
  59636. + if (ep->xfer_len > ep->maxpacket) {
  59637. + ep->xfer_len = ep->maxpacket;
  59638. + deptsiz.b.xfersize = ep->maxpacket;
  59639. + } else {
  59640. + deptsiz.b.xfersize = ep->xfer_len;
  59641. + }
  59642. + deptsiz.b.pktcnt = 1;
  59643. +
  59644. + }
  59645. + DWC_DEBUGPL(DBG_PCDV,
  59646. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59647. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59648. + deptsiz.d32);
  59649. +
  59650. + /* Write the DMA register */
  59651. + if (core_if->dma_enable) {
  59652. + if (core_if->dma_desc_enable == 0) {
  59653. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  59654. + deptsiz.d32);
  59655. +
  59656. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59657. + (uint32_t) ep->dma_addr);
  59658. + } else {
  59659. + dma_desc = core_if->dev_if->in_desc_addr;
  59660. +
  59661. + /** DMA Descriptor Setup */
  59662. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59663. + dma_desc->status.b.l = 1;
  59664. + dma_desc->status.b.ioc = 1;
  59665. + dma_desc->status.b.sp =
  59666. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  59667. + dma_desc->status.b.bytes = ep->xfer_len;
  59668. + dma_desc->buf = ep->dma_addr;
  59669. + dma_desc->status.b.sts = 0;
  59670. + dma_desc->status.b.bs = BS_HOST_READY;
  59671. +
  59672. + /** DIEPDMA0 Register write */
  59673. + DWC_WRITE_REG32(&in_regs->diepdma,
  59674. + core_if->
  59675. + dev_if->dma_in_desc_addr);
  59676. + }
  59677. + } else {
  59678. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59679. + }
  59680. +
  59681. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59682. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59683. + /* EP enable, IN data in FIFO */
  59684. + depctl.b.cnak = 1;
  59685. + depctl.b.epena = 1;
  59686. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59687. +
  59688. + /**
  59689. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  59690. + * data will be written into the fifo by the ISR.
  59691. + */
  59692. + if (!core_if->dma_enable) {
  59693. + if (core_if->en_multiple_tx_fifo == 0) {
  59694. + intr_mask.b.nptxfempty = 1;
  59695. + DWC_MODIFY_REG32(&core_if->
  59696. + core_global_regs->gintmsk,
  59697. + intr_mask.d32, intr_mask.d32);
  59698. + } else {
  59699. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59700. + if (ep->xfer_len > 0) {
  59701. + uint32_t fifoemptymsk = 0;
  59702. + fifoemptymsk |= 1 << ep->num;
  59703. + DWC_MODIFY_REG32(&core_if->
  59704. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59705. + 0, fifoemptymsk);
  59706. + }
  59707. + }
  59708. + }
  59709. + } else {
  59710. + /* OUT endpoint */
  59711. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59712. + core_if->dev_if->out_ep_regs[0];
  59713. +
  59714. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  59715. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  59716. +
  59717. + /* Program the transfer size and packet count as follows:
  59718. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  59719. + * pktcnt = N */
  59720. + /* Zero Length Packet */
  59721. + deptsiz.b.xfersize = ep->maxpacket;
  59722. + deptsiz.b.pktcnt = 1;
  59723. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  59724. + deptsiz.b.supcnt = 3;
  59725. +
  59726. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  59727. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  59728. +
  59729. + if (core_if->dma_enable) {
  59730. + if (!core_if->dma_desc_enable) {
  59731. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59732. + deptsiz.d32);
  59733. +
  59734. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59735. + (uint32_t) ep->dma_addr);
  59736. + } else {
  59737. + dma_desc = core_if->dev_if->out_desc_addr;
  59738. +
  59739. + /** DMA Descriptor Setup */
  59740. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59741. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  59742. + dma_desc->status.b.mtrf = 0;
  59743. + dma_desc->status.b.sr = 0;
  59744. + }
  59745. + dma_desc->status.b.l = 1;
  59746. + dma_desc->status.b.ioc = 1;
  59747. + dma_desc->status.b.bytes = ep->maxpacket;
  59748. + dma_desc->buf = ep->dma_addr;
  59749. + dma_desc->status.b.sts = 0;
  59750. + dma_desc->status.b.bs = BS_HOST_READY;
  59751. +
  59752. + /** DOEPDMA0 Register write */
  59753. + DWC_WRITE_REG32(&out_regs->doepdma,
  59754. + core_if->dev_if->
  59755. + dma_out_desc_addr);
  59756. + }
  59757. + } else {
  59758. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59759. + }
  59760. +
  59761. + /* EP enable */
  59762. + depctl.b.cnak = 1;
  59763. + depctl.b.epena = 1;
  59764. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  59765. + }
  59766. +}
  59767. +
  59768. +/**
  59769. + * This function continues control IN transfers started by
  59770. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  59771. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  59772. + * bit for the packet count.
  59773. + *
  59774. + * @param core_if Programming view of DWC_otg controller.
  59775. + * @param ep The EP0 data.
  59776. + */
  59777. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59778. +{
  59779. + depctl_data_t depctl;
  59780. + deptsiz0_data_t deptsiz;
  59781. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59782. + dwc_otg_dev_dma_desc_t *dma_desc;
  59783. +
  59784. + if (ep->is_in == 1) {
  59785. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59786. + core_if->dev_if->in_ep_regs[0];
  59787. + gnptxsts_data_t tx_status = {.d32 = 0 };
  59788. +
  59789. + tx_status.d32 =
  59790. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  59791. + /** @todo Should there be check for room in the Tx
  59792. + * Status Queue. If not remove the code above this comment. */
  59793. +
  59794. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  59795. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  59796. +
  59797. + /* Program the transfer size and packet count
  59798. + * as follows: xfersize = N * maxpacket +
  59799. + * short_packet pktcnt = N + (short_packet
  59800. + * exist ? 1 : 0)
  59801. + */
  59802. +
  59803. + if (core_if->dma_desc_enable == 0) {
  59804. + deptsiz.b.xfersize =
  59805. + (ep->total_len - ep->xfer_count) >
  59806. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  59807. + ep->xfer_count);
  59808. + deptsiz.b.pktcnt = 1;
  59809. + if (core_if->dma_enable == 0) {
  59810. + ep->xfer_len += deptsiz.b.xfersize;
  59811. + } else {
  59812. + ep->xfer_len = deptsiz.b.xfersize;
  59813. + }
  59814. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59815. + } else {
  59816. + ep->xfer_len =
  59817. + (ep->total_len - ep->xfer_count) >
  59818. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  59819. + ep->xfer_count);
  59820. +
  59821. + dma_desc = core_if->dev_if->in_desc_addr;
  59822. +
  59823. + /** DMA Descriptor Setup */
  59824. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59825. + dma_desc->status.b.l = 1;
  59826. + dma_desc->status.b.ioc = 1;
  59827. + dma_desc->status.b.sp =
  59828. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  59829. + dma_desc->status.b.bytes = ep->xfer_len;
  59830. + dma_desc->buf = ep->dma_addr;
  59831. + dma_desc->status.b.sts = 0;
  59832. + dma_desc->status.b.bs = BS_HOST_READY;
  59833. +
  59834. + /** DIEPDMA0 Register write */
  59835. + DWC_WRITE_REG32(&in_regs->diepdma,
  59836. + core_if->dev_if->dma_in_desc_addr);
  59837. + }
  59838. +
  59839. + DWC_DEBUGPL(DBG_PCDV,
  59840. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59841. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59842. + deptsiz.d32);
  59843. +
  59844. + /* Write the DMA register */
  59845. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  59846. + if (core_if->dma_desc_enable == 0)
  59847. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59848. + (uint32_t) ep->dma_addr);
  59849. + }
  59850. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59851. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59852. + /* EP enable, IN data in FIFO */
  59853. + depctl.b.cnak = 1;
  59854. + depctl.b.epena = 1;
  59855. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59856. +
  59857. + /**
  59858. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  59859. + * data will be written into the fifo by the ISR.
  59860. + */
  59861. + if (!core_if->dma_enable) {
  59862. + if (core_if->en_multiple_tx_fifo == 0) {
  59863. + /* First clear it from GINTSTS */
  59864. + intr_mask.b.nptxfempty = 1;
  59865. + DWC_MODIFY_REG32(&core_if->
  59866. + core_global_regs->gintmsk,
  59867. + intr_mask.d32, intr_mask.d32);
  59868. +
  59869. + } else {
  59870. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59871. + if (ep->xfer_len > 0) {
  59872. + uint32_t fifoemptymsk = 0;
  59873. + fifoemptymsk |= 1 << ep->num;
  59874. + DWC_MODIFY_REG32(&core_if->
  59875. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59876. + 0, fifoemptymsk);
  59877. + }
  59878. + }
  59879. + }
  59880. + } else {
  59881. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59882. + core_if->dev_if->out_ep_regs[0];
  59883. +
  59884. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  59885. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  59886. +
  59887. + /* Program the transfer size and packet count
  59888. + * as follows: xfersize = N * maxpacket +
  59889. + * short_packet pktcnt = N + (short_packet
  59890. + * exist ? 1 : 0)
  59891. + */
  59892. + deptsiz.b.xfersize = ep->maxpacket;
  59893. + deptsiz.b.pktcnt = 1;
  59894. +
  59895. + if (core_if->dma_desc_enable == 0) {
  59896. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59897. + } else {
  59898. + dma_desc = core_if->dev_if->out_desc_addr;
  59899. +
  59900. + /** DMA Descriptor Setup */
  59901. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59902. + dma_desc->status.b.l = 1;
  59903. + dma_desc->status.b.ioc = 1;
  59904. + dma_desc->status.b.bytes = ep->maxpacket;
  59905. + dma_desc->buf = ep->dma_addr;
  59906. + dma_desc->status.b.sts = 0;
  59907. + dma_desc->status.b.bs = BS_HOST_READY;
  59908. +
  59909. + /** DOEPDMA0 Register write */
  59910. + DWC_WRITE_REG32(&out_regs->doepdma,
  59911. + core_if->dev_if->dma_out_desc_addr);
  59912. + }
  59913. +
  59914. + DWC_DEBUGPL(DBG_PCDV,
  59915. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59916. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59917. + deptsiz.d32);
  59918. +
  59919. + /* Write the DMA register */
  59920. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  59921. + if (core_if->dma_desc_enable == 0)
  59922. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59923. + (uint32_t) ep->dma_addr);
  59924. +
  59925. + }
  59926. +
  59927. + /* EP enable, IN data in FIFO */
  59928. + depctl.b.cnak = 1;
  59929. + depctl.b.epena = 1;
  59930. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  59931. +
  59932. + }
  59933. +}
  59934. +
  59935. +#ifdef DEBUG
  59936. +void dump_msg(const u8 * buf, unsigned int length)
  59937. +{
  59938. + unsigned int start, num, i;
  59939. + char line[52], *p;
  59940. +
  59941. + if (length >= 512)
  59942. + return;
  59943. + start = 0;
  59944. + while (length > 0) {
  59945. + num = length < 16u ? length : 16u;
  59946. + p = line;
  59947. + for (i = 0; i < num; ++i) {
  59948. + if (i == 8)
  59949. + *p++ = ' ';
  59950. + DWC_SPRINTF(p, " %02x", buf[i]);
  59951. + p += 3;
  59952. + }
  59953. + *p = 0;
  59954. + DWC_PRINTF("%6x: %s\n", start, line);
  59955. + buf += num;
  59956. + start += num;
  59957. + length -= num;
  59958. + }
  59959. +}
  59960. +#else
  59961. +static inline void dump_msg(const u8 * buf, unsigned int length)
  59962. +{
  59963. +}
  59964. +#endif
  59965. +
  59966. +/**
  59967. + * This function writes a packet into the Tx FIFO associated with the
  59968. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  59969. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  59970. + * with all packets for the next micro-frame.
  59971. + *
  59972. + * @param core_if Programming view of DWC_otg controller.
  59973. + * @param ep The EP to write packet for.
  59974. + * @param dma Indicates if DMA is being used.
  59975. + */
  59976. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  59977. + int dma)
  59978. +{
  59979. + /**
  59980. + * The buffer is padded to DWORD on a per packet basis in
  59981. + * slave/dma mode if the MPS is not DWORD aligned. The last
  59982. + * packet, if short, is also padded to a multiple of DWORD.
  59983. + *
  59984. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  59985. + * multiple of DWORD in length
  59986. + *
  59987. + * ep->xfer_len can be any number of bytes
  59988. + *
  59989. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  59990. + * packet
  59991. + *
  59992. + * FIFO access is DWORD */
  59993. +
  59994. + uint32_t i;
  59995. + uint32_t byte_count;
  59996. + uint32_t dword_count;
  59997. + uint32_t *fifo;
  59998. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  59999. +
  60000. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  60001. + ep);
  60002. + if (ep->xfer_count >= ep->xfer_len) {
  60003. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  60004. + return;
  60005. + }
  60006. +
  60007. + /* Find the byte length of the packet either short packet or MPS */
  60008. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  60009. + byte_count = ep->xfer_len - ep->xfer_count;
  60010. + } else {
  60011. + byte_count = ep->maxpacket;
  60012. + }
  60013. +
  60014. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  60015. + * is not a multiple of DWORD */
  60016. + dword_count = (byte_count + 3) / 4;
  60017. +
  60018. +#ifdef VERBOSE
  60019. + dump_msg(ep->xfer_buff, byte_count);
  60020. +#endif
  60021. +
  60022. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  60023. + * intialized? What should this be? */
  60024. +
  60025. + fifo = core_if->data_fifo[ep->num];
  60026. +
  60027. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  60028. + fifo, data_buff, *data_buff, byte_count);
  60029. +
  60030. + if (!dma) {
  60031. + for (i = 0; i < dword_count; i++, data_buff++) {
  60032. + DWC_WRITE_REG32(fifo, *data_buff);
  60033. + }
  60034. + }
  60035. +
  60036. + ep->xfer_count += byte_count;
  60037. + ep->xfer_buff += byte_count;
  60038. + ep->dma_addr += byte_count;
  60039. +}
  60040. +
  60041. +/**
  60042. + * Set the EP STALL.
  60043. + *
  60044. + * @param core_if Programming view of DWC_otg controller.
  60045. + * @param ep The EP to set the stall on.
  60046. + */
  60047. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  60048. +{
  60049. + depctl_data_t depctl;
  60050. + volatile uint32_t *depctl_addr;
  60051. +
  60052. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  60053. + (ep->is_in ? "IN" : "OUT"));
  60054. +
  60055. + if (ep->is_in == 1) {
  60056. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  60057. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  60058. +
  60059. + /* set the disable and stall bits */
  60060. + if (depctl.b.epena) {
  60061. + depctl.b.epdis = 1;
  60062. + }
  60063. + depctl.b.stall = 1;
  60064. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  60065. + } else {
  60066. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  60067. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  60068. +
  60069. + /* set the stall bit */
  60070. + depctl.b.stall = 1;
  60071. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  60072. + }
  60073. +
  60074. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  60075. +
  60076. + return;
  60077. +}
  60078. +
  60079. +/**
  60080. + * Clear the EP STALL.
  60081. + *
  60082. + * @param core_if Programming view of DWC_otg controller.
  60083. + * @param ep The EP to clear stall from.
  60084. + */
  60085. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  60086. +{
  60087. + depctl_data_t depctl;
  60088. + volatile uint32_t *depctl_addr;
  60089. +
  60090. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  60091. + (ep->is_in ? "IN" : "OUT"));
  60092. +
  60093. + if (ep->is_in == 1) {
  60094. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  60095. + } else {
  60096. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  60097. + }
  60098. +
  60099. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  60100. +
  60101. + /* clear the stall bits */
  60102. + depctl.b.stall = 0;
  60103. +
  60104. + /*
  60105. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  60106. + * of whether an endpoint has the Halt feature set, a
  60107. + * ClearFeature(ENDPOINT_HALT) request always results in the
  60108. + * data toggle being reinitialized to DATA0.
  60109. + */
  60110. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  60111. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  60112. + depctl.b.setd0pid = 1; /* DATA0 */
  60113. + }
  60114. +
  60115. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  60116. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  60117. + return;
  60118. +}
  60119. +
  60120. +/**
  60121. + * This function reads a packet from the Rx FIFO into the destination
  60122. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  60123. + *
  60124. + * @param core_if Programming view of DWC_otg controller.
  60125. + * @param dest Destination buffer for the packet.
  60126. + * @param bytes Number of bytes to copy to the destination.
  60127. + */
  60128. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  60129. + uint8_t * dest, uint16_t bytes)
  60130. +{
  60131. + int i;
  60132. + int word_count = (bytes + 3) / 4;
  60133. +
  60134. + volatile uint32_t *fifo = core_if->data_fifo[0];
  60135. + uint32_t *data_buff = (uint32_t *) dest;
  60136. +
  60137. + /**
  60138. + * @todo Account for the case where _dest is not dword aligned. This
  60139. + * requires reading data from the FIFO into a uint32_t temp buffer,
  60140. + * then moving it into the data buffer.
  60141. + */
  60142. +
  60143. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  60144. + core_if, dest, bytes);
  60145. +
  60146. + for (i = 0; i < word_count; i++, data_buff++) {
  60147. + *data_buff = DWC_READ_REG32(fifo);
  60148. + }
  60149. +
  60150. + return;
  60151. +}
  60152. +
  60153. +/**
  60154. + * This functions reads the device registers and prints them
  60155. + *
  60156. + * @param core_if Programming view of DWC_otg controller.
  60157. + */
  60158. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  60159. +{
  60160. + int i;
  60161. + volatile uint32_t *addr;
  60162. +
  60163. + DWC_PRINTF("Device Global Registers\n");
  60164. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  60165. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  60166. + (unsigned long)addr, DWC_READ_REG32(addr));
  60167. + addr = &core_if->dev_if->dev_global_regs->dctl;
  60168. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  60169. + (unsigned long)addr, DWC_READ_REG32(addr));
  60170. + addr = &core_if->dev_if->dev_global_regs->dsts;
  60171. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  60172. + (unsigned long)addr, DWC_READ_REG32(addr));
  60173. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  60174. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60175. + DWC_READ_REG32(addr));
  60176. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  60177. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60178. + DWC_READ_REG32(addr));
  60179. + addr = &core_if->dev_if->dev_global_regs->daint;
  60180. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60181. + DWC_READ_REG32(addr));
  60182. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  60183. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60184. + DWC_READ_REG32(addr));
  60185. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  60186. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60187. + DWC_READ_REG32(addr));
  60188. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  60189. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  60190. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  60191. + (unsigned long)addr, DWC_READ_REG32(addr));
  60192. + }
  60193. +
  60194. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  60195. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60196. + DWC_READ_REG32(addr));
  60197. +
  60198. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  60199. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  60200. + (unsigned long)addr, DWC_READ_REG32(addr));
  60201. +
  60202. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  60203. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  60204. + (unsigned long)addr, DWC_READ_REG32(addr));
  60205. +
  60206. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  60207. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  60208. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  60209. + (unsigned long)addr, DWC_READ_REG32(addr));
  60210. + }
  60211. +
  60212. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  60213. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60214. + DWC_READ_REG32(addr));
  60215. +
  60216. + if (core_if->hwcfg2.b.multi_proc_int) {
  60217. +
  60218. + addr = &core_if->dev_if->dev_global_regs->deachint;
  60219. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  60220. + (unsigned long)addr, DWC_READ_REG32(addr));
  60221. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  60222. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  60223. + (unsigned long)addr, DWC_READ_REG32(addr));
  60224. +
  60225. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  60226. + addr =
  60227. + &core_if->dev_if->
  60228. + dev_global_regs->diepeachintmsk[i];
  60229. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  60230. + i, (unsigned long)addr,
  60231. + DWC_READ_REG32(addr));
  60232. + }
  60233. +
  60234. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  60235. + addr =
  60236. + &core_if->dev_if->
  60237. + dev_global_regs->doepeachintmsk[i];
  60238. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  60239. + i, (unsigned long)addr,
  60240. + DWC_READ_REG32(addr));
  60241. + }
  60242. + }
  60243. +
  60244. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  60245. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  60246. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  60247. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  60248. + (unsigned long)addr, DWC_READ_REG32(addr));
  60249. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  60250. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  60251. + (unsigned long)addr, DWC_READ_REG32(addr));
  60252. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  60253. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  60254. + (unsigned long)addr, DWC_READ_REG32(addr));
  60255. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  60256. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  60257. + (unsigned long)addr, DWC_READ_REG32(addr));
  60258. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  60259. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  60260. + (unsigned long)addr, DWC_READ_REG32(addr));
  60261. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  60262. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  60263. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  60264. + }
  60265. +
  60266. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  60267. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  60268. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  60269. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  60270. + (unsigned long)addr, DWC_READ_REG32(addr));
  60271. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  60272. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  60273. + (unsigned long)addr, DWC_READ_REG32(addr));
  60274. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  60275. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  60276. + (unsigned long)addr, DWC_READ_REG32(addr));
  60277. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  60278. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  60279. + (unsigned long)addr, DWC_READ_REG32(addr));
  60280. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  60281. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  60282. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  60283. + (unsigned long)addr, DWC_READ_REG32(addr));
  60284. + }
  60285. +
  60286. + }
  60287. +}
  60288. +
  60289. +/**
  60290. + * This functions reads the SPRAM and prints its content
  60291. + *
  60292. + * @param core_if Programming view of DWC_otg controller.
  60293. + */
  60294. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  60295. +{
  60296. + volatile uint8_t *addr, *start_addr, *end_addr;
  60297. +
  60298. + DWC_PRINTF("SPRAM Data:\n");
  60299. + start_addr = (void *)core_if->core_global_regs;
  60300. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  60301. + start_addr += 0x00028000;
  60302. + end_addr = (void *)core_if->core_global_regs;
  60303. + end_addr += 0x000280e0;
  60304. +
  60305. + for (addr = start_addr; addr < end_addr; addr += 16) {
  60306. + DWC_PRINTF
  60307. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  60308. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  60309. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  60310. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  60311. + );
  60312. + }
  60313. +
  60314. + return;
  60315. +}
  60316. +
  60317. +/**
  60318. + * This function reads the host registers and prints them
  60319. + *
  60320. + * @param core_if Programming view of DWC_otg controller.
  60321. + */
  60322. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  60323. +{
  60324. + int i;
  60325. + volatile uint32_t *addr;
  60326. +
  60327. + DWC_PRINTF("Host Global Registers\n");
  60328. + addr = &core_if->host_if->host_global_regs->hcfg;
  60329. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  60330. + (unsigned long)addr, DWC_READ_REG32(addr));
  60331. + addr = &core_if->host_if->host_global_regs->hfir;
  60332. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  60333. + (unsigned long)addr, DWC_READ_REG32(addr));
  60334. + addr = &core_if->host_if->host_global_regs->hfnum;
  60335. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60336. + DWC_READ_REG32(addr));
  60337. + addr = &core_if->host_if->host_global_regs->hptxsts;
  60338. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60339. + DWC_READ_REG32(addr));
  60340. + addr = &core_if->host_if->host_global_regs->haint;
  60341. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60342. + DWC_READ_REG32(addr));
  60343. + addr = &core_if->host_if->host_global_regs->haintmsk;
  60344. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60345. + DWC_READ_REG32(addr));
  60346. + if (core_if->dma_desc_enable) {
  60347. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  60348. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  60349. + (unsigned long)addr, DWC_READ_REG32(addr));
  60350. + }
  60351. +
  60352. + addr = core_if->host_if->hprt0;
  60353. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60354. + DWC_READ_REG32(addr));
  60355. +
  60356. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  60357. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  60358. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  60359. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  60360. + (unsigned long)addr, DWC_READ_REG32(addr));
  60361. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  60362. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  60363. + (unsigned long)addr, DWC_READ_REG32(addr));
  60364. + addr = &core_if->host_if->hc_regs[i]->hcint;
  60365. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  60366. + (unsigned long)addr, DWC_READ_REG32(addr));
  60367. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  60368. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  60369. + (unsigned long)addr, DWC_READ_REG32(addr));
  60370. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  60371. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  60372. + (unsigned long)addr, DWC_READ_REG32(addr));
  60373. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  60374. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  60375. + (unsigned long)addr, DWC_READ_REG32(addr));
  60376. + if (core_if->dma_desc_enable) {
  60377. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  60378. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  60379. + (unsigned long)addr, DWC_READ_REG32(addr));
  60380. + }
  60381. +
  60382. + }
  60383. + return;
  60384. +}
  60385. +
  60386. +/**
  60387. + * This function reads the core global registers and prints them
  60388. + *
  60389. + * @param core_if Programming view of DWC_otg controller.
  60390. + */
  60391. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  60392. +{
  60393. + int i, ep_num;
  60394. + volatile uint32_t *addr;
  60395. + char *txfsiz;
  60396. +
  60397. + DWC_PRINTF("Core Global Registers\n");
  60398. + addr = &core_if->core_global_regs->gotgctl;
  60399. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60400. + DWC_READ_REG32(addr));
  60401. + addr = &core_if->core_global_regs->gotgint;
  60402. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60403. + DWC_READ_REG32(addr));
  60404. + addr = &core_if->core_global_regs->gahbcfg;
  60405. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60406. + DWC_READ_REG32(addr));
  60407. + addr = &core_if->core_global_regs->gusbcfg;
  60408. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60409. + DWC_READ_REG32(addr));
  60410. + addr = &core_if->core_global_regs->grstctl;
  60411. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60412. + DWC_READ_REG32(addr));
  60413. + addr = &core_if->core_global_regs->gintsts;
  60414. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60415. + DWC_READ_REG32(addr));
  60416. + addr = &core_if->core_global_regs->gintmsk;
  60417. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60418. + DWC_READ_REG32(addr));
  60419. + addr = &core_if->core_global_regs->grxstsr;
  60420. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60421. + DWC_READ_REG32(addr));
  60422. + addr = &core_if->core_global_regs->grxfsiz;
  60423. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60424. + DWC_READ_REG32(addr));
  60425. + addr = &core_if->core_global_regs->gnptxfsiz;
  60426. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60427. + DWC_READ_REG32(addr));
  60428. + addr = &core_if->core_global_regs->gnptxsts;
  60429. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60430. + DWC_READ_REG32(addr));
  60431. + addr = &core_if->core_global_regs->gi2cctl;
  60432. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60433. + DWC_READ_REG32(addr));
  60434. + addr = &core_if->core_global_regs->gpvndctl;
  60435. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60436. + DWC_READ_REG32(addr));
  60437. + addr = &core_if->core_global_regs->ggpio;
  60438. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60439. + DWC_READ_REG32(addr));
  60440. + addr = &core_if->core_global_regs->guid;
  60441. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  60442. + (unsigned long)addr, DWC_READ_REG32(addr));
  60443. + addr = &core_if->core_global_regs->gsnpsid;
  60444. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60445. + DWC_READ_REG32(addr));
  60446. + addr = &core_if->core_global_regs->ghwcfg1;
  60447. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60448. + DWC_READ_REG32(addr));
  60449. + addr = &core_if->core_global_regs->ghwcfg2;
  60450. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60451. + DWC_READ_REG32(addr));
  60452. + addr = &core_if->core_global_regs->ghwcfg3;
  60453. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60454. + DWC_READ_REG32(addr));
  60455. + addr = &core_if->core_global_regs->ghwcfg4;
  60456. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60457. + DWC_READ_REG32(addr));
  60458. + addr = &core_if->core_global_regs->glpmcfg;
  60459. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60460. + DWC_READ_REG32(addr));
  60461. + addr = &core_if->core_global_regs->gpwrdn;
  60462. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60463. + DWC_READ_REG32(addr));
  60464. + addr = &core_if->core_global_regs->gdfifocfg;
  60465. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60466. + DWC_READ_REG32(addr));
  60467. + addr = &core_if->core_global_regs->adpctl;
  60468. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60469. + dwc_otg_adp_read_reg(core_if));
  60470. + addr = &core_if->core_global_regs->hptxfsiz;
  60471. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60472. + DWC_READ_REG32(addr));
  60473. +
  60474. + if (core_if->en_multiple_tx_fifo == 0) {
  60475. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  60476. + txfsiz = "DPTXFSIZ";
  60477. + } else {
  60478. + ep_num = core_if->hwcfg4.b.num_in_eps;
  60479. + txfsiz = "DIENPTXF";
  60480. + }
  60481. + for (i = 0; i < ep_num; i++) {
  60482. + addr = &core_if->core_global_regs->dtxfsiz[i];
  60483. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  60484. + (unsigned long)addr, DWC_READ_REG32(addr));
  60485. + }
  60486. + addr = core_if->pcgcctl;
  60487. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60488. + DWC_READ_REG32(addr));
  60489. +}
  60490. +
  60491. +/**
  60492. + * Flush a Tx FIFO.
  60493. + *
  60494. + * @param core_if Programming view of DWC_otg controller.
  60495. + * @param num Tx FIFO to flush.
  60496. + */
  60497. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  60498. +{
  60499. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  60500. + volatile grstctl_t greset = {.d32 = 0 };
  60501. + int count = 0;
  60502. +
  60503. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  60504. +
  60505. + greset.b.txfflsh = 1;
  60506. + greset.b.txfnum = num;
  60507. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  60508. +
  60509. + do {
  60510. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60511. + if (++count > 10000) {
  60512. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  60513. + __func__, greset.d32,
  60514. + DWC_READ_REG32(&global_regs->gnptxsts));
  60515. + break;
  60516. + }
  60517. + dwc_udelay(1);
  60518. + } while (greset.b.txfflsh == 1);
  60519. +
  60520. + /* Wait for 3 PHY Clocks */
  60521. + dwc_udelay(1);
  60522. +}
  60523. +
  60524. +/**
  60525. + * Flush Rx FIFO.
  60526. + *
  60527. + * @param core_if Programming view of DWC_otg controller.
  60528. + */
  60529. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  60530. +{
  60531. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  60532. + volatile grstctl_t greset = {.d32 = 0 };
  60533. + int count = 0;
  60534. +
  60535. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  60536. + /*
  60537. + *
  60538. + */
  60539. + greset.b.rxfflsh = 1;
  60540. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  60541. +
  60542. + do {
  60543. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60544. + if (++count > 10000) {
  60545. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  60546. + greset.d32);
  60547. + break;
  60548. + }
  60549. + dwc_udelay(1);
  60550. + } while (greset.b.rxfflsh == 1);
  60551. +
  60552. + /* Wait for 3 PHY Clocks */
  60553. + dwc_udelay(1);
  60554. +}
  60555. +
  60556. +/**
  60557. + * Do core a soft reset of the core. Be careful with this because it
  60558. + * resets all the internal state machines of the core.
  60559. + */
  60560. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  60561. +{
  60562. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  60563. + volatile grstctl_t greset = {.d32 = 0 };
  60564. + int count = 0;
  60565. +
  60566. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  60567. + /* Wait for AHB master IDLE state. */
  60568. + do {
  60569. + dwc_udelay(10);
  60570. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60571. + if (++count > 100000) {
  60572. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  60573. + greset.d32);
  60574. + return;
  60575. + }
  60576. + }
  60577. + while (greset.b.ahbidle == 0);
  60578. +
  60579. + /* Core Soft Reset */
  60580. + count = 0;
  60581. + greset.b.csftrst = 1;
  60582. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  60583. + do {
  60584. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60585. + if (++count > 10000) {
  60586. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  60587. + __func__, greset.d32);
  60588. + break;
  60589. + }
  60590. + dwc_udelay(1);
  60591. + }
  60592. + while (greset.b.csftrst == 1);
  60593. +
  60594. + /* Wait for 3 PHY Clocks */
  60595. + dwc_mdelay(100);
  60596. +}
  60597. +
  60598. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  60599. +{
  60600. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  60601. +}
  60602. +
  60603. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  60604. +{
  60605. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  60606. +}
  60607. +
  60608. +/**
  60609. + * Register HCD callbacks. The callbacks are used to start and stop
  60610. + * the HCD for interrupt processing.
  60611. + *
  60612. + * @param core_if Programming view of DWC_otg controller.
  60613. + * @param cb the HCD callback structure.
  60614. + * @param p pointer to be passed to callback function (usb_hcd*).
  60615. + */
  60616. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  60617. + dwc_otg_cil_callbacks_t * cb, void *p)
  60618. +{
  60619. + core_if->hcd_cb = cb;
  60620. + cb->p = p;
  60621. +}
  60622. +
  60623. +/**
  60624. + * Register PCD callbacks. The callbacks are used to start and stop
  60625. + * the PCD for interrupt processing.
  60626. + *
  60627. + * @param core_if Programming view of DWC_otg controller.
  60628. + * @param cb the PCD callback structure.
  60629. + * @param p pointer to be passed to callback function (pcd*).
  60630. + */
  60631. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  60632. + dwc_otg_cil_callbacks_t * cb, void *p)
  60633. +{
  60634. + core_if->pcd_cb = cb;
  60635. + cb->p = p;
  60636. +}
  60637. +
  60638. +#ifdef DWC_EN_ISOC
  60639. +
  60640. +/**
  60641. + * This function writes isoc data per 1 (micro)frame into tx fifo
  60642. + *
  60643. + * @param core_if Programming view of DWC_otg controller.
  60644. + * @param ep The EP to start the transfer on.
  60645. + *
  60646. + */
  60647. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  60648. +{
  60649. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  60650. + dtxfsts_data_t txstatus = {.d32 = 0 };
  60651. + uint32_t len = 0;
  60652. + uint32_t dwords;
  60653. +
  60654. + ep->xfer_len = ep->data_per_frame;
  60655. + ep->xfer_count = 0;
  60656. +
  60657. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  60658. +
  60659. + len = ep->xfer_len - ep->xfer_count;
  60660. +
  60661. + if (len > ep->maxpacket) {
  60662. + len = ep->maxpacket;
  60663. + }
  60664. +
  60665. + dwords = (len + 3) / 4;
  60666. +
  60667. + /* While there is space in the queue and space in the FIFO and
  60668. + * More data to tranfer, Write packets to the Tx FIFO */
  60669. + txstatus.d32 =
  60670. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  60671. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  60672. +
  60673. + while (txstatus.b.txfspcavail > dwords &&
  60674. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  60675. + /* Write the FIFO */
  60676. + dwc_otg_ep_write_packet(core_if, ep, 0);
  60677. +
  60678. + len = ep->xfer_len - ep->xfer_count;
  60679. + if (len > ep->maxpacket) {
  60680. + len = ep->maxpacket;
  60681. + }
  60682. +
  60683. + dwords = (len + 3) / 4;
  60684. + txstatus.d32 =
  60685. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  60686. + dtxfsts);
  60687. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  60688. + txstatus.d32);
  60689. + }
  60690. +}
  60691. +
  60692. +/**
  60693. + * This function initializes a descriptor chain for Isochronous transfer
  60694. + *
  60695. + * @param core_if Programming view of DWC_otg controller.
  60696. + * @param ep The EP to start the transfer on.
  60697. + *
  60698. + */
  60699. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  60700. + dwc_ep_t * ep)
  60701. +{
  60702. + deptsiz_data_t deptsiz = {.d32 = 0 };
  60703. + depctl_data_t depctl = {.d32 = 0 };
  60704. + dsts_data_t dsts = {.d32 = 0 };
  60705. + volatile uint32_t *addr;
  60706. +
  60707. + if (ep->is_in) {
  60708. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  60709. + } else {
  60710. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  60711. + }
  60712. +
  60713. + ep->xfer_len = ep->data_per_frame;
  60714. + ep->xfer_count = 0;
  60715. + ep->xfer_buff = ep->cur_pkt_addr;
  60716. + ep->dma_addr = ep->cur_pkt_dma_addr;
  60717. +
  60718. + if (ep->is_in) {
  60719. + /* Program the transfer size and packet count
  60720. + * as follows: xfersize = N * maxpacket +
  60721. + * short_packet pktcnt = N + (short_packet
  60722. + * exist ? 1 : 0)
  60723. + */
  60724. + deptsiz.b.xfersize = ep->xfer_len;
  60725. + deptsiz.b.pktcnt =
  60726. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  60727. + deptsiz.b.mc = deptsiz.b.pktcnt;
  60728. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  60729. + deptsiz.d32);
  60730. +
  60731. + /* Write the DMA register */
  60732. + if (core_if->dma_enable) {
  60733. + DWC_WRITE_REG32(&
  60734. + (core_if->dev_if->in_ep_regs[ep->num]->
  60735. + diepdma), (uint32_t) ep->dma_addr);
  60736. + }
  60737. + } else {
  60738. + deptsiz.b.pktcnt =
  60739. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  60740. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  60741. +
  60742. + DWC_WRITE_REG32(&core_if->dev_if->
  60743. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  60744. +
  60745. + if (core_if->dma_enable) {
  60746. + DWC_WRITE_REG32(&
  60747. + (core_if->dev_if->
  60748. + out_ep_regs[ep->num]->doepdma),
  60749. + (uint32_t) ep->dma_addr);
  60750. + }
  60751. + }
  60752. +
  60753. + /** Enable endpoint, clear nak */
  60754. +
  60755. + depctl.d32 = 0;
  60756. + if (ep->bInterval == 1) {
  60757. + dsts.d32 =
  60758. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  60759. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  60760. +
  60761. + if (ep->next_frame & 0x1) {
  60762. + depctl.b.setd1pid = 1;
  60763. + } else {
  60764. + depctl.b.setd0pid = 1;
  60765. + }
  60766. + } else {
  60767. + ep->next_frame += ep->bInterval;
  60768. +
  60769. + if (ep->next_frame & 0x1) {
  60770. + depctl.b.setd1pid = 1;
  60771. + } else {
  60772. + depctl.b.setd0pid = 1;
  60773. + }
  60774. + }
  60775. + depctl.b.epena = 1;
  60776. + depctl.b.cnak = 1;
  60777. +
  60778. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  60779. + depctl.d32 = DWC_READ_REG32(addr);
  60780. +
  60781. + if (ep->is_in && core_if->dma_enable == 0) {
  60782. + write_isoc_frame_data(core_if, ep);
  60783. + }
  60784. +
  60785. +}
  60786. +#endif /* DWC_EN_ISOC */
  60787. +
  60788. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  60789. +{
  60790. + int i;
  60791. + for (i = 0; i < size; i++) {
  60792. + p[i] = -1;
  60793. + }
  60794. +}
  60795. +
  60796. +static int dwc_otg_param_initialized(int32_t val)
  60797. +{
  60798. + return val != -1;
  60799. +}
  60800. +
  60801. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  60802. +{
  60803. + int i;
  60804. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  60805. + if (!core_if->core_params) {
  60806. + return -DWC_E_NO_MEMORY;
  60807. + }
  60808. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  60809. + sizeof(*core_if->core_params) /
  60810. + sizeof(int32_t));
  60811. + DWC_PRINTF("Setting default values for core params\n");
  60812. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  60813. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  60814. + dwc_otg_set_param_dma_desc_enable(core_if,
  60815. + dwc_param_dma_desc_enable_default);
  60816. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  60817. + dwc_otg_set_param_dma_burst_size(core_if,
  60818. + dwc_param_dma_burst_size_default);
  60819. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  60820. + dwc_param_host_support_fs_ls_low_power_default);
  60821. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  60822. + dwc_param_enable_dynamic_fifo_default);
  60823. + dwc_otg_set_param_data_fifo_size(core_if,
  60824. + dwc_param_data_fifo_size_default);
  60825. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  60826. + dwc_param_dev_rx_fifo_size_default);
  60827. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  60828. + dwc_param_dev_nperio_tx_fifo_size_default);
  60829. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  60830. + dwc_param_host_rx_fifo_size_default);
  60831. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  60832. + dwc_param_host_nperio_tx_fifo_size_default);
  60833. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  60834. + dwc_param_host_perio_tx_fifo_size_default);
  60835. + dwc_otg_set_param_max_transfer_size(core_if,
  60836. + dwc_param_max_transfer_size_default);
  60837. + dwc_otg_set_param_max_packet_count(core_if,
  60838. + dwc_param_max_packet_count_default);
  60839. + dwc_otg_set_param_host_channels(core_if,
  60840. + dwc_param_host_channels_default);
  60841. + dwc_otg_set_param_dev_endpoints(core_if,
  60842. + dwc_param_dev_endpoints_default);
  60843. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  60844. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  60845. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  60846. + dwc_param_host_ls_low_power_phy_clk_default);
  60847. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  60848. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  60849. + dwc_param_phy_ulpi_ext_vbus_default);
  60850. + dwc_otg_set_param_phy_utmi_width(core_if,
  60851. + dwc_param_phy_utmi_width_default);
  60852. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  60853. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  60854. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  60855. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  60856. + dwc_param_en_multiple_tx_fifo_default);
  60857. + for (i = 0; i < 15; i++) {
  60858. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  60859. + dwc_param_dev_perio_tx_fifo_size_default,
  60860. + i);
  60861. + }
  60862. +
  60863. + for (i = 0; i < 15; i++) {
  60864. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  60865. + dwc_param_dev_tx_fifo_size_default,
  60866. + i);
  60867. + }
  60868. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  60869. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  60870. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  60871. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  60872. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  60873. + dwc_otg_set_param_tx_thr_length(core_if,
  60874. + dwc_param_tx_thr_length_default);
  60875. + dwc_otg_set_param_rx_thr_length(core_if,
  60876. + dwc_param_rx_thr_length_default);
  60877. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  60878. + dwc_param_ahb_thr_ratio_default);
  60879. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  60880. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  60881. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  60882. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  60883. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  60884. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  60885. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  60886. + DWC_PRINTF("Finished setting default values for core params\n");
  60887. +
  60888. + return 0;
  60889. +}
  60890. +
  60891. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  60892. +{
  60893. + return core_if->dma_enable;
  60894. +}
  60895. +
  60896. +/* Checks if the parameter is outside of its valid range of values */
  60897. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  60898. + (((_param_) < (_low_)) || \
  60899. + ((_param_) > (_high_)))
  60900. +
  60901. +/* Parameter access functions */
  60902. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  60903. +{
  60904. + int valid;
  60905. + int retval = 0;
  60906. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  60907. + DWC_WARN("Wrong value for otg_cap parameter\n");
  60908. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  60909. + retval = -DWC_E_INVALID;
  60910. + goto out;
  60911. + }
  60912. +
  60913. + valid = 1;
  60914. + switch (val) {
  60915. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  60916. + if (core_if->hwcfg2.b.op_mode !=
  60917. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60918. + valid = 0;
  60919. + break;
  60920. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  60921. + if ((core_if->hwcfg2.b.op_mode !=
  60922. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60923. + && (core_if->hwcfg2.b.op_mode !=
  60924. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  60925. + && (core_if->hwcfg2.b.op_mode !=
  60926. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  60927. + && (core_if->hwcfg2.b.op_mode !=
  60928. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  60929. + valid = 0;
  60930. + }
  60931. + break;
  60932. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  60933. + /* always valid */
  60934. + break;
  60935. + }
  60936. + if (!valid) {
  60937. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  60938. + DWC_ERROR
  60939. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  60940. + val);
  60941. + }
  60942. + val =
  60943. + (((core_if->hwcfg2.b.op_mode ==
  60944. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60945. + || (core_if->hwcfg2.b.op_mode ==
  60946. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  60947. + || (core_if->hwcfg2.b.op_mode ==
  60948. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  60949. + || (core_if->hwcfg2.b.op_mode ==
  60950. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  60951. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  60952. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  60953. + retval = -DWC_E_INVALID;
  60954. + }
  60955. +
  60956. + core_if->core_params->otg_cap = val;
  60957. +out:
  60958. + return retval;
  60959. +}
  60960. +
  60961. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  60962. +{
  60963. + return core_if->core_params->otg_cap;
  60964. +}
  60965. +
  60966. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  60967. +{
  60968. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60969. + DWC_WARN("Wrong value for opt parameter\n");
  60970. + return -DWC_E_INVALID;
  60971. + }
  60972. + core_if->core_params->opt = val;
  60973. + return 0;
  60974. +}
  60975. +
  60976. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  60977. +{
  60978. + return core_if->core_params->opt;
  60979. +}
  60980. +
  60981. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  60982. +{
  60983. + int retval = 0;
  60984. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60985. + DWC_WARN("Wrong value for dma enable\n");
  60986. + return -DWC_E_INVALID;
  60987. + }
  60988. +
  60989. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  60990. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  60991. + DWC_ERROR
  60992. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  60993. + val);
  60994. + }
  60995. + val = 0;
  60996. + retval = -DWC_E_INVALID;
  60997. + }
  60998. +
  60999. + core_if->core_params->dma_enable = val;
  61000. + if (val == 0) {
  61001. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  61002. + }
  61003. + return retval;
  61004. +}
  61005. +
  61006. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  61007. +{
  61008. + return core_if->core_params->dma_enable;
  61009. +}
  61010. +
  61011. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61012. +{
  61013. + int retval = 0;
  61014. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61015. + DWC_WARN("Wrong value for dma_enable\n");
  61016. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  61017. + return -DWC_E_INVALID;
  61018. + }
  61019. +
  61020. + if ((val == 1)
  61021. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  61022. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  61023. + if (dwc_otg_param_initialized
  61024. + (core_if->core_params->dma_desc_enable)) {
  61025. + DWC_ERROR
  61026. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  61027. + val);
  61028. + }
  61029. + val = 0;
  61030. + retval = -DWC_E_INVALID;
  61031. + }
  61032. + core_if->core_params->dma_desc_enable = val;
  61033. + return retval;
  61034. +}
  61035. +
  61036. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  61037. +{
  61038. + return core_if->core_params->dma_desc_enable;
  61039. +}
  61040. +
  61041. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  61042. + int32_t val)
  61043. +{
  61044. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61045. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  61046. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  61047. + return -DWC_E_INVALID;
  61048. + }
  61049. + core_if->core_params->host_support_fs_ls_low_power = val;
  61050. + return 0;
  61051. +}
  61052. +
  61053. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  61054. + core_if)
  61055. +{
  61056. + return core_if->core_params->host_support_fs_ls_low_power;
  61057. +}
  61058. +
  61059. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  61060. + int32_t val)
  61061. +{
  61062. + int retval = 0;
  61063. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61064. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  61065. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  61066. + return -DWC_E_INVALID;
  61067. + }
  61068. +
  61069. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  61070. + if (dwc_otg_param_initialized
  61071. + (core_if->core_params->enable_dynamic_fifo)) {
  61072. + DWC_ERROR
  61073. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  61074. + val);
  61075. + }
  61076. + val = 0;
  61077. + retval = -DWC_E_INVALID;
  61078. + }
  61079. + core_if->core_params->enable_dynamic_fifo = val;
  61080. + return retval;
  61081. +}
  61082. +
  61083. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  61084. +{
  61085. + return core_if->core_params->enable_dynamic_fifo;
  61086. +}
  61087. +
  61088. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  61089. +{
  61090. + int retval = 0;
  61091. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  61092. + DWC_WARN("Wrong value for data_fifo_size\n");
  61093. + DWC_WARN("data_fifo_size must be 32-32768\n");
  61094. + return -DWC_E_INVALID;
  61095. + }
  61096. +
  61097. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  61098. + if (dwc_otg_param_initialized
  61099. + (core_if->core_params->data_fifo_size)) {
  61100. + DWC_ERROR
  61101. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  61102. + val);
  61103. + }
  61104. + val = core_if->hwcfg3.b.dfifo_depth;
  61105. + retval = -DWC_E_INVALID;
  61106. + }
  61107. +
  61108. + core_if->core_params->data_fifo_size = val;
  61109. + return retval;
  61110. +}
  61111. +
  61112. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  61113. +{
  61114. + return core_if->core_params->data_fifo_size;
  61115. +}
  61116. +
  61117. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  61118. +{
  61119. + int retval = 0;
  61120. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61121. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  61122. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  61123. + return -DWC_E_INVALID;
  61124. + }
  61125. +
  61126. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  61127. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  61128. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  61129. + }
  61130. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  61131. + retval = -DWC_E_INVALID;
  61132. + }
  61133. +
  61134. + core_if->core_params->dev_rx_fifo_size = val;
  61135. + return retval;
  61136. +}
  61137. +
  61138. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  61139. +{
  61140. + return core_if->core_params->dev_rx_fifo_size;
  61141. +}
  61142. +
  61143. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61144. + int32_t val)
  61145. +{
  61146. + int retval = 0;
  61147. +
  61148. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61149. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  61150. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  61151. + return -DWC_E_INVALID;
  61152. + }
  61153. +
  61154. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  61155. + if (dwc_otg_param_initialized
  61156. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  61157. + DWC_ERROR
  61158. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  61159. + val);
  61160. + }
  61161. + val =
  61162. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  61163. + 16);
  61164. + retval = -DWC_E_INVALID;
  61165. + }
  61166. +
  61167. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  61168. + return retval;
  61169. +}
  61170. +
  61171. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  61172. +{
  61173. + return core_if->core_params->dev_nperio_tx_fifo_size;
  61174. +}
  61175. +
  61176. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  61177. + int32_t val)
  61178. +{
  61179. + int retval = 0;
  61180. +
  61181. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61182. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  61183. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  61184. + return -DWC_E_INVALID;
  61185. + }
  61186. +
  61187. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  61188. + if (dwc_otg_param_initialized
  61189. + (core_if->core_params->host_rx_fifo_size)) {
  61190. + DWC_ERROR
  61191. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  61192. + val);
  61193. + }
  61194. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  61195. + retval = -DWC_E_INVALID;
  61196. + }
  61197. +
  61198. + core_if->core_params->host_rx_fifo_size = val;
  61199. + return retval;
  61200. +
  61201. +}
  61202. +
  61203. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  61204. +{
  61205. + return core_if->core_params->host_rx_fifo_size;
  61206. +}
  61207. +
  61208. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61209. + int32_t val)
  61210. +{
  61211. + int retval = 0;
  61212. +
  61213. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61214. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  61215. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  61216. + return -DWC_E_INVALID;
  61217. + }
  61218. +
  61219. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  61220. + if (dwc_otg_param_initialized
  61221. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  61222. + DWC_ERROR
  61223. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  61224. + val);
  61225. + }
  61226. + val =
  61227. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  61228. + 16);
  61229. + retval = -DWC_E_INVALID;
  61230. + }
  61231. +
  61232. + core_if->core_params->host_nperio_tx_fifo_size = val;
  61233. + return retval;
  61234. +}
  61235. +
  61236. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  61237. +{
  61238. + return core_if->core_params->host_nperio_tx_fifo_size;
  61239. +}
  61240. +
  61241. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61242. + int32_t val)
  61243. +{
  61244. + int retval = 0;
  61245. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61246. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  61247. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  61248. + return -DWC_E_INVALID;
  61249. + }
  61250. +
  61251. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  61252. + if (dwc_otg_param_initialized
  61253. + (core_if->core_params->host_perio_tx_fifo_size)) {
  61254. + DWC_ERROR
  61255. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  61256. + val);
  61257. + }
  61258. + val = (core_if->hptxfsiz.d32) >> 16;
  61259. + retval = -DWC_E_INVALID;
  61260. + }
  61261. +
  61262. + core_if->core_params->host_perio_tx_fifo_size = val;
  61263. + return retval;
  61264. +}
  61265. +
  61266. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  61267. +{
  61268. + return core_if->core_params->host_perio_tx_fifo_size;
  61269. +}
  61270. +
  61271. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  61272. + int32_t val)
  61273. +{
  61274. + int retval = 0;
  61275. +
  61276. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  61277. + DWC_WARN("Wrong value for max_transfer_size\n");
  61278. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  61279. + return -DWC_E_INVALID;
  61280. + }
  61281. +
  61282. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  61283. + if (dwc_otg_param_initialized
  61284. + (core_if->core_params->max_transfer_size)) {
  61285. + DWC_ERROR
  61286. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  61287. + val);
  61288. + }
  61289. + val =
  61290. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  61291. + 1);
  61292. + retval = -DWC_E_INVALID;
  61293. + }
  61294. +
  61295. + core_if->core_params->max_transfer_size = val;
  61296. + return retval;
  61297. +}
  61298. +
  61299. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  61300. +{
  61301. + return core_if->core_params->max_transfer_size;
  61302. +}
  61303. +
  61304. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  61305. +{
  61306. + int retval = 0;
  61307. +
  61308. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  61309. + DWC_WARN("Wrong value for max_packet_count\n");
  61310. + DWC_WARN("max_packet_count must be 15-511\n");
  61311. + return -DWC_E_INVALID;
  61312. + }
  61313. +
  61314. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  61315. + if (dwc_otg_param_initialized
  61316. + (core_if->core_params->max_packet_count)) {
  61317. + DWC_ERROR
  61318. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  61319. + val);
  61320. + }
  61321. + val =
  61322. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  61323. + retval = -DWC_E_INVALID;
  61324. + }
  61325. +
  61326. + core_if->core_params->max_packet_count = val;
  61327. + return retval;
  61328. +}
  61329. +
  61330. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  61331. +{
  61332. + return core_if->core_params->max_packet_count;
  61333. +}
  61334. +
  61335. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  61336. +{
  61337. + int retval = 0;
  61338. +
  61339. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  61340. + DWC_WARN("Wrong value for host_channels\n");
  61341. + DWC_WARN("host_channels must be 1-16\n");
  61342. + return -DWC_E_INVALID;
  61343. + }
  61344. +
  61345. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  61346. + if (dwc_otg_param_initialized
  61347. + (core_if->core_params->host_channels)) {
  61348. + DWC_ERROR
  61349. + ("%d invalid for host_channels. Check HW configurations.\n",
  61350. + val);
  61351. + }
  61352. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  61353. + retval = -DWC_E_INVALID;
  61354. + }
  61355. +
  61356. + core_if->core_params->host_channels = val;
  61357. + return retval;
  61358. +}
  61359. +
  61360. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  61361. +{
  61362. + return core_if->core_params->host_channels;
  61363. +}
  61364. +
  61365. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  61366. +{
  61367. + int retval = 0;
  61368. +
  61369. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  61370. + DWC_WARN("Wrong value for dev_endpoints\n");
  61371. + DWC_WARN("dev_endpoints must be 1-15\n");
  61372. + return -DWC_E_INVALID;
  61373. + }
  61374. +
  61375. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  61376. + if (dwc_otg_param_initialized
  61377. + (core_if->core_params->dev_endpoints)) {
  61378. + DWC_ERROR
  61379. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  61380. + val);
  61381. + }
  61382. + val = core_if->hwcfg2.b.num_dev_ep;
  61383. + retval = -DWC_E_INVALID;
  61384. + }
  61385. +
  61386. + core_if->core_params->dev_endpoints = val;
  61387. + return retval;
  61388. +}
  61389. +
  61390. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  61391. +{
  61392. + return core_if->core_params->dev_endpoints;
  61393. +}
  61394. +
  61395. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  61396. +{
  61397. + int retval = 0;
  61398. + int valid = 0;
  61399. +
  61400. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  61401. + DWC_WARN("Wrong value for phy_type\n");
  61402. + DWC_WARN("phy_type must be 0,1 or 2\n");
  61403. + return -DWC_E_INVALID;
  61404. + }
  61405. +#ifndef NO_FS_PHY_HW_CHECKS
  61406. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  61407. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  61408. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  61409. + valid = 1;
  61410. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  61411. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  61412. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  61413. + valid = 1;
  61414. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  61415. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  61416. + valid = 1;
  61417. + }
  61418. + if (!valid) {
  61419. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  61420. + DWC_ERROR
  61421. + ("%d invalid for phy_type. Check HW configurations.\n",
  61422. + val);
  61423. + }
  61424. + if (core_if->hwcfg2.b.hs_phy_type) {
  61425. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  61426. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  61427. + val = DWC_PHY_TYPE_PARAM_UTMI;
  61428. + } else {
  61429. + val = DWC_PHY_TYPE_PARAM_ULPI;
  61430. + }
  61431. + }
  61432. + retval = -DWC_E_INVALID;
  61433. + }
  61434. +#endif
  61435. + core_if->core_params->phy_type = val;
  61436. + return retval;
  61437. +}
  61438. +
  61439. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  61440. +{
  61441. + return core_if->core_params->phy_type;
  61442. +}
  61443. +
  61444. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  61445. +{
  61446. + int retval = 0;
  61447. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61448. + DWC_WARN("Wrong value for speed parameter\n");
  61449. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  61450. + return -DWC_E_INVALID;
  61451. + }
  61452. + if ((val == 0)
  61453. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  61454. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  61455. + DWC_ERROR
  61456. + ("%d invalid for speed paremter. Check HW configuration.\n",
  61457. + val);
  61458. + }
  61459. + val =
  61460. + (dwc_otg_get_param_phy_type(core_if) ==
  61461. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  61462. + retval = -DWC_E_INVALID;
  61463. + }
  61464. + core_if->core_params->speed = val;
  61465. + return retval;
  61466. +}
  61467. +
  61468. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  61469. +{
  61470. + return core_if->core_params->speed;
  61471. +}
  61472. +
  61473. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  61474. + int32_t val)
  61475. +{
  61476. + int retval = 0;
  61477. +
  61478. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61479. + DWC_WARN
  61480. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  61481. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  61482. + return -DWC_E_INVALID;
  61483. + }
  61484. +
  61485. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  61486. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  61487. + if (dwc_otg_param_initialized
  61488. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  61489. + DWC_ERROR
  61490. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  61491. + val);
  61492. + }
  61493. + val =
  61494. + (dwc_otg_get_param_phy_type(core_if) ==
  61495. + DWC_PHY_TYPE_PARAM_FS) ?
  61496. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  61497. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  61498. + retval = -DWC_E_INVALID;
  61499. + }
  61500. +
  61501. + core_if->core_params->host_ls_low_power_phy_clk = val;
  61502. + return retval;
  61503. +}
  61504. +
  61505. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  61506. +{
  61507. + return core_if->core_params->host_ls_low_power_phy_clk;
  61508. +}
  61509. +
  61510. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  61511. +{
  61512. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61513. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  61514. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  61515. + return -DWC_E_INVALID;
  61516. + }
  61517. +
  61518. + core_if->core_params->phy_ulpi_ddr = val;
  61519. + return 0;
  61520. +}
  61521. +
  61522. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  61523. +{
  61524. + return core_if->core_params->phy_ulpi_ddr;
  61525. +}
  61526. +
  61527. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  61528. + int32_t val)
  61529. +{
  61530. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61531. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  61532. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  61533. + return -DWC_E_INVALID;
  61534. + }
  61535. +
  61536. + core_if->core_params->phy_ulpi_ext_vbus = val;
  61537. + return 0;
  61538. +}
  61539. +
  61540. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  61541. +{
  61542. + return core_if->core_params->phy_ulpi_ext_vbus;
  61543. +}
  61544. +
  61545. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  61546. +{
  61547. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  61548. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  61549. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  61550. + return -DWC_E_INVALID;
  61551. + }
  61552. +
  61553. + core_if->core_params->phy_utmi_width = val;
  61554. + return 0;
  61555. +}
  61556. +
  61557. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  61558. +{
  61559. + return core_if->core_params->phy_utmi_width;
  61560. +}
  61561. +
  61562. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  61563. +{
  61564. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61565. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  61566. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  61567. + return -DWC_E_INVALID;
  61568. + }
  61569. +
  61570. + core_if->core_params->ulpi_fs_ls = val;
  61571. + return 0;
  61572. +}
  61573. +
  61574. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  61575. +{
  61576. + return core_if->core_params->ulpi_fs_ls;
  61577. +}
  61578. +
  61579. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  61580. +{
  61581. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61582. + DWC_WARN("Wrong valaue for ts_dline\n");
  61583. + DWC_WARN("ts_dline must be 0 or 1\n");
  61584. + return -DWC_E_INVALID;
  61585. + }
  61586. +
  61587. + core_if->core_params->ts_dline = val;
  61588. + return 0;
  61589. +}
  61590. +
  61591. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  61592. +{
  61593. + return core_if->core_params->ts_dline;
  61594. +}
  61595. +
  61596. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61597. +{
  61598. + int retval = 0;
  61599. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61600. + DWC_WARN("Wrong valaue for i2c_enable\n");
  61601. + DWC_WARN("i2c_enable must be 0 or 1\n");
  61602. + return -DWC_E_INVALID;
  61603. + }
  61604. +#ifndef NO_FS_PHY_HW_CHECK
  61605. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  61606. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  61607. + DWC_ERROR
  61608. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  61609. + val);
  61610. + }
  61611. + val = 0;
  61612. + retval = -DWC_E_INVALID;
  61613. + }
  61614. +#endif
  61615. +
  61616. + core_if->core_params->i2c_enable = val;
  61617. + return retval;
  61618. +}
  61619. +
  61620. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  61621. +{
  61622. + return core_if->core_params->i2c_enable;
  61623. +}
  61624. +
  61625. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61626. + int32_t val, int fifo_num)
  61627. +{
  61628. + int retval = 0;
  61629. +
  61630. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  61631. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  61632. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  61633. + return -DWC_E_INVALID;
  61634. + }
  61635. +
  61636. + if (val >
  61637. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  61638. + if (dwc_otg_param_initialized
  61639. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  61640. + DWC_ERROR
  61641. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  61642. + val, fifo_num);
  61643. + }
  61644. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  61645. + retval = -DWC_E_INVALID;
  61646. + }
  61647. +
  61648. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  61649. + return retval;
  61650. +}
  61651. +
  61652. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61653. + int fifo_num)
  61654. +{
  61655. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  61656. +}
  61657. +
  61658. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  61659. + int32_t val)
  61660. +{
  61661. + int retval = 0;
  61662. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61663. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  61664. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  61665. + return -DWC_E_INVALID;
  61666. + }
  61667. +
  61668. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  61669. + if (dwc_otg_param_initialized
  61670. + (core_if->core_params->en_multiple_tx_fifo)) {
  61671. + DWC_ERROR
  61672. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  61673. + val);
  61674. + }
  61675. + val = 0;
  61676. + retval = -DWC_E_INVALID;
  61677. + }
  61678. +
  61679. + core_if->core_params->en_multiple_tx_fifo = val;
  61680. + return retval;
  61681. +}
  61682. +
  61683. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  61684. +{
  61685. + return core_if->core_params->en_multiple_tx_fifo;
  61686. +}
  61687. +
  61688. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  61689. + int fifo_num)
  61690. +{
  61691. + int retval = 0;
  61692. +
  61693. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  61694. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  61695. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  61696. + return -DWC_E_INVALID;
  61697. + }
  61698. +
  61699. + if (val >
  61700. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  61701. + if (dwc_otg_param_initialized
  61702. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  61703. + DWC_ERROR
  61704. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  61705. + val, fifo_num);
  61706. + }
  61707. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  61708. + retval = -DWC_E_INVALID;
  61709. + }
  61710. +
  61711. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  61712. + return retval;
  61713. +}
  61714. +
  61715. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61716. + int fifo_num)
  61717. +{
  61718. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  61719. +}
  61720. +
  61721. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  61722. +{
  61723. + int retval = 0;
  61724. +
  61725. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  61726. + DWC_WARN("Wrong value for thr_ctl\n");
  61727. + DWC_WARN("thr_ctl must be 0-7\n");
  61728. + return -DWC_E_INVALID;
  61729. + }
  61730. +
  61731. + if ((val != 0) &&
  61732. + (!dwc_otg_get_param_dma_enable(core_if) ||
  61733. + !core_if->hwcfg4.b.ded_fifo_en)) {
  61734. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  61735. + DWC_ERROR
  61736. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  61737. + val);
  61738. + }
  61739. + val = 0;
  61740. + retval = -DWC_E_INVALID;
  61741. + }
  61742. +
  61743. + core_if->core_params->thr_ctl = val;
  61744. + return retval;
  61745. +}
  61746. +
  61747. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  61748. +{
  61749. + return core_if->core_params->thr_ctl;
  61750. +}
  61751. +
  61752. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61753. +{
  61754. + int retval = 0;
  61755. +
  61756. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61757. + DWC_WARN("Wrong value for lpm_enable\n");
  61758. + DWC_WARN("lpm_enable must be 0 or 1\n");
  61759. + return -DWC_E_INVALID;
  61760. + }
  61761. +
  61762. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  61763. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  61764. + DWC_ERROR
  61765. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  61766. + val);
  61767. + }
  61768. + val = 0;
  61769. + retval = -DWC_E_INVALID;
  61770. + }
  61771. +
  61772. + core_if->core_params->lpm_enable = val;
  61773. + return retval;
  61774. +}
  61775. +
  61776. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  61777. +{
  61778. + return core_if->core_params->lpm_enable;
  61779. +}
  61780. +
  61781. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  61782. +{
  61783. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  61784. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  61785. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  61786. + return -DWC_E_INVALID;
  61787. + }
  61788. +
  61789. + core_if->core_params->tx_thr_length = val;
  61790. + return 0;
  61791. +}
  61792. +
  61793. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  61794. +{
  61795. + return core_if->core_params->tx_thr_length;
  61796. +}
  61797. +
  61798. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  61799. +{
  61800. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  61801. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  61802. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  61803. + return -DWC_E_INVALID;
  61804. + }
  61805. +
  61806. + core_if->core_params->rx_thr_length = val;
  61807. + return 0;
  61808. +}
  61809. +
  61810. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  61811. +{
  61812. + return core_if->core_params->rx_thr_length;
  61813. +}
  61814. +
  61815. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  61816. +{
  61817. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  61818. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  61819. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  61820. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  61821. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  61822. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  61823. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  61824. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  61825. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  61826. + return -DWC_E_INVALID;
  61827. + }
  61828. + core_if->core_params->dma_burst_size = val;
  61829. + return 0;
  61830. +}
  61831. +
  61832. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  61833. +{
  61834. + return core_if->core_params->dma_burst_size;
  61835. +}
  61836. +
  61837. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61838. +{
  61839. + int retval = 0;
  61840. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61841. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  61842. + return -DWC_E_INVALID;
  61843. + }
  61844. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  61845. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  61846. + DWC_ERROR
  61847. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  61848. + val);
  61849. + }
  61850. + retval = -DWC_E_INVALID;
  61851. + val = 0;
  61852. + }
  61853. + core_if->core_params->pti_enable = val;
  61854. + return retval;
  61855. +}
  61856. +
  61857. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  61858. +{
  61859. + return core_if->core_params->pti_enable;
  61860. +}
  61861. +
  61862. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61863. +{
  61864. + int retval = 0;
  61865. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61866. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  61867. + return -DWC_E_INVALID;
  61868. + }
  61869. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  61870. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  61871. + DWC_ERROR
  61872. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  61873. + val);
  61874. + }
  61875. + retval = -DWC_E_INVALID;
  61876. + val = 0;
  61877. + }
  61878. + core_if->core_params->mpi_enable = val;
  61879. + return retval;
  61880. +}
  61881. +
  61882. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  61883. +{
  61884. + return core_if->core_params->mpi_enable;
  61885. +}
  61886. +
  61887. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61888. +{
  61889. + int retval = 0;
  61890. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61891. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  61892. + return -DWC_E_INVALID;
  61893. + }
  61894. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  61895. + if (dwc_otg_param_initialized
  61896. + (core_if->core_params->adp_supp_enable)) {
  61897. + DWC_ERROR
  61898. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  61899. + val);
  61900. + }
  61901. + retval = -DWC_E_INVALID;
  61902. + val = 0;
  61903. + }
  61904. + core_if->core_params->adp_supp_enable = val;
  61905. + /*Set OTG version 2.0 in case of enabling ADP*/
  61906. + if (val)
  61907. + dwc_otg_set_param_otg_ver(core_if, 1);
  61908. +
  61909. + return retval;
  61910. +}
  61911. +
  61912. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  61913. +{
  61914. + return core_if->core_params->adp_supp_enable;
  61915. +}
  61916. +
  61917. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  61918. +{
  61919. + int retval = 0;
  61920. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61921. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  61922. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  61923. + return -DWC_E_INVALID;
  61924. + }
  61925. +
  61926. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  61927. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  61928. + DWC_ERROR
  61929. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  61930. + val);
  61931. + }
  61932. + retval = -DWC_E_INVALID;
  61933. + val = 0;
  61934. + }
  61935. + core_if->core_params->ic_usb_cap = val;
  61936. + return retval;
  61937. +}
  61938. +
  61939. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  61940. +{
  61941. + return core_if->core_params->ic_usb_cap;
  61942. +}
  61943. +
  61944. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  61945. +{
  61946. + int retval = 0;
  61947. + int valid = 1;
  61948. +
  61949. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  61950. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  61951. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  61952. + return -DWC_E_INVALID;
  61953. + }
  61954. +
  61955. + if (val
  61956. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  61957. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  61958. + valid = 0;
  61959. + } else if (val
  61960. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  61961. + 4)) {
  61962. + valid = 0;
  61963. + }
  61964. + if (valid == 0) {
  61965. + if (dwc_otg_param_initialized
  61966. + (core_if->core_params->ahb_thr_ratio)) {
  61967. + DWC_ERROR
  61968. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  61969. + val);
  61970. + }
  61971. + retval = -DWC_E_INVALID;
  61972. + val = 0;
  61973. + }
  61974. +
  61975. + core_if->core_params->ahb_thr_ratio = val;
  61976. + return retval;
  61977. +}
  61978. +
  61979. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  61980. +{
  61981. + return core_if->core_params->ahb_thr_ratio;
  61982. +}
  61983. +
  61984. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  61985. +{
  61986. + int retval = 0;
  61987. + int valid = 1;
  61988. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  61989. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  61990. +
  61991. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  61992. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  61993. + DWC_WARN("power_down must be 0 - 2\n");
  61994. + return -DWC_E_INVALID;
  61995. + }
  61996. +
  61997. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  61998. + valid = 0;
  61999. + }
  62000. + if ((val == 3)
  62001. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  62002. + || (hwcfg4.b.xhiber == 0))) {
  62003. + valid = 0;
  62004. + }
  62005. + if (valid == 0) {
  62006. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  62007. + DWC_ERROR
  62008. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  62009. + val);
  62010. + }
  62011. + retval = -DWC_E_INVALID;
  62012. + val = 0;
  62013. + }
  62014. + core_if->core_params->power_down = val;
  62015. + return retval;
  62016. +}
  62017. +
  62018. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  62019. +{
  62020. + return core_if->core_params->power_down;
  62021. +}
  62022. +
  62023. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  62024. +{
  62025. + int retval = 0;
  62026. + int valid = 1;
  62027. +
  62028. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62029. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  62030. + DWC_WARN("reload_ctl must be 0 or 1\n");
  62031. + return -DWC_E_INVALID;
  62032. + }
  62033. +
  62034. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  62035. + valid = 0;
  62036. + }
  62037. + if (valid == 0) {
  62038. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  62039. + DWC_ERROR("%d invalid for parameter reload_ctl."
  62040. + "Check HW configuration.\n", val);
  62041. + }
  62042. + retval = -DWC_E_INVALID;
  62043. + val = 0;
  62044. + }
  62045. + core_if->core_params->reload_ctl = val;
  62046. + return retval;
  62047. +}
  62048. +
  62049. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  62050. +{
  62051. + return core_if->core_params->reload_ctl;
  62052. +}
  62053. +
  62054. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  62055. +{
  62056. + int retval = 0;
  62057. + int valid = 1;
  62058. +
  62059. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62060. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  62061. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  62062. + return -DWC_E_INVALID;
  62063. + }
  62064. +
  62065. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  62066. + !(core_if->core_params->dma_desc_enable))) {
  62067. + valid = 0;
  62068. + }
  62069. + if (valid == 0) {
  62070. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  62071. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  62072. + "Check HW configuration.\n", val);
  62073. + }
  62074. + retval = -DWC_E_INVALID;
  62075. + val = 0;
  62076. + }
  62077. + core_if->core_params->dev_out_nak = val;
  62078. + return retval;
  62079. +}
  62080. +
  62081. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  62082. +{
  62083. + return core_if->core_params->dev_out_nak;
  62084. +}
  62085. +
  62086. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  62087. +{
  62088. + int retval = 0;
  62089. + int valid = 1;
  62090. +
  62091. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62092. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  62093. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  62094. + return -DWC_E_INVALID;
  62095. + }
  62096. +
  62097. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  62098. + !(core_if->core_params->dma_desc_enable))) {
  62099. + valid = 0;
  62100. + }
  62101. + if (valid == 0) {
  62102. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  62103. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  62104. + "Check HW configuration.\n", val);
  62105. + }
  62106. + retval = -DWC_E_INVALID;
  62107. + val = 0;
  62108. + }
  62109. + core_if->core_params->cont_on_bna = val;
  62110. + return retval;
  62111. +}
  62112. +
  62113. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  62114. +{
  62115. + return core_if->core_params->cont_on_bna;
  62116. +}
  62117. +
  62118. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  62119. +{
  62120. + int retval = 0;
  62121. + int valid = 1;
  62122. +
  62123. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62124. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  62125. + DWC_WARN("ahb_single must be 0 or 1\n");
  62126. + return -DWC_E_INVALID;
  62127. + }
  62128. +
  62129. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  62130. + valid = 0;
  62131. + }
  62132. + if (valid == 0) {
  62133. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  62134. + DWC_ERROR("%d invalid for parameter ahb_single."
  62135. + "Check HW configuration.\n", val);
  62136. + }
  62137. + retval = -DWC_E_INVALID;
  62138. + val = 0;
  62139. + }
  62140. + core_if->core_params->ahb_single = val;
  62141. + return retval;
  62142. +}
  62143. +
  62144. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  62145. +{
  62146. + return core_if->core_params->ahb_single;
  62147. +}
  62148. +
  62149. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  62150. +{
  62151. + int retval = 0;
  62152. +
  62153. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62154. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  62155. + DWC_WARN
  62156. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  62157. + return -DWC_E_INVALID;
  62158. + }
  62159. +
  62160. + core_if->core_params->otg_ver = val;
  62161. + return retval;
  62162. +}
  62163. +
  62164. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  62165. +{
  62166. + return core_if->core_params->otg_ver;
  62167. +}
  62168. +
  62169. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  62170. +{
  62171. + gotgctl_data_t otgctl;
  62172. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62173. + return otgctl.b.hstnegscs;
  62174. +}
  62175. +
  62176. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  62177. +{
  62178. + gotgctl_data_t otgctl;
  62179. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62180. + return otgctl.b.sesreqscs;
  62181. +}
  62182. +
  62183. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  62184. +{
  62185. + if(core_if->otg_ver == 0) {
  62186. + gotgctl_data_t otgctl;
  62187. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62188. + otgctl.b.hnpreq = val;
  62189. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  62190. + } else {
  62191. + core_if->otg_sts = val;
  62192. + }
  62193. +}
  62194. +
  62195. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  62196. +{
  62197. + return core_if->snpsid;
  62198. +}
  62199. +
  62200. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  62201. +{
  62202. + gintsts_data_t gintsts;
  62203. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  62204. + return gintsts.b.curmode;
  62205. +}
  62206. +
  62207. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  62208. +{
  62209. + gusbcfg_data_t usbcfg;
  62210. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62211. + return usbcfg.b.hnpcap;
  62212. +}
  62213. +
  62214. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  62215. +{
  62216. + gusbcfg_data_t usbcfg;
  62217. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62218. + usbcfg.b.hnpcap = val;
  62219. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  62220. +}
  62221. +
  62222. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  62223. +{
  62224. + gusbcfg_data_t usbcfg;
  62225. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62226. + return usbcfg.b.srpcap;
  62227. +}
  62228. +
  62229. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  62230. +{
  62231. + gusbcfg_data_t usbcfg;
  62232. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62233. + usbcfg.b.srpcap = val;
  62234. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  62235. +}
  62236. +
  62237. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  62238. +{
  62239. + dcfg_data_t dcfg;
  62240. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  62241. +
  62242. + dcfg.d32 = -1; //GRAYG
  62243. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  62244. + if (NULL == core_if)
  62245. + DWC_ERROR("reg request with NULL core_if\n");
  62246. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  62247. + core_if, core_if->dev_if);
  62248. + if (NULL == core_if->dev_if)
  62249. + DWC_ERROR("reg request with NULL dev_if\n");
  62250. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  62251. + "dev_global_regs(%p)\n", __func__,
  62252. + core_if, core_if->dev_if,
  62253. + core_if->dev_if->dev_global_regs);
  62254. + if (NULL == core_if->dev_if->dev_global_regs)
  62255. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  62256. + else {
  62257. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  62258. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  62259. + core_if, core_if->dev_if,
  62260. + core_if->dev_if->dev_global_regs,
  62261. + &core_if->dev_if->dev_global_regs->dcfg);
  62262. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  62263. + }
  62264. + return dcfg.b.devspd;
  62265. +}
  62266. +
  62267. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  62268. +{
  62269. + dcfg_data_t dcfg;
  62270. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  62271. + dcfg.b.devspd = val;
  62272. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  62273. +}
  62274. +
  62275. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  62276. +{
  62277. + hprt0_data_t hprt0;
  62278. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  62279. + return hprt0.b.prtconnsts;
  62280. +}
  62281. +
  62282. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  62283. +{
  62284. + dsts_data_t dsts;
  62285. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  62286. + return dsts.b.enumspd;
  62287. +}
  62288. +
  62289. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  62290. +{
  62291. + hprt0_data_t hprt0;
  62292. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  62293. + return hprt0.b.prtpwr;
  62294. +
  62295. +}
  62296. +
  62297. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  62298. +{
  62299. + return core_if->hibernation_suspend;
  62300. +}
  62301. +
  62302. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  62303. +{
  62304. + hprt0_data_t hprt0;
  62305. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62306. + hprt0.b.prtpwr = val;
  62307. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62308. +}
  62309. +
  62310. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  62311. +{
  62312. + hprt0_data_t hprt0;
  62313. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  62314. + return hprt0.b.prtsusp;
  62315. +
  62316. +}
  62317. +
  62318. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  62319. +{
  62320. + hprt0_data_t hprt0;
  62321. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62322. + hprt0.b.prtsusp = val;
  62323. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62324. +}
  62325. +
  62326. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  62327. +{
  62328. + hfir_data_t hfir;
  62329. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  62330. + return hfir.b.frint;
  62331. +
  62332. +}
  62333. +
  62334. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  62335. +{
  62336. + hfir_data_t hfir;
  62337. + uint32_t fram_int;
  62338. + fram_int = calc_frame_interval(core_if);
  62339. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  62340. + if (!core_if->core_params->reload_ctl) {
  62341. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  62342. + "not set to 1.\nShould load driver with reload_ctl=1"
  62343. + " module parameter\n");
  62344. + return;
  62345. + }
  62346. + switch (fram_int) {
  62347. + case 3750:
  62348. + if ((val < 3350) || (val > 4150)) {
  62349. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  62350. + "clock freq should be from 3350 to 4150\n");
  62351. + return;
  62352. + }
  62353. + break;
  62354. + case 30000:
  62355. + if ((val < 26820) || (val > 33180)) {
  62356. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  62357. + "clock freq should be from 26820 to 33180\n");
  62358. + return;
  62359. + }
  62360. + break;
  62361. + case 6000:
  62362. + if ((val < 5360) || (val > 6640)) {
  62363. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  62364. + "clock freq should be from 5360 to 6640\n");
  62365. + return;
  62366. + }
  62367. + break;
  62368. + case 48000:
  62369. + if ((val < 42912) || (val > 53088)) {
  62370. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  62371. + "clock freq should be from 42912 to 53088\n");
  62372. + return;
  62373. + }
  62374. + break;
  62375. + case 7500:
  62376. + if ((val < 6700) || (val > 8300)) {
  62377. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  62378. + "clock freq should be from 6700 to 8300\n");
  62379. + return;
  62380. + }
  62381. + break;
  62382. + case 60000:
  62383. + if ((val < 53640) || (val > 65536)) {
  62384. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  62385. + "clock freq should be from 53640 to 65536\n");
  62386. + return;
  62387. + }
  62388. + break;
  62389. + default:
  62390. + DWC_WARN("Unknown frame interval\n");
  62391. + return;
  62392. + break;
  62393. +
  62394. + }
  62395. + hfir.b.frint = val;
  62396. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  62397. +}
  62398. +
  62399. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  62400. +{
  62401. + hcfg_data_t hcfg;
  62402. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  62403. + return hcfg.b.modechtimen;
  62404. +
  62405. +}
  62406. +
  62407. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  62408. +{
  62409. + hcfg_data_t hcfg;
  62410. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  62411. + hcfg.b.modechtimen = val;
  62412. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  62413. +}
  62414. +
  62415. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  62416. +{
  62417. + hprt0_data_t hprt0;
  62418. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62419. + hprt0.b.prtres = val;
  62420. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62421. +}
  62422. +
  62423. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  62424. +{
  62425. + dctl_data_t dctl;
  62426. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  62427. + return dctl.b.rmtwkupsig;
  62428. +}
  62429. +
  62430. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  62431. +{
  62432. + glpmcfg_data_t lpmcfg;
  62433. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62434. +
  62435. + DWC_ASSERT(!
  62436. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  62437. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  62438. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  62439. +
  62440. + return lpmcfg.b.prt_sleep_sts;
  62441. +}
  62442. +
  62443. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  62444. +{
  62445. + glpmcfg_data_t lpmcfg;
  62446. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62447. + return lpmcfg.b.rem_wkup_en;
  62448. +}
  62449. +
  62450. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  62451. +{
  62452. + glpmcfg_data_t lpmcfg;
  62453. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62454. + return lpmcfg.b.appl_resp;
  62455. +}
  62456. +
  62457. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  62458. +{
  62459. + glpmcfg_data_t lpmcfg;
  62460. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62461. + lpmcfg.b.appl_resp = val;
  62462. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  62463. +}
  62464. +
  62465. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  62466. +{
  62467. + glpmcfg_data_t lpmcfg;
  62468. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62469. + return lpmcfg.b.hsic_connect;
  62470. +}
  62471. +
  62472. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  62473. +{
  62474. + glpmcfg_data_t lpmcfg;
  62475. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62476. + lpmcfg.b.hsic_connect = val;
  62477. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  62478. +}
  62479. +
  62480. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  62481. +{
  62482. + glpmcfg_data_t lpmcfg;
  62483. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62484. + return lpmcfg.b.inv_sel_hsic;
  62485. +
  62486. +}
  62487. +
  62488. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  62489. +{
  62490. + glpmcfg_data_t lpmcfg;
  62491. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62492. + lpmcfg.b.inv_sel_hsic = val;
  62493. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  62494. +}
  62495. +
  62496. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  62497. +{
  62498. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62499. +}
  62500. +
  62501. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  62502. +{
  62503. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  62504. +}
  62505. +
  62506. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  62507. +{
  62508. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62509. +}
  62510. +
  62511. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  62512. +{
  62513. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  62514. +}
  62515. +
  62516. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  62517. +{
  62518. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  62519. +}
  62520. +
  62521. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  62522. +{
  62523. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  62524. +}
  62525. +
  62526. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  62527. +{
  62528. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  62529. +}
  62530. +
  62531. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  62532. +{
  62533. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  62534. +}
  62535. +
  62536. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  62537. +{
  62538. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  62539. +}
  62540. +
  62541. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  62542. +{
  62543. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  62544. +}
  62545. +
  62546. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  62547. +{
  62548. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  62549. +}
  62550. +
  62551. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  62552. +{
  62553. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  62554. +}
  62555. +
  62556. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  62557. +{
  62558. + return DWC_READ_REG32(core_if->host_if->hprt0);
  62559. +
  62560. +}
  62561. +
  62562. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  62563. +{
  62564. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  62565. +}
  62566. +
  62567. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  62568. +{
  62569. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  62570. +}
  62571. +
  62572. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  62573. +{
  62574. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  62575. +}
  62576. +
  62577. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  62578. +{
  62579. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  62580. +}
  62581. +
  62582. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  62583. +{
  62584. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  62585. +}
  62586. +
  62587. +/**
  62588. + * Start the SRP timer to detect when the SRP does not complete within
  62589. + * 6 seconds.
  62590. + *
  62591. + * @param core_if the pointer to core_if strucure.
  62592. + */
  62593. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  62594. +{
  62595. + core_if->srp_timer_started = 1;
  62596. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  62597. +}
  62598. +
  62599. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  62600. +{
  62601. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  62602. + gotgctl_data_t mem;
  62603. + gotgctl_data_t val;
  62604. +
  62605. + val.d32 = DWC_READ_REG32(addr);
  62606. + if (val.b.sesreq) {
  62607. + DWC_ERROR("Session Request Already active!\n");
  62608. + return;
  62609. + }
  62610. +
  62611. + DWC_INFO("Session Request Initated\n"); //NOTICE
  62612. + mem.d32 = DWC_READ_REG32(addr);
  62613. + mem.b.sesreq = 1;
  62614. + DWC_WRITE_REG32(addr, mem.d32);
  62615. +
  62616. + /* Start the SRP timer */
  62617. + dwc_otg_pcd_start_srp_timer(core_if);
  62618. + return;
  62619. +}
  62620. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cil.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  62621. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cil.h 1970-01-01 01:00:00.000000000 +0100
  62622. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.h 2014-03-11 17:51:27.000000000 +0100
  62623. @@ -0,0 +1,1464 @@
  62624. +/* ==========================================================================
  62625. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  62626. + * $Revision: #123 $
  62627. + * $Date: 2012/08/10 $
  62628. + * $Change: 2047372 $
  62629. + *
  62630. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  62631. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  62632. + * otherwise expressly agreed to in writing between Synopsys and you.
  62633. + *
  62634. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  62635. + * any End User Software License Agreement or Agreement for Licensed Product
  62636. + * with Synopsys or any supplement thereto. You are permitted to use and
  62637. + * redistribute this Software in source and binary forms, with or without
  62638. + * modification, provided that redistributions of source code must retain this
  62639. + * notice. You may not view, use, disclose, copy or distribute this file or
  62640. + * any information contained herein except pursuant to this license grant from
  62641. + * Synopsys. If you do not agree with this notice, including the disclaimer
  62642. + * below, then you are not authorized to use the Software.
  62643. + *
  62644. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  62645. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  62646. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  62647. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  62648. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  62649. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  62650. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  62651. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  62652. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  62653. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  62654. + * DAMAGE.
  62655. + * ========================================================================== */
  62656. +
  62657. +#if !defined(__DWC_CIL_H__)
  62658. +#define __DWC_CIL_H__
  62659. +
  62660. +#include "dwc_list.h"
  62661. +#include "dwc_otg_dbg.h"
  62662. +#include "dwc_otg_regs.h"
  62663. +
  62664. +#include "dwc_otg_core_if.h"
  62665. +#include "dwc_otg_adp.h"
  62666. +
  62667. +/**
  62668. + * @file
  62669. + * This file contains the interface to the Core Interface Layer.
  62670. + */
  62671. +
  62672. +#ifdef DWC_UTE_CFI
  62673. +
  62674. +#define MAX_DMA_DESCS_PER_EP 256
  62675. +
  62676. +/**
  62677. + * Enumeration for the data buffer mode
  62678. + */
  62679. +typedef enum _data_buffer_mode {
  62680. + BM_STANDARD = 0, /* data buffer is in normal mode */
  62681. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  62682. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  62683. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  62684. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  62685. +} data_buffer_mode_e;
  62686. +#endif //DWC_UTE_CFI
  62687. +
  62688. +/** Macros defined for DWC OTG HW Release version */
  62689. +
  62690. +#define OTG_CORE_REV_2_60a 0x4F54260A
  62691. +#define OTG_CORE_REV_2_71a 0x4F54271A
  62692. +#define OTG_CORE_REV_2_72a 0x4F54272A
  62693. +#define OTG_CORE_REV_2_80a 0x4F54280A
  62694. +#define OTG_CORE_REV_2_81a 0x4F54281A
  62695. +#define OTG_CORE_REV_2_90a 0x4F54290A
  62696. +#define OTG_CORE_REV_2_91a 0x4F54291A
  62697. +#define OTG_CORE_REV_2_92a 0x4F54292A
  62698. +#define OTG_CORE_REV_2_93a 0x4F54293A
  62699. +#define OTG_CORE_REV_2_94a 0x4F54294A
  62700. +#define OTG_CORE_REV_3_00a 0x4F54300A
  62701. +
  62702. +/**
  62703. + * Information for each ISOC packet.
  62704. + */
  62705. +typedef struct iso_pkt_info {
  62706. + uint32_t offset;
  62707. + uint32_t length;
  62708. + int32_t status;
  62709. +} iso_pkt_info_t;
  62710. +
  62711. +/**
  62712. + * The <code>dwc_ep</code> structure represents the state of a single
  62713. + * endpoint when acting in device mode. It contains the data items
  62714. + * needed for an endpoint to be activated and transfer packets.
  62715. + */
  62716. +typedef struct dwc_ep {
  62717. + /** EP number used for register address lookup */
  62718. + uint8_t num;
  62719. + /** EP direction 0 = OUT */
  62720. + unsigned is_in:1;
  62721. + /** EP active. */
  62722. + unsigned active:1;
  62723. +
  62724. + /**
  62725. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  62726. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  62727. + unsigned tx_fifo_num:4;
  62728. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  62729. + unsigned type:2;
  62730. +#define DWC_OTG_EP_TYPE_CONTROL 0
  62731. +#define DWC_OTG_EP_TYPE_ISOC 1
  62732. +#define DWC_OTG_EP_TYPE_BULK 2
  62733. +#define DWC_OTG_EP_TYPE_INTR 3
  62734. +
  62735. + /** DATA start PID for INTR and BULK EP */
  62736. + unsigned data_pid_start:1;
  62737. + /** Frame (even/odd) for ISOC EP */
  62738. + unsigned even_odd_frame:1;
  62739. + /** Max Packet bytes */
  62740. + unsigned maxpacket:11;
  62741. +
  62742. + /** Max Transfer size */
  62743. + uint32_t maxxfer;
  62744. +
  62745. + /** @name Transfer state */
  62746. + /** @{ */
  62747. +
  62748. + /**
  62749. + * Pointer to the beginning of the transfer buffer -- do not modify
  62750. + * during transfer.
  62751. + */
  62752. +
  62753. + dwc_dma_t dma_addr;
  62754. +
  62755. + dwc_dma_t dma_desc_addr;
  62756. + dwc_otg_dev_dma_desc_t *desc_addr;
  62757. +
  62758. + uint8_t *start_xfer_buff;
  62759. + /** pointer to the transfer buffer */
  62760. + uint8_t *xfer_buff;
  62761. + /** Number of bytes to transfer */
  62762. + unsigned xfer_len:19;
  62763. + /** Number of bytes transferred. */
  62764. + unsigned xfer_count:19;
  62765. + /** Sent ZLP */
  62766. + unsigned sent_zlp:1;
  62767. + /** Total len for control transfer */
  62768. + unsigned total_len:19;
  62769. +
  62770. + /** stall clear flag */
  62771. + unsigned stall_clear_flag:1;
  62772. +
  62773. + /** SETUP pkt cnt rollover flag for EP0 out*/
  62774. + unsigned stp_rollover;
  62775. +
  62776. +#ifdef DWC_UTE_CFI
  62777. + /* The buffer mode */
  62778. + data_buffer_mode_e buff_mode;
  62779. +
  62780. + /* The chain of DMA descriptors.
  62781. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  62782. + */
  62783. + dwc_otg_dma_desc_t *descs;
  62784. +
  62785. + /* The DMA address of the descriptors chain start */
  62786. + dma_addr_t descs_dma_addr;
  62787. + /** This variable stores the length of the last enqueued request */
  62788. + uint32_t cfi_req_len;
  62789. +#endif //DWC_UTE_CFI
  62790. +
  62791. +/** Max DMA Descriptor count for any EP */
  62792. +#define MAX_DMA_DESC_CNT 256
  62793. + /** Allocated DMA Desc count */
  62794. + uint32_t desc_cnt;
  62795. +
  62796. + /** bInterval */
  62797. + uint32_t bInterval;
  62798. + /** Next frame num to setup next ISOC transfer */
  62799. + uint32_t frame_num;
  62800. + /** Indicates SOF number overrun in DSTS */
  62801. + uint8_t frm_overrun;
  62802. +
  62803. +#ifdef DWC_UTE_PER_IO
  62804. + /** Next frame num for which will be setup DMA Desc */
  62805. + uint32_t xiso_frame_num;
  62806. + /** bInterval */
  62807. + uint32_t xiso_bInterval;
  62808. + /** Count of currently active transfers - shall be either 0 or 1 */
  62809. + int xiso_active_xfers;
  62810. + int xiso_queued_xfers;
  62811. +#endif
  62812. +#ifdef DWC_EN_ISOC
  62813. + /**
  62814. + * Variables specific for ISOC EPs
  62815. + *
  62816. + */
  62817. + /** DMA addresses of ISOC buffers */
  62818. + dwc_dma_t dma_addr0;
  62819. + dwc_dma_t dma_addr1;
  62820. +
  62821. + dwc_dma_t iso_dma_desc_addr;
  62822. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  62823. +
  62824. + /** pointer to the transfer buffers */
  62825. + uint8_t *xfer_buff0;
  62826. + uint8_t *xfer_buff1;
  62827. +
  62828. + /** number of ISOC Buffer is processing */
  62829. + uint32_t proc_buf_num;
  62830. + /** Interval of ISOC Buffer processing */
  62831. + uint32_t buf_proc_intrvl;
  62832. + /** Data size for regular frame */
  62833. + uint32_t data_per_frame;
  62834. +
  62835. + /* todo - pattern data support is to be implemented in the future */
  62836. + /** Data size for pattern frame */
  62837. + uint32_t data_pattern_frame;
  62838. + /** Frame number of pattern data */
  62839. + uint32_t sync_frame;
  62840. +
  62841. + /** bInterval */
  62842. + uint32_t bInterval;
  62843. + /** ISO Packet number per frame */
  62844. + uint32_t pkt_per_frm;
  62845. + /** Next frame num for which will be setup DMA Desc */
  62846. + uint32_t next_frame;
  62847. + /** Number of packets per buffer processing */
  62848. + uint32_t pkt_cnt;
  62849. + /** Info for all isoc packets */
  62850. + iso_pkt_info_t *pkt_info;
  62851. + /** current pkt number */
  62852. + uint32_t cur_pkt;
  62853. + /** current pkt number */
  62854. + uint8_t *cur_pkt_addr;
  62855. + /** current pkt number */
  62856. + uint32_t cur_pkt_dma_addr;
  62857. +#endif /* DWC_EN_ISOC */
  62858. +
  62859. +/** @} */
  62860. +} dwc_ep_t;
  62861. +
  62862. +/*
  62863. + * Reasons for halting a host channel.
  62864. + */
  62865. +typedef enum dwc_otg_halt_status {
  62866. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  62867. + DWC_OTG_HC_XFER_COMPLETE,
  62868. + DWC_OTG_HC_XFER_URB_COMPLETE,
  62869. + DWC_OTG_HC_XFER_ACK,
  62870. + DWC_OTG_HC_XFER_NAK,
  62871. + DWC_OTG_HC_XFER_NYET,
  62872. + DWC_OTG_HC_XFER_STALL,
  62873. + DWC_OTG_HC_XFER_XACT_ERR,
  62874. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  62875. + DWC_OTG_HC_XFER_BABBLE_ERR,
  62876. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  62877. + DWC_OTG_HC_XFER_AHB_ERR,
  62878. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  62879. + DWC_OTG_HC_XFER_URB_DEQUEUE
  62880. +} dwc_otg_halt_status_e;
  62881. +
  62882. +/**
  62883. + * Host channel descriptor. This structure represents the state of a single
  62884. + * host channel when acting in host mode. It contains the data items needed to
  62885. + * transfer packets to an endpoint via a host channel.
  62886. + */
  62887. +typedef struct dwc_hc {
  62888. + /** Host channel number used for register address lookup */
  62889. + uint8_t hc_num;
  62890. +
  62891. + /** Device to access */
  62892. + unsigned dev_addr:7;
  62893. +
  62894. + /** EP to access */
  62895. + unsigned ep_num:4;
  62896. +
  62897. + /** EP direction. 0: OUT, 1: IN */
  62898. + unsigned ep_is_in:1;
  62899. +
  62900. + /**
  62901. + * EP speed.
  62902. + * One of the following values:
  62903. + * - DWC_OTG_EP_SPEED_LOW
  62904. + * - DWC_OTG_EP_SPEED_FULL
  62905. + * - DWC_OTG_EP_SPEED_HIGH
  62906. + */
  62907. + unsigned speed:2;
  62908. +#define DWC_OTG_EP_SPEED_LOW 0
  62909. +#define DWC_OTG_EP_SPEED_FULL 1
  62910. +#define DWC_OTG_EP_SPEED_HIGH 2
  62911. +
  62912. + /**
  62913. + * Endpoint type.
  62914. + * One of the following values:
  62915. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  62916. + * - DWC_OTG_EP_TYPE_ISOC: 1
  62917. + * - DWC_OTG_EP_TYPE_BULK: 2
  62918. + * - DWC_OTG_EP_TYPE_INTR: 3
  62919. + */
  62920. + unsigned ep_type:2;
  62921. +
  62922. + /** Max packet size in bytes */
  62923. + unsigned max_packet:11;
  62924. +
  62925. + /**
  62926. + * PID for initial transaction.
  62927. + * 0: DATA0,<br>
  62928. + * 1: DATA2,<br>
  62929. + * 2: DATA1,<br>
  62930. + * 3: MDATA (non-Control EP),
  62931. + * SETUP (Control EP)
  62932. + */
  62933. + unsigned data_pid_start:2;
  62934. +#define DWC_OTG_HC_PID_DATA0 0
  62935. +#define DWC_OTG_HC_PID_DATA2 1
  62936. +#define DWC_OTG_HC_PID_DATA1 2
  62937. +#define DWC_OTG_HC_PID_MDATA 3
  62938. +#define DWC_OTG_HC_PID_SETUP 3
  62939. +
  62940. + /** Number of periodic transactions per (micro)frame */
  62941. + unsigned multi_count:2;
  62942. +
  62943. + /** @name Transfer State */
  62944. + /** @{ */
  62945. +
  62946. + /** Pointer to the current transfer buffer position. */
  62947. + uint8_t *xfer_buff;
  62948. + /**
  62949. + * In Buffer DMA mode this buffer will be used
  62950. + * if xfer_buff is not DWORD aligned.
  62951. + */
  62952. + dwc_dma_t align_buff;
  62953. + /** Total number of bytes to transfer. */
  62954. + uint32_t xfer_len;
  62955. + /** Number of bytes transferred so far. */
  62956. + uint32_t xfer_count;
  62957. + /** Packet count at start of transfer.*/
  62958. + uint16_t start_pkt_count;
  62959. +
  62960. + /**
  62961. + * Flag to indicate whether the transfer has been started. Set to 1 if
  62962. + * it has been started, 0 otherwise.
  62963. + */
  62964. + uint8_t xfer_started;
  62965. +
  62966. + /**
  62967. + * Set to 1 to indicate that a PING request should be issued on this
  62968. + * channel. If 0, process normally.
  62969. + */
  62970. + uint8_t do_ping;
  62971. +
  62972. + /**
  62973. + * Set to 1 to indicate that the error count for this transaction is
  62974. + * non-zero. Set to 0 if the error count is 0.
  62975. + */
  62976. + uint8_t error_state;
  62977. +
  62978. + /**
  62979. + * Set to 1 to indicate that this channel should be halted the next
  62980. + * time a request is queued for the channel. This is necessary in
  62981. + * slave mode if no request queue space is available when an attempt
  62982. + * is made to halt the channel.
  62983. + */
  62984. + uint8_t halt_on_queue;
  62985. +
  62986. + /**
  62987. + * Set to 1 if the host channel has been halted, but the core is not
  62988. + * finished flushing queued requests. Otherwise 0.
  62989. + */
  62990. + uint8_t halt_pending;
  62991. +
  62992. + /**
  62993. + * Reason for halting the host channel.
  62994. + */
  62995. + dwc_otg_halt_status_e halt_status;
  62996. +
  62997. + /*
  62998. + * Split settings for the host channel
  62999. + */
  63000. + uint8_t do_split; /**< Enable split for the channel */
  63001. + uint8_t complete_split; /**< Enable complete split */
  63002. + uint8_t hub_addr; /**< Address of high speed hub */
  63003. +
  63004. + uint8_t port_addr; /**< Port of the low/full speed device */
  63005. + /** Split transaction position
  63006. + * One of the following values:
  63007. + * - DWC_HCSPLIT_XACTPOS_MID
  63008. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  63009. + * - DWC_HCSPLIT_XACTPOS_END
  63010. + * - DWC_HCSPLIT_XACTPOS_ALL */
  63011. + uint8_t xact_pos;
  63012. +
  63013. + /** Set when the host channel does a short read. */
  63014. + uint8_t short_read;
  63015. +
  63016. + /**
  63017. + * Number of requests issued for this channel since it was assigned to
  63018. + * the current transfer (not counting PINGs).
  63019. + */
  63020. + uint8_t requests;
  63021. +
  63022. + /**
  63023. + * Queue Head for the transfer being processed by this channel.
  63024. + */
  63025. + struct dwc_otg_qh *qh;
  63026. +
  63027. + /** @} */
  63028. +
  63029. + /** Entry in list of host channels. */
  63030. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  63031. +
  63032. + /** @name Descriptor DMA support */
  63033. + /** @{ */
  63034. +
  63035. + /** Number of Transfer Descriptors */
  63036. + uint16_t ntd;
  63037. +
  63038. + /** Descriptor List DMA address */
  63039. + dwc_dma_t desc_list_addr;
  63040. +
  63041. + /** Scheduling micro-frame bitmap. */
  63042. + uint8_t schinfo;
  63043. +
  63044. + /** @} */
  63045. +} dwc_hc_t;
  63046. +
  63047. +/**
  63048. + * The following parameters may be specified when starting the module. These
  63049. + * parameters define how the DWC_otg controller should be configured.
  63050. + */
  63051. +typedef struct dwc_otg_core_params {
  63052. + int32_t opt;
  63053. +
  63054. + /**
  63055. + * Specifies the OTG capabilities. The driver will automatically
  63056. + * detect the value for this parameter if none is specified.
  63057. + * 0 - HNP and SRP capable (default)
  63058. + * 1 - SRP Only capable
  63059. + * 2 - No HNP/SRP capable
  63060. + */
  63061. + int32_t otg_cap;
  63062. +
  63063. + /**
  63064. + * Specifies whether to use slave or DMA mode for accessing the data
  63065. + * FIFOs. The driver will automatically detect the value for this
  63066. + * parameter if none is specified.
  63067. + * 0 - Slave
  63068. + * 1 - DMA (default, if available)
  63069. + */
  63070. + int32_t dma_enable;
  63071. +
  63072. + /**
  63073. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  63074. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  63075. + * will automatically detect the value for this if none is specified.
  63076. + * 0 - address DMA
  63077. + * 1 - DMA Descriptor(default, if available)
  63078. + */
  63079. + int32_t dma_desc_enable;
  63080. + /** The DMA Burst size (applicable only for External DMA
  63081. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  63082. + */
  63083. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  63084. +
  63085. + /**
  63086. + * Specifies the maximum speed of operation in host and device mode.
  63087. + * The actual speed depends on the speed of the attached device and
  63088. + * the value of phy_type. The actual speed depends on the speed of the
  63089. + * attached device.
  63090. + * 0 - High Speed (default)
  63091. + * 1 - Full Speed
  63092. + */
  63093. + int32_t speed;
  63094. + /** Specifies whether low power mode is supported when attached
  63095. + * to a Full Speed or Low Speed device in host mode.
  63096. + * 0 - Don't support low power mode (default)
  63097. + * 1 - Support low power mode
  63098. + */
  63099. + int32_t host_support_fs_ls_low_power;
  63100. +
  63101. + /** Specifies the PHY clock rate in low power mode when connected to a
  63102. + * Low Speed device in host mode. This parameter is applicable only if
  63103. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  63104. + * then defaults to 6 MHZ otherwise 48 MHZ.
  63105. + *
  63106. + * 0 - 48 MHz
  63107. + * 1 - 6 MHz
  63108. + */
  63109. + int32_t host_ls_low_power_phy_clk;
  63110. +
  63111. + /**
  63112. + * 0 - Use cC FIFO size parameters
  63113. + * 1 - Allow dynamic FIFO sizing (default)
  63114. + */
  63115. + int32_t enable_dynamic_fifo;
  63116. +
  63117. + /** Total number of 4-byte words in the data FIFO memory. This
  63118. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  63119. + * Tx FIFOs.
  63120. + * 32 to 32768 (default 8192)
  63121. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  63122. + */
  63123. + int32_t data_fifo_size;
  63124. +
  63125. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  63126. + * FIFO sizing is enabled.
  63127. + * 16 to 32768 (default 1064)
  63128. + */
  63129. + int32_t dev_rx_fifo_size;
  63130. +
  63131. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  63132. + * when dynamic FIFO sizing is enabled.
  63133. + * 16 to 32768 (default 1024)
  63134. + */
  63135. + int32_t dev_nperio_tx_fifo_size;
  63136. +
  63137. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  63138. + * mode when dynamic FIFO sizing is enabled.
  63139. + * 4 to 768 (default 256)
  63140. + */
  63141. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  63142. +
  63143. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  63144. + * FIFO sizing is enabled.
  63145. + * 16 to 32768 (default 1024)
  63146. + */
  63147. + int32_t host_rx_fifo_size;
  63148. +
  63149. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  63150. + * when Dynamic FIFO sizing is enabled in the core.
  63151. + * 16 to 32768 (default 1024)
  63152. + */
  63153. + int32_t host_nperio_tx_fifo_size;
  63154. +
  63155. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  63156. + * FIFO sizing is enabled.
  63157. + * 16 to 32768 (default 1024)
  63158. + */
  63159. + int32_t host_perio_tx_fifo_size;
  63160. +
  63161. + /** The maximum transfer size supported in bytes.
  63162. + * 2047 to 65,535 (default 65,535)
  63163. + */
  63164. + int32_t max_transfer_size;
  63165. +
  63166. + /** The maximum number of packets in a transfer.
  63167. + * 15 to 511 (default 511)
  63168. + */
  63169. + int32_t max_packet_count;
  63170. +
  63171. + /** The number of host channel registers to use.
  63172. + * 1 to 16 (default 12)
  63173. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  63174. + */
  63175. + int32_t host_channels;
  63176. +
  63177. + /** The number of endpoints in addition to EP0 available for device
  63178. + * mode operations.
  63179. + * 1 to 15 (default 6 IN and OUT)
  63180. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  63181. + * endpoints in addition to EP0.
  63182. + */
  63183. + int32_t dev_endpoints;
  63184. +
  63185. + /**
  63186. + * Specifies the type of PHY interface to use. By default, the driver
  63187. + * will automatically detect the phy_type.
  63188. + *
  63189. + * 0 - Full Speed PHY
  63190. + * 1 - UTMI+ (default)
  63191. + * 2 - ULPI
  63192. + */
  63193. + int32_t phy_type;
  63194. +
  63195. + /**
  63196. + * Specifies the UTMI+ Data Width. This parameter is
  63197. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  63198. + * PHY_TYPE, this parameter indicates the data width between
  63199. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  63200. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  63201. + * to "8 and 16 bits", meaning that the core has been
  63202. + * configured to work at either data path width.
  63203. + *
  63204. + * 8 or 16 bits (default 16)
  63205. + */
  63206. + int32_t phy_utmi_width;
  63207. +
  63208. + /**
  63209. + * Specifies whether the ULPI operates at double or single
  63210. + * data rate. This parameter is only applicable if PHY_TYPE is
  63211. + * ULPI.
  63212. + *
  63213. + * 0 - single data rate ULPI interface with 8 bit wide data
  63214. + * bus (default)
  63215. + * 1 - double data rate ULPI interface with 4 bit wide data
  63216. + * bus
  63217. + */
  63218. + int32_t phy_ulpi_ddr;
  63219. +
  63220. + /**
  63221. + * Specifies whether to use the internal or external supply to
  63222. + * drive the vbus with a ULPI phy.
  63223. + */
  63224. + int32_t phy_ulpi_ext_vbus;
  63225. +
  63226. + /**
  63227. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  63228. + * parameter is only applicable if PHY_TYPE is FS.
  63229. + * 0 - No (default)
  63230. + * 1 - Yes
  63231. + */
  63232. + int32_t i2c_enable;
  63233. +
  63234. + int32_t ulpi_fs_ls;
  63235. +
  63236. + int32_t ts_dline;
  63237. +
  63238. + /**
  63239. + * Specifies whether dedicated transmit FIFOs are
  63240. + * enabled for non periodic IN endpoints in device mode
  63241. + * 0 - No
  63242. + * 1 - Yes
  63243. + */
  63244. + int32_t en_multiple_tx_fifo;
  63245. +
  63246. + /** Number of 4-byte words in each of the Tx FIFOs in device
  63247. + * mode when dynamic FIFO sizing is enabled.
  63248. + * 4 to 768 (default 256)
  63249. + */
  63250. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  63251. +
  63252. + /** Thresholding enable flag-
  63253. + * bit 0 - enable non-ISO Tx thresholding
  63254. + * bit 1 - enable ISO Tx thresholding
  63255. + * bit 2 - enable Rx thresholding
  63256. + */
  63257. + uint32_t thr_ctl;
  63258. +
  63259. + /** Thresholding length for Tx
  63260. + * FIFOs in 32 bit DWORDs
  63261. + */
  63262. + uint32_t tx_thr_length;
  63263. +
  63264. + /** Thresholding length for Rx
  63265. + * FIFOs in 32 bit DWORDs
  63266. + */
  63267. + uint32_t rx_thr_length;
  63268. +
  63269. + /**
  63270. + * Specifies whether LPM (Link Power Management) support is enabled
  63271. + */
  63272. + int32_t lpm_enable;
  63273. +
  63274. + /** Per Transfer Interrupt
  63275. + * mode enable flag
  63276. + * 1 - Enabled
  63277. + * 0 - Disabled
  63278. + */
  63279. + int32_t pti_enable;
  63280. +
  63281. + /** Multi Processor Interrupt
  63282. + * mode enable flag
  63283. + * 1 - Enabled
  63284. + * 0 - Disabled
  63285. + */
  63286. + int32_t mpi_enable;
  63287. +
  63288. + /** IS_USB Capability
  63289. + * 1 - Enabled
  63290. + * 0 - Disabled
  63291. + */
  63292. + int32_t ic_usb_cap;
  63293. +
  63294. + /** AHB Threshold Ratio
  63295. + * 2'b00 AHB Threshold = MAC Threshold
  63296. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  63297. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  63298. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  63299. + */
  63300. + int32_t ahb_thr_ratio;
  63301. +
  63302. + /** ADP Support
  63303. + * 1 - Enabled
  63304. + * 0 - Disabled
  63305. + */
  63306. + int32_t adp_supp_enable;
  63307. +
  63308. + /** HFIR Reload Control
  63309. + * 0 - The HFIR cannot be reloaded dynamically.
  63310. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  63311. + */
  63312. + int32_t reload_ctl;
  63313. +
  63314. + /** DCFG: Enable device Out NAK
  63315. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  63316. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  63317. + */
  63318. + int32_t dev_out_nak;
  63319. +
  63320. + /** DCFG: Enable Continue on BNA
  63321. + * After receiving BNA interrupt the core disables the endpoint,when the
  63322. + * endpoint is re-enabled by the application the core starts processing
  63323. + * 0 - from the DOEPDMA descriptor
  63324. + * 1 - from the descriptor which received the BNA.
  63325. + */
  63326. + int32_t cont_on_bna;
  63327. +
  63328. + /** GAHBCFG: AHB Single Support
  63329. + * This bit when programmed supports SINGLE transfers for remainder
  63330. + * data in a transfer for DMA mode of operation.
  63331. + * 0 - in this case the remainder data will be sent using INCR burst size.
  63332. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  63333. + */
  63334. + int32_t ahb_single;
  63335. +
  63336. + /** Core Power down mode
  63337. + * 0 - No Power Down is enabled
  63338. + * 1 - Reserved
  63339. + * 2 - Complete Power Down (Hibernation)
  63340. + */
  63341. + int32_t power_down;
  63342. +
  63343. + /** OTG revision supported
  63344. + * 0 - OTG 1.3 revision
  63345. + * 1 - OTG 2.0 revision
  63346. + */
  63347. + int32_t otg_ver;
  63348. +
  63349. +} dwc_otg_core_params_t;
  63350. +
  63351. +#ifdef DEBUG
  63352. +struct dwc_otg_core_if;
  63353. +typedef struct hc_xfer_info {
  63354. + struct dwc_otg_core_if *core_if;
  63355. + dwc_hc_t *hc;
  63356. +} hc_xfer_info_t;
  63357. +#endif
  63358. +
  63359. +typedef struct ep_xfer_info {
  63360. + struct dwc_otg_core_if *core_if;
  63361. + dwc_ep_t *ep;
  63362. + uint8_t state;
  63363. +} ep_xfer_info_t;
  63364. +/*
  63365. + * Device States
  63366. + */
  63367. +typedef enum dwc_otg_lx_state {
  63368. + /** On state */
  63369. + DWC_OTG_L0,
  63370. + /** LPM sleep state*/
  63371. + DWC_OTG_L1,
  63372. + /** USB suspend state*/
  63373. + DWC_OTG_L2,
  63374. + /** Off state*/
  63375. + DWC_OTG_L3
  63376. +} dwc_otg_lx_state_e;
  63377. +
  63378. +struct dwc_otg_global_regs_backup {
  63379. + uint32_t gotgctl_local;
  63380. + uint32_t gintmsk_local;
  63381. + uint32_t gahbcfg_local;
  63382. + uint32_t gusbcfg_local;
  63383. + uint32_t grxfsiz_local;
  63384. + uint32_t gnptxfsiz_local;
  63385. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63386. + uint32_t glpmcfg_local;
  63387. +#endif
  63388. + uint32_t gi2cctl_local;
  63389. + uint32_t hptxfsiz_local;
  63390. + uint32_t pcgcctl_local;
  63391. + uint32_t gdfifocfg_local;
  63392. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  63393. + uint32_t gpwrdn_local;
  63394. + uint32_t xhib_pcgcctl;
  63395. + uint32_t xhib_gpwrdn;
  63396. +};
  63397. +
  63398. +struct dwc_otg_host_regs_backup {
  63399. + uint32_t hcfg_local;
  63400. + uint32_t haintmsk_local;
  63401. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  63402. + uint32_t hprt0_local;
  63403. + uint32_t hfir_local;
  63404. +};
  63405. +
  63406. +struct dwc_otg_dev_regs_backup {
  63407. + uint32_t dcfg;
  63408. + uint32_t dctl;
  63409. + uint32_t daintmsk;
  63410. + uint32_t diepmsk;
  63411. + uint32_t doepmsk;
  63412. + uint32_t diepctl[MAX_EPS_CHANNELS];
  63413. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  63414. + uint32_t diepdma[MAX_EPS_CHANNELS];
  63415. +};
  63416. +/**
  63417. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  63418. + * the DWC_otg controller acting in either host or device mode. It
  63419. + * represents the programming view of the controller as a whole.
  63420. + */
  63421. +struct dwc_otg_core_if {
  63422. + /** Parameters that define how the core should be configured.*/
  63423. + dwc_otg_core_params_t *core_params;
  63424. +
  63425. + /** Core Global registers starting at offset 000h. */
  63426. + dwc_otg_core_global_regs_t *core_global_regs;
  63427. +
  63428. + /** Device-specific information */
  63429. + dwc_otg_dev_if_t *dev_if;
  63430. + /** Host-specific information */
  63431. + dwc_otg_host_if_t *host_if;
  63432. +
  63433. + /** Value from SNPSID register */
  63434. + uint32_t snpsid;
  63435. +
  63436. + /*
  63437. + * Set to 1 if the core PHY interface bits in USBCFG have been
  63438. + * initialized.
  63439. + */
  63440. + uint8_t phy_init_done;
  63441. +
  63442. + /*
  63443. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  63444. + */
  63445. + uint8_t srp_success;
  63446. + uint8_t srp_timer_started;
  63447. + /** Timer for SRP. If it expires before SRP is successful
  63448. + * clear the SRP. */
  63449. + dwc_timer_t *srp_timer;
  63450. +
  63451. +#ifdef DWC_DEV_SRPCAP
  63452. + /* This timer is needed to power on the hibernated host core if SRP is not
  63453. + * initiated on connected SRP capable device for limited period of time
  63454. + */
  63455. + uint8_t pwron_timer_started;
  63456. + dwc_timer_t *pwron_timer;
  63457. +#endif
  63458. + /* Common configuration information */
  63459. + /** Power and Clock Gating Control Register */
  63460. + volatile uint32_t *pcgcctl;
  63461. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  63462. +
  63463. + /** Push/pop addresses for endpoints or host channels.*/
  63464. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  63465. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  63466. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  63467. +
  63468. + /** Total RAM for FIFOs (Bytes) */
  63469. + uint16_t total_fifo_size;
  63470. + /** Size of Rx FIFO (Bytes) */
  63471. + uint16_t rx_fifo_size;
  63472. + /** Size of Non-periodic Tx FIFO (Bytes) */
  63473. + uint16_t nperio_tx_fifo_size;
  63474. +
  63475. + /** 1 if DMA is enabled, 0 otherwise. */
  63476. + uint8_t dma_enable;
  63477. +
  63478. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  63479. + uint8_t dma_desc_enable;
  63480. +
  63481. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  63482. + uint8_t pti_enh_enable;
  63483. +
  63484. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  63485. + uint8_t multiproc_int_enable;
  63486. +
  63487. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  63488. + uint8_t en_multiple_tx_fifo;
  63489. +
  63490. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  63491. + * process of being queued */
  63492. + uint8_t queuing_high_bandwidth;
  63493. +
  63494. + /** Hardware Configuration -- stored here for convenience.*/
  63495. + hwcfg1_data_t hwcfg1;
  63496. + hwcfg2_data_t hwcfg2;
  63497. + hwcfg3_data_t hwcfg3;
  63498. + hwcfg4_data_t hwcfg4;
  63499. + fifosize_data_t hptxfsiz;
  63500. +
  63501. + /** Host and Device Configuration -- stored here for convenience.*/
  63502. + hcfg_data_t hcfg;
  63503. + dcfg_data_t dcfg;
  63504. +
  63505. + /** The operational State, during transations
  63506. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  63507. + * match the core but allows the software to determine
  63508. + * transitions.
  63509. + */
  63510. + uint8_t op_state;
  63511. +
  63512. + /**
  63513. + * Set to 1 if the HCD needs to be restarted on a session request
  63514. + * interrupt. This is required if no connector ID status change has
  63515. + * occurred since the HCD was last disconnected.
  63516. + */
  63517. + uint8_t restart_hcd_on_session_req;
  63518. +
  63519. + /** HCD callbacks */
  63520. + /** A-Device is a_host */
  63521. +#define A_HOST (1)
  63522. + /** A-Device is a_suspend */
  63523. +#define A_SUSPEND (2)
  63524. + /** A-Device is a_peripherial */
  63525. +#define A_PERIPHERAL (3)
  63526. + /** B-Device is operating as a Peripheral. */
  63527. +#define B_PERIPHERAL (4)
  63528. + /** B-Device is operating as a Host. */
  63529. +#define B_HOST (5)
  63530. +
  63531. + /** HCD callbacks */
  63532. + struct dwc_otg_cil_callbacks *hcd_cb;
  63533. + /** PCD callbacks */
  63534. + struct dwc_otg_cil_callbacks *pcd_cb;
  63535. +
  63536. + /** Device mode Periodic Tx FIFO Mask */
  63537. + uint32_t p_tx_msk;
  63538. + /** Device mode Periodic Tx FIFO Mask */
  63539. + uint32_t tx_msk;
  63540. +
  63541. + /** Workqueue object used for handling several interrupts */
  63542. + dwc_workq_t *wq_otg;
  63543. +
  63544. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  63545. + dwc_timer_t *wkp_timer;
  63546. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  63547. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  63548. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  63549. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  63550. +#ifdef DEBUG
  63551. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  63552. +
  63553. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  63554. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  63555. +
  63556. + uint32_t hfnum_7_samples;
  63557. + uint64_t hfnum_7_frrem_accum;
  63558. + uint32_t hfnum_0_samples;
  63559. + uint64_t hfnum_0_frrem_accum;
  63560. + uint32_t hfnum_other_samples;
  63561. + uint64_t hfnum_other_frrem_accum;
  63562. +#endif
  63563. +
  63564. +#ifdef DWC_UTE_CFI
  63565. + uint16_t pwron_rxfsiz;
  63566. + uint16_t pwron_gnptxfsiz;
  63567. + uint16_t pwron_txfsiz[15];
  63568. +
  63569. + uint16_t init_rxfsiz;
  63570. + uint16_t init_gnptxfsiz;
  63571. + uint16_t init_txfsiz[15];
  63572. +#endif
  63573. +
  63574. + /** Lx state of device */
  63575. + dwc_otg_lx_state_e lx_state;
  63576. +
  63577. + /** Saved Core Global registers */
  63578. + struct dwc_otg_global_regs_backup *gr_backup;
  63579. + /** Saved Host registers */
  63580. + struct dwc_otg_host_regs_backup *hr_backup;
  63581. + /** Saved Device registers */
  63582. + struct dwc_otg_dev_regs_backup *dr_backup;
  63583. +
  63584. + /** Power Down Enable */
  63585. + uint32_t power_down;
  63586. +
  63587. + /** ADP support Enable */
  63588. + uint32_t adp_enable;
  63589. +
  63590. + /** ADP structure object */
  63591. + dwc_otg_adp_t adp;
  63592. +
  63593. + /** hibernation/suspend flag */
  63594. + int hibernation_suspend;
  63595. +
  63596. + /** Device mode extended hibernation flag */
  63597. + int xhib;
  63598. +
  63599. + /** OTG revision supported */
  63600. + uint32_t otg_ver;
  63601. +
  63602. + /** OTG status flag used for HNP polling */
  63603. + uint8_t otg_sts;
  63604. +
  63605. + /** Pointer to either hcd->lock or pcd->lock */
  63606. + dwc_spinlock_t *lock;
  63607. +
  63608. + /** Start predict NextEP based on Learning Queue if equal 1,
  63609. + * also used as counter of disabled NP IN EP's */
  63610. + uint8_t start_predict;
  63611. +
  63612. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  63613. + * active, 0xff otherwise */
  63614. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  63615. +
  63616. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  63617. + uint8_t first_in_nextep_seq;
  63618. +
  63619. + /** Frame number while entering to ISR - needed for ISOCs **/
  63620. + uint32_t frame_num;
  63621. +
  63622. +};
  63623. +
  63624. +#ifdef DEBUG
  63625. +/*
  63626. + * This function is called when transfer is timed out.
  63627. + */
  63628. +extern void hc_xfer_timeout(void *ptr);
  63629. +#endif
  63630. +
  63631. +/*
  63632. + * This function is called when transfer is timed out on endpoint.
  63633. + */
  63634. +extern void ep_xfer_timeout(void *ptr);
  63635. +
  63636. +/*
  63637. + * The following functions are functions for works
  63638. + * using during handling some interrupts
  63639. + */
  63640. +extern void w_conn_id_status_change(void *p);
  63641. +
  63642. +extern void w_wakeup_detected(void *p);
  63643. +
  63644. +/** Saves global register values into system memory. */
  63645. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  63646. +/** Saves device register values into system memory. */
  63647. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  63648. +/** Saves host register values into system memory. */
  63649. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  63650. +/** Restore global register values. */
  63651. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  63652. +/** Restore host register values. */
  63653. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  63654. +/** Restore device register values. */
  63655. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  63656. + int rem_wakeup);
  63657. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  63658. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  63659. + int is_host);
  63660. +
  63661. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  63662. + int restore_mode, int reset);
  63663. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  63664. + int rem_wakeup, int reset);
  63665. +
  63666. +/*
  63667. + * The following functions support initialization of the CIL driver component
  63668. + * and the DWC_otg controller.
  63669. + */
  63670. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  63671. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  63672. +
  63673. +/** @name Device CIL Functions
  63674. + * The following functions support managing the DWC_otg controller in device
  63675. + * mode.
  63676. + */
  63677. +/**@{*/
  63678. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  63679. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  63680. + uint32_t * _dest);
  63681. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  63682. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63683. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63684. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63685. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  63686. + dwc_ep_t * _ep);
  63687. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  63688. + dwc_ep_t * _ep);
  63689. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  63690. + dwc_ep_t * _ep);
  63691. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  63692. + dwc_ep_t * _ep);
  63693. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  63694. + dwc_ep_t * _ep, int _dma);
  63695. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63696. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  63697. + dwc_ep_t * _ep);
  63698. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  63699. +
  63700. +#ifdef DWC_EN_ISOC
  63701. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  63702. + dwc_ep_t * ep);
  63703. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  63704. + dwc_ep_t * ep);
  63705. +#endif /* DWC_EN_ISOC */
  63706. +/**@}*/
  63707. +
  63708. +/** @name Host CIL Functions
  63709. + * The following functions support managing the DWC_otg controller in host
  63710. + * mode.
  63711. + */
  63712. +/**@{*/
  63713. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63714. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  63715. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  63716. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63717. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  63718. + dwc_hc_t * _hc);
  63719. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  63720. + dwc_hc_t * _hc);
  63721. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63722. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  63723. + dwc_hc_t * _hc);
  63724. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  63725. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  63726. +
  63727. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  63728. + dwc_hc_t * hc);
  63729. +
  63730. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  63731. +
  63732. +/* Macro used to clear one channel interrupt */
  63733. +#define clear_hc_int(_hc_regs_, _intr_) \
  63734. +do { \
  63735. + hcint_data_t hcint_clear = {.d32 = 0}; \
  63736. + hcint_clear.b._intr_ = 1; \
  63737. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  63738. +} while (0)
  63739. +
  63740. +/*
  63741. + * Macro used to disable one channel interrupt. Channel interrupts are
  63742. + * disabled when the channel is halted or released by the interrupt handler.
  63743. + * There is no need to handle further interrupts of that type until the
  63744. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  63745. + * because the channel structures are cleaned up when the channel is released.
  63746. + */
  63747. +#define disable_hc_int(_hc_regs_, _intr_) \
  63748. +do { \
  63749. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  63750. + hcintmsk.b._intr_ = 1; \
  63751. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  63752. +} while (0)
  63753. +
  63754. +/**
  63755. + * This function Reads HPRT0 in preparation to modify. It keeps the
  63756. + * WC bits 0 so that if they are read as 1, they won't clear when you
  63757. + * write it back
  63758. + */
  63759. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  63760. +{
  63761. + hprt0_data_t hprt0;
  63762. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  63763. + hprt0.b.prtena = 0;
  63764. + hprt0.b.prtconndet = 0;
  63765. + hprt0.b.prtenchng = 0;
  63766. + hprt0.b.prtovrcurrchng = 0;
  63767. + return hprt0.d32;
  63768. +}
  63769. +
  63770. +/**@}*/
  63771. +
  63772. +/** @name Common CIL Functions
  63773. + * The following functions support managing the DWC_otg controller in either
  63774. + * device or host mode.
  63775. + */
  63776. +/**@{*/
  63777. +
  63778. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  63779. + uint8_t * dest, uint16_t bytes);
  63780. +
  63781. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  63782. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  63783. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  63784. +
  63785. +/**
  63786. + * This function returns the Core Interrupt register.
  63787. + */
  63788. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  63789. +{
  63790. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  63791. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  63792. +}
  63793. +
  63794. +/**
  63795. + * This function returns the OTG Interrupt register.
  63796. + */
  63797. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  63798. +{
  63799. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  63800. +}
  63801. +
  63802. +/**
  63803. + * This function reads the Device All Endpoints Interrupt register and
  63804. + * returns the IN endpoint interrupt bits.
  63805. + */
  63806. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  63807. + core_if)
  63808. +{
  63809. +
  63810. + uint32_t v;
  63811. +
  63812. + if (core_if->multiproc_int_enable) {
  63813. + v = DWC_READ_REG32(&core_if->dev_if->
  63814. + dev_global_regs->deachint) &
  63815. + DWC_READ_REG32(&core_if->
  63816. + dev_if->dev_global_regs->deachintmsk);
  63817. + } else {
  63818. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  63819. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  63820. + }
  63821. + return (v & 0xffff);
  63822. +}
  63823. +
  63824. +/**
  63825. + * This function reads the Device All Endpoints Interrupt register and
  63826. + * returns the OUT endpoint interrupt bits.
  63827. + */
  63828. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  63829. + core_if)
  63830. +{
  63831. + uint32_t v;
  63832. +
  63833. + if (core_if->multiproc_int_enable) {
  63834. + v = DWC_READ_REG32(&core_if->dev_if->
  63835. + dev_global_regs->deachint) &
  63836. + DWC_READ_REG32(&core_if->
  63837. + dev_if->dev_global_regs->deachintmsk);
  63838. + } else {
  63839. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  63840. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  63841. + }
  63842. +
  63843. + return ((v & 0xffff0000) >> 16);
  63844. +}
  63845. +
  63846. +/**
  63847. + * This function returns the Device IN EP Interrupt register
  63848. + */
  63849. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  63850. + dwc_ep_t * ep)
  63851. +{
  63852. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  63853. + uint32_t v, msk, emp;
  63854. +
  63855. + if (core_if->multiproc_int_enable) {
  63856. + msk =
  63857. + DWC_READ_REG32(&dev_if->
  63858. + dev_global_regs->diepeachintmsk[ep->num]);
  63859. + emp =
  63860. + DWC_READ_REG32(&dev_if->
  63861. + dev_global_regs->dtknqr4_fifoemptymsk);
  63862. + msk |= ((emp >> ep->num) & 0x1) << 7;
  63863. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  63864. + } else {
  63865. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  63866. + emp =
  63867. + DWC_READ_REG32(&dev_if->
  63868. + dev_global_regs->dtknqr4_fifoemptymsk);
  63869. + msk |= ((emp >> ep->num) & 0x1) << 7;
  63870. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  63871. + }
  63872. +
  63873. + return v;
  63874. +}
  63875. +
  63876. +/**
  63877. + * This function returns the Device OUT EP Interrupt register
  63878. + */
  63879. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  63880. + _core_if, dwc_ep_t * _ep)
  63881. +{
  63882. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  63883. + uint32_t v;
  63884. + doepmsk_data_t msk = {.d32 = 0 };
  63885. +
  63886. + if (_core_if->multiproc_int_enable) {
  63887. + msk.d32 =
  63888. + DWC_READ_REG32(&dev_if->
  63889. + dev_global_regs->doepeachintmsk[_ep->num]);
  63890. + if (_core_if->pti_enh_enable) {
  63891. + msk.b.pktdrpsts = 1;
  63892. + }
  63893. + v = DWC_READ_REG32(&dev_if->
  63894. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  63895. + } else {
  63896. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  63897. + if (_core_if->pti_enh_enable) {
  63898. + msk.b.pktdrpsts = 1;
  63899. + }
  63900. + v = DWC_READ_REG32(&dev_if->
  63901. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  63902. + }
  63903. + return v;
  63904. +}
  63905. +
  63906. +/**
  63907. + * This function returns the Host All Channel Interrupt register
  63908. + */
  63909. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  63910. + _core_if)
  63911. +{
  63912. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  63913. +}
  63914. +
  63915. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  63916. + _core_if, dwc_hc_t * _hc)
  63917. +{
  63918. + return (DWC_READ_REG32
  63919. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  63920. +}
  63921. +
  63922. +/**
  63923. + * This function returns the mode of the operation, host or device.
  63924. + *
  63925. + * @return 0 - Device Mode, 1 - Host Mode
  63926. + */
  63927. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  63928. +{
  63929. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  63930. +}
  63931. +
  63932. +/**@}*/
  63933. +
  63934. +/**
  63935. + * DWC_otg CIL callback structure. This structure allows the HCD and
  63936. + * PCD to register functions used for starting and stopping the PCD
  63937. + * and HCD for role change on for a DRD.
  63938. + */
  63939. +typedef struct dwc_otg_cil_callbacks {
  63940. + /** Start function for role change */
  63941. + int (*start) (void *_p);
  63942. + /** Stop Function for role change */
  63943. + int (*stop) (void *_p);
  63944. + /** Disconnect Function for role change */
  63945. + int (*disconnect) (void *_p);
  63946. + /** Resume/Remote wakeup Function */
  63947. + int (*resume_wakeup) (void *_p);
  63948. + /** Suspend function */
  63949. + int (*suspend) (void *_p);
  63950. + /** Session Start (SRP) */
  63951. + int (*session_start) (void *_p);
  63952. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63953. + /** Sleep (switch to L0 state) */
  63954. + int (*sleep) (void *_p);
  63955. +#endif
  63956. + /** Pointer passed to start() and stop() */
  63957. + void *p;
  63958. +} dwc_otg_cil_callbacks_t;
  63959. +
  63960. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  63961. + dwc_otg_cil_callbacks_t * _cb,
  63962. + void *_p);
  63963. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  63964. + dwc_otg_cil_callbacks_t * _cb,
  63965. + void *_p);
  63966. +
  63967. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  63968. +
  63969. +//////////////////////////////////////////////////////////////////////
  63970. +/** Start the HCD. Helper function for using the HCD callbacks.
  63971. + *
  63972. + * @param core_if Programming view of DWC_otg controller.
  63973. + */
  63974. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  63975. +{
  63976. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  63977. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  63978. + }
  63979. +}
  63980. +
  63981. +/** Stop the HCD. Helper function for using the HCD callbacks.
  63982. + *
  63983. + * @param core_if Programming view of DWC_otg controller.
  63984. + */
  63985. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  63986. +{
  63987. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  63988. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  63989. + }
  63990. +}
  63991. +
  63992. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  63993. + *
  63994. + * @param core_if Programming view of DWC_otg controller.
  63995. + */
  63996. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  63997. +{
  63998. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  63999. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  64000. + }
  64001. +}
  64002. +
  64003. +/** Inform the HCD the a New Session has begun. Helper function for
  64004. + * using the HCD callbacks.
  64005. + *
  64006. + * @param core_if Programming view of DWC_otg controller.
  64007. + */
  64008. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  64009. +{
  64010. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  64011. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  64012. + }
  64013. +}
  64014. +
  64015. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64016. +/**
  64017. + * Inform the HCD about LPM sleep.
  64018. + * Helper function for using the HCD callbacks.
  64019. + *
  64020. + * @param core_if Programming view of DWC_otg controller.
  64021. + */
  64022. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  64023. +{
  64024. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  64025. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  64026. + }
  64027. +}
  64028. +#endif
  64029. +
  64030. +/** Resume the HCD. Helper function for using the HCD callbacks.
  64031. + *
  64032. + * @param core_if Programming view of DWC_otg controller.
  64033. + */
  64034. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  64035. +{
  64036. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  64037. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  64038. + }
  64039. +}
  64040. +
  64041. +/** Start the PCD. Helper function for using the PCD callbacks.
  64042. + *
  64043. + * @param core_if Programming view of DWC_otg controller.
  64044. + */
  64045. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  64046. +{
  64047. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  64048. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  64049. + }
  64050. +}
  64051. +
  64052. +/** Stop the PCD. Helper function for using the PCD callbacks.
  64053. + *
  64054. + * @param core_if Programming view of DWC_otg controller.
  64055. + */
  64056. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  64057. +{
  64058. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  64059. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  64060. + }
  64061. +}
  64062. +
  64063. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  64064. + *
  64065. + * @param core_if Programming view of DWC_otg controller.
  64066. + */
  64067. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  64068. +{
  64069. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  64070. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  64071. + }
  64072. +}
  64073. +
  64074. +/** Resume the PCD. Helper function for using the PCD callbacks.
  64075. + *
  64076. + * @param core_if Programming view of DWC_otg controller.
  64077. + */
  64078. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  64079. +{
  64080. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  64081. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  64082. + }
  64083. +}
  64084. +
  64085. +//////////////////////////////////////////////////////////////////////
  64086. +
  64087. +#endif
  64088. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  64089. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 1970-01-01 01:00:00.000000000 +0100
  64090. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 2014-03-11 17:51:27.000000000 +0100
  64091. @@ -0,0 +1,1588 @@
  64092. +/* ==========================================================================
  64093. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  64094. + * $Revision: #32 $
  64095. + * $Date: 2012/08/10 $
  64096. + * $Change: 2047372 $
  64097. + *
  64098. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  64099. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  64100. + * otherwise expressly agreed to in writing between Synopsys and you.
  64101. + *
  64102. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  64103. + * any End User Software License Agreement or Agreement for Licensed Product
  64104. + * with Synopsys or any supplement thereto. You are permitted to use and
  64105. + * redistribute this Software in source and binary forms, with or without
  64106. + * modification, provided that redistributions of source code must retain this
  64107. + * notice. You may not view, use, disclose, copy or distribute this file or
  64108. + * any information contained herein except pursuant to this license grant from
  64109. + * Synopsys. If you do not agree with this notice, including the disclaimer
  64110. + * below, then you are not authorized to use the Software.
  64111. + *
  64112. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  64113. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  64114. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  64115. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  64116. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  64117. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  64118. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  64119. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  64120. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  64121. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  64122. + * DAMAGE.
  64123. + * ========================================================================== */
  64124. +
  64125. +/** @file
  64126. + *
  64127. + * The Core Interface Layer provides basic services for accessing and
  64128. + * managing the DWC_otg hardware. These services are used by both the
  64129. + * Host Controller Driver and the Peripheral Controller Driver.
  64130. + *
  64131. + * This file contains the Common Interrupt handlers.
  64132. + */
  64133. +#include "dwc_os.h"
  64134. +#include "dwc_otg_regs.h"
  64135. +#include "dwc_otg_cil.h"
  64136. +#include "dwc_otg_driver.h"
  64137. +#include "dwc_otg_pcd.h"
  64138. +#include "dwc_otg_hcd.h"
  64139. +#include "dwc_otg_mphi_fix.h"
  64140. +
  64141. +#ifdef DEBUG
  64142. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  64143. +{
  64144. + return (core_if->op_state == A_HOST ? "a_host" :
  64145. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  64146. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  64147. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  64148. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  64149. +}
  64150. +#endif
  64151. +
  64152. +/** This function will log a debug message
  64153. + *
  64154. + * @param core_if Programming view of DWC_otg controller.
  64155. + */
  64156. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  64157. +{
  64158. + gintsts_data_t gintsts;
  64159. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  64160. + dwc_otg_mode(core_if) ? "Host" : "Device");
  64161. +
  64162. + /* Clear interrupt */
  64163. + gintsts.d32 = 0;
  64164. + gintsts.b.modemismatch = 1;
  64165. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64166. + return 1;
  64167. +}
  64168. +
  64169. +/**
  64170. + * This function handles the OTG Interrupts. It reads the OTG
  64171. + * Interrupt Register (GOTGINT) to determine what interrupt has
  64172. + * occurred.
  64173. + *
  64174. + * @param core_if Programming view of DWC_otg controller.
  64175. + */
  64176. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  64177. +{
  64178. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  64179. + gotgint_data_t gotgint;
  64180. + gotgctl_data_t gotgctl;
  64181. + gintmsk_data_t gintmsk;
  64182. + gpwrdn_data_t gpwrdn;
  64183. +
  64184. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  64185. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64186. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  64187. + op_state_str(core_if));
  64188. +
  64189. + if (gotgint.b.sesenddet) {
  64190. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64191. + "Session End Detected++ (%s)\n",
  64192. + op_state_str(core_if));
  64193. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64194. +
  64195. + if (core_if->op_state == B_HOST) {
  64196. + cil_pcd_start(core_if);
  64197. + core_if->op_state = B_PERIPHERAL;
  64198. + } else {
  64199. + /* If not B_HOST and Device HNP still set. HNP
  64200. + * Did not succeed!*/
  64201. + if (gotgctl.b.devhnpen) {
  64202. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  64203. + __DWC_ERROR("Device Not Connected/Responding!\n");
  64204. + }
  64205. +
  64206. + /* If Session End Detected the B-Cable has
  64207. + * been disconnected. */
  64208. + /* Reset PCD and Gadget driver to a
  64209. + * clean state. */
  64210. + core_if->lx_state = DWC_OTG_L0;
  64211. + DWC_SPINUNLOCK(core_if->lock);
  64212. + cil_pcd_stop(core_if);
  64213. + DWC_SPINLOCK(core_if->lock);
  64214. +
  64215. + if (core_if->adp_enable) {
  64216. + if (core_if->power_down == 2) {
  64217. + gpwrdn.d32 = 0;
  64218. + gpwrdn.b.pwrdnswtch = 1;
  64219. + DWC_MODIFY_REG32(&core_if->
  64220. + core_global_regs->
  64221. + gpwrdn, gpwrdn.d32, 0);
  64222. + }
  64223. +
  64224. + gpwrdn.d32 = 0;
  64225. + gpwrdn.b.pmuintsel = 1;
  64226. + gpwrdn.b.pmuactv = 1;
  64227. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  64228. + gpwrdn, 0, gpwrdn.d32);
  64229. +
  64230. + dwc_otg_adp_sense_start(core_if);
  64231. + }
  64232. + }
  64233. +
  64234. + gotgctl.d32 = 0;
  64235. + gotgctl.b.devhnpen = 1;
  64236. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  64237. + }
  64238. + if (gotgint.b.sesreqsucstschng) {
  64239. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64240. + "Session Reqeust Success Status Change++\n");
  64241. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64242. + if (gotgctl.b.sesreqscs) {
  64243. +
  64244. + if ((core_if->core_params->phy_type ==
  64245. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  64246. + core_if->srp_success = 1;
  64247. + } else {
  64248. + DWC_SPINUNLOCK(core_if->lock);
  64249. + cil_pcd_resume(core_if);
  64250. + DWC_SPINLOCK(core_if->lock);
  64251. + /* Clear Session Request */
  64252. + gotgctl.d32 = 0;
  64253. + gotgctl.b.sesreq = 1;
  64254. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  64255. + gotgctl.d32, 0);
  64256. + }
  64257. + }
  64258. + }
  64259. + if (gotgint.b.hstnegsucstschng) {
  64260. + /* Print statements during the HNP interrupt handling
  64261. + * can cause it to fail.*/
  64262. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64263. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  64264. + * this does not help*/
  64265. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  64266. + dwc_udelay(100);
  64267. + if (gotgctl.b.hstnegscs) {
  64268. + if (dwc_otg_is_host_mode(core_if)) {
  64269. + core_if->op_state = B_HOST;
  64270. + /*
  64271. + * Need to disable SOF interrupt immediately.
  64272. + * When switching from device to host, the PCD
  64273. + * interrupt handler won't handle the
  64274. + * interrupt if host mode is already set. The
  64275. + * HCD interrupt handler won't get called if
  64276. + * the HCD state is HALT. This means that the
  64277. + * interrupt does not get handled and Linux
  64278. + * complains loudly.
  64279. + */
  64280. + gintmsk.d32 = 0;
  64281. + gintmsk.b.sofintr = 1;
  64282. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  64283. + gintmsk.d32, 0);
  64284. + /* Call callback function with spin lock released */
  64285. + DWC_SPINUNLOCK(core_if->lock);
  64286. + cil_pcd_stop(core_if);
  64287. + /*
  64288. + * Initialize the Core for Host mode.
  64289. + */
  64290. + cil_hcd_start(core_if);
  64291. + DWC_SPINLOCK(core_if->lock);
  64292. + core_if->op_state = B_HOST;
  64293. + }
  64294. + } else {
  64295. + gotgctl.d32 = 0;
  64296. + gotgctl.b.hnpreq = 1;
  64297. + gotgctl.b.devhnpen = 1;
  64298. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  64299. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  64300. + __DWC_ERROR("Device Not Connected/Responding\n");
  64301. + }
  64302. + }
  64303. + if (gotgint.b.hstnegdet) {
  64304. + /* The disconnect interrupt is set at the same time as
  64305. + * Host Negotiation Detected. During the mode
  64306. + * switch all interrupts are cleared so the disconnect
  64307. + * interrupt handler will not get executed.
  64308. + */
  64309. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64310. + "Host Negotiation Detected++ (%s)\n",
  64311. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  64312. + "Device"));
  64313. + if (dwc_otg_is_device_mode(core_if)) {
  64314. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  64315. + core_if->op_state);
  64316. + DWC_SPINUNLOCK(core_if->lock);
  64317. + cil_hcd_disconnect(core_if);
  64318. + cil_pcd_start(core_if);
  64319. + DWC_SPINLOCK(core_if->lock);
  64320. + core_if->op_state = A_PERIPHERAL;
  64321. + } else {
  64322. + /*
  64323. + * Need to disable SOF interrupt immediately. When
  64324. + * switching from device to host, the PCD interrupt
  64325. + * handler won't handle the interrupt if host mode is
  64326. + * already set. The HCD interrupt handler won't get
  64327. + * called if the HCD state is HALT. This means that
  64328. + * the interrupt does not get handled and Linux
  64329. + * complains loudly.
  64330. + */
  64331. + gintmsk.d32 = 0;
  64332. + gintmsk.b.sofintr = 1;
  64333. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  64334. + DWC_SPINUNLOCK(core_if->lock);
  64335. + cil_pcd_stop(core_if);
  64336. + cil_hcd_start(core_if);
  64337. + DWC_SPINLOCK(core_if->lock);
  64338. + core_if->op_state = A_HOST;
  64339. + }
  64340. + }
  64341. + if (gotgint.b.adevtoutchng) {
  64342. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64343. + "A-Device Timeout Change++\n");
  64344. + }
  64345. + if (gotgint.b.debdone) {
  64346. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  64347. + }
  64348. +
  64349. + /* Clear GOTGINT */
  64350. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  64351. +
  64352. + return 1;
  64353. +}
  64354. +
  64355. +void w_conn_id_status_change(void *p)
  64356. +{
  64357. + dwc_otg_core_if_t *core_if = p;
  64358. + uint32_t count = 0;
  64359. + gotgctl_data_t gotgctl = {.d32 = 0 };
  64360. +
  64361. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  64362. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  64363. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  64364. +
  64365. + /* B-Device connector (Device Mode) */
  64366. + if (gotgctl.b.conidsts) {
  64367. + /* Wait for switch to device mode. */
  64368. + while (!dwc_otg_is_device_mode(core_if)) {
  64369. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  64370. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  64371. + "Peripheral"));
  64372. + dwc_mdelay(100);
  64373. + if (++count > 10000)
  64374. + break;
  64375. + }
  64376. + DWC_ASSERT(++count < 10000,
  64377. + "Connection id status change timed out");
  64378. + core_if->op_state = B_PERIPHERAL;
  64379. + dwc_otg_core_init(core_if);
  64380. + dwc_otg_enable_global_interrupts(core_if);
  64381. + cil_pcd_start(core_if);
  64382. + } else {
  64383. + /* A-Device connector (Host Mode) */
  64384. + while (!dwc_otg_is_host_mode(core_if)) {
  64385. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  64386. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  64387. + "Peripheral"));
  64388. + dwc_mdelay(100);
  64389. + if (++count > 10000)
  64390. + break;
  64391. + }
  64392. + DWC_ASSERT(++count < 10000,
  64393. + "Connection id status change timed out");
  64394. + core_if->op_state = A_HOST;
  64395. + /*
  64396. + * Initialize the Core for Host mode.
  64397. + */
  64398. + dwc_otg_core_init(core_if);
  64399. + dwc_otg_enable_global_interrupts(core_if);
  64400. + cil_hcd_start(core_if);
  64401. + }
  64402. +}
  64403. +
  64404. +/**
  64405. + * This function handles the Connector ID Status Change Interrupt. It
  64406. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  64407. + * is a Device to Host Mode transition or a Host Mode to Device
  64408. + * Transition.
  64409. + *
  64410. + * This only occurs when the cable is connected/removed from the PHY
  64411. + * connector.
  64412. + *
  64413. + * @param core_if Programming view of DWC_otg controller.
  64414. + */
  64415. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  64416. +{
  64417. +
  64418. + /*
  64419. + * Need to disable SOF interrupt immediately. If switching from device
  64420. + * to host, the PCD interrupt handler won't handle the interrupt if
  64421. + * host mode is already set. The HCD interrupt handler won't get
  64422. + * called if the HCD state is HALT. This means that the interrupt does
  64423. + * not get handled and Linux complains loudly.
  64424. + */
  64425. + gintmsk_data_t gintmsk = {.d32 = 0 };
  64426. + gintsts_data_t gintsts = {.d32 = 0 };
  64427. +
  64428. + gintmsk.b.sofintr = 1;
  64429. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  64430. +
  64431. + DWC_DEBUGPL(DBG_CIL,
  64432. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  64433. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  64434. +
  64435. + DWC_SPINUNLOCK(core_if->lock);
  64436. +
  64437. + /*
  64438. + * Need to schedule a work, as there are possible DELAY function calls
  64439. + * Release lock before scheduling workq as it holds spinlock during scheduling
  64440. + */
  64441. +
  64442. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  64443. + core_if, "connection id status change");
  64444. + DWC_SPINLOCK(core_if->lock);
  64445. +
  64446. + /* Set flag and clear interrupt */
  64447. + gintsts.b.conidstschng = 1;
  64448. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64449. +
  64450. + return 1;
  64451. +}
  64452. +
  64453. +/**
  64454. + * This interrupt indicates that a device is initiating the Session
  64455. + * Request Protocol to request the host to turn on bus power so a new
  64456. + * session can begin. The handler responds by turning on bus power. If
  64457. + * the DWC_otg controller is in low power mode, the handler brings the
  64458. + * controller out of low power mode before turning on bus power.
  64459. + *
  64460. + * @param core_if Programming view of DWC_otg controller.
  64461. + */
  64462. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  64463. +{
  64464. + gintsts_data_t gintsts;
  64465. +
  64466. +#ifndef DWC_HOST_ONLY
  64467. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  64468. +
  64469. + if (dwc_otg_is_device_mode(core_if)) {
  64470. + DWC_PRINTF("SRP: Device mode\n");
  64471. + } else {
  64472. + hprt0_data_t hprt0;
  64473. + DWC_PRINTF("SRP: Host mode\n");
  64474. +
  64475. + /* Turn on the port power bit. */
  64476. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  64477. + hprt0.b.prtpwr = 1;
  64478. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64479. +
  64480. + /* Start the Connection timer. So a message can be displayed
  64481. + * if connect does not occur within 10 seconds. */
  64482. + cil_hcd_session_start(core_if);
  64483. + }
  64484. +#endif
  64485. +
  64486. + /* Clear interrupt */
  64487. + gintsts.d32 = 0;
  64488. + gintsts.b.sessreqintr = 1;
  64489. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64490. +
  64491. + return 1;
  64492. +}
  64493. +
  64494. +void w_wakeup_detected(void *p)
  64495. +{
  64496. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  64497. + /*
  64498. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  64499. + * so that OPT tests pass with all PHYs).
  64500. + */
  64501. + hprt0_data_t hprt0 = {.d32 = 0 };
  64502. +#if 0
  64503. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64504. + /* Restart the Phy Clock */
  64505. + pcgcctl.b.stoppclk = 1;
  64506. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  64507. + dwc_udelay(10);
  64508. +#endif //0
  64509. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  64510. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  64511. +// dwc_mdelay(70);
  64512. + hprt0.b.prtres = 0; /* Resume */
  64513. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64514. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  64515. + DWC_READ_REG32(core_if->host_if->hprt0));
  64516. +
  64517. + cil_hcd_resume(core_if);
  64518. +
  64519. + /** Change to L0 state*/
  64520. + core_if->lx_state = DWC_OTG_L0;
  64521. +}
  64522. +
  64523. +/**
  64524. + * This interrupt indicates that the DWC_otg controller has detected a
  64525. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  64526. + * low power mode, the handler must brings the controller out of low
  64527. + * power mode. The controller automatically begins resume
  64528. + * signaling. The handler schedules a time to stop resume signaling.
  64529. + */
  64530. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  64531. +{
  64532. + gintsts_data_t gintsts;
  64533. +
  64534. + DWC_DEBUGPL(DBG_ANY,
  64535. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  64536. +
  64537. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  64538. +
  64539. + if (dwc_otg_is_device_mode(core_if)) {
  64540. + dctl_data_t dctl = {.d32 = 0 };
  64541. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  64542. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  64543. + dsts));
  64544. + if (core_if->lx_state == DWC_OTG_L2) {
  64545. +#ifdef PARTIAL_POWER_DOWN
  64546. + if (core_if->hwcfg4.b.power_optimiz) {
  64547. + pcgcctl_data_t power = {.d32 = 0 };
  64548. +
  64549. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  64550. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  64551. + power.d32);
  64552. +
  64553. + power.b.stoppclk = 0;
  64554. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  64555. +
  64556. + power.b.pwrclmp = 0;
  64557. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  64558. +
  64559. + power.b.rstpdwnmodule = 0;
  64560. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  64561. + }
  64562. +#endif
  64563. + /* Clear the Remote Wakeup Signaling */
  64564. + dctl.b.rmtwkupsig = 1;
  64565. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  64566. + dctl, dctl.d32, 0);
  64567. +
  64568. + DWC_SPINUNLOCK(core_if->lock);
  64569. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  64570. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  64571. + }
  64572. + DWC_SPINLOCK(core_if->lock);
  64573. + } else {
  64574. + glpmcfg_data_t lpmcfg;
  64575. + lpmcfg.d32 =
  64576. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  64577. + lpmcfg.b.hird_thres &= (~(1 << 4));
  64578. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  64579. + lpmcfg.d32);
  64580. + }
  64581. + /** Change to L0 state*/
  64582. + core_if->lx_state = DWC_OTG_L0;
  64583. + } else {
  64584. + if (core_if->lx_state != DWC_OTG_L1) {
  64585. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64586. +
  64587. + /* Restart the Phy Clock */
  64588. + pcgcctl.b.stoppclk = 1;
  64589. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  64590. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  64591. + } else {
  64592. + /** Change to L0 state*/
  64593. + core_if->lx_state = DWC_OTG_L0;
  64594. + }
  64595. + }
  64596. +
  64597. + /* Clear interrupt */
  64598. + gintsts.d32 = 0;
  64599. + gintsts.b.wkupintr = 1;
  64600. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64601. +
  64602. + return 1;
  64603. +}
  64604. +
  64605. +/**
  64606. + * This interrupt indicates that the Wakeup Logic has detected a
  64607. + * Device disconnect.
  64608. + */
  64609. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  64610. +{
  64611. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  64612. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  64613. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64614. +
  64615. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64616. +
  64617. + if (!core_if->hibernation_suspend) {
  64618. + DWC_PRINTF("Already exited from Hibernation\n");
  64619. + return 1;
  64620. + }
  64621. +
  64622. + /* Switch on the voltage to the core */
  64623. + gpwrdn.b.pwrdnswtch = 1;
  64624. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64625. + dwc_udelay(10);
  64626. +
  64627. + /* Reset the core */
  64628. + gpwrdn.d32 = 0;
  64629. + gpwrdn.b.pwrdnrstn = 1;
  64630. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64631. + dwc_udelay(10);
  64632. +
  64633. + /* Disable power clamps*/
  64634. + gpwrdn.d32 = 0;
  64635. + gpwrdn.b.pwrdnclmp = 1;
  64636. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64637. +
  64638. + /* Remove reset the core signal */
  64639. + gpwrdn.d32 = 0;
  64640. + gpwrdn.b.pwrdnrstn = 1;
  64641. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64642. + dwc_udelay(10);
  64643. +
  64644. + /* Disable PMU interrupt */
  64645. + gpwrdn.d32 = 0;
  64646. + gpwrdn.b.pmuintsel = 1;
  64647. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64648. +
  64649. + core_if->hibernation_suspend = 0;
  64650. +
  64651. + /* Disable PMU */
  64652. + gpwrdn.d32 = 0;
  64653. + gpwrdn.b.pmuactv = 1;
  64654. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64655. + dwc_udelay(10);
  64656. +
  64657. + if (gpwrdn_temp.b.idsts) {
  64658. + core_if->op_state = B_PERIPHERAL;
  64659. + dwc_otg_core_init(core_if);
  64660. + dwc_otg_enable_global_interrupts(core_if);
  64661. + cil_pcd_start(core_if);
  64662. + } else {
  64663. + core_if->op_state = A_HOST;
  64664. + dwc_otg_core_init(core_if);
  64665. + dwc_otg_enable_global_interrupts(core_if);
  64666. + cil_hcd_start(core_if);
  64667. + }
  64668. +
  64669. + return 1;
  64670. +}
  64671. +
  64672. +/**
  64673. + * This interrupt indicates that the Wakeup Logic has detected a
  64674. + * remote wakeup sequence.
  64675. + */
  64676. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  64677. +{
  64678. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64679. + DWC_DEBUGPL(DBG_ANY,
  64680. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  64681. +
  64682. + if (!core_if->hibernation_suspend) {
  64683. + DWC_PRINTF("Already exited from Hibernation\n");
  64684. + return 1;
  64685. + }
  64686. +
  64687. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64688. + if (gpwrdn.b.idsts) { // Device Mode
  64689. + if ((core_if->power_down == 2)
  64690. + && (core_if->hibernation_suspend == 1)) {
  64691. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  64692. + }
  64693. + } else {
  64694. + if ((core_if->power_down == 2)
  64695. + && (core_if->hibernation_suspend == 1)) {
  64696. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  64697. + }
  64698. + }
  64699. + return 1;
  64700. +}
  64701. +
  64702. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  64703. +{
  64704. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64705. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  64706. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  64707. +
  64708. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  64709. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64710. + if (core_if->power_down == 2) {
  64711. + if (!core_if->hibernation_suspend) {
  64712. + DWC_PRINTF("Already exited from Hibernation\n");
  64713. + return 1;
  64714. + }
  64715. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  64716. + /* Switch on the voltage to the core */
  64717. + gpwrdn.b.pwrdnswtch = 1;
  64718. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64719. + dwc_udelay(10);
  64720. +
  64721. + /* Reset the core */
  64722. + gpwrdn.d32 = 0;
  64723. + gpwrdn.b.pwrdnrstn = 1;
  64724. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64725. + dwc_udelay(10);
  64726. +
  64727. + /* Disable power clamps */
  64728. + gpwrdn.d32 = 0;
  64729. + gpwrdn.b.pwrdnclmp = 1;
  64730. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64731. +
  64732. + /* Remove reset the core signal */
  64733. + gpwrdn.d32 = 0;
  64734. + gpwrdn.b.pwrdnrstn = 1;
  64735. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64736. + dwc_udelay(10);
  64737. +
  64738. + /* Disable PMU interrupt */
  64739. + gpwrdn.d32 = 0;
  64740. + gpwrdn.b.pmuintsel = 1;
  64741. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64742. +
  64743. + /*Indicates that we are exiting from hibernation */
  64744. + core_if->hibernation_suspend = 0;
  64745. +
  64746. + /* Disable PMU */
  64747. + gpwrdn.d32 = 0;
  64748. + gpwrdn.b.pmuactv = 1;
  64749. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64750. + dwc_udelay(10);
  64751. +
  64752. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  64753. + if (gpwrdn.b.dis_vbus == 1) {
  64754. + gpwrdn.d32 = 0;
  64755. + gpwrdn.b.dis_vbus = 1;
  64756. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64757. + }
  64758. +
  64759. + if (gpwrdn_temp.b.idsts) {
  64760. + core_if->op_state = B_PERIPHERAL;
  64761. + dwc_otg_core_init(core_if);
  64762. + dwc_otg_enable_global_interrupts(core_if);
  64763. + cil_pcd_start(core_if);
  64764. + } else {
  64765. + core_if->op_state = A_HOST;
  64766. + dwc_otg_core_init(core_if);
  64767. + dwc_otg_enable_global_interrupts(core_if);
  64768. + cil_hcd_start(core_if);
  64769. + }
  64770. + }
  64771. +
  64772. + if (core_if->adp_enable) {
  64773. + uint8_t is_host = 0;
  64774. + DWC_SPINUNLOCK(core_if->lock);
  64775. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  64776. +#ifndef DWC_HOST_ONLY
  64777. + if (gpwrdn_temp.b.idsts)
  64778. + core_if->lock = otg_dev->pcd->lock;
  64779. +#endif
  64780. +#ifndef DWC_DEVICE_ONLY
  64781. + if (!gpwrdn_temp.b.idsts) {
  64782. + core_if->lock = otg_dev->hcd->lock;
  64783. + is_host = 1;
  64784. + }
  64785. +#endif
  64786. + DWC_PRINTF("RESTART ADP\n");
  64787. + if (core_if->adp.probe_enabled)
  64788. + dwc_otg_adp_probe_stop(core_if);
  64789. + if (core_if->adp.sense_enabled)
  64790. + dwc_otg_adp_sense_stop(core_if);
  64791. + if (core_if->adp.sense_timer_started)
  64792. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  64793. + if (core_if->adp.vbuson_timer_started)
  64794. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  64795. + core_if->adp.probe_timer_values[0] = -1;
  64796. + core_if->adp.probe_timer_values[1] = -1;
  64797. + core_if->adp.sense_timer_started = 0;
  64798. + core_if->adp.vbuson_timer_started = 0;
  64799. + core_if->adp.probe_counter = 0;
  64800. + core_if->adp.gpwrdn = 0;
  64801. +
  64802. + /* Disable PMU and restart ADP */
  64803. + gpwrdn_temp.d32 = 0;
  64804. + gpwrdn_temp.b.pmuactv = 1;
  64805. + gpwrdn_temp.b.pmuintsel = 1;
  64806. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64807. + DWC_PRINTF("Check point 1\n");
  64808. + dwc_mdelay(110);
  64809. + dwc_otg_adp_start(core_if, is_host);
  64810. + DWC_SPINLOCK(core_if->lock);
  64811. + }
  64812. +
  64813. +
  64814. + return 1;
  64815. +}
  64816. +
  64817. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  64818. +{
  64819. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64820. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  64821. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  64822. +
  64823. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64824. + if (core_if->power_down == 2) {
  64825. + if (!core_if->hibernation_suspend) {
  64826. + DWC_PRINTF("Already exited from Hibernation\n");
  64827. + return 1;
  64828. + }
  64829. +
  64830. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  64831. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  64832. + gpwrdn.b.bsessvld == 0) {
  64833. + /* Save gpwrdn register for further usage if stschng interrupt */
  64834. + core_if->gr_backup->gpwrdn_local =
  64835. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64836. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  64837. + return 1;
  64838. + }
  64839. +
  64840. + /* Switch on the voltage to the core */
  64841. + gpwrdn.d32 = 0;
  64842. + gpwrdn.b.pwrdnswtch = 1;
  64843. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64844. + dwc_udelay(10);
  64845. +
  64846. + /* Reset the core */
  64847. + gpwrdn.d32 = 0;
  64848. + gpwrdn.b.pwrdnrstn = 1;
  64849. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64850. + dwc_udelay(10);
  64851. +
  64852. + /* Disable power clamps */
  64853. + gpwrdn.d32 = 0;
  64854. + gpwrdn.b.pwrdnclmp = 1;
  64855. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64856. +
  64857. + /* Remove reset the core signal */
  64858. + gpwrdn.d32 = 0;
  64859. + gpwrdn.b.pwrdnrstn = 1;
  64860. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64861. + dwc_udelay(10);
  64862. +
  64863. + /* Disable PMU interrupt */
  64864. + gpwrdn.d32 = 0;
  64865. + gpwrdn.b.pmuintsel = 1;
  64866. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64867. + dwc_udelay(10);
  64868. +
  64869. + /*Indicates that we are exiting from hibernation */
  64870. + core_if->hibernation_suspend = 0;
  64871. +
  64872. + /* Disable PMU */
  64873. + gpwrdn.d32 = 0;
  64874. + gpwrdn.b.pmuactv = 1;
  64875. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64876. + dwc_udelay(10);
  64877. +
  64878. + core_if->op_state = B_PERIPHERAL;
  64879. + dwc_otg_core_init(core_if);
  64880. + dwc_otg_enable_global_interrupts(core_if);
  64881. + cil_pcd_start(core_if);
  64882. +
  64883. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  64884. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  64885. + /*
  64886. + * Initiate SRP after initial ADP probe.
  64887. + */
  64888. + dwc_otg_initiate_srp(core_if);
  64889. + }
  64890. + }
  64891. +
  64892. + return 1;
  64893. +}
  64894. +/**
  64895. + * This interrupt indicates that the Wakeup Logic has detected a
  64896. + * status change either on IDDIG or BSessVld.
  64897. + */
  64898. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  64899. +{
  64900. + int retval;
  64901. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64902. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  64903. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  64904. +
  64905. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64906. +
  64907. + if (core_if->power_down == 2) {
  64908. + if (core_if->hibernation_suspend <= 0) {
  64909. + DWC_PRINTF("Already exited from Hibernation\n");
  64910. + return 1;
  64911. + } else
  64912. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  64913. +
  64914. + } else {
  64915. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  64916. + }
  64917. +
  64918. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64919. +
  64920. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  64921. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  64922. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  64923. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  64924. + }
  64925. +
  64926. + return retval;
  64927. +}
  64928. +
  64929. +/**
  64930. + * This interrupt indicates that the Wakeup Logic has detected a
  64931. + * SRP.
  64932. + */
  64933. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  64934. +{
  64935. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64936. +
  64937. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64938. +
  64939. + if (!core_if->hibernation_suspend) {
  64940. + DWC_PRINTF("Already exited from Hibernation\n");
  64941. + return 1;
  64942. + }
  64943. +#ifdef DWC_DEV_SRPCAP
  64944. + if (core_if->pwron_timer_started) {
  64945. + core_if->pwron_timer_started = 0;
  64946. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  64947. + }
  64948. +#endif
  64949. +
  64950. + /* Switch on the voltage to the core */
  64951. + gpwrdn.b.pwrdnswtch = 1;
  64952. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64953. + dwc_udelay(10);
  64954. +
  64955. + /* Reset the core */
  64956. + gpwrdn.d32 = 0;
  64957. + gpwrdn.b.pwrdnrstn = 1;
  64958. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64959. + dwc_udelay(10);
  64960. +
  64961. + /* Disable power clamps */
  64962. + gpwrdn.d32 = 0;
  64963. + gpwrdn.b.pwrdnclmp = 1;
  64964. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64965. +
  64966. + /* Remove reset the core signal */
  64967. + gpwrdn.d32 = 0;
  64968. + gpwrdn.b.pwrdnrstn = 1;
  64969. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64970. + dwc_udelay(10);
  64971. +
  64972. + /* Disable PMU interrupt */
  64973. + gpwrdn.d32 = 0;
  64974. + gpwrdn.b.pmuintsel = 1;
  64975. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64976. +
  64977. + /* Indicates that we are exiting from hibernation */
  64978. + core_if->hibernation_suspend = 0;
  64979. +
  64980. + /* Disable PMU */
  64981. + gpwrdn.d32 = 0;
  64982. + gpwrdn.b.pmuactv = 1;
  64983. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64984. + dwc_udelay(10);
  64985. +
  64986. + /* Programm Disable VBUS to 0 */
  64987. + gpwrdn.d32 = 0;
  64988. + gpwrdn.b.dis_vbus = 1;
  64989. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64990. +
  64991. + /*Initialize the core as Host */
  64992. + core_if->op_state = A_HOST;
  64993. + dwc_otg_core_init(core_if);
  64994. + dwc_otg_enable_global_interrupts(core_if);
  64995. + cil_hcd_start(core_if);
  64996. +
  64997. + return 1;
  64998. +}
  64999. +
  65000. +/** This interrupt indicates that restore command after Hibernation
  65001. + * was completed by the core. */
  65002. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  65003. +{
  65004. + pcgcctl_data_t pcgcctl;
  65005. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  65006. +
  65007. + //TODO De-assert restore signal. 8.a
  65008. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  65009. + if (pcgcctl.b.restoremode == 1) {
  65010. + gintmsk_data_t gintmsk = {.d32 = 0 };
  65011. + /*
  65012. + * If restore mode is Remote Wakeup,
  65013. + * unmask Remote Wakeup interrupt.
  65014. + */
  65015. + gintmsk.b.wkupintr = 1;
  65016. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  65017. + 0, gintmsk.d32);
  65018. + }
  65019. +
  65020. + return 1;
  65021. +}
  65022. +
  65023. +/**
  65024. + * This interrupt indicates that a device has been disconnected from
  65025. + * the root port.
  65026. + */
  65027. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  65028. +{
  65029. + gintsts_data_t gintsts;
  65030. +
  65031. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  65032. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  65033. + op_state_str(core_if));
  65034. +
  65035. +/** @todo Consolidate this if statement. */
  65036. +#ifndef DWC_HOST_ONLY
  65037. + if (core_if->op_state == B_HOST) {
  65038. + /* If in device mode Disconnect and stop the HCD, then
  65039. + * start the PCD. */
  65040. + DWC_SPINUNLOCK(core_if->lock);
  65041. + cil_hcd_disconnect(core_if);
  65042. + cil_pcd_start(core_if);
  65043. + DWC_SPINLOCK(core_if->lock);
  65044. + core_if->op_state = B_PERIPHERAL;
  65045. + } else if (dwc_otg_is_device_mode(core_if)) {
  65046. + gotgctl_data_t gotgctl = {.d32 = 0 };
  65047. + gotgctl.d32 =
  65048. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  65049. + if (gotgctl.b.hstsethnpen == 1) {
  65050. + /* Do nothing, if HNP in process the OTG
  65051. + * interrupt "Host Negotiation Detected"
  65052. + * interrupt will do the mode switch.
  65053. + */
  65054. + } else if (gotgctl.b.devhnpen == 0) {
  65055. + /* If in device mode Disconnect and stop the HCD, then
  65056. + * start the PCD. */
  65057. + DWC_SPINUNLOCK(core_if->lock);
  65058. + cil_hcd_disconnect(core_if);
  65059. + cil_pcd_start(core_if);
  65060. + DWC_SPINLOCK(core_if->lock);
  65061. + core_if->op_state = B_PERIPHERAL;
  65062. + } else {
  65063. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  65064. + }
  65065. + } else {
  65066. + if (core_if->op_state == A_HOST) {
  65067. + /* A-Cable still connected but device disconnected. */
  65068. + cil_hcd_disconnect(core_if);
  65069. + if (core_if->adp_enable) {
  65070. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  65071. + cil_hcd_stop(core_if);
  65072. + /* Enable Power Down Logic */
  65073. + gpwrdn.b.pmuintsel = 1;
  65074. + gpwrdn.b.pmuactv = 1;
  65075. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65076. + gpwrdn, 0, gpwrdn.d32);
  65077. + dwc_otg_adp_probe_start(core_if);
  65078. +
  65079. + /* Power off the core */
  65080. + if (core_if->power_down == 2) {
  65081. + gpwrdn.d32 = 0;
  65082. + gpwrdn.b.pwrdnswtch = 1;
  65083. + DWC_MODIFY_REG32
  65084. + (&core_if->core_global_regs->gpwrdn,
  65085. + gpwrdn.d32, 0);
  65086. + }
  65087. + }
  65088. + }
  65089. + }
  65090. +#endif
  65091. + /* Change to L3(OFF) state */
  65092. + core_if->lx_state = DWC_OTG_L3;
  65093. +
  65094. + gintsts.d32 = 0;
  65095. + gintsts.b.disconnect = 1;
  65096. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65097. + return 1;
  65098. +}
  65099. +
  65100. +/**
  65101. + * This interrupt indicates that SUSPEND state has been detected on
  65102. + * the USB.
  65103. + *
  65104. + * For HNP the USB Suspend interrupt signals the change from
  65105. + * "a_peripheral" to "a_host".
  65106. + *
  65107. + * When power management is enabled the core will be put in low power
  65108. + * mode.
  65109. + */
  65110. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  65111. +{
  65112. + dsts_data_t dsts;
  65113. + gintsts_data_t gintsts;
  65114. + dcfg_data_t dcfg;
  65115. +
  65116. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  65117. +
  65118. + if (dwc_otg_is_device_mode(core_if)) {
  65119. + /* Check the Device status register to determine if the Suspend
  65120. + * state is active. */
  65121. + dsts.d32 =
  65122. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  65123. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  65124. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  65125. + "HWCFG4.power Optimize=%d\n",
  65126. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  65127. +
  65128. +#ifdef PARTIAL_POWER_DOWN
  65129. +/** @todo Add a module parameter for power management. */
  65130. +
  65131. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  65132. + pcgcctl_data_t power = {.d32 = 0 };
  65133. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  65134. +
  65135. + power.b.pwrclmp = 1;
  65136. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  65137. +
  65138. + power.b.rstpdwnmodule = 1;
  65139. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  65140. +
  65141. + power.b.stoppclk = 1;
  65142. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  65143. +
  65144. + } else {
  65145. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  65146. + }
  65147. +#endif
  65148. + /* PCD callback for suspend. Release the lock inside of callback function */
  65149. + cil_pcd_suspend(core_if);
  65150. + if (core_if->power_down == 2)
  65151. + {
  65152. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  65153. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  65154. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  65155. +
  65156. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  65157. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65158. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65159. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  65160. +
  65161. + /* Change to L2(suspend) state */
  65162. + core_if->lx_state = DWC_OTG_L2;
  65163. +
  65164. + /* Clear interrupt in gintsts */
  65165. + gintsts.d32 = 0;
  65166. + gintsts.b.usbsuspend = 1;
  65167. + DWC_WRITE_REG32(&core_if->core_global_regs->
  65168. + gintsts, gintsts.d32);
  65169. + DWC_PRINTF("Start of hibernation completed\n");
  65170. + dwc_otg_save_global_regs(core_if);
  65171. + dwc_otg_save_dev_regs(core_if);
  65172. +
  65173. + gusbcfg.d32 =
  65174. + DWC_READ_REG32(&core_if->core_global_regs->
  65175. + gusbcfg);
  65176. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  65177. + /* ULPI interface */
  65178. + /* Suspend the Phy Clock */
  65179. + pcgcctl.d32 = 0;
  65180. + pcgcctl.b.stoppclk = 1;
  65181. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  65182. + pcgcctl.d32);
  65183. + dwc_udelay(10);
  65184. + gpwrdn.b.pmuactv = 1;
  65185. + DWC_MODIFY_REG32(&core_if->
  65186. + core_global_regs->
  65187. + gpwrdn, 0, gpwrdn.d32);
  65188. + } else {
  65189. + /* UTMI+ Interface */
  65190. + gpwrdn.b.pmuactv = 1;
  65191. + DWC_MODIFY_REG32(&core_if->
  65192. + core_global_regs->
  65193. + gpwrdn, 0, gpwrdn.d32);
  65194. + dwc_udelay(10);
  65195. + pcgcctl.b.stoppclk = 1;
  65196. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  65197. + pcgcctl.d32);
  65198. + dwc_udelay(10);
  65199. + }
  65200. +
  65201. + /* Set flag to indicate that we are in hibernation */
  65202. + core_if->hibernation_suspend = 1;
  65203. + /* Enable interrupts from wake up logic */
  65204. + gpwrdn.d32 = 0;
  65205. + gpwrdn.b.pmuintsel = 1;
  65206. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65207. + gpwrdn, 0, gpwrdn.d32);
  65208. + dwc_udelay(10);
  65209. +
  65210. + /* Unmask device mode interrupts in GPWRDN */
  65211. + gpwrdn.d32 = 0;
  65212. + gpwrdn.b.rst_det_msk = 1;
  65213. + gpwrdn.b.lnstchng_msk = 1;
  65214. + gpwrdn.b.sts_chngint_msk = 1;
  65215. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65216. + gpwrdn, 0, gpwrdn.d32);
  65217. + dwc_udelay(10);
  65218. +
  65219. + /* Enable Power Down Clamp */
  65220. + gpwrdn.d32 = 0;
  65221. + gpwrdn.b.pwrdnclmp = 1;
  65222. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65223. + gpwrdn, 0, gpwrdn.d32);
  65224. + dwc_udelay(10);
  65225. +
  65226. + /* Switch off VDD */
  65227. + gpwrdn.d32 = 0;
  65228. + gpwrdn.b.pwrdnswtch = 1;
  65229. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65230. + gpwrdn, 0, gpwrdn.d32);
  65231. +
  65232. + /* Save gpwrdn register for further usage if stschng interrupt */
  65233. + core_if->gr_backup->gpwrdn_local =
  65234. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  65235. + DWC_PRINTF("Hibernation completed\n");
  65236. +
  65237. + return 1;
  65238. + }
  65239. + } else if (core_if->power_down == 3) {
  65240. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65241. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  65242. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  65243. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  65244. +
  65245. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  65246. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  65247. + core_if->xhib = 1;
  65248. +
  65249. + /* Clear interrupt in gintsts */
  65250. + gintsts.d32 = 0;
  65251. + gintsts.b.usbsuspend = 1;
  65252. + DWC_WRITE_REG32(&core_if->core_global_regs->
  65253. + gintsts, gintsts.d32);
  65254. +
  65255. + dwc_otg_save_global_regs(core_if);
  65256. + dwc_otg_save_dev_regs(core_if);
  65257. +
  65258. + /* Wait for 10 PHY clocks */
  65259. + dwc_udelay(10);
  65260. +
  65261. + /* Program GPIO register while entering to xHib */
  65262. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  65263. +
  65264. + pcgcctl.b.enbl_extnd_hiber = 1;
  65265. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65266. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65267. +
  65268. + pcgcctl.d32 = 0;
  65269. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  65270. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65271. +
  65272. + pcgcctl.d32 = 0;
  65273. + pcgcctl.b.extnd_hiber_switch = 1;
  65274. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  65275. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  65276. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65277. +
  65278. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  65279. +
  65280. + return 1;
  65281. + }
  65282. + }
  65283. + } else {
  65284. + if (core_if->op_state == A_PERIPHERAL) {
  65285. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  65286. + /* Clear the a_peripheral flag, back to a_host. */
  65287. + DWC_SPINUNLOCK(core_if->lock);
  65288. + cil_pcd_stop(core_if);
  65289. + cil_hcd_start(core_if);
  65290. + DWC_SPINLOCK(core_if->lock);
  65291. + core_if->op_state = A_HOST;
  65292. + }
  65293. + }
  65294. +
  65295. + /* Change to L2(suspend) state */
  65296. + core_if->lx_state = DWC_OTG_L2;
  65297. +
  65298. + /* Clear interrupt */
  65299. + gintsts.d32 = 0;
  65300. + gintsts.b.usbsuspend = 1;
  65301. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65302. +
  65303. + return 1;
  65304. +}
  65305. +
  65306. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  65307. +{
  65308. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65309. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65310. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  65311. +
  65312. + dwc_udelay(10);
  65313. +
  65314. + /* Program GPIO register while entering to xHib */
  65315. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  65316. +
  65317. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  65318. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  65319. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65320. + dwc_udelay(10);
  65321. +
  65322. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  65323. + gpwrdn.b.restore = 1;
  65324. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  65325. + dwc_udelay(10);
  65326. +
  65327. + restore_lpm_i2c_regs(core_if);
  65328. +
  65329. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  65330. + pcgcctl.b.max_xcvrselect = 1;
  65331. + pcgcctl.b.ess_reg_restored = 0;
  65332. + pcgcctl.b.extnd_hiber_switch = 0;
  65333. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  65334. + pcgcctl.b.enbl_extnd_hiber = 1;
  65335. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65336. +
  65337. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  65338. + gahbcfg.b.glblintrmsk = 1;
  65339. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  65340. +
  65341. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  65342. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  65343. +
  65344. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  65345. + core_if->gr_backup->gusbcfg_local);
  65346. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  65347. + core_if->dr_backup->dcfg);
  65348. +
  65349. + pcgcctl.d32 = 0;
  65350. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  65351. + pcgcctl.b.max_xcvrselect = 1;
  65352. + pcgcctl.d32 |= 0x608;
  65353. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65354. + dwc_udelay(10);
  65355. +
  65356. + pcgcctl.d32 = 0;
  65357. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  65358. + pcgcctl.b.max_xcvrselect = 1;
  65359. + pcgcctl.b.ess_reg_restored = 1;
  65360. + pcgcctl.b.enbl_extnd_hiber = 1;
  65361. + pcgcctl.b.rstpdwnmodule = 1;
  65362. + pcgcctl.b.restoremode = 1;
  65363. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65364. +
  65365. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  65366. +
  65367. + return 1;
  65368. +}
  65369. +
  65370. +#ifdef CONFIG_USB_DWC_OTG_LPM
  65371. +/**
  65372. + * This function hadles LPM transaction received interrupt.
  65373. + */
  65374. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  65375. +{
  65376. + glpmcfg_data_t lpmcfg;
  65377. + gintsts_data_t gintsts;
  65378. +
  65379. + if (!core_if->core_params->lpm_enable) {
  65380. + DWC_PRINTF("Unexpected LPM interrupt\n");
  65381. + }
  65382. +
  65383. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  65384. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  65385. +
  65386. + if (dwc_otg_is_host_mode(core_if)) {
  65387. + cil_hcd_sleep(core_if);
  65388. + } else {
  65389. + lpmcfg.b.hird_thres |= (1 << 4);
  65390. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  65391. + lpmcfg.d32);
  65392. + }
  65393. +
  65394. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  65395. + dwc_udelay(10);
  65396. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  65397. + if (lpmcfg.b.prt_sleep_sts) {
  65398. + /* Save the current state */
  65399. + core_if->lx_state = DWC_OTG_L1;
  65400. + }
  65401. +
  65402. + /* Clear interrupt */
  65403. + gintsts.d32 = 0;
  65404. + gintsts.b.lpmtranrcvd = 1;
  65405. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65406. + return 1;
  65407. +}
  65408. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  65409. +
  65410. +/**
  65411. + * This function returns the Core Interrupt register.
  65412. + */
  65413. +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk)
  65414. +{
  65415. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  65416. + gintsts_data_t gintsts;
  65417. + gintmsk_data_t gintmsk;
  65418. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  65419. + gintmsk_common.b.wkupintr = 1;
  65420. + gintmsk_common.b.sessreqintr = 1;
  65421. + gintmsk_common.b.conidstschng = 1;
  65422. + gintmsk_common.b.otgintr = 1;
  65423. + gintmsk_common.b.modemismatch = 1;
  65424. + gintmsk_common.b.disconnect = 1;
  65425. + gintmsk_common.b.usbsuspend = 1;
  65426. +#ifdef CONFIG_USB_DWC_OTG_LPM
  65427. + gintmsk_common.b.lpmtranrcvd = 1;
  65428. +#endif
  65429. + gintmsk_common.b.restoredone = 1;
  65430. + if(dwc_otg_is_device_mode(core_if))
  65431. + {
  65432. + /** @todo: The port interrupt occurs while in device
  65433. + * mode. Added code to CIL to clear the interrupt for now!
  65434. + */
  65435. + gintmsk_common.b.portintr = 1;
  65436. + }
  65437. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  65438. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  65439. + {
  65440. + unsigned long flags;
  65441. +
  65442. + // Re-enable the saved interrupts
  65443. + local_irq_save(flags);
  65444. + local_fiq_disable();
  65445. + gintmsk.d32 |= gintmsk_common.d32;
  65446. + gintsts_saved.d32 &= ~gintmsk_common.d32;
  65447. + reenable_gintmsk->d32 = gintmsk.d32;
  65448. + local_irq_restore(flags);
  65449. + }
  65450. +
  65451. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  65452. +
  65453. +#ifdef DEBUG
  65454. + /* if any common interrupts set */
  65455. + if (gintsts.d32 & gintmsk_common.d32) {
  65456. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  65457. + gintsts.d32, gintmsk.d32);
  65458. + }
  65459. +#endif
  65460. + if (!fiq_fix_enable){
  65461. + if (gahbcfg.b.glblintrmsk)
  65462. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  65463. + else
  65464. + return 0;
  65465. + }
  65466. + else {
  65467. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  65468. + }
  65469. +
  65470. +}
  65471. +
  65472. +/* MACRO for clearing interupt bits in GPWRDN register */
  65473. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  65474. +do { \
  65475. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  65476. + gpwrdn.b.__intr = 1; \
  65477. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  65478. + 0, gpwrdn.d32); \
  65479. +} while (0)
  65480. +
  65481. +/**
  65482. + * Common interrupt handler.
  65483. + *
  65484. + * The common interrupts are those that occur in both Host and Device mode.
  65485. + * This handler handles the following interrupts:
  65486. + * - Mode Mismatch Interrupt
  65487. + * - Disconnect Interrupt
  65488. + * - OTG Interrupt
  65489. + * - Connector ID Status Change Interrupt
  65490. + * - Session Request Interrupt.
  65491. + * - Resume / Remote Wakeup Detected Interrupt.
  65492. + * - LPM Transaction Received Interrupt
  65493. + * - ADP Transaction Received Interrupt
  65494. + *
  65495. + */
  65496. +int32_t dwc_otg_handle_common_intr(void *dev)
  65497. +{
  65498. + int retval = 0;
  65499. + gintsts_data_t gintsts;
  65500. + gintmsk_data_t reenable_gintmsk;
  65501. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65502. + dwc_otg_device_t *otg_dev = dev;
  65503. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  65504. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  65505. + if (dwc_otg_is_device_mode(core_if))
  65506. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  65507. +
  65508. + if (core_if->lock)
  65509. + DWC_SPINLOCK(core_if->lock);
  65510. +
  65511. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  65512. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  65513. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  65514. + core_if->xhib = 2;
  65515. + if (core_if->lock)
  65516. + DWC_SPINUNLOCK(core_if->lock);
  65517. +
  65518. + return retval;
  65519. + }
  65520. +
  65521. + if (core_if->hibernation_suspend <= 0) {
  65522. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &reenable_gintmsk);
  65523. +
  65524. + if (gintsts.b.modemismatch) {
  65525. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  65526. + }
  65527. + if (gintsts.b.otgintr) {
  65528. + retval |= dwc_otg_handle_otg_intr(core_if);
  65529. + }
  65530. + if (gintsts.b.conidstschng) {
  65531. + retval |=
  65532. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  65533. + }
  65534. + if (gintsts.b.disconnect) {
  65535. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  65536. + }
  65537. + if (gintsts.b.sessreqintr) {
  65538. + retval |= dwc_otg_handle_session_req_intr(core_if);
  65539. + }
  65540. + if (gintsts.b.wkupintr) {
  65541. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  65542. + }
  65543. + if (gintsts.b.usbsuspend) {
  65544. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  65545. + }
  65546. +#ifdef CONFIG_USB_DWC_OTG_LPM
  65547. + if (gintsts.b.lpmtranrcvd) {
  65548. + retval |= dwc_otg_handle_lpm_intr(core_if);
  65549. + }
  65550. +#endif
  65551. + if (gintsts.b.restoredone) {
  65552. + gintsts.d32 = 0;
  65553. + if (core_if->power_down == 2)
  65554. + core_if->hibernation_suspend = -1;
  65555. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  65556. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65557. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65558. + dctl_data_t dctl = {.d32 = 0 };
  65559. +
  65560. + DWC_WRITE_REG32(&core_if->core_global_regs->
  65561. + gintsts, 0xFFFFFFFF);
  65562. +
  65563. + DWC_DEBUGPL(DBG_ANY,
  65564. + "RESTORE DONE generated\n");
  65565. +
  65566. + gpwrdn.b.restore = 1;
  65567. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  65568. + dwc_udelay(10);
  65569. +
  65570. + pcgcctl.b.rstpdwnmodule = 1;
  65571. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  65572. +
  65573. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  65574. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  65575. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  65576. + dwc_udelay(50);
  65577. +
  65578. + dctl.b.pwronprgdone = 1;
  65579. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  65580. + dwc_udelay(10);
  65581. +
  65582. + dwc_otg_restore_global_regs(core_if);
  65583. + dwc_otg_restore_dev_regs(core_if, 0);
  65584. +
  65585. + dctl.d32 = 0;
  65586. + dctl.b.pwronprgdone = 1;
  65587. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  65588. + dwc_udelay(10);
  65589. +
  65590. + pcgcctl.d32 = 0;
  65591. + pcgcctl.b.enbl_extnd_hiber = 1;
  65592. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  65593. +
  65594. + /* The core will be in ON STATE */
  65595. + core_if->lx_state = DWC_OTG_L0;
  65596. + core_if->xhib = 0;
  65597. +
  65598. + DWC_SPINUNLOCK(core_if->lock);
  65599. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  65600. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  65601. + }
  65602. + DWC_SPINLOCK(core_if->lock);
  65603. +
  65604. + }
  65605. +
  65606. + gintsts.b.restoredone = 1;
  65607. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  65608. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  65609. + retval |= 1;
  65610. + }
  65611. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  65612. + /* The port interrupt occurs while in device mode with HPRT0
  65613. + * Port Enable/Disable.
  65614. + */
  65615. + gintsts.d32 = 0;
  65616. + gintsts.b.portintr = 1;
  65617. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  65618. + retval |= 1;
  65619. + reenable_gintmsk.b.portintr = 1;
  65620. +
  65621. + }
  65622. +
  65623. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, reenable_gintmsk.d32);
  65624. +
  65625. + } else {
  65626. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  65627. +
  65628. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  65629. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  65630. + if (gpwrdn.b.linestate == 0) {
  65631. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  65632. + } else {
  65633. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  65634. + }
  65635. +
  65636. + retval |= 1;
  65637. + }
  65638. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  65639. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  65640. + /* remote wakeup from hibernation */
  65641. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  65642. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  65643. + } else {
  65644. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  65645. + }
  65646. + retval |= 1;
  65647. + }
  65648. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  65649. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  65650. + if (gpwrdn.b.linestate == 0) {
  65651. + DWC_PRINTF("Reset detected\n");
  65652. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  65653. + }
  65654. + }
  65655. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  65656. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  65657. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  65658. + retval |= 1;
  65659. + }
  65660. + }
  65661. + /* Handle ADP interrupt here */
  65662. + if (gpwrdn.b.adp_int) {
  65663. + DWC_PRINTF("ADP interrupt\n");
  65664. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  65665. + dwc_otg_adp_handle_intr(core_if);
  65666. + retval |= 1;
  65667. + }
  65668. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  65669. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  65670. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  65671. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  65672. +
  65673. + retval |= 1;
  65674. + }
  65675. + if (core_if->lock)
  65676. + DWC_SPINUNLOCK(core_if->lock);
  65677. +
  65678. + return retval;
  65679. +}
  65680. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_core_if.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  65681. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 1970-01-01 01:00:00.000000000 +0100
  65682. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 2014-03-11 17:51:27.000000000 +0100
  65683. @@ -0,0 +1,705 @@
  65684. +/* ==========================================================================
  65685. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  65686. + * $Revision: #13 $
  65687. + * $Date: 2012/08/10 $
  65688. + * $Change: 2047372 $
  65689. + *
  65690. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  65691. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  65692. + * otherwise expressly agreed to in writing between Synopsys and you.
  65693. + *
  65694. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  65695. + * any End User Software License Agreement or Agreement for Licensed Product
  65696. + * with Synopsys or any supplement thereto. You are permitted to use and
  65697. + * redistribute this Software in source and binary forms, with or without
  65698. + * modification, provided that redistributions of source code must retain this
  65699. + * notice. You may not view, use, disclose, copy or distribute this file or
  65700. + * any information contained herein except pursuant to this license grant from
  65701. + * Synopsys. If you do not agree with this notice, including the disclaimer
  65702. + * below, then you are not authorized to use the Software.
  65703. + *
  65704. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  65705. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65706. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  65707. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  65708. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  65709. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  65710. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  65711. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  65712. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65713. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  65714. + * DAMAGE.
  65715. + * ========================================================================== */
  65716. +#if !defined(__DWC_CORE_IF_H__)
  65717. +#define __DWC_CORE_IF_H__
  65718. +
  65719. +#include "dwc_os.h"
  65720. +
  65721. +/** @file
  65722. + * This file defines DWC_OTG Core API
  65723. + */
  65724. +
  65725. +struct dwc_otg_core_if;
  65726. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  65727. +
  65728. +/** Maximum number of Periodic FIFOs */
  65729. +#define MAX_PERIO_FIFOS 15
  65730. +/** Maximum number of Periodic FIFOs */
  65731. +#define MAX_TX_FIFOS 15
  65732. +
  65733. +/** Maximum number of Endpoints/HostChannels */
  65734. +#define MAX_EPS_CHANNELS 16
  65735. +
  65736. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  65737. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  65738. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  65739. +
  65740. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  65741. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  65742. +
  65743. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  65744. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  65745. +
  65746. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  65747. +
  65748. +/** This function should be called on every hardware interrupt. */
  65749. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  65750. +
  65751. +/** @name OTG Core Parameters */
  65752. +/** @{ */
  65753. +
  65754. +/**
  65755. + * Specifies the OTG capabilities. The driver will automatically
  65756. + * detect the value for this parameter if none is specified.
  65757. + * 0 - HNP and SRP capable (default)
  65758. + * 1 - SRP Only capable
  65759. + * 2 - No HNP/SRP capable
  65760. + */
  65761. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  65762. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  65763. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  65764. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  65765. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  65766. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  65767. +
  65768. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  65769. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  65770. +#define dwc_param_opt_default 1
  65771. +
  65772. +/**
  65773. + * Specifies whether to use slave or DMA mode for accessing the data
  65774. + * FIFOs. The driver will automatically detect the value for this
  65775. + * parameter if none is specified.
  65776. + * 0 - Slave
  65777. + * 1 - DMA (default, if available)
  65778. + */
  65779. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  65780. + int32_t val);
  65781. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  65782. +#define dwc_param_dma_enable_default 1
  65783. +
  65784. +/**
  65785. + * When DMA mode is enabled specifies whether to use
  65786. + * address DMA or DMA Descritor mode for accessing the data
  65787. + * FIFOs in device mode. The driver will automatically detect
  65788. + * the value for this parameter if none is specified.
  65789. + * 0 - address DMA
  65790. + * 1 - DMA Descriptor(default, if available)
  65791. + */
  65792. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  65793. + int32_t val);
  65794. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  65795. +//#define dwc_param_dma_desc_enable_default 1
  65796. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  65797. +
  65798. +/** The DMA Burst size (applicable only for External DMA
  65799. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  65800. + */
  65801. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  65802. + int32_t val);
  65803. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  65804. +#define dwc_param_dma_burst_size_default 32
  65805. +
  65806. +/**
  65807. + * Specifies the maximum speed of operation in host and device mode.
  65808. + * The actual speed depends on the speed of the attached device and
  65809. + * the value of phy_type. The actual speed depends on the speed of the
  65810. + * attached device.
  65811. + * 0 - High Speed (default)
  65812. + * 1 - Full Speed
  65813. + */
  65814. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  65815. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  65816. +#define dwc_param_speed_default 0
  65817. +#define DWC_SPEED_PARAM_HIGH 0
  65818. +#define DWC_SPEED_PARAM_FULL 1
  65819. +
  65820. +/** Specifies whether low power mode is supported when attached
  65821. + * to a Full Speed or Low Speed device in host mode.
  65822. + * 0 - Don't support low power mode (default)
  65823. + * 1 - Support low power mode
  65824. + */
  65825. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  65826. + core_if, int32_t val);
  65827. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  65828. + * core_if);
  65829. +#define dwc_param_host_support_fs_ls_low_power_default 0
  65830. +
  65831. +/** Specifies the PHY clock rate in low power mode when connected to a
  65832. + * Low Speed device in host mode. This parameter is applicable only if
  65833. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  65834. + * then defaults to 6 MHZ otherwise 48 MHZ.
  65835. + *
  65836. + * 0 - 48 MHz
  65837. + * 1 - 6 MHz
  65838. + */
  65839. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  65840. + core_if, int32_t val);
  65841. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  65842. + core_if);
  65843. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  65844. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  65845. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  65846. +
  65847. +/**
  65848. + * 0 - Use cC FIFO size parameters
  65849. + * 1 - Allow dynamic FIFO sizing (default)
  65850. + */
  65851. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  65852. + int32_t val);
  65853. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  65854. + core_if);
  65855. +#define dwc_param_enable_dynamic_fifo_default 1
  65856. +
  65857. +/** Total number of 4-byte words in the data FIFO memory. This
  65858. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  65859. + * Tx FIFOs.
  65860. + * 32 to 32768 (default 8192)
  65861. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  65862. + */
  65863. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  65864. + int32_t val);
  65865. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  65866. +//#define dwc_param_data_fifo_size_default 8192
  65867. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  65868. +
  65869. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  65870. + * FIFO sizing is enabled.
  65871. + * 16 to 32768 (default 1064)
  65872. + */
  65873. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  65874. + int32_t val);
  65875. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  65876. +#define dwc_param_dev_rx_fifo_size_default 1064
  65877. +
  65878. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  65879. + * when dynamic FIFO sizing is enabled.
  65880. + * 16 to 32768 (default 1024)
  65881. + */
  65882. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65883. + core_if, int32_t val);
  65884. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65885. + core_if);
  65886. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  65887. +
  65888. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  65889. + * mode when dynamic FIFO sizing is enabled.
  65890. + * 4 to 768 (default 256)
  65891. + */
  65892. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  65893. + int32_t val, int fifo_num);
  65894. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  65895. + core_if, int fifo_num);
  65896. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  65897. +
  65898. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  65899. + * FIFO sizing is enabled.
  65900. + * 16 to 32768 (default 1024)
  65901. + */
  65902. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  65903. + int32_t val);
  65904. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  65905. +//#define dwc_param_host_rx_fifo_size_default 1024
  65906. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  65907. +
  65908. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  65909. + * when Dynamic FIFO sizing is enabled in the core.
  65910. + * 16 to 32768 (default 1024)
  65911. + */
  65912. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65913. + core_if, int32_t val);
  65914. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65915. + core_if);
  65916. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  65917. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  65918. +
  65919. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  65920. + * FIFO sizing is enabled.
  65921. + * 16 to 32768 (default 1024)
  65922. + */
  65923. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  65924. + core_if, int32_t val);
  65925. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  65926. + core_if);
  65927. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  65928. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  65929. +
  65930. +/** The maximum transfer size supported in bytes.
  65931. + * 2047 to 65,535 (default 65,535)
  65932. + */
  65933. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  65934. + int32_t val);
  65935. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  65936. +#define dwc_param_max_transfer_size_default 65535
  65937. +
  65938. +/** The maximum number of packets in a transfer.
  65939. + * 15 to 511 (default 511)
  65940. + */
  65941. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  65942. + int32_t val);
  65943. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  65944. +#define dwc_param_max_packet_count_default 511
  65945. +
  65946. +/** The number of host channel registers to use.
  65947. + * 1 to 16 (default 12)
  65948. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  65949. + */
  65950. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  65951. + int32_t val);
  65952. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  65953. +//#define dwc_param_host_channels_default 12
  65954. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  65955. +
  65956. +/** The number of endpoints in addition to EP0 available for device
  65957. + * mode operations.
  65958. + * 1 to 15 (default 6 IN and OUT)
  65959. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  65960. + * endpoints in addition to EP0.
  65961. + */
  65962. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  65963. + int32_t val);
  65964. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  65965. +#define dwc_param_dev_endpoints_default 6
  65966. +
  65967. +/**
  65968. + * Specifies the type of PHY interface to use. By default, the driver
  65969. + * will automatically detect the phy_type.
  65970. + *
  65971. + * 0 - Full Speed PHY
  65972. + * 1 - UTMI+ (default)
  65973. + * 2 - ULPI
  65974. + */
  65975. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  65976. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  65977. +#define DWC_PHY_TYPE_PARAM_FS 0
  65978. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  65979. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  65980. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  65981. +
  65982. +/**
  65983. + * Specifies the UTMI+ Data Width. This parameter is
  65984. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  65985. + * PHY_TYPE, this parameter indicates the data width between
  65986. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  65987. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  65988. + * to "8 and 16 bits", meaning that the core has been
  65989. + * configured to work at either data path width.
  65990. + *
  65991. + * 8 or 16 bits (default 16)
  65992. + */
  65993. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  65994. + int32_t val);
  65995. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  65996. +//#define dwc_param_phy_utmi_width_default 16
  65997. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  65998. +
  65999. +/**
  66000. + * Specifies whether the ULPI operates at double or single
  66001. + * data rate. This parameter is only applicable if PHY_TYPE is
  66002. + * ULPI.
  66003. + *
  66004. + * 0 - single data rate ULPI interface with 8 bit wide data
  66005. + * bus (default)
  66006. + * 1 - double data rate ULPI interface with 4 bit wide data
  66007. + * bus
  66008. + */
  66009. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  66010. + int32_t val);
  66011. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  66012. +#define dwc_param_phy_ulpi_ddr_default 0
  66013. +
  66014. +/**
  66015. + * Specifies whether to use the internal or external supply to
  66016. + * drive the vbus with a ULPI phy.
  66017. + */
  66018. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  66019. + int32_t val);
  66020. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  66021. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  66022. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  66023. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  66024. +
  66025. +/**
  66026. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  66027. + * parameter is only applicable if PHY_TYPE is FS.
  66028. + * 0 - No (default)
  66029. + * 1 - Yes
  66030. + */
  66031. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  66032. + int32_t val);
  66033. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  66034. +#define dwc_param_i2c_enable_default 0
  66035. +
  66036. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  66037. + int32_t val);
  66038. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  66039. +#define dwc_param_ulpi_fs_ls_default 0
  66040. +
  66041. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  66042. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  66043. +#define dwc_param_ts_dline_default 0
  66044. +
  66045. +/**
  66046. + * Specifies whether dedicated transmit FIFOs are
  66047. + * enabled for non periodic IN endpoints in device mode
  66048. + * 0 - No
  66049. + * 1 - Yes
  66050. + */
  66051. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  66052. + int32_t val);
  66053. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  66054. + core_if);
  66055. +#define dwc_param_en_multiple_tx_fifo_default 1
  66056. +
  66057. +/** Number of 4-byte words in each of the Tx FIFOs in device
  66058. + * mode when dynamic FIFO sizing is enabled.
  66059. + * 4 to 768 (default 256)
  66060. + */
  66061. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  66062. + int fifo_num, int32_t val);
  66063. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  66064. + int fifo_num);
  66065. +#define dwc_param_dev_tx_fifo_size_default 768
  66066. +
  66067. +/** Thresholding enable flag-
  66068. + * bit 0 - enable non-ISO Tx thresholding
  66069. + * bit 1 - enable ISO Tx thresholding
  66070. + * bit 2 - enable Rx thresholding
  66071. + */
  66072. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  66073. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  66074. +#define dwc_param_thr_ctl_default 0
  66075. +
  66076. +/** Thresholding length for Tx
  66077. + * FIFOs in 32 bit DWORDs
  66078. + */
  66079. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  66080. + int32_t val);
  66081. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  66082. +#define dwc_param_tx_thr_length_default 64
  66083. +
  66084. +/** Thresholding length for Rx
  66085. + * FIFOs in 32 bit DWORDs
  66086. + */
  66087. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  66088. + int32_t val);
  66089. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  66090. +#define dwc_param_rx_thr_length_default 64
  66091. +
  66092. +/**
  66093. + * Specifies whether LPM (Link Power Management) support is enabled
  66094. + */
  66095. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  66096. + int32_t val);
  66097. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  66098. +#define dwc_param_lpm_enable_default 1
  66099. +
  66100. +/**
  66101. + * Specifies whether PTI enhancement is enabled
  66102. + */
  66103. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  66104. + int32_t val);
  66105. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  66106. +#define dwc_param_pti_enable_default 0
  66107. +
  66108. +/**
  66109. + * Specifies whether MPI enhancement is enabled
  66110. + */
  66111. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  66112. + int32_t val);
  66113. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  66114. +#define dwc_param_mpi_enable_default 0
  66115. +
  66116. +/**
  66117. + * Specifies whether ADP capability is enabled
  66118. + */
  66119. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  66120. + int32_t val);
  66121. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  66122. +#define dwc_param_adp_enable_default 0
  66123. +
  66124. +/**
  66125. + * Specifies whether IC_USB capability is enabled
  66126. + */
  66127. +
  66128. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  66129. + int32_t val);
  66130. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  66131. +#define dwc_param_ic_usb_cap_default 0
  66132. +
  66133. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  66134. + int32_t val);
  66135. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  66136. +#define dwc_param_ahb_thr_ratio_default 0
  66137. +
  66138. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  66139. + int32_t val);
  66140. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  66141. +#define dwc_param_power_down_default 0
  66142. +
  66143. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  66144. + int32_t val);
  66145. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  66146. +#define dwc_param_reload_ctl_default 0
  66147. +
  66148. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  66149. + int32_t val);
  66150. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  66151. +#define dwc_param_dev_out_nak_default 0
  66152. +
  66153. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  66154. + int32_t val);
  66155. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  66156. +#define dwc_param_cont_on_bna_default 0
  66157. +
  66158. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  66159. + int32_t val);
  66160. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  66161. +#define dwc_param_ahb_single_default 0
  66162. +
  66163. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  66164. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  66165. +#define dwc_param_otg_ver_default 0
  66166. +
  66167. +/** @} */
  66168. +
  66169. +/** @name Access to registers and bit-fields */
  66170. +
  66171. +/**
  66172. + * Dump core registers and SPRAM
  66173. + */
  66174. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  66175. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  66176. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  66177. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  66178. +
  66179. +/**
  66180. + * Get host negotiation status.
  66181. + */
  66182. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  66183. +
  66184. +/**
  66185. + * Get srp status
  66186. + */
  66187. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  66188. +
  66189. +/**
  66190. + * Set hnpreq bit in the GOTGCTL register.
  66191. + */
  66192. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  66193. +
  66194. +/**
  66195. + * Get Content of SNPSID register.
  66196. + */
  66197. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  66198. +
  66199. +/**
  66200. + * Get current mode.
  66201. + * Returns 0 if in device mode, and 1 if in host mode.
  66202. + */
  66203. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  66204. +
  66205. +/**
  66206. + * Get value of hnpcapable field in the GUSBCFG register
  66207. + */
  66208. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  66209. +/**
  66210. + * Set value of hnpcapable field in the GUSBCFG register
  66211. + */
  66212. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  66213. +
  66214. +/**
  66215. + * Get value of srpcapable field in the GUSBCFG register
  66216. + */
  66217. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  66218. +/**
  66219. + * Set value of srpcapable field in the GUSBCFG register
  66220. + */
  66221. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  66222. +
  66223. +/**
  66224. + * Get value of devspeed field in the DCFG register
  66225. + */
  66226. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  66227. +/**
  66228. + * Set value of devspeed field in the DCFG register
  66229. + */
  66230. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  66231. +
  66232. +/**
  66233. + * Get the value of busconnected field from the HPRT0 register
  66234. + */
  66235. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  66236. +
  66237. +/**
  66238. + * Gets the device enumeration Speed.
  66239. + */
  66240. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  66241. +
  66242. +/**
  66243. + * Get value of prtpwr field from the HPRT0 register
  66244. + */
  66245. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  66246. +
  66247. +/**
  66248. + * Get value of flag indicating core state - hibernated or not
  66249. + */
  66250. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  66251. +
  66252. +/**
  66253. + * Set value of prtpwr field from the HPRT0 register
  66254. + */
  66255. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  66256. +
  66257. +/**
  66258. + * Get value of prtsusp field from the HPRT0 regsiter
  66259. + */
  66260. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  66261. +/**
  66262. + * Set value of prtpwr field from the HPRT0 register
  66263. + */
  66264. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  66265. +
  66266. +/**
  66267. + * Get value of ModeChTimEn field from the HCFG regsiter
  66268. + */
  66269. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  66270. +/**
  66271. + * Set value of ModeChTimEn field from the HCFG regsiter
  66272. + */
  66273. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  66274. +
  66275. +/**
  66276. + * Get value of Fram Interval field from the HFIR regsiter
  66277. + */
  66278. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  66279. +/**
  66280. + * Set value of Frame Interval field from the HFIR regsiter
  66281. + */
  66282. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  66283. +
  66284. +/**
  66285. + * Set value of prtres field from the HPRT0 register
  66286. + *FIXME Remove?
  66287. + */
  66288. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  66289. +
  66290. +/**
  66291. + * Get value of rmtwkupsig bit in DCTL register
  66292. + */
  66293. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  66294. +
  66295. +/**
  66296. + * Get value of prt_sleep_sts field from the GLPMCFG register
  66297. + */
  66298. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  66299. +
  66300. +/**
  66301. + * Get value of rem_wkup_en field from the GLPMCFG register
  66302. + */
  66303. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  66304. +
  66305. +/**
  66306. + * Get value of appl_resp field from the GLPMCFG register
  66307. + */
  66308. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  66309. +/**
  66310. + * Set value of appl_resp field from the GLPMCFG register
  66311. + */
  66312. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  66313. +
  66314. +/**
  66315. + * Get value of hsic_connect field from the GLPMCFG register
  66316. + */
  66317. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  66318. +/**
  66319. + * Set value of hsic_connect field from the GLPMCFG register
  66320. + */
  66321. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  66322. +
  66323. +/**
  66324. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  66325. + */
  66326. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  66327. +/**
  66328. + * Set value of inv_sel_hsic field from the GLPMFG register.
  66329. + */
  66330. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  66331. +
  66332. +/*
  66333. + * Some functions for accessing registers
  66334. + */
  66335. +
  66336. +/**
  66337. + * GOTGCTL register
  66338. + */
  66339. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  66340. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  66341. +
  66342. +/**
  66343. + * GUSBCFG register
  66344. + */
  66345. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  66346. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  66347. +
  66348. +/**
  66349. + * GRXFSIZ register
  66350. + */
  66351. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  66352. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  66353. +
  66354. +/**
  66355. + * GNPTXFSIZ register
  66356. + */
  66357. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  66358. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  66359. +
  66360. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  66361. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  66362. +
  66363. +/**
  66364. + * GGPIO register
  66365. + */
  66366. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  66367. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  66368. +
  66369. +/**
  66370. + * GUID register
  66371. + */
  66372. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  66373. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  66374. +
  66375. +/**
  66376. + * HPRT0 register
  66377. + */
  66378. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  66379. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  66380. +
  66381. +/**
  66382. + * GHPTXFSIZE
  66383. + */
  66384. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  66385. +
  66386. +/** @} */
  66387. +
  66388. +#endif /* __DWC_CORE_IF_H__ */
  66389. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_dbg.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  66390. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 1970-01-01 01:00:00.000000000 +0100
  66391. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 2014-03-11 17:51:27.000000000 +0100
  66392. @@ -0,0 +1,117 @@
  66393. +/* ==========================================================================
  66394. + *
  66395. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  66396. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  66397. + * otherwise expressly agreed to in writing between Synopsys and you.
  66398. + *
  66399. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  66400. + * any End User Software License Agreement or Agreement for Licensed Product
  66401. + * with Synopsys or any supplement thereto. You are permitted to use and
  66402. + * redistribute this Software in source and binary forms, with or without
  66403. + * modification, provided that redistributions of source code must retain this
  66404. + * notice. You may not view, use, disclose, copy or distribute this file or
  66405. + * any information contained herein except pursuant to this license grant from
  66406. + * Synopsys. If you do not agree with this notice, including the disclaimer
  66407. + * below, then you are not authorized to use the Software.
  66408. + *
  66409. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  66410. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  66411. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  66412. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  66413. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  66414. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  66415. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  66416. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66417. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  66418. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  66419. + * DAMAGE.
  66420. + * ========================================================================== */
  66421. +
  66422. +#ifndef __DWC_OTG_DBG_H__
  66423. +#define __DWC_OTG_DBG_H__
  66424. +
  66425. +/** @file
  66426. + * This file defines debug levels.
  66427. + * Debugging support vanishes in non-debug builds.
  66428. + */
  66429. +
  66430. +/**
  66431. + * The Debug Level bit-mask variable.
  66432. + */
  66433. +extern uint32_t g_dbg_lvl;
  66434. +/**
  66435. + * Set the Debug Level variable.
  66436. + */
  66437. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  66438. +{
  66439. + uint32_t old = g_dbg_lvl;
  66440. + g_dbg_lvl = new;
  66441. + return old;
  66442. +}
  66443. +
  66444. +#define DBG_USER (0x1)
  66445. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  66446. +#define DBG_CIL (0x2)
  66447. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  66448. + * messages */
  66449. +#define DBG_CILV (0x20)
  66450. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  66451. + * messages */
  66452. +#define DBG_PCD (0x4)
  66453. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  66454. + * messages */
  66455. +#define DBG_PCDV (0x40)
  66456. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  66457. +#define DBG_HCD (0x8)
  66458. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  66459. + * messages */
  66460. +#define DBG_HCDV (0x80)
  66461. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  66462. + * mode. */
  66463. +#define DBG_HCD_URB (0x800)
  66464. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  66465. + * messages. */
  66466. +#define DBG_HCDI (0x1000)
  66467. +
  66468. +/** When debug level has any bit set, display debug messages */
  66469. +#define DBG_ANY (0xFF)
  66470. +
  66471. +/** All debug messages off */
  66472. +#define DBG_OFF 0
  66473. +
  66474. +/** Prefix string for DWC_DEBUG print macros. */
  66475. +#define USB_DWC "DWC_otg: "
  66476. +
  66477. +/**
  66478. + * Print a debug message when the Global debug level variable contains
  66479. + * the bit defined in <code>lvl</code>.
  66480. + *
  66481. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  66482. + * @param[in] x - like printf
  66483. + *
  66484. + * Example:<p>
  66485. + * <code>
  66486. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  66487. + * </code>
  66488. + * <br>
  66489. + * results in:<br>
  66490. + * <code>
  66491. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  66492. + * </code>
  66493. + */
  66494. +#ifdef DEBUG
  66495. +
  66496. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  66497. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  66498. +
  66499. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  66500. +
  66501. +#else
  66502. +
  66503. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  66504. +# define DWC_DEBUGP(x...)
  66505. +
  66506. +# define CHK_DEBUG_LEVEL(level) (0)
  66507. +
  66508. +#endif /*DEBUG*/
  66509. +#endif
  66510. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_driver.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  66511. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_driver.c 1970-01-01 01:00:00.000000000 +0100
  66512. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.c 2014-03-11 17:51:27.000000000 +0100
  66513. @@ -0,0 +1,1742 @@
  66514. +/* ==========================================================================
  66515. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  66516. + * $Revision: #92 $
  66517. + * $Date: 2012/08/10 $
  66518. + * $Change: 2047372 $
  66519. + *
  66520. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  66521. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  66522. + * otherwise expressly agreed to in writing between Synopsys and you.
  66523. + *
  66524. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  66525. + * any End User Software License Agreement or Agreement for Licensed Product
  66526. + * with Synopsys or any supplement thereto. You are permitted to use and
  66527. + * redistribute this Software in source and binary forms, with or without
  66528. + * modification, provided that redistributions of source code must retain this
  66529. + * notice. You may not view, use, disclose, copy or distribute this file or
  66530. + * any information contained herein except pursuant to this license grant from
  66531. + * Synopsys. If you do not agree with this notice, including the disclaimer
  66532. + * below, then you are not authorized to use the Software.
  66533. + *
  66534. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  66535. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  66536. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  66537. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  66538. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  66539. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  66540. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  66541. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66542. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  66543. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  66544. + * DAMAGE.
  66545. + * ========================================================================== */
  66546. +
  66547. +/** @file
  66548. + * The dwc_otg_driver module provides the initialization and cleanup entry
  66549. + * points for the DWC_otg driver. This module will be dynamically installed
  66550. + * after Linux is booted using the insmod command. When the module is
  66551. + * installed, the dwc_otg_driver_init function is called. When the module is
  66552. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  66553. + *
  66554. + * This module also defines a data structure for the dwc_otg_driver, which is
  66555. + * used in conjunction with the standard ARM lm_device structure. These
  66556. + * structures allow the OTG driver to comply with the standard Linux driver
  66557. + * model in which devices and drivers are registered with a bus driver. This
  66558. + * has the benefit that Linux can expose attributes of the driver and device
  66559. + * in its special sysfs file system. Users can then read or write files in
  66560. + * this file system to perform diagnostics on the driver components or the
  66561. + * device.
  66562. + */
  66563. +
  66564. +#include "dwc_otg_os_dep.h"
  66565. +#include "dwc_os.h"
  66566. +#include "dwc_otg_dbg.h"
  66567. +#include "dwc_otg_driver.h"
  66568. +#include "dwc_otg_attr.h"
  66569. +#include "dwc_otg_core_if.h"
  66570. +#include "dwc_otg_pcd_if.h"
  66571. +#include "dwc_otg_hcd_if.h"
  66572. +
  66573. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  66574. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  66575. +
  66576. +bool microframe_schedule=true;
  66577. +
  66578. +static const char dwc_driver_name[] = "dwc_otg";
  66579. +
  66580. +extern void* dummy_send;
  66581. +
  66582. +extern int pcd_init(
  66583. +#ifdef LM_INTERFACE
  66584. + struct lm_device *_dev
  66585. +#elif defined(PCI_INTERFACE)
  66586. + struct pci_dev *_dev
  66587. +#elif defined(PLATFORM_INTERFACE)
  66588. + struct platform_device *dev
  66589. +#endif
  66590. + );
  66591. +extern int hcd_init(
  66592. +#ifdef LM_INTERFACE
  66593. + struct lm_device *_dev
  66594. +#elif defined(PCI_INTERFACE)
  66595. + struct pci_dev *_dev
  66596. +#elif defined(PLATFORM_INTERFACE)
  66597. + struct platform_device *dev
  66598. +#endif
  66599. + );
  66600. +
  66601. +extern int pcd_remove(
  66602. +#ifdef LM_INTERFACE
  66603. + struct lm_device *_dev
  66604. +#elif defined(PCI_INTERFACE)
  66605. + struct pci_dev *_dev
  66606. +#elif defined(PLATFORM_INTERFACE)
  66607. + struct platform_device *_dev
  66608. +#endif
  66609. + );
  66610. +
  66611. +extern void hcd_remove(
  66612. +#ifdef LM_INTERFACE
  66613. + struct lm_device *_dev
  66614. +#elif defined(PCI_INTERFACE)
  66615. + struct pci_dev *_dev
  66616. +#elif defined(PLATFORM_INTERFACE)
  66617. + struct platform_device *_dev
  66618. +#endif
  66619. + );
  66620. +
  66621. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  66622. +
  66623. +/*-------------------------------------------------------------------------*/
  66624. +/* Encapsulate the module parameter settings */
  66625. +
  66626. +struct dwc_otg_driver_module_params {
  66627. + int32_t opt;
  66628. + int32_t otg_cap;
  66629. + int32_t dma_enable;
  66630. + int32_t dma_desc_enable;
  66631. + int32_t dma_burst_size;
  66632. + int32_t speed;
  66633. + int32_t host_support_fs_ls_low_power;
  66634. + int32_t host_ls_low_power_phy_clk;
  66635. + int32_t enable_dynamic_fifo;
  66636. + int32_t data_fifo_size;
  66637. + int32_t dev_rx_fifo_size;
  66638. + int32_t dev_nperio_tx_fifo_size;
  66639. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  66640. + int32_t host_rx_fifo_size;
  66641. + int32_t host_nperio_tx_fifo_size;
  66642. + int32_t host_perio_tx_fifo_size;
  66643. + int32_t max_transfer_size;
  66644. + int32_t max_packet_count;
  66645. + int32_t host_channels;
  66646. + int32_t dev_endpoints;
  66647. + int32_t phy_type;
  66648. + int32_t phy_utmi_width;
  66649. + int32_t phy_ulpi_ddr;
  66650. + int32_t phy_ulpi_ext_vbus;
  66651. + int32_t i2c_enable;
  66652. + int32_t ulpi_fs_ls;
  66653. + int32_t ts_dline;
  66654. + int32_t en_multiple_tx_fifo;
  66655. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  66656. + uint32_t thr_ctl;
  66657. + uint32_t tx_thr_length;
  66658. + uint32_t rx_thr_length;
  66659. + int32_t pti_enable;
  66660. + int32_t mpi_enable;
  66661. + int32_t lpm_enable;
  66662. + int32_t ic_usb_cap;
  66663. + int32_t ahb_thr_ratio;
  66664. + int32_t power_down;
  66665. + int32_t reload_ctl;
  66666. + int32_t dev_out_nak;
  66667. + int32_t cont_on_bna;
  66668. + int32_t ahb_single;
  66669. + int32_t otg_ver;
  66670. + int32_t adp_enable;
  66671. +};
  66672. +
  66673. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  66674. + .opt = -1,
  66675. + .otg_cap = -1,
  66676. + .dma_enable = -1,
  66677. + .dma_desc_enable = -1,
  66678. + .dma_burst_size = -1,
  66679. + .speed = -1,
  66680. + .host_support_fs_ls_low_power = -1,
  66681. + .host_ls_low_power_phy_clk = -1,
  66682. + .enable_dynamic_fifo = -1,
  66683. + .data_fifo_size = -1,
  66684. + .dev_rx_fifo_size = -1,
  66685. + .dev_nperio_tx_fifo_size = -1,
  66686. + .dev_perio_tx_fifo_size = {
  66687. + /* dev_perio_tx_fifo_size_1 */
  66688. + -1,
  66689. + -1,
  66690. + -1,
  66691. + -1,
  66692. + -1,
  66693. + -1,
  66694. + -1,
  66695. + -1,
  66696. + -1,
  66697. + -1,
  66698. + -1,
  66699. + -1,
  66700. + -1,
  66701. + -1,
  66702. + -1
  66703. + /* 15 */
  66704. + },
  66705. + .host_rx_fifo_size = -1,
  66706. + .host_nperio_tx_fifo_size = -1,
  66707. + .host_perio_tx_fifo_size = -1,
  66708. + .max_transfer_size = -1,
  66709. + .max_packet_count = -1,
  66710. + .host_channels = -1,
  66711. + .dev_endpoints = -1,
  66712. + .phy_type = -1,
  66713. + .phy_utmi_width = -1,
  66714. + .phy_ulpi_ddr = -1,
  66715. + .phy_ulpi_ext_vbus = -1,
  66716. + .i2c_enable = -1,
  66717. + .ulpi_fs_ls = -1,
  66718. + .ts_dline = -1,
  66719. + .en_multiple_tx_fifo = -1,
  66720. + .dev_tx_fifo_size = {
  66721. + /* dev_tx_fifo_size */
  66722. + -1,
  66723. + -1,
  66724. + -1,
  66725. + -1,
  66726. + -1,
  66727. + -1,
  66728. + -1,
  66729. + -1,
  66730. + -1,
  66731. + -1,
  66732. + -1,
  66733. + -1,
  66734. + -1,
  66735. + -1,
  66736. + -1
  66737. + /* 15 */
  66738. + },
  66739. + .thr_ctl = -1,
  66740. + .tx_thr_length = -1,
  66741. + .rx_thr_length = -1,
  66742. + .pti_enable = -1,
  66743. + .mpi_enable = -1,
  66744. + .lpm_enable = 0,
  66745. + .ic_usb_cap = -1,
  66746. + .ahb_thr_ratio = -1,
  66747. + .power_down = -1,
  66748. + .reload_ctl = -1,
  66749. + .dev_out_nak = -1,
  66750. + .cont_on_bna = -1,
  66751. + .ahb_single = -1,
  66752. + .otg_ver = -1,
  66753. + .adp_enable = -1,
  66754. +};
  66755. +
  66756. +//Global variable to switch the fiq fix on or off (declared in bcm2708.c)
  66757. +extern bool fiq_fix_enable;
  66758. +// Global variable to enable the split transaction fix
  66759. +bool fiq_split_enable = true;
  66760. +//Global variable to switch the nak holdoff on or off
  66761. +bool nak_holdoff_enable = true;
  66762. +
  66763. +
  66764. +/**
  66765. + * This function shows the Driver Version.
  66766. + */
  66767. +static ssize_t version_show(struct device_driver *dev, char *buf)
  66768. +{
  66769. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  66770. + DWC_DRIVER_VERSION);
  66771. +}
  66772. +
  66773. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  66774. +
  66775. +/**
  66776. + * Global Debug Level Mask.
  66777. + */
  66778. +uint32_t g_dbg_lvl = 0; /* OFF */
  66779. +
  66780. +/**
  66781. + * This function shows the driver Debug Level.
  66782. + */
  66783. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  66784. +{
  66785. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  66786. +}
  66787. +
  66788. +/**
  66789. + * This function stores the driver Debug Level.
  66790. + */
  66791. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  66792. + size_t count)
  66793. +{
  66794. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  66795. + return count;
  66796. +}
  66797. +
  66798. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  66799. + dbg_level_store);
  66800. +
  66801. +/**
  66802. + * This function is called during module intialization
  66803. + * to pass module parameters to the DWC_OTG CORE.
  66804. + */
  66805. +static int set_parameters(dwc_otg_core_if_t * core_if)
  66806. +{
  66807. + int retval = 0;
  66808. + int i;
  66809. +
  66810. + if (dwc_otg_module_params.otg_cap != -1) {
  66811. + retval +=
  66812. + dwc_otg_set_param_otg_cap(core_if,
  66813. + dwc_otg_module_params.otg_cap);
  66814. + }
  66815. + if (dwc_otg_module_params.dma_enable != -1) {
  66816. + retval +=
  66817. + dwc_otg_set_param_dma_enable(core_if,
  66818. + dwc_otg_module_params.
  66819. + dma_enable);
  66820. + }
  66821. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  66822. + retval +=
  66823. + dwc_otg_set_param_dma_desc_enable(core_if,
  66824. + dwc_otg_module_params.
  66825. + dma_desc_enable);
  66826. + }
  66827. + if (dwc_otg_module_params.opt != -1) {
  66828. + retval +=
  66829. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  66830. + }
  66831. + if (dwc_otg_module_params.dma_burst_size != -1) {
  66832. + retval +=
  66833. + dwc_otg_set_param_dma_burst_size(core_if,
  66834. + dwc_otg_module_params.
  66835. + dma_burst_size);
  66836. + }
  66837. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  66838. + retval +=
  66839. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  66840. + dwc_otg_module_params.
  66841. + host_support_fs_ls_low_power);
  66842. + }
  66843. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  66844. + retval +=
  66845. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  66846. + dwc_otg_module_params.
  66847. + enable_dynamic_fifo);
  66848. + }
  66849. + if (dwc_otg_module_params.data_fifo_size != -1) {
  66850. + retval +=
  66851. + dwc_otg_set_param_data_fifo_size(core_if,
  66852. + dwc_otg_module_params.
  66853. + data_fifo_size);
  66854. + }
  66855. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  66856. + retval +=
  66857. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  66858. + dwc_otg_module_params.
  66859. + dev_rx_fifo_size);
  66860. + }
  66861. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  66862. + retval +=
  66863. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  66864. + dwc_otg_module_params.
  66865. + dev_nperio_tx_fifo_size);
  66866. + }
  66867. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  66868. + retval +=
  66869. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  66870. + dwc_otg_module_params.host_rx_fifo_size);
  66871. + }
  66872. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  66873. + retval +=
  66874. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  66875. + dwc_otg_module_params.
  66876. + host_nperio_tx_fifo_size);
  66877. + }
  66878. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  66879. + retval +=
  66880. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  66881. + dwc_otg_module_params.
  66882. + host_perio_tx_fifo_size);
  66883. + }
  66884. + if (dwc_otg_module_params.max_transfer_size != -1) {
  66885. + retval +=
  66886. + dwc_otg_set_param_max_transfer_size(core_if,
  66887. + dwc_otg_module_params.
  66888. + max_transfer_size);
  66889. + }
  66890. + if (dwc_otg_module_params.max_packet_count != -1) {
  66891. + retval +=
  66892. + dwc_otg_set_param_max_packet_count(core_if,
  66893. + dwc_otg_module_params.
  66894. + max_packet_count);
  66895. + }
  66896. + if (dwc_otg_module_params.host_channels != -1) {
  66897. + retval +=
  66898. + dwc_otg_set_param_host_channels(core_if,
  66899. + dwc_otg_module_params.
  66900. + host_channels);
  66901. + }
  66902. + if (dwc_otg_module_params.dev_endpoints != -1) {
  66903. + retval +=
  66904. + dwc_otg_set_param_dev_endpoints(core_if,
  66905. + dwc_otg_module_params.
  66906. + dev_endpoints);
  66907. + }
  66908. + if (dwc_otg_module_params.phy_type != -1) {
  66909. + retval +=
  66910. + dwc_otg_set_param_phy_type(core_if,
  66911. + dwc_otg_module_params.phy_type);
  66912. + }
  66913. + if (dwc_otg_module_params.speed != -1) {
  66914. + retval +=
  66915. + dwc_otg_set_param_speed(core_if,
  66916. + dwc_otg_module_params.speed);
  66917. + }
  66918. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  66919. + retval +=
  66920. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  66921. + dwc_otg_module_params.
  66922. + host_ls_low_power_phy_clk);
  66923. + }
  66924. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  66925. + retval +=
  66926. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  66927. + dwc_otg_module_params.
  66928. + phy_ulpi_ddr);
  66929. + }
  66930. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  66931. + retval +=
  66932. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  66933. + dwc_otg_module_params.
  66934. + phy_ulpi_ext_vbus);
  66935. + }
  66936. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  66937. + retval +=
  66938. + dwc_otg_set_param_phy_utmi_width(core_if,
  66939. + dwc_otg_module_params.
  66940. + phy_utmi_width);
  66941. + }
  66942. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  66943. + retval +=
  66944. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  66945. + dwc_otg_module_params.ulpi_fs_ls);
  66946. + }
  66947. + if (dwc_otg_module_params.ts_dline != -1) {
  66948. + retval +=
  66949. + dwc_otg_set_param_ts_dline(core_if,
  66950. + dwc_otg_module_params.ts_dline);
  66951. + }
  66952. + if (dwc_otg_module_params.i2c_enable != -1) {
  66953. + retval +=
  66954. + dwc_otg_set_param_i2c_enable(core_if,
  66955. + dwc_otg_module_params.
  66956. + i2c_enable);
  66957. + }
  66958. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  66959. + retval +=
  66960. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  66961. + dwc_otg_module_params.
  66962. + en_multiple_tx_fifo);
  66963. + }
  66964. + for (i = 0; i < 15; i++) {
  66965. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  66966. + retval +=
  66967. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  66968. + dwc_otg_module_params.
  66969. + dev_perio_tx_fifo_size
  66970. + [i], i);
  66971. + }
  66972. + }
  66973. +
  66974. + for (i = 0; i < 15; i++) {
  66975. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  66976. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  66977. + dwc_otg_module_params.
  66978. + dev_tx_fifo_size
  66979. + [i], i);
  66980. + }
  66981. + }
  66982. + if (dwc_otg_module_params.thr_ctl != -1) {
  66983. + retval +=
  66984. + dwc_otg_set_param_thr_ctl(core_if,
  66985. + dwc_otg_module_params.thr_ctl);
  66986. + }
  66987. + if (dwc_otg_module_params.mpi_enable != -1) {
  66988. + retval +=
  66989. + dwc_otg_set_param_mpi_enable(core_if,
  66990. + dwc_otg_module_params.
  66991. + mpi_enable);
  66992. + }
  66993. + if (dwc_otg_module_params.pti_enable != -1) {
  66994. + retval +=
  66995. + dwc_otg_set_param_pti_enable(core_if,
  66996. + dwc_otg_module_params.
  66997. + pti_enable);
  66998. + }
  66999. + if (dwc_otg_module_params.lpm_enable != -1) {
  67000. + retval +=
  67001. + dwc_otg_set_param_lpm_enable(core_if,
  67002. + dwc_otg_module_params.
  67003. + lpm_enable);
  67004. + }
  67005. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  67006. + retval +=
  67007. + dwc_otg_set_param_ic_usb_cap(core_if,
  67008. + dwc_otg_module_params.
  67009. + ic_usb_cap);
  67010. + }
  67011. + if (dwc_otg_module_params.tx_thr_length != -1) {
  67012. + retval +=
  67013. + dwc_otg_set_param_tx_thr_length(core_if,
  67014. + dwc_otg_module_params.tx_thr_length);
  67015. + }
  67016. + if (dwc_otg_module_params.rx_thr_length != -1) {
  67017. + retval +=
  67018. + dwc_otg_set_param_rx_thr_length(core_if,
  67019. + dwc_otg_module_params.
  67020. + rx_thr_length);
  67021. + }
  67022. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  67023. + retval +=
  67024. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  67025. + dwc_otg_module_params.ahb_thr_ratio);
  67026. + }
  67027. + if (dwc_otg_module_params.power_down != -1) {
  67028. + retval +=
  67029. + dwc_otg_set_param_power_down(core_if,
  67030. + dwc_otg_module_params.power_down);
  67031. + }
  67032. + if (dwc_otg_module_params.reload_ctl != -1) {
  67033. + retval +=
  67034. + dwc_otg_set_param_reload_ctl(core_if,
  67035. + dwc_otg_module_params.reload_ctl);
  67036. + }
  67037. +
  67038. + if (dwc_otg_module_params.dev_out_nak != -1) {
  67039. + retval +=
  67040. + dwc_otg_set_param_dev_out_nak(core_if,
  67041. + dwc_otg_module_params.dev_out_nak);
  67042. + }
  67043. +
  67044. + if (dwc_otg_module_params.cont_on_bna != -1) {
  67045. + retval +=
  67046. + dwc_otg_set_param_cont_on_bna(core_if,
  67047. + dwc_otg_module_params.cont_on_bna);
  67048. + }
  67049. +
  67050. + if (dwc_otg_module_params.ahb_single != -1) {
  67051. + retval +=
  67052. + dwc_otg_set_param_ahb_single(core_if,
  67053. + dwc_otg_module_params.ahb_single);
  67054. + }
  67055. +
  67056. + if (dwc_otg_module_params.otg_ver != -1) {
  67057. + retval +=
  67058. + dwc_otg_set_param_otg_ver(core_if,
  67059. + dwc_otg_module_params.otg_ver);
  67060. + }
  67061. + if (dwc_otg_module_params.adp_enable != -1) {
  67062. + retval +=
  67063. + dwc_otg_set_param_adp_enable(core_if,
  67064. + dwc_otg_module_params.
  67065. + adp_enable);
  67066. + }
  67067. + return retval;
  67068. +}
  67069. +
  67070. +/**
  67071. + * This function is the top level interrupt handler for the Common
  67072. + * (Device and host modes) interrupts.
  67073. + */
  67074. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  67075. +{
  67076. + int32_t retval = IRQ_NONE;
  67077. +
  67078. + retval = dwc_otg_handle_common_intr(dev);
  67079. + if (retval != 0) {
  67080. + S3C2410X_CLEAR_EINTPEND();
  67081. + }
  67082. + return IRQ_RETVAL(retval);
  67083. +}
  67084. +
  67085. +/**
  67086. + * This function is called when a lm_device is unregistered with the
  67087. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  67088. + * executed. The device may or may not be electrically present. If it is
  67089. + * present, the driver stops device processing. Any resources used on behalf
  67090. + * of this device are freed.
  67091. + *
  67092. + * @param _dev
  67093. + */
  67094. +#ifdef LM_INTERFACE
  67095. +#define REM_RETVAL(n)
  67096. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  67097. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  67098. +#elif defined(PCI_INTERFACE)
  67099. +#define REM_RETVAL(n)
  67100. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  67101. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  67102. +#elif defined(PLATFORM_INTERFACE)
  67103. +#define REM_RETVAL(n) n
  67104. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  67105. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  67106. +#endif
  67107. +
  67108. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  67109. +
  67110. + if (!otg_dev) {
  67111. + /* Memory allocation for the dwc_otg_device failed. */
  67112. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  67113. + return REM_RETVAL(-ENOMEM);
  67114. + }
  67115. +#ifndef DWC_DEVICE_ONLY
  67116. + if (otg_dev->hcd) {
  67117. + hcd_remove(_dev);
  67118. + } else {
  67119. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  67120. + return REM_RETVAL(-EINVAL);
  67121. + }
  67122. +#endif
  67123. +
  67124. +#ifndef DWC_HOST_ONLY
  67125. + if (otg_dev->pcd) {
  67126. + pcd_remove(_dev);
  67127. + } else {
  67128. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  67129. + return REM_RETVAL(-EINVAL);
  67130. + }
  67131. +#endif
  67132. + /*
  67133. + * Free the IRQ
  67134. + */
  67135. + if (otg_dev->common_irq_installed) {
  67136. +#ifdef PLATFORM_INTERFACE
  67137. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  67138. +#else
  67139. + free_irq(_dev->irq, otg_dev);
  67140. +#endif
  67141. + } else {
  67142. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  67143. + return REM_RETVAL(-ENXIO);
  67144. + }
  67145. +
  67146. + if (otg_dev->core_if) {
  67147. + dwc_otg_cil_remove(otg_dev->core_if);
  67148. + } else {
  67149. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  67150. + return REM_RETVAL(-ENXIO);
  67151. + }
  67152. +
  67153. + /*
  67154. + * Remove the device attributes
  67155. + */
  67156. + dwc_otg_attr_remove(_dev);
  67157. +
  67158. + /*
  67159. + * Return the memory.
  67160. + */
  67161. + if (otg_dev->os_dep.base) {
  67162. + iounmap(otg_dev->os_dep.base);
  67163. + }
  67164. + DWC_FREE(otg_dev);
  67165. +
  67166. + /*
  67167. + * Clear the drvdata pointer.
  67168. + */
  67169. +#ifdef LM_INTERFACE
  67170. + lm_set_drvdata(_dev, 0);
  67171. +#elif defined(PCI_INTERFACE)
  67172. + release_mem_region(otg_dev->os_dep.rsrc_start,
  67173. + otg_dev->os_dep.rsrc_len);
  67174. + pci_set_drvdata(_dev, 0);
  67175. +#elif defined(PLATFORM_INTERFACE)
  67176. + platform_set_drvdata(_dev, 0);
  67177. +#endif
  67178. + return REM_RETVAL(0);
  67179. +}
  67180. +
  67181. +/**
  67182. + * This function is called when an lm_device is bound to a
  67183. + * dwc_otg_driver. It creates the driver components required to
  67184. + * control the device (CIL, HCD, and PCD) and it initializes the
  67185. + * device. The driver components are stored in a dwc_otg_device
  67186. + * structure. A reference to the dwc_otg_device is saved in the
  67187. + * lm_device. This allows the driver to access the dwc_otg_device
  67188. + * structure on subsequent calls to driver methods for this device.
  67189. + *
  67190. + * @param _dev Bus device
  67191. + */
  67192. +static int dwc_otg_driver_probe(
  67193. +#ifdef LM_INTERFACE
  67194. + struct lm_device *_dev
  67195. +#elif defined(PCI_INTERFACE)
  67196. + struct pci_dev *_dev,
  67197. + const struct pci_device_id *id
  67198. +#elif defined(PLATFORM_INTERFACE)
  67199. + struct platform_device *_dev
  67200. +#endif
  67201. + )
  67202. +{
  67203. + int retval = 0;
  67204. + dwc_otg_device_t *dwc_otg_device;
  67205. + int devirq;
  67206. +
  67207. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  67208. +#ifdef LM_INTERFACE
  67209. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  67210. +#elif defined(PCI_INTERFACE)
  67211. + if (!id) {
  67212. + DWC_ERROR("Invalid pci_device_id %p", id);
  67213. + return -EINVAL;
  67214. + }
  67215. +
  67216. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  67217. + DWC_ERROR("Invalid pci_device %p", _dev);
  67218. + return -ENODEV;
  67219. + }
  67220. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  67221. + /* other stuff needed as well? */
  67222. +
  67223. +#elif defined(PLATFORM_INTERFACE)
  67224. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  67225. + (unsigned)_dev->resource->start,
  67226. + (unsigned)(_dev->resource->end - _dev->resource->start));
  67227. +#endif
  67228. +
  67229. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  67230. +
  67231. + if (!dwc_otg_device) {
  67232. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  67233. + return -ENOMEM;
  67234. + }
  67235. +
  67236. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  67237. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  67238. +
  67239. + /*
  67240. + * Map the DWC_otg Core memory into virtual address space.
  67241. + */
  67242. +#ifdef LM_INTERFACE
  67243. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  67244. +
  67245. + if (!dwc_otg_device->os_dep.base) {
  67246. + dev_err(&_dev->dev, "ioremap() failed\n");
  67247. + DWC_FREE(dwc_otg_device);
  67248. + return -ENOMEM;
  67249. + }
  67250. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  67251. + (unsigned)dwc_otg_device->os_dep.base);
  67252. +#elif defined(PCI_INTERFACE)
  67253. + _dev->current_state = PCI_D0;
  67254. + _dev->dev.power.power_state = PMSG_ON;
  67255. +
  67256. + if (!_dev->irq) {
  67257. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  67258. + pci_name(_dev));
  67259. + iounmap(dwc_otg_device->os_dep.base);
  67260. + DWC_FREE(dwc_otg_device);
  67261. + return -ENODEV;
  67262. + }
  67263. +
  67264. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  67265. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  67266. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  67267. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  67268. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  67269. + if (!request_mem_region
  67270. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  67271. + "dwc_otg")) {
  67272. + dev_dbg(&_dev->dev, "error requesting memory\n");
  67273. + iounmap(dwc_otg_device->os_dep.base);
  67274. + DWC_FREE(dwc_otg_device);
  67275. + return -EFAULT;
  67276. + }
  67277. +
  67278. + dwc_otg_device->os_dep.base =
  67279. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  67280. + dwc_otg_device->os_dep.rsrc_len);
  67281. + if (dwc_otg_device->os_dep.base == NULL) {
  67282. + dev_dbg(&_dev->dev, "error mapping memory\n");
  67283. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  67284. + dwc_otg_device->os_dep.rsrc_len);
  67285. + iounmap(dwc_otg_device->os_dep.base);
  67286. + DWC_FREE(dwc_otg_device);
  67287. + return -EFAULT;
  67288. + }
  67289. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  67290. + dwc_otg_device->os_dep.base);
  67291. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  67292. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  67293. + dwc_otg_device->os_dep.base);
  67294. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  67295. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  67296. + dwc_otg_device->os_dep.base);
  67297. +
  67298. + pci_set_master(_dev);
  67299. + pci_set_drvdata(_dev, dwc_otg_device);
  67300. +#elif defined(PLATFORM_INTERFACE)
  67301. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  67302. + _dev->resource->start,
  67303. + _dev->resource->end - _dev->resource->start + 1);
  67304. +#if 1
  67305. + if (!request_mem_region(_dev->resource[0].start,
  67306. + _dev->resource[0].end - _dev->resource[0].start + 1,
  67307. + "dwc_otg")) {
  67308. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  67309. + retval = -EFAULT;
  67310. + goto fail;
  67311. + }
  67312. +
  67313. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  67314. + _dev->resource[0].end -
  67315. + _dev->resource[0].start+1);
  67316. + if (fiq_fix_enable)
  67317. + {
  67318. + if (!request_mem_region(_dev->resource[1].start,
  67319. + _dev->resource[1].end - _dev->resource[1].start + 1,
  67320. + "dwc_otg")) {
  67321. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  67322. + retval = -EFAULT;
  67323. + goto fail;
  67324. + }
  67325. +
  67326. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  67327. + _dev->resource[1].end -
  67328. + _dev->resource[1].start + 1);
  67329. + dummy_send = (void *) kmalloc(16, GFP_ATOMIC);
  67330. + }
  67331. +
  67332. +#else
  67333. + {
  67334. + struct map_desc desc = {
  67335. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  67336. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  67337. + .length = SZ_128K,
  67338. + .type = MT_DEVICE
  67339. + };
  67340. + iotable_init(&desc, 1);
  67341. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  67342. + }
  67343. +#endif
  67344. + if (!dwc_otg_device->os_dep.base) {
  67345. + dev_err(&_dev->dev, "ioremap() failed\n");
  67346. + retval = -ENOMEM;
  67347. + goto fail;
  67348. + }
  67349. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  67350. + (unsigned)dwc_otg_device->os_dep.base);
  67351. +#endif
  67352. +
  67353. + /*
  67354. + * Initialize driver data to point to the global DWC_otg
  67355. + * Device structure.
  67356. + */
  67357. +#ifdef LM_INTERFACE
  67358. + lm_set_drvdata(_dev, dwc_otg_device);
  67359. +#elif defined(PLATFORM_INTERFACE)
  67360. + platform_set_drvdata(_dev, dwc_otg_device);
  67361. +#endif
  67362. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  67363. +
  67364. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  67365. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  67366. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  67367. +
  67368. + if (!dwc_otg_device->core_if) {
  67369. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  67370. + retval = -ENOMEM;
  67371. + goto fail;
  67372. + }
  67373. +
  67374. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  67375. + /*
  67376. + * Attempt to ensure this device is really a DWC_otg Controller.
  67377. + * Read and verify the SNPSID register contents. The value should be
  67378. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  67379. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  67380. + */
  67381. +
  67382. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  67383. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  67384. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  67385. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  67386. + retval = -EINVAL;
  67387. + goto fail;
  67388. + }
  67389. +
  67390. + /*
  67391. + * Validate parameter values.
  67392. + */
  67393. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  67394. + if (set_parameters(dwc_otg_device->core_if)) {
  67395. + retval = -EINVAL;
  67396. + goto fail;
  67397. + }
  67398. +
  67399. + /*
  67400. + * Create Device Attributes in sysfs
  67401. + */
  67402. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  67403. + dwc_otg_attr_create(_dev);
  67404. +
  67405. + /*
  67406. + * Disable the global interrupt until all the interrupt
  67407. + * handlers are installed.
  67408. + */
  67409. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  67410. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  67411. +
  67412. + /*
  67413. + * Install the interrupt handler for the common interrupts before
  67414. + * enabling common interrupts in core_init below.
  67415. + */
  67416. +
  67417. +#if defined(PLATFORM_INTERFACE)
  67418. + devirq = platform_get_irq(_dev, 0);
  67419. +#else
  67420. + devirq = _dev->irq;
  67421. +#endif
  67422. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  67423. + devirq);
  67424. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  67425. + retval = request_irq(devirq, dwc_otg_common_irq,
  67426. + IRQF_SHARED,
  67427. + "dwc_otg", dwc_otg_device);
  67428. + if (retval) {
  67429. + DWC_ERROR("request of irq%d failed\n", devirq);
  67430. + retval = -EBUSY;
  67431. + goto fail;
  67432. + } else {
  67433. + dwc_otg_device->common_irq_installed = 1;
  67434. + }
  67435. +
  67436. +#ifndef IRQF_TRIGGER_LOW
  67437. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  67438. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  67439. + set_irq_type(devirq,
  67440. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  67441. + IRQT_LOW
  67442. +#else
  67443. + IRQ_TYPE_LEVEL_LOW
  67444. +#endif
  67445. + );
  67446. +#endif
  67447. +#endif /*IRQF_TRIGGER_LOW*/
  67448. +
  67449. + /*
  67450. + * Initialize the DWC_otg core.
  67451. + */
  67452. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  67453. + dwc_otg_core_init(dwc_otg_device->core_if);
  67454. +
  67455. +#ifndef DWC_HOST_ONLY
  67456. + /*
  67457. + * Initialize the PCD
  67458. + */
  67459. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  67460. + retval = pcd_init(_dev);
  67461. + if (retval != 0) {
  67462. + DWC_ERROR("pcd_init failed\n");
  67463. + dwc_otg_device->pcd = NULL;
  67464. + goto fail;
  67465. + }
  67466. +#endif
  67467. +#ifndef DWC_DEVICE_ONLY
  67468. + /*
  67469. + * Initialize the HCD
  67470. + */
  67471. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  67472. + retval = hcd_init(_dev);
  67473. + if (retval != 0) {
  67474. + DWC_ERROR("hcd_init failed\n");
  67475. + dwc_otg_device->hcd = NULL;
  67476. + goto fail;
  67477. + }
  67478. +#endif
  67479. + /* Recover from drvdata having been overwritten by hcd_init() */
  67480. +#ifdef LM_INTERFACE
  67481. + lm_set_drvdata(_dev, dwc_otg_device);
  67482. +#elif defined(PLATFORM_INTERFACE)
  67483. + platform_set_drvdata(_dev, dwc_otg_device);
  67484. +#elif defined(PCI_INTERFACE)
  67485. + pci_set_drvdata(_dev, dwc_otg_device);
  67486. + dwc_otg_device->os_dep.pcidev = _dev;
  67487. +#endif
  67488. +
  67489. + /*
  67490. + * Enable the global interrupt after all the interrupt
  67491. + * handlers are installed if there is no ADP support else
  67492. + * perform initial actions required for Internal ADP logic.
  67493. + */
  67494. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  67495. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  67496. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  67497. + dev_dbg(&_dev->dev, "Done\n");
  67498. + } else
  67499. + dwc_otg_adp_start(dwc_otg_device->core_if,
  67500. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  67501. +
  67502. + return 0;
  67503. +
  67504. +fail:
  67505. + dwc_otg_driver_remove(_dev);
  67506. + return retval;
  67507. +}
  67508. +
  67509. +/**
  67510. + * This structure defines the methods to be called by a bus driver
  67511. + * during the lifecycle of a device on that bus. Both drivers and
  67512. + * devices are registered with a bus driver. The bus driver matches
  67513. + * devices to drivers based on information in the device and driver
  67514. + * structures.
  67515. + *
  67516. + * The probe function is called when the bus driver matches a device
  67517. + * to this driver. The remove function is called when a device is
  67518. + * unregistered with the bus driver.
  67519. + */
  67520. +#ifdef LM_INTERFACE
  67521. +static struct lm_driver dwc_otg_driver = {
  67522. + .drv = {.name = (char *)dwc_driver_name,},
  67523. + .probe = dwc_otg_driver_probe,
  67524. + .remove = dwc_otg_driver_remove,
  67525. + // 'suspend' and 'resume' absent
  67526. +};
  67527. +#elif defined(PCI_INTERFACE)
  67528. +static const struct pci_device_id pci_ids[] = { {
  67529. + PCI_DEVICE(0x16c3, 0xabcd),
  67530. + .driver_data =
  67531. + (unsigned long)0xdeadbeef,
  67532. + }, { /* end: all zeroes */ }
  67533. +};
  67534. +
  67535. +MODULE_DEVICE_TABLE(pci, pci_ids);
  67536. +
  67537. +/* pci driver glue; this is a "new style" PCI driver module */
  67538. +static struct pci_driver dwc_otg_driver = {
  67539. + .name = "dwc_otg",
  67540. + .id_table = pci_ids,
  67541. +
  67542. + .probe = dwc_otg_driver_probe,
  67543. + .remove = dwc_otg_driver_remove,
  67544. +
  67545. + .driver = {
  67546. + .name = (char *)dwc_driver_name,
  67547. + },
  67548. +};
  67549. +#elif defined(PLATFORM_INTERFACE)
  67550. +static struct platform_device_id platform_ids[] = {
  67551. + {
  67552. + .name = "bcm2708_usb",
  67553. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  67554. + },
  67555. + { /* end: all zeroes */ }
  67556. +};
  67557. +MODULE_DEVICE_TABLE(platform, platform_ids);
  67558. +
  67559. +static struct platform_driver dwc_otg_driver = {
  67560. + .driver = {
  67561. + .name = (char *)dwc_driver_name,
  67562. + },
  67563. + .id_table = platform_ids,
  67564. +
  67565. + .probe = dwc_otg_driver_probe,
  67566. + .remove = dwc_otg_driver_remove,
  67567. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  67568. +};
  67569. +#endif
  67570. +
  67571. +/**
  67572. + * This function is called when the dwc_otg_driver is installed with the
  67573. + * insmod command. It registers the dwc_otg_driver structure with the
  67574. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  67575. + * to be called. In addition, the bus driver will automatically expose
  67576. + * attributes defined for the device and driver in the special sysfs file
  67577. + * system.
  67578. + *
  67579. + * @return
  67580. + */
  67581. +static int __init dwc_otg_driver_init(void)
  67582. +{
  67583. + int retval = 0;
  67584. + int error;
  67585. + struct device_driver *drv;
  67586. +
  67587. + if(fiq_split_enable && !fiq_fix_enable) {
  67588. + printk(KERN_WARNING "dwc_otg: fiq_split_enable was set without fiq_fix_enable! Correcting.\n");
  67589. + fiq_fix_enable = 1;
  67590. + }
  67591. +
  67592. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  67593. + DWC_DRIVER_VERSION,
  67594. +#ifdef LM_INTERFACE
  67595. + "logicmodule");
  67596. + retval = lm_driver_register(&dwc_otg_driver);
  67597. + drv = &dwc_otg_driver.drv;
  67598. +#elif defined(PCI_INTERFACE)
  67599. + "pci");
  67600. + retval = pci_register_driver(&dwc_otg_driver);
  67601. + drv = &dwc_otg_driver.driver;
  67602. +#elif defined(PLATFORM_INTERFACE)
  67603. + "platform");
  67604. + retval = platform_driver_register(&dwc_otg_driver);
  67605. + drv = &dwc_otg_driver.driver;
  67606. +#endif
  67607. + if (retval < 0) {
  67608. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  67609. + return retval;
  67610. + }
  67611. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_fix_enable ? "enabled":"disabled");
  67612. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff_enable ? "enabled":"disabled");
  67613. + printk(KERN_DEBUG "dwc_otg: FIQ split fix %s\n", fiq_split_enable ? "enabled":"disabled");
  67614. +
  67615. + error = driver_create_file(drv, &driver_attr_version);
  67616. +#ifdef DEBUG
  67617. + error = driver_create_file(drv, &driver_attr_debuglevel);
  67618. +#endif
  67619. + return retval;
  67620. +}
  67621. +
  67622. +module_init(dwc_otg_driver_init);
  67623. +
  67624. +/**
  67625. + * This function is called when the driver is removed from the kernel
  67626. + * with the rmmod command. The driver unregisters itself with its bus
  67627. + * driver.
  67628. + *
  67629. + */
  67630. +static void __exit dwc_otg_driver_cleanup(void)
  67631. +{
  67632. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  67633. +
  67634. +#ifdef LM_INTERFACE
  67635. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  67636. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  67637. + lm_driver_unregister(&dwc_otg_driver);
  67638. +#elif defined(PCI_INTERFACE)
  67639. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  67640. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  67641. + pci_unregister_driver(&dwc_otg_driver);
  67642. +#elif defined(PLATFORM_INTERFACE)
  67643. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  67644. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  67645. + platform_driver_unregister(&dwc_otg_driver);
  67646. +#endif
  67647. +
  67648. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  67649. +}
  67650. +
  67651. +module_exit(dwc_otg_driver_cleanup);
  67652. +
  67653. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  67654. +MODULE_AUTHOR("Synopsys Inc.");
  67655. +MODULE_LICENSE("GPL");
  67656. +
  67657. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  67658. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  67659. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  67660. +MODULE_PARM_DESC(opt, "OPT Mode");
  67661. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  67662. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  67663. +
  67664. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  67665. + 0444);
  67666. +MODULE_PARM_DESC(dma_desc_enable,
  67667. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  67668. +
  67669. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  67670. + 0444);
  67671. +MODULE_PARM_DESC(dma_burst_size,
  67672. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  67673. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  67674. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  67675. +module_param_named(host_support_fs_ls_low_power,
  67676. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  67677. + 0444);
  67678. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  67679. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  67680. +module_param_named(host_ls_low_power_phy_clk,
  67681. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  67682. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  67683. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  67684. +module_param_named(enable_dynamic_fifo,
  67685. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  67686. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  67687. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  67688. + 0444);
  67689. +MODULE_PARM_DESC(data_fifo_size,
  67690. + "Total number of words in the data FIFO memory 32-32768");
  67691. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  67692. + int, 0444);
  67693. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  67694. +module_param_named(dev_nperio_tx_fifo_size,
  67695. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  67696. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  67697. + "Number of words in the non-periodic Tx FIFO 16-32768");
  67698. +module_param_named(dev_perio_tx_fifo_size_1,
  67699. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  67700. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  67701. + "Number of words in the periodic Tx FIFO 4-768");
  67702. +module_param_named(dev_perio_tx_fifo_size_2,
  67703. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  67704. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  67705. + "Number of words in the periodic Tx FIFO 4-768");
  67706. +module_param_named(dev_perio_tx_fifo_size_3,
  67707. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  67708. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  67709. + "Number of words in the periodic Tx FIFO 4-768");
  67710. +module_param_named(dev_perio_tx_fifo_size_4,
  67711. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  67712. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  67713. + "Number of words in the periodic Tx FIFO 4-768");
  67714. +module_param_named(dev_perio_tx_fifo_size_5,
  67715. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  67716. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  67717. + "Number of words in the periodic Tx FIFO 4-768");
  67718. +module_param_named(dev_perio_tx_fifo_size_6,
  67719. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  67720. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  67721. + "Number of words in the periodic Tx FIFO 4-768");
  67722. +module_param_named(dev_perio_tx_fifo_size_7,
  67723. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  67724. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  67725. + "Number of words in the periodic Tx FIFO 4-768");
  67726. +module_param_named(dev_perio_tx_fifo_size_8,
  67727. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  67728. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  67729. + "Number of words in the periodic Tx FIFO 4-768");
  67730. +module_param_named(dev_perio_tx_fifo_size_9,
  67731. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  67732. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  67733. + "Number of words in the periodic Tx FIFO 4-768");
  67734. +module_param_named(dev_perio_tx_fifo_size_10,
  67735. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  67736. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  67737. + "Number of words in the periodic Tx FIFO 4-768");
  67738. +module_param_named(dev_perio_tx_fifo_size_11,
  67739. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  67740. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  67741. + "Number of words in the periodic Tx FIFO 4-768");
  67742. +module_param_named(dev_perio_tx_fifo_size_12,
  67743. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  67744. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  67745. + "Number of words in the periodic Tx FIFO 4-768");
  67746. +module_param_named(dev_perio_tx_fifo_size_13,
  67747. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  67748. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  67749. + "Number of words in the periodic Tx FIFO 4-768");
  67750. +module_param_named(dev_perio_tx_fifo_size_14,
  67751. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  67752. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  67753. + "Number of words in the periodic Tx FIFO 4-768");
  67754. +module_param_named(dev_perio_tx_fifo_size_15,
  67755. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  67756. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  67757. + "Number of words in the periodic Tx FIFO 4-768");
  67758. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  67759. + int, 0444);
  67760. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  67761. +module_param_named(host_nperio_tx_fifo_size,
  67762. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  67763. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  67764. + "Number of words in the non-periodic Tx FIFO 16-32768");
  67765. +module_param_named(host_perio_tx_fifo_size,
  67766. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  67767. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  67768. + "Number of words in the host periodic Tx FIFO 16-32768");
  67769. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  67770. + int, 0444);
  67771. +/** @todo Set the max to 512K, modify checks */
  67772. +MODULE_PARM_DESC(max_transfer_size,
  67773. + "The maximum transfer size supported in bytes 2047-65535");
  67774. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  67775. + int, 0444);
  67776. +MODULE_PARM_DESC(max_packet_count,
  67777. + "The maximum number of packets in a transfer 15-511");
  67778. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  67779. + 0444);
  67780. +MODULE_PARM_DESC(host_channels,
  67781. + "The number of host channel registers to use 1-16");
  67782. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  67783. + 0444);
  67784. +MODULE_PARM_DESC(dev_endpoints,
  67785. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  67786. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  67787. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  67788. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  67789. + 0444);
  67790. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  67791. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  67792. +MODULE_PARM_DESC(phy_ulpi_ddr,
  67793. + "ULPI at double or single data rate 0=Single 1=Double");
  67794. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  67795. + int, 0444);
  67796. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  67797. + "ULPI PHY using internal or external vbus 0=Internal");
  67798. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  67799. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  67800. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  67801. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  67802. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  67803. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  67804. +module_param_named(debug, g_dbg_lvl, int, 0444);
  67805. +MODULE_PARM_DESC(debug, "");
  67806. +
  67807. +module_param_named(en_multiple_tx_fifo,
  67808. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  67809. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  67810. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  67811. +module_param_named(dev_tx_fifo_size_1,
  67812. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  67813. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  67814. +module_param_named(dev_tx_fifo_size_2,
  67815. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  67816. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  67817. +module_param_named(dev_tx_fifo_size_3,
  67818. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  67819. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  67820. +module_param_named(dev_tx_fifo_size_4,
  67821. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  67822. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  67823. +module_param_named(dev_tx_fifo_size_5,
  67824. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  67825. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  67826. +module_param_named(dev_tx_fifo_size_6,
  67827. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  67828. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  67829. +module_param_named(dev_tx_fifo_size_7,
  67830. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  67831. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  67832. +module_param_named(dev_tx_fifo_size_8,
  67833. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  67834. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  67835. +module_param_named(dev_tx_fifo_size_9,
  67836. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  67837. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  67838. +module_param_named(dev_tx_fifo_size_10,
  67839. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  67840. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  67841. +module_param_named(dev_tx_fifo_size_11,
  67842. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  67843. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  67844. +module_param_named(dev_tx_fifo_size_12,
  67845. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  67846. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  67847. +module_param_named(dev_tx_fifo_size_13,
  67848. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  67849. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  67850. +module_param_named(dev_tx_fifo_size_14,
  67851. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  67852. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  67853. +module_param_named(dev_tx_fifo_size_15,
  67854. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  67855. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  67856. +
  67857. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  67858. +MODULE_PARM_DESC(thr_ctl,
  67859. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  67860. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  67861. + 0444);
  67862. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  67863. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  67864. + 0444);
  67865. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  67866. +
  67867. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  67868. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  67869. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  67870. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  67871. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  67872. +MODULE_PARM_DESC(ic_usb_cap,
  67873. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  67874. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  67875. + 0444);
  67876. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  67877. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  67878. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  67879. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  67880. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  67881. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  67882. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  67883. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  67884. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  67885. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  67886. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  67887. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  67888. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  67889. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  67890. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  67891. +module_param(microframe_schedule, bool, 0444);
  67892. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  67893. +
  67894. +module_param(fiq_fix_enable, bool, 0444);
  67895. +MODULE_PARM_DESC(fiq_fix_enable, "Enable the fiq fix");
  67896. +module_param(nak_holdoff_enable, bool, 0444);
  67897. +MODULE_PARM_DESC(nak_holdoff_enable, "Enable the NAK holdoff");
  67898. +module_param(fiq_split_enable, bool, 0444);
  67899. +MODULE_PARM_DESC(fiq_split_enable, "Enable the FIQ fix on split transactions");
  67900. +
  67901. +/** @page "Module Parameters"
  67902. + *
  67903. + * The following parameters may be specified when starting the module.
  67904. + * These parameters define how the DWC_otg controller should be
  67905. + * configured. Parameter values are passed to the CIL initialization
  67906. + * function dwc_otg_cil_init
  67907. + *
  67908. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  67909. + *
  67910. +
  67911. + <table>
  67912. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  67913. +
  67914. + <tr>
  67915. + <td>otg_cap</td>
  67916. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  67917. + value for this parameter if none is specified.
  67918. + - 0: HNP and SRP capable (default, if available)
  67919. + - 1: SRP Only capable
  67920. + - 2: No HNP/SRP capable
  67921. + </td></tr>
  67922. +
  67923. + <tr>
  67924. + <td>dma_enable</td>
  67925. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  67926. + The driver will automatically detect the value for this parameter if none is
  67927. + specified.
  67928. + - 0: Slave
  67929. + - 1: DMA (default, if available)
  67930. + </td></tr>
  67931. +
  67932. + <tr>
  67933. + <td>dma_burst_size</td>
  67934. + <td>The DMA Burst size (applicable only for External DMA Mode).
  67935. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  67936. + </td></tr>
  67937. +
  67938. + <tr>
  67939. + <td>speed</td>
  67940. + <td>Specifies the maximum speed of operation in host and device mode. The
  67941. + actual speed depends on the speed of the attached device and the value of
  67942. + phy_type.
  67943. + - 0: High Speed (default)
  67944. + - 1: Full Speed
  67945. + </td></tr>
  67946. +
  67947. + <tr>
  67948. + <td>host_support_fs_ls_low_power</td>
  67949. + <td>Specifies whether low power mode is supported when attached to a Full
  67950. + Speed or Low Speed device in host mode.
  67951. + - 0: Don't support low power mode (default)
  67952. + - 1: Support low power mode
  67953. + </td></tr>
  67954. +
  67955. + <tr>
  67956. + <td>host_ls_low_power_phy_clk</td>
  67957. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  67958. + Speed device in host mode. This parameter is applicable only if
  67959. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  67960. + - 0: 48 MHz (default)
  67961. + - 1: 6 MHz
  67962. + </td></tr>
  67963. +
  67964. + <tr>
  67965. + <td>enable_dynamic_fifo</td>
  67966. + <td> Specifies whether FIFOs may be resized by the driver software.
  67967. + - 0: Use cC FIFO size parameters
  67968. + - 1: Allow dynamic FIFO sizing (default)
  67969. + </td></tr>
  67970. +
  67971. + <tr>
  67972. + <td>data_fifo_size</td>
  67973. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  67974. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  67975. + - Values: 32 to 32768 (default 8192)
  67976. +
  67977. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  67978. + </td></tr>
  67979. +
  67980. + <tr>
  67981. + <td>dev_rx_fifo_size</td>
  67982. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  67983. + FIFO sizing is enabled.
  67984. + - Values: 16 to 32768 (default 1064)
  67985. + </td></tr>
  67986. +
  67987. + <tr>
  67988. + <td>dev_nperio_tx_fifo_size</td>
  67989. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  67990. + dynamic FIFO sizing is enabled.
  67991. + - Values: 16 to 32768 (default 1024)
  67992. + </td></tr>
  67993. +
  67994. + <tr>
  67995. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  67996. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  67997. + when dynamic FIFO sizing is enabled.
  67998. + - Values: 4 to 768 (default 256)
  67999. + </td></tr>
  68000. +
  68001. + <tr>
  68002. + <td>host_rx_fifo_size</td>
  68003. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  68004. + sizing is enabled.
  68005. + - Values: 16 to 32768 (default 1024)
  68006. + </td></tr>
  68007. +
  68008. + <tr>
  68009. + <td>host_nperio_tx_fifo_size</td>
  68010. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  68011. + dynamic FIFO sizing is enabled in the core.
  68012. + - Values: 16 to 32768 (default 1024)
  68013. + </td></tr>
  68014. +
  68015. + <tr>
  68016. + <td>host_perio_tx_fifo_size</td>
  68017. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  68018. + sizing is enabled.
  68019. + - Values: 16 to 32768 (default 1024)
  68020. + </td></tr>
  68021. +
  68022. + <tr>
  68023. + <td>max_transfer_size</td>
  68024. + <td>The maximum transfer size supported in bytes.
  68025. + - Values: 2047 to 65,535 (default 65,535)
  68026. + </td></tr>
  68027. +
  68028. + <tr>
  68029. + <td>max_packet_count</td>
  68030. + <td>The maximum number of packets in a transfer.
  68031. + - Values: 15 to 511 (default 511)
  68032. + </td></tr>
  68033. +
  68034. + <tr>
  68035. + <td>host_channels</td>
  68036. + <td>The number of host channel registers to use.
  68037. + - Values: 1 to 16 (default 12)
  68038. +
  68039. + Note: The FPGA configuration supports a maximum of 12 host channels.
  68040. + </td></tr>
  68041. +
  68042. + <tr>
  68043. + <td>dev_endpoints</td>
  68044. + <td>The number of endpoints in addition to EP0 available for device mode
  68045. + operations.
  68046. + - Values: 1 to 15 (default 6 IN and OUT)
  68047. +
  68048. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  68049. + addition to EP0.
  68050. + </td></tr>
  68051. +
  68052. + <tr>
  68053. + <td>phy_type</td>
  68054. + <td>Specifies the type of PHY interface to use. By default, the driver will
  68055. + automatically detect the phy_type.
  68056. + - 0: Full Speed
  68057. + - 1: UTMI+ (default, if available)
  68058. + - 2: ULPI
  68059. + </td></tr>
  68060. +
  68061. + <tr>
  68062. + <td>phy_utmi_width</td>
  68063. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  68064. + phy_type of UTMI+. Also, this parameter is applicable only if the
  68065. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  68066. + core has been configured to work at either data path width.
  68067. + - Values: 8 or 16 bits (default 16)
  68068. + </td></tr>
  68069. +
  68070. + <tr>
  68071. + <td>phy_ulpi_ddr</td>
  68072. + <td>Specifies whether the ULPI operates at double or single data rate. This
  68073. + parameter is only applicable if phy_type is ULPI.
  68074. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  68075. + - 1: double data rate ULPI interface with 4 bit wide data bus
  68076. + </td></tr>
  68077. +
  68078. + <tr>
  68079. + <td>i2c_enable</td>
  68080. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  68081. + parameter is only applicable if PHY_TYPE is FS.
  68082. + - 0: Disabled (default)
  68083. + - 1: Enabled
  68084. + </td></tr>
  68085. +
  68086. + <tr>
  68087. + <td>ulpi_fs_ls</td>
  68088. + <td>Specifies whether to use ULPI FS/LS mode only.
  68089. + - 0: Disabled (default)
  68090. + - 1: Enabled
  68091. + </td></tr>
  68092. +
  68093. + <tr>
  68094. + <td>ts_dline</td>
  68095. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  68096. + - 0: Disabled (default)
  68097. + - 1: Enabled
  68098. + </td></tr>
  68099. +
  68100. + <tr>
  68101. + <td>en_multiple_tx_fifo</td>
  68102. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  68103. + The driver will automatically detect the value for this parameter if none is
  68104. + specified.
  68105. + - 0: Disabled
  68106. + - 1: Enabled (default, if available)
  68107. + </td></tr>
  68108. +
  68109. + <tr>
  68110. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  68111. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  68112. + when dynamic FIFO sizing is enabled.
  68113. + - Values: 4 to 768 (default 256)
  68114. + </td></tr>
  68115. +
  68116. + <tr>
  68117. + <td>tx_thr_length</td>
  68118. + <td>Transmit Threshold length in 32 bit double words
  68119. + - Values: 8 to 128 (default 64)
  68120. + </td></tr>
  68121. +
  68122. + <tr>
  68123. + <td>rx_thr_length</td>
  68124. + <td>Receive Threshold length in 32 bit double words
  68125. + - Values: 8 to 128 (default 64)
  68126. + </td></tr>
  68127. +
  68128. +<tr>
  68129. + <td>thr_ctl</td>
  68130. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  68131. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  68132. + Rx transfers accordingly.
  68133. + The driver will automatically detect the value for this parameter if none is
  68134. + specified.
  68135. + - Values: 0 to 7 (default 0)
  68136. + Bit values indicate:
  68137. + - 0: Thresholding disabled
  68138. + - 1: Thresholding enabled
  68139. + </td></tr>
  68140. +
  68141. +<tr>
  68142. + <td>dma_desc_enable</td>
  68143. + <td>Specifies whether to enable Descriptor DMA mode.
  68144. + The driver will automatically detect the value for this parameter if none is
  68145. + specified.
  68146. + - 0: Descriptor DMA disabled
  68147. + - 1: Descriptor DMA (default, if available)
  68148. + </td></tr>
  68149. +
  68150. +<tr>
  68151. + <td>mpi_enable</td>
  68152. + <td>Specifies whether to enable MPI enhancement mode.
  68153. + The driver will automatically detect the value for this parameter if none is
  68154. + specified.
  68155. + - 0: MPI disabled (default)
  68156. + - 1: MPI enable
  68157. + </td></tr>
  68158. +
  68159. +<tr>
  68160. + <td>pti_enable</td>
  68161. + <td>Specifies whether to enable PTI enhancement support.
  68162. + The driver will automatically detect the value for this parameter if none is
  68163. + specified.
  68164. + - 0: PTI disabled (default)
  68165. + - 1: PTI enable
  68166. + </td></tr>
  68167. +
  68168. +<tr>
  68169. + <td>lpm_enable</td>
  68170. + <td>Specifies whether to enable LPM support.
  68171. + The driver will automatically detect the value for this parameter if none is
  68172. + specified.
  68173. + - 0: LPM disabled
  68174. + - 1: LPM enable (default, if available)
  68175. + </td></tr>
  68176. +
  68177. +<tr>
  68178. + <td>ic_usb_cap</td>
  68179. + <td>Specifies whether to enable IC_USB capability.
  68180. + The driver will automatically detect the value for this parameter if none is
  68181. + specified.
  68182. + - 0: IC_USB disabled (default, if available)
  68183. + - 1: IC_USB enable
  68184. + </td></tr>
  68185. +
  68186. +<tr>
  68187. + <td>ahb_thr_ratio</td>
  68188. + <td>Specifies AHB Threshold ratio.
  68189. + - Values: 0 to 3 (default 0)
  68190. + </td></tr>
  68191. +
  68192. +<tr>
  68193. + <td>power_down</td>
  68194. + <td>Specifies Power Down(Hibernation) Mode.
  68195. + The driver will automatically detect the value for this parameter if none is
  68196. + specified.
  68197. + - 0: Power Down disabled (default)
  68198. + - 2: Power Down enabled
  68199. + </td></tr>
  68200. +
  68201. + <tr>
  68202. + <td>reload_ctl</td>
  68203. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  68204. + run time. The driver will automatically detect the value for this parameter if
  68205. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  68206. + the core might misbehave.
  68207. + - 0: Reload Control disabled (default)
  68208. + - 1: Reload Control enabled
  68209. + </td></tr>
  68210. +
  68211. + <tr>
  68212. + <td>dev_out_nak</td>
  68213. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  68214. + The driver will automatically detect the value for this parameter if
  68215. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  68216. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  68217. + - 1: The core sets NAK after Bulk OUT transfer complete
  68218. + </td></tr>
  68219. +
  68220. + <tr>
  68221. + <td>cont_on_bna</td>
  68222. + <td>Specifies whether Enable Continue on BNA enabled or no.
  68223. + After receiving BNA interrupt the core disables the endpoint,when the
  68224. + endpoint is re-enabled by the application the
  68225. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  68226. + - 1: Core starts processing from the descriptor which received the BNA.
  68227. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  68228. + </td></tr>
  68229. +
  68230. + <tr>
  68231. + <td>ahb_single</td>
  68232. + <td>This bit when programmed supports SINGLE transfers for remainder data
  68233. + in a transfer for DMA mode of operation.
  68234. + - 0: The remainder data will be sent using INCR burst size (default)
  68235. + - 1: The remainder data will be sent using SINGLE burst size.
  68236. + </td></tr>
  68237. +
  68238. +<tr>
  68239. + <td>adp_enable</td>
  68240. + <td>Specifies whether ADP feature is enabled.
  68241. + The driver will automatically detect the value for this parameter if none is
  68242. + specified.
  68243. + - 0: ADP feature disabled (default)
  68244. + - 1: ADP feature enabled
  68245. + </td></tr>
  68246. +
  68247. + <tr>
  68248. + <td>otg_ver</td>
  68249. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  68250. + USB OTG device.
  68251. + - 0: OTG 2.0 support disabled (default)
  68252. + - 1: OTG 2.0 support enabled
  68253. + </td></tr>
  68254. +
  68255. +*/
  68256. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_driver.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  68257. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_driver.h 1970-01-01 01:00:00.000000000 +0100
  68258. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.h 2014-03-11 17:51:27.000000000 +0100
  68259. @@ -0,0 +1,86 @@
  68260. +/* ==========================================================================
  68261. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  68262. + * $Revision: #19 $
  68263. + * $Date: 2010/11/15 $
  68264. + * $Change: 1627671 $
  68265. + *
  68266. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  68267. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  68268. + * otherwise expressly agreed to in writing between Synopsys and you.
  68269. + *
  68270. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  68271. + * any End User Software License Agreement or Agreement for Licensed Product
  68272. + * with Synopsys or any supplement thereto. You are permitted to use and
  68273. + * redistribute this Software in source and binary forms, with or without
  68274. + * modification, provided that redistributions of source code must retain this
  68275. + * notice. You may not view, use, disclose, copy or distribute this file or
  68276. + * any information contained herein except pursuant to this license grant from
  68277. + * Synopsys. If you do not agree with this notice, including the disclaimer
  68278. + * below, then you are not authorized to use the Software.
  68279. + *
  68280. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  68281. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68282. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68283. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  68284. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  68285. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  68286. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  68287. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  68288. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  68289. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  68290. + * DAMAGE.
  68291. + * ========================================================================== */
  68292. +
  68293. +#ifndef __DWC_OTG_DRIVER_H__
  68294. +#define __DWC_OTG_DRIVER_H__
  68295. +
  68296. +/** @file
  68297. + * This file contains the interface to the Linux driver.
  68298. + */
  68299. +#include "dwc_otg_os_dep.h"
  68300. +#include "dwc_otg_core_if.h"
  68301. +
  68302. +/* Type declarations */
  68303. +struct dwc_otg_pcd;
  68304. +struct dwc_otg_hcd;
  68305. +
  68306. +/**
  68307. + * This structure is a wrapper that encapsulates the driver components used to
  68308. + * manage a single DWC_otg controller.
  68309. + */
  68310. +typedef struct dwc_otg_device {
  68311. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  68312. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  68313. + * require this. */
  68314. + struct os_dependent os_dep;
  68315. +
  68316. + /** Pointer to the core interface structure. */
  68317. + dwc_otg_core_if_t *core_if;
  68318. +
  68319. + /** Pointer to the PCD structure. */
  68320. + struct dwc_otg_pcd *pcd;
  68321. +
  68322. + /** Pointer to the HCD structure. */
  68323. + struct dwc_otg_hcd *hcd;
  68324. +
  68325. + /** Flag to indicate whether the common IRQ handler is installed. */
  68326. + uint8_t common_irq_installed;
  68327. +
  68328. +} dwc_otg_device_t;
  68329. +
  68330. +/*We must clear S3C24XX_EINTPEND external interrupt register
  68331. + * because after clearing in this register trigerred IRQ from
  68332. + * H/W core in kernel interrupt can be occured again before OTG
  68333. + * handlers clear all IRQ sources of Core registers because of
  68334. + * timing latencies and Low Level IRQ Type.
  68335. + */
  68336. +#ifdef CONFIG_MACH_IPMATE
  68337. +#define S3C2410X_CLEAR_EINTPEND() \
  68338. +do { \
  68339. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  68340. +} while (0)
  68341. +#else
  68342. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  68343. +#endif
  68344. +
  68345. +#endif
  68346. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  68347. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 1970-01-01 01:00:00.000000000 +0100
  68348. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 2014-03-11 17:51:27.000000000 +0100
  68349. @@ -0,0 +1,3685 @@
  68350. +
  68351. +/* ==========================================================================
  68352. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  68353. + * $Revision: #104 $
  68354. + * $Date: 2011/10/24 $
  68355. + * $Change: 1871159 $
  68356. + *
  68357. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  68358. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  68359. + * otherwise expressly agreed to in writing between Synopsys and you.
  68360. + *
  68361. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  68362. + * any End User Software License Agreement or Agreement for Licensed Product
  68363. + * with Synopsys or any supplement thereto. You are permitted to use and
  68364. + * redistribute this Software in source and binary forms, with or without
  68365. + * modification, provided that redistributions of source code must retain this
  68366. + * notice. You may not view, use, disclose, copy or distribute this file or
  68367. + * any information contained herein except pursuant to this license grant from
  68368. + * Synopsys. If you do not agree with this notice, including the disclaimer
  68369. + * below, then you are not authorized to use the Software.
  68370. + *
  68371. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  68372. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68373. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68374. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  68375. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  68376. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  68377. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  68378. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  68379. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  68380. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  68381. + * DAMAGE.
  68382. + * ========================================================================== */
  68383. +#ifndef DWC_DEVICE_ONLY
  68384. +
  68385. +/** @file
  68386. + * This file implements HCD Core. All code in this file is portable and doesn't
  68387. + * use any OS specific functions.
  68388. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  68389. + * header file.
  68390. + */
  68391. +
  68392. +#include <linux/usb.h>
  68393. +#include <linux/usb/hcd.h>
  68394. +
  68395. +#include "dwc_otg_hcd.h"
  68396. +#include "dwc_otg_regs.h"
  68397. +#include "dwc_otg_mphi_fix.h"
  68398. +
  68399. +extern bool microframe_schedule, nak_holdoff_enable;
  68400. +
  68401. +//#define DEBUG_HOST_CHANNELS
  68402. +#ifdef DEBUG_HOST_CHANNELS
  68403. +static int last_sel_trans_num_per_scheduled = 0;
  68404. +static int last_sel_trans_num_nonper_scheduled = 0;
  68405. +static int last_sel_trans_num_avail_hc_at_start = 0;
  68406. +static int last_sel_trans_num_avail_hc_at_end = 0;
  68407. +#endif /* DEBUG_HOST_CHANNELS */
  68408. +
  68409. +extern int g_next_sched_frame, g_np_count, g_np_sent;
  68410. +
  68411. +extern haint_data_t haint_saved;
  68412. +extern hcintmsk_data_t hcintmsk_saved[MAX_EPS_CHANNELS];
  68413. +extern hcint_data_t hcint_saved[MAX_EPS_CHANNELS];
  68414. +extern gintsts_data_t ginsts_saved;
  68415. +
  68416. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  68417. +{
  68418. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  68419. +}
  68420. +
  68421. +/**
  68422. + * Connection timeout function. An OTG host is required to display a
  68423. + * message if the device does not connect within 10 seconds.
  68424. + */
  68425. +void dwc_otg_hcd_connect_timeout(void *ptr)
  68426. +{
  68427. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  68428. + DWC_PRINTF("Connect Timeout\n");
  68429. + __DWC_ERROR("Device Not Connected/Responding\n");
  68430. +}
  68431. +
  68432. +#if defined(DEBUG)
  68433. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  68434. +{
  68435. + if (qh->channel != NULL) {
  68436. + dwc_hc_t *hc = qh->channel;
  68437. + dwc_list_link_t *item;
  68438. + dwc_otg_qh_t *qh_item;
  68439. + int num_channels = hcd->core_if->core_params->host_channels;
  68440. + int i;
  68441. +
  68442. + dwc_otg_hc_regs_t *hc_regs;
  68443. + hcchar_data_t hcchar;
  68444. + hcsplt_data_t hcsplt;
  68445. + hctsiz_data_t hctsiz;
  68446. + uint32_t hcdma;
  68447. +
  68448. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  68449. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  68450. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  68451. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  68452. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  68453. +
  68454. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  68455. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  68456. + hcsplt.d32);
  68457. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  68458. + hcdma);
  68459. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  68460. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  68461. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  68462. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  68463. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  68464. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  68465. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  68466. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  68467. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  68468. + DWC_PRINTF(" qh: %p\n", hc->qh);
  68469. + DWC_PRINTF(" NP inactive sched:\n");
  68470. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  68471. + qh_item =
  68472. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  68473. + DWC_PRINTF(" %p\n", qh_item);
  68474. + }
  68475. + DWC_PRINTF(" NP active sched:\n");
  68476. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  68477. + qh_item =
  68478. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  68479. + DWC_PRINTF(" %p\n", qh_item);
  68480. + }
  68481. + DWC_PRINTF(" Channels: \n");
  68482. + for (i = 0; i < num_channels; i++) {
  68483. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  68484. + DWC_PRINTF(" %2d: %p\n", i, hc);
  68485. + }
  68486. + }
  68487. +}
  68488. +#else
  68489. +#define dump_channel_info(hcd, qh)
  68490. +#endif /* DEBUG */
  68491. +
  68492. +/**
  68493. + * Work queue function for starting the HCD when A-Cable is connected.
  68494. + * The hcd_start() must be called in a process context.
  68495. + */
  68496. +static void hcd_start_func(void *_vp)
  68497. +{
  68498. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  68499. +
  68500. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  68501. + if (hcd) {
  68502. + hcd->fops->start(hcd);
  68503. + }
  68504. +}
  68505. +
  68506. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  68507. +{
  68508. +#ifdef DEBUG
  68509. + int i;
  68510. + int num_channels = hcd->core_if->core_params->host_channels;
  68511. + for (i = 0; i < num_channels; i++) {
  68512. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  68513. + }
  68514. +#endif
  68515. +}
  68516. +
  68517. +static void del_timers(dwc_otg_hcd_t * hcd)
  68518. +{
  68519. + del_xfer_timers(hcd);
  68520. + DWC_TIMER_CANCEL(hcd->conn_timer);
  68521. +}
  68522. +
  68523. +/**
  68524. + * Processes all the URBs in a single list of QHs. Completes them with
  68525. + * -ESHUTDOWN and frees the QTD.
  68526. + */
  68527. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  68528. +{
  68529. + dwc_list_link_t *qh_item, *qh_tmp;
  68530. + dwc_otg_qh_t *qh;
  68531. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  68532. +
  68533. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  68534. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  68535. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  68536. + &qh->qtd_list, qtd_list_entry) {
  68537. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  68538. + if (qtd->urb != NULL) {
  68539. + hcd->fops->complete(hcd, qtd->urb->priv,
  68540. + qtd->urb, -DWC_E_SHUTDOWN);
  68541. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  68542. + }
  68543. +
  68544. + }
  68545. + if(qh->channel) {
  68546. + /* Using hcchar.chen == 1 is not a reliable test.
  68547. + * It is possible that the channel has already halted
  68548. + * but not yet been through the IRQ handler.
  68549. + */
  68550. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  68551. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  68552. + if(microframe_schedule)
  68553. + hcd->available_host_channels++;
  68554. + qh->channel = NULL;
  68555. + }
  68556. + dwc_otg_hcd_qh_remove(hcd, qh);
  68557. + }
  68558. +}
  68559. +
  68560. +/**
  68561. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  68562. + * and periodic schedules. The QTD associated with each URB is removed from
  68563. + * the schedule and freed. This function may be called when a disconnect is
  68564. + * detected or when the HCD is being stopped.
  68565. + */
  68566. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  68567. +{
  68568. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  68569. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  68570. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  68571. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  68572. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  68573. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  68574. +}
  68575. +
  68576. +/**
  68577. + * Start the connection timer. An OTG host is required to display a
  68578. + * message if the device does not connect within 10 seconds. The
  68579. + * timer is deleted if a port connect interrupt occurs before the
  68580. + * timer expires.
  68581. + */
  68582. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  68583. +{
  68584. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  68585. +}
  68586. +
  68587. +/**
  68588. + * HCD Callback function for disconnect of the HCD.
  68589. + *
  68590. + * @param p void pointer to the <code>struct usb_hcd</code>
  68591. + */
  68592. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  68593. +{
  68594. + dwc_otg_hcd_t *dwc_otg_hcd;
  68595. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  68596. + dwc_otg_hcd = p;
  68597. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  68598. + return 1;
  68599. +}
  68600. +
  68601. +/**
  68602. + * HCD Callback function for starting the HCD when A-Cable is
  68603. + * connected.
  68604. + *
  68605. + * @param p void pointer to the <code>struct usb_hcd</code>
  68606. + */
  68607. +static int32_t dwc_otg_hcd_start_cb(void *p)
  68608. +{
  68609. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68610. + dwc_otg_core_if_t *core_if;
  68611. + hprt0_data_t hprt0;
  68612. +
  68613. + core_if = dwc_otg_hcd->core_if;
  68614. +
  68615. + if (core_if->op_state == B_HOST) {
  68616. + /*
  68617. + * Reset the port. During a HNP mode switch the reset
  68618. + * needs to occur within 1ms and have a duration of at
  68619. + * least 50ms.
  68620. + */
  68621. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  68622. + hprt0.b.prtrst = 1;
  68623. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  68624. + }
  68625. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  68626. + hcd_start_func, dwc_otg_hcd, 50,
  68627. + "start hcd");
  68628. +
  68629. + return 1;
  68630. +}
  68631. +
  68632. +/**
  68633. + * HCD Callback function for disconnect of the HCD.
  68634. + *
  68635. + * @param p void pointer to the <code>struct usb_hcd</code>
  68636. + */
  68637. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  68638. +{
  68639. + gintsts_data_t intr;
  68640. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68641. +
  68642. + /*
  68643. + * Set status flags for the hub driver.
  68644. + */
  68645. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  68646. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  68647. + if(fiq_fix_enable)
  68648. + local_fiq_disable();
  68649. + /*
  68650. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  68651. + * interrupt mask and status bits and disabling subsequent host
  68652. + * channel interrupts.
  68653. + */
  68654. + intr.d32 = 0;
  68655. + intr.b.nptxfempty = 1;
  68656. + intr.b.ptxfempty = 1;
  68657. + intr.b.hcintr = 1;
  68658. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  68659. + intr.d32, 0);
  68660. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  68661. + intr.d32, 0);
  68662. +
  68663. + del_timers(dwc_otg_hcd);
  68664. +
  68665. + /*
  68666. + * Turn off the vbus power only if the core has transitioned to device
  68667. + * mode. If still in host mode, need to keep power on to detect a
  68668. + * reconnection.
  68669. + */
  68670. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  68671. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  68672. + hprt0_data_t hprt0 = {.d32 = 0 };
  68673. + DWC_PRINTF("Disconnect: PortPower off\n");
  68674. + hprt0.b.prtpwr = 0;
  68675. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  68676. + hprt0.d32);
  68677. + }
  68678. +
  68679. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  68680. + }
  68681. +
  68682. + /* Respond with an error status to all URBs in the schedule. */
  68683. + kill_all_urbs(dwc_otg_hcd);
  68684. +
  68685. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  68686. + /* Clean up any host channels that were in use. */
  68687. + int num_channels;
  68688. + int i;
  68689. + dwc_hc_t *channel;
  68690. + dwc_otg_hc_regs_t *hc_regs;
  68691. + hcchar_data_t hcchar;
  68692. +
  68693. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  68694. +
  68695. + if (!dwc_otg_hcd->core_if->dma_enable) {
  68696. + /* Flush out any channel requests in slave mode. */
  68697. + for (i = 0; i < num_channels; i++) {
  68698. + channel = dwc_otg_hcd->hc_ptr_array[i];
  68699. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  68700. + (channel, hc_list_entry)) {
  68701. + hc_regs =
  68702. + dwc_otg_hcd->core_if->
  68703. + host_if->hc_regs[i];
  68704. + hcchar.d32 =
  68705. + DWC_READ_REG32(&hc_regs->hcchar);
  68706. + if (hcchar.b.chen) {
  68707. + hcchar.b.chen = 0;
  68708. + hcchar.b.chdis = 1;
  68709. + hcchar.b.epdir = 0;
  68710. + DWC_WRITE_REG32
  68711. + (&hc_regs->hcchar,
  68712. + hcchar.d32);
  68713. + }
  68714. + }
  68715. + }
  68716. + }
  68717. +
  68718. + for (i = 0; i < num_channels; i++) {
  68719. + channel = dwc_otg_hcd->hc_ptr_array[i];
  68720. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  68721. + hc_regs =
  68722. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  68723. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  68724. + if (hcchar.b.chen) {
  68725. + /* Halt the channel. */
  68726. + hcchar.b.chdis = 1;
  68727. + DWC_WRITE_REG32(&hc_regs->hcchar,
  68728. + hcchar.d32);
  68729. + }
  68730. +
  68731. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  68732. + channel);
  68733. + DWC_CIRCLEQ_INSERT_TAIL
  68734. + (&dwc_otg_hcd->free_hc_list, channel,
  68735. + hc_list_entry);
  68736. + /*
  68737. + * Added for Descriptor DMA to prevent channel double cleanup
  68738. + * in release_channel_ddma(). Which called from ep_disable
  68739. + * when device disconnect.
  68740. + */
  68741. + channel->qh = NULL;
  68742. + }
  68743. + }
  68744. + if(fiq_split_enable) {
  68745. + for(i=0; i < 128; i++) {
  68746. + dwc_otg_hcd->hub_port[i] = 0;
  68747. + }
  68748. + haint_saved.d32 = 0;
  68749. + for(i=0; i < MAX_EPS_CHANNELS; i++) {
  68750. + hcint_saved[i].d32 = 0;
  68751. + hcintmsk_saved[i].d32 = 0;
  68752. + }
  68753. + }
  68754. +
  68755. + }
  68756. +
  68757. + if(fiq_fix_enable)
  68758. + local_fiq_enable();
  68759. +
  68760. + if (dwc_otg_hcd->fops->disconnect) {
  68761. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  68762. + }
  68763. +
  68764. + return 1;
  68765. +}
  68766. +
  68767. +/**
  68768. + * HCD Callback function for stopping the HCD.
  68769. + *
  68770. + * @param p void pointer to the <code>struct usb_hcd</code>
  68771. + */
  68772. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  68773. +{
  68774. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68775. +
  68776. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  68777. + dwc_otg_hcd_stop(dwc_otg_hcd);
  68778. + return 1;
  68779. +}
  68780. +
  68781. +#ifdef CONFIG_USB_DWC_OTG_LPM
  68782. +/**
  68783. + * HCD Callback function for sleep of HCD.
  68784. + *
  68785. + * @param p void pointer to the <code>struct usb_hcd</code>
  68786. + */
  68787. +static int dwc_otg_hcd_sleep_cb(void *p)
  68788. +{
  68789. + dwc_otg_hcd_t *hcd = p;
  68790. +
  68791. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  68792. +
  68793. + return 0;
  68794. +}
  68795. +#endif
  68796. +
  68797. +
  68798. +/**
  68799. + * HCD Callback function for Remote Wakeup.
  68800. + *
  68801. + * @param p void pointer to the <code>struct usb_hcd</code>
  68802. + */
  68803. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  68804. +{
  68805. + dwc_otg_hcd_t *hcd = p;
  68806. +
  68807. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  68808. + hcd->flags.b.port_suspend_change = 1;
  68809. + }
  68810. +#ifdef CONFIG_USB_DWC_OTG_LPM
  68811. + else {
  68812. + hcd->flags.b.port_l1_change = 1;
  68813. + }
  68814. +#endif
  68815. + return 0;
  68816. +}
  68817. +
  68818. +/**
  68819. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  68820. + * stopped.
  68821. + */
  68822. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  68823. +{
  68824. + hprt0_data_t hprt0 = {.d32 = 0 };
  68825. +
  68826. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  68827. +
  68828. + /*
  68829. + * The root hub should be disconnected before this function is called.
  68830. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  68831. + * and the QH lists (via ..._hcd_endpoint_disable).
  68832. + */
  68833. +
  68834. + /* Turn off all host-specific interrupts. */
  68835. + dwc_otg_disable_host_interrupts(hcd->core_if);
  68836. +
  68837. + /* Turn off the vbus power */
  68838. + DWC_PRINTF("PortPower off\n");
  68839. + hprt0.b.prtpwr = 0;
  68840. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  68841. + dwc_mdelay(1);
  68842. +}
  68843. +
  68844. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  68845. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  68846. + int atomic_alloc)
  68847. +{
  68848. + int retval = 0;
  68849. + uint8_t needs_scheduling = 0;
  68850. + dwc_otg_transaction_type_e tr_type;
  68851. + dwc_otg_qtd_t *qtd;
  68852. + gintmsk_data_t intr_mask = {.d32 = 0 };
  68853. + hprt0_data_t hprt0 = { .d32 = 0 };
  68854. +
  68855. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68856. + if (NULL == hcd->core_if) {
  68857. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  68858. + /* No longer connected. */
  68859. + return -DWC_E_INVALID;
  68860. + }
  68861. +#endif
  68862. + if (!hcd->flags.b.port_connect_status) {
  68863. + /* No longer connected. */
  68864. + DWC_ERROR("Not connected\n");
  68865. + return -DWC_E_NO_DEVICE;
  68866. + }
  68867. +
  68868. + /* Some core configurations cannot support LS traffic on a FS root port */
  68869. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  68870. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  68871. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  68872. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  68873. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  68874. + return -DWC_E_NO_DEVICE;
  68875. + }
  68876. + }
  68877. +
  68878. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  68879. + if (qtd == NULL) {
  68880. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  68881. + return -DWC_E_NO_MEMORY;
  68882. + }
  68883. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68884. + if (qtd->urb == NULL) {
  68885. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  68886. + return -DWC_E_NO_MEMORY;
  68887. + }
  68888. + if (qtd->urb->priv == NULL) {
  68889. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  68890. + return -DWC_E_NO_MEMORY;
  68891. + }
  68892. +#endif
  68893. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  68894. + if(!intr_mask.b.sofintr) needs_scheduling = 1;
  68895. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  68896. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  68897. + needs_scheduling = 0;
  68898. +
  68899. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  68900. + // creates a new queue in ep_handle if it doesn't exist already
  68901. + if (retval < 0) {
  68902. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  68903. + "Error status %d\n", retval);
  68904. + dwc_otg_hcd_qtd_free(qtd);
  68905. + return retval;
  68906. + }
  68907. +
  68908. + if(needs_scheduling) {
  68909. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  68910. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  68911. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  68912. + }
  68913. + }
  68914. + return retval;
  68915. +}
  68916. +
  68917. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  68918. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  68919. +{
  68920. + dwc_otg_qh_t *qh;
  68921. + dwc_otg_qtd_t *urb_qtd;
  68922. + BUG_ON(!hcd);
  68923. + BUG_ON(!dwc_otg_urb);
  68924. +
  68925. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68926. +
  68927. + if (hcd == NULL) {
  68928. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  68929. + return -DWC_E_INVALID;
  68930. + }
  68931. + if (dwc_otg_urb == NULL) {
  68932. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  68933. + return -DWC_E_INVALID;
  68934. + }
  68935. + if (dwc_otg_urb->qtd == NULL) {
  68936. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  68937. + return -DWC_E_INVALID;
  68938. + }
  68939. + urb_qtd = dwc_otg_urb->qtd;
  68940. + BUG_ON(!urb_qtd);
  68941. + if (urb_qtd->qh == NULL) {
  68942. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  68943. + return -DWC_E_INVALID;
  68944. + }
  68945. +#else
  68946. + urb_qtd = dwc_otg_urb->qtd;
  68947. + BUG_ON(!urb_qtd);
  68948. +#endif
  68949. + qh = urb_qtd->qh;
  68950. + BUG_ON(!qh);
  68951. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  68952. + if (urb_qtd->in_process) {
  68953. + dump_channel_info(hcd, qh);
  68954. + }
  68955. + }
  68956. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68957. + if (hcd->core_if == NULL) {
  68958. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  68959. + return -DWC_E_INVALID;
  68960. + }
  68961. +#endif
  68962. + if (urb_qtd->in_process && qh->channel) {
  68963. + /* The QTD is in process (it has been assigned to a channel). */
  68964. + if (hcd->flags.b.port_connect_status) {
  68965. + /*
  68966. + * If still connected (i.e. in host mode), halt the
  68967. + * channel so it can be used for other transfers. If
  68968. + * no longer connected, the host registers can't be
  68969. + * written to halt the channel since the core is in
  68970. + * device mode.
  68971. + */
  68972. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  68973. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  68974. +
  68975. + dwc_otg_hcd_release_port(hcd, qh);
  68976. + }
  68977. + }
  68978. +
  68979. + /*
  68980. + * Free the QTD and clean up the associated QH. Leave the QH in the
  68981. + * schedule if it has any remaining QTDs.
  68982. + */
  68983. +
  68984. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  68985. + "delete %sQueue handler\n",
  68986. + hcd->core_if->dma_desc_enable?"DMA ":"");
  68987. + if (!hcd->core_if->dma_desc_enable) {
  68988. + uint8_t b = urb_qtd->in_process;
  68989. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  68990. + if (b) {
  68991. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  68992. + qh->channel = NULL;
  68993. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  68994. + dwc_otg_hcd_qh_remove(hcd, qh);
  68995. + }
  68996. + } else {
  68997. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  68998. + }
  68999. + return 0;
  69000. +}
  69001. +
  69002. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  69003. + int retry)
  69004. +{
  69005. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  69006. + int retval = 0;
  69007. + dwc_irqflags_t flags;
  69008. +
  69009. + if (retry < 0) {
  69010. + retval = -DWC_E_INVALID;
  69011. + goto done;
  69012. + }
  69013. +
  69014. + if (!qh) {
  69015. + retval = -DWC_E_INVALID;
  69016. + goto done;
  69017. + }
  69018. +
  69019. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69020. +
  69021. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  69022. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69023. + retry--;
  69024. + dwc_msleep(5);
  69025. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69026. + }
  69027. +
  69028. + dwc_otg_hcd_qh_remove(hcd, qh);
  69029. +
  69030. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69031. + /*
  69032. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  69033. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  69034. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  69035. + * and dwc_otg_hcd_frame_list_alloc().
  69036. + */
  69037. + dwc_otg_hcd_qh_free(hcd, qh);
  69038. +
  69039. +done:
  69040. + return retval;
  69041. +}
  69042. +
  69043. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  69044. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  69045. +{
  69046. + int retval = 0;
  69047. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  69048. + if (!qh)
  69049. + return -DWC_E_INVALID;
  69050. +
  69051. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  69052. + return retval;
  69053. +}
  69054. +#endif
  69055. +
  69056. +/**
  69057. + * HCD Callback structure for handling mode switching.
  69058. + */
  69059. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  69060. + .start = dwc_otg_hcd_start_cb,
  69061. + .stop = dwc_otg_hcd_stop_cb,
  69062. + .disconnect = dwc_otg_hcd_disconnect_cb,
  69063. + .session_start = dwc_otg_hcd_session_start_cb,
  69064. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  69065. +#ifdef CONFIG_USB_DWC_OTG_LPM
  69066. + .sleep = dwc_otg_hcd_sleep_cb,
  69067. +#endif
  69068. + .p = 0,
  69069. +};
  69070. +
  69071. +/**
  69072. + * Reset tasklet function
  69073. + */
  69074. +static void reset_tasklet_func(void *data)
  69075. +{
  69076. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  69077. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  69078. + hprt0_data_t hprt0;
  69079. +
  69080. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  69081. +
  69082. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  69083. + hprt0.b.prtrst = 1;
  69084. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  69085. + dwc_mdelay(60);
  69086. +
  69087. + hprt0.b.prtrst = 0;
  69088. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  69089. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  69090. +}
  69091. +
  69092. +static void completion_tasklet_func(void *ptr)
  69093. +{
  69094. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  69095. + struct urb *urb;
  69096. + urb_tq_entry_t *item;
  69097. + dwc_irqflags_t flags;
  69098. +
  69099. + /* This could just be spin_lock_irq */
  69100. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69101. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  69102. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  69103. + urb = item->urb;
  69104. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  69105. + urb_tq_entries);
  69106. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69107. + DWC_FREE(item);
  69108. +
  69109. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  69110. +
  69111. + fiq_print(FIQDBG_PORTHUB, "COMPLETE");
  69112. +
  69113. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69114. + }
  69115. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69116. + return;
  69117. +}
  69118. +
  69119. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  69120. +{
  69121. + dwc_list_link_t *item;
  69122. + dwc_otg_qh_t *qh;
  69123. + dwc_irqflags_t flags;
  69124. +
  69125. + if (!qh_list->next) {
  69126. + /* The list hasn't been initialized yet. */
  69127. + return;
  69128. + }
  69129. + /*
  69130. + * Hold spinlock here. Not needed in that case if bellow
  69131. + * function is being called from ISR
  69132. + */
  69133. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69134. + /* Ensure there are no QTDs or URBs left. */
  69135. + kill_urbs_in_qh_list(hcd, qh_list);
  69136. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69137. +
  69138. + DWC_LIST_FOREACH(item, qh_list) {
  69139. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  69140. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  69141. + }
  69142. +}
  69143. +
  69144. +/**
  69145. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  69146. + * Device during SRP time by host power up.
  69147. + */
  69148. +void dwc_otg_hcd_power_up(void *ptr)
  69149. +{
  69150. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  69151. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  69152. +
  69153. + DWC_PRINTF("%s called\n", __FUNCTION__);
  69154. +
  69155. + if (!core_if->hibernation_suspend) {
  69156. + DWC_PRINTF("Already exited from Hibernation\n");
  69157. + return;
  69158. + }
  69159. +
  69160. + /* Switch on the voltage to the core */
  69161. + gpwrdn.b.pwrdnswtch = 1;
  69162. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69163. + dwc_udelay(10);
  69164. +
  69165. + /* Reset the core */
  69166. + gpwrdn.d32 = 0;
  69167. + gpwrdn.b.pwrdnrstn = 1;
  69168. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69169. + dwc_udelay(10);
  69170. +
  69171. + /* Disable power clamps */
  69172. + gpwrdn.d32 = 0;
  69173. + gpwrdn.b.pwrdnclmp = 1;
  69174. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69175. +
  69176. + /* Remove reset the core signal */
  69177. + gpwrdn.d32 = 0;
  69178. + gpwrdn.b.pwrdnrstn = 1;
  69179. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  69180. + dwc_udelay(10);
  69181. +
  69182. + /* Disable PMU interrupt */
  69183. + gpwrdn.d32 = 0;
  69184. + gpwrdn.b.pmuintsel = 1;
  69185. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69186. +
  69187. + core_if->hibernation_suspend = 0;
  69188. +
  69189. + /* Disable PMU */
  69190. + gpwrdn.d32 = 0;
  69191. + gpwrdn.b.pmuactv = 1;
  69192. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69193. + dwc_udelay(10);
  69194. +
  69195. + /* Enable VBUS */
  69196. + gpwrdn.d32 = 0;
  69197. + gpwrdn.b.dis_vbus = 1;
  69198. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69199. +
  69200. + core_if->op_state = A_HOST;
  69201. + dwc_otg_core_init(core_if);
  69202. + dwc_otg_enable_global_interrupts(core_if);
  69203. + cil_hcd_start(core_if);
  69204. +}
  69205. +
  69206. +/**
  69207. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  69208. + * in the struct usb_hcd field.
  69209. + */
  69210. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  69211. +{
  69212. + int i;
  69213. +
  69214. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  69215. +
  69216. + del_timers(dwc_otg_hcd);
  69217. +
  69218. + /* Free memory for QH/QTD lists */
  69219. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  69220. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  69221. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  69222. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  69223. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  69224. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  69225. +
  69226. + /* Free memory for the host channels. */
  69227. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  69228. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  69229. +
  69230. +#ifdef DEBUG
  69231. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  69232. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  69233. + }
  69234. +#endif
  69235. + if (hc != NULL) {
  69236. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  69237. + i, hc);
  69238. + DWC_FREE(hc);
  69239. + }
  69240. + }
  69241. +
  69242. + if (dwc_otg_hcd->core_if->dma_enable) {
  69243. + if (dwc_otg_hcd->status_buf_dma) {
  69244. + DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
  69245. + dwc_otg_hcd->status_buf,
  69246. + dwc_otg_hcd->status_buf_dma);
  69247. + }
  69248. + } else if (dwc_otg_hcd->status_buf != NULL) {
  69249. + DWC_FREE(dwc_otg_hcd->status_buf);
  69250. + }
  69251. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  69252. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  69253. + /* Set core_if's lock pointer to NULL */
  69254. + dwc_otg_hcd->core_if->lock = NULL;
  69255. +
  69256. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  69257. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  69258. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  69259. +
  69260. +#ifdef DWC_DEV_SRPCAP
  69261. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  69262. + dwc_otg_hcd->core_if->pwron_timer) {
  69263. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  69264. + }
  69265. +#endif
  69266. + DWC_FREE(dwc_otg_hcd);
  69267. +}
  69268. +
  69269. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  69270. +
  69271. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  69272. +{
  69273. + int retval = 0;
  69274. + int num_channels;
  69275. + int i;
  69276. + dwc_hc_t *channel;
  69277. +
  69278. + hcd->lock = DWC_SPINLOCK_ALLOC();
  69279. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  69280. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  69281. + hcd, core_if);
  69282. + if (!hcd->lock) {
  69283. + DWC_ERROR("Could not allocate lock for pcd");
  69284. + DWC_FREE(hcd);
  69285. + retval = -DWC_E_NO_MEMORY;
  69286. + goto out;
  69287. + }
  69288. + hcd->core_if = core_if;
  69289. +
  69290. + /* Register the HCD CIL Callbacks */
  69291. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  69292. + &hcd_cil_callbacks, hcd);
  69293. +
  69294. + /* Initialize the non-periodic schedule. */
  69295. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  69296. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  69297. +
  69298. + /* Initialize the periodic schedule. */
  69299. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  69300. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  69301. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  69302. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  69303. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  69304. + /*
  69305. + * Create a host channel descriptor for each host channel implemented
  69306. + * in the controller. Initialize the channel descriptor array.
  69307. + */
  69308. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  69309. + num_channels = hcd->core_if->core_params->host_channels;
  69310. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  69311. + for (i = 0; i < num_channels; i++) {
  69312. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  69313. + if (channel == NULL) {
  69314. + retval = -DWC_E_NO_MEMORY;
  69315. + DWC_ERROR("%s: host channel allocation failed\n",
  69316. + __func__);
  69317. + dwc_otg_hcd_free(hcd);
  69318. + goto out;
  69319. + }
  69320. + channel->hc_num = i;
  69321. + hcd->hc_ptr_array[i] = channel;
  69322. +#ifdef DEBUG
  69323. + hcd->core_if->hc_xfer_timer[i] =
  69324. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  69325. + &hcd->core_if->hc_xfer_info[i]);
  69326. +#endif
  69327. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  69328. + channel);
  69329. + }
  69330. +
  69331. + /* Initialize the Connection timeout timer. */
  69332. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  69333. + dwc_otg_hcd_connect_timeout, 0);
  69334. +
  69335. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  69336. + if (microframe_schedule)
  69337. + init_hcd_usecs(hcd);
  69338. +
  69339. + /* Initialize reset tasklet. */
  69340. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  69341. +
  69342. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  69343. + completion_tasklet_func, hcd);
  69344. +#ifdef DWC_DEV_SRPCAP
  69345. + if (hcd->core_if->power_down == 2) {
  69346. + /* Initialize Power on timer for Host power up in case hibernation */
  69347. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  69348. + dwc_otg_hcd_power_up, core_if);
  69349. + }
  69350. +#endif
  69351. +
  69352. + /*
  69353. + * Allocate space for storing data on status transactions. Normally no
  69354. + * data is sent, but this space acts as a bit bucket. This must be
  69355. + * done after usb_add_hcd since that function allocates the DMA buffer
  69356. + * pool.
  69357. + */
  69358. + if (hcd->core_if->dma_enable) {
  69359. + hcd->status_buf =
  69360. + DWC_DMA_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE,
  69361. + &hcd->status_buf_dma);
  69362. + } else {
  69363. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  69364. + }
  69365. + if (!hcd->status_buf) {
  69366. + retval = -DWC_E_NO_MEMORY;
  69367. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  69368. + dwc_otg_hcd_free(hcd);
  69369. + goto out;
  69370. + }
  69371. +
  69372. + hcd->otg_port = 1;
  69373. + hcd->frame_list = NULL;
  69374. + hcd->frame_list_dma = 0;
  69375. + hcd->periodic_qh_count = 0;
  69376. +
  69377. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  69378. +#ifdef FIQ_DEBUG
  69379. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  69380. +#endif
  69381. +
  69382. +out:
  69383. + return retval;
  69384. +}
  69385. +
  69386. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  69387. +{
  69388. + /* Turn off all host-specific interrupts. */
  69389. + dwc_otg_disable_host_interrupts(hcd->core_if);
  69390. +
  69391. + dwc_otg_hcd_free(hcd);
  69392. +}
  69393. +
  69394. +/**
  69395. + * Initializes dynamic portions of the DWC_otg HCD state.
  69396. + */
  69397. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  69398. +{
  69399. + int num_channels;
  69400. + int i;
  69401. + dwc_hc_t *channel;
  69402. + dwc_hc_t *channel_tmp;
  69403. +
  69404. + hcd->flags.d32 = 0;
  69405. +
  69406. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  69407. + if (!microframe_schedule) {
  69408. + hcd->non_periodic_channels = 0;
  69409. + hcd->periodic_channels = 0;
  69410. + } else {
  69411. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  69412. + }
  69413. + /*
  69414. + * Put all channels in the free channel list and clean up channel
  69415. + * states.
  69416. + */
  69417. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  69418. + &hcd->free_hc_list, hc_list_entry) {
  69419. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  69420. + }
  69421. +
  69422. + num_channels = hcd->core_if->core_params->host_channels;
  69423. + for (i = 0; i < num_channels; i++) {
  69424. + channel = hcd->hc_ptr_array[i];
  69425. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  69426. + hc_list_entry);
  69427. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  69428. + }
  69429. +
  69430. + /* Initialize the DWC core for host mode operation. */
  69431. + dwc_otg_core_host_init(hcd->core_if);
  69432. +
  69433. + /* Set core_if's lock pointer to the hcd->lock */
  69434. + hcd->core_if->lock = hcd->lock;
  69435. +}
  69436. +
  69437. +/**
  69438. + * Assigns transactions from a QTD to a free host channel and initializes the
  69439. + * host channel to perform the transactions. The host channel is removed from
  69440. + * the free list.
  69441. + *
  69442. + * @param hcd The HCD state structure.
  69443. + * @param qh Transactions from the first QTD for this QH are selected and
  69444. + * assigned to a free host channel.
  69445. + */
  69446. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  69447. +{
  69448. + dwc_hc_t *hc;
  69449. + dwc_otg_qtd_t *qtd;
  69450. + dwc_otg_hcd_urb_t *urb;
  69451. + void* ptr = NULL;
  69452. +
  69453. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  69454. +
  69455. + urb = qtd->urb;
  69456. +
  69457. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  69458. +
  69459. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  69460. + urb->actual_length = urb->length;
  69461. +
  69462. +
  69463. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  69464. +
  69465. + /* Remove the host channel from the free list. */
  69466. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  69467. +
  69468. + qh->channel = hc;
  69469. +
  69470. + qtd->in_process = 1;
  69471. +
  69472. + /*
  69473. + * Use usb_pipedevice to determine device address. This address is
  69474. + * 0 before the SET_ADDRESS command and the correct address afterward.
  69475. + */
  69476. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  69477. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  69478. + hc->speed = qh->dev_speed;
  69479. + hc->max_packet = dwc_max_packet(qh->maxp);
  69480. +
  69481. + hc->xfer_started = 0;
  69482. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  69483. + hc->error_state = (qtd->error_count > 0);
  69484. + hc->halt_on_queue = 0;
  69485. + hc->halt_pending = 0;
  69486. + hc->requests = 0;
  69487. +
  69488. + /*
  69489. + * The following values may be modified in the transfer type section
  69490. + * below. The xfer_len value may be reduced when the transfer is
  69491. + * started to accommodate the max widths of the XferSize and PktCnt
  69492. + * fields in the HCTSIZn register.
  69493. + */
  69494. +
  69495. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  69496. + if (hc->ep_is_in) {
  69497. + hc->do_ping = 0;
  69498. + } else {
  69499. + hc->do_ping = qh->ping_state;
  69500. + }
  69501. +
  69502. + hc->data_pid_start = qh->data_toggle;
  69503. + hc->multi_count = 1;
  69504. +
  69505. + if (hcd->core_if->dma_enable) {
  69506. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  69507. +
  69508. + /* For non-dword aligned case */
  69509. + if (((unsigned long)hc->xfer_buff & 0x3)
  69510. + && !hcd->core_if->dma_desc_enable) {
  69511. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  69512. + }
  69513. + } else {
  69514. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  69515. + }
  69516. + hc->xfer_len = urb->length - urb->actual_length;
  69517. + hc->xfer_count = 0;
  69518. +
  69519. + /*
  69520. + * Set the split attributes
  69521. + */
  69522. + hc->do_split = 0;
  69523. + if (qh->do_split) {
  69524. + uint32_t hub_addr, port_addr;
  69525. + hc->do_split = 1;
  69526. + hc->xact_pos = qtd->isoc_split_pos;
  69527. + /* We don't need to do complete splits anymore */
  69528. + if(fiq_split_enable)
  69529. + hc->complete_split = qtd->complete_split = 0;
  69530. + else
  69531. + hc->complete_split = qtd->complete_split;
  69532. +
  69533. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  69534. + hc->hub_addr = (uint8_t) hub_addr;
  69535. + hc->port_addr = (uint8_t) port_addr;
  69536. + }
  69537. +
  69538. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  69539. + case UE_CONTROL:
  69540. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  69541. + switch (qtd->control_phase) {
  69542. + case DWC_OTG_CONTROL_SETUP:
  69543. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  69544. + hc->do_ping = 0;
  69545. + hc->ep_is_in = 0;
  69546. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  69547. + if (hcd->core_if->dma_enable) {
  69548. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  69549. + } else {
  69550. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  69551. + }
  69552. + hc->xfer_len = 8;
  69553. + ptr = NULL;
  69554. + break;
  69555. + case DWC_OTG_CONTROL_DATA:
  69556. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  69557. + hc->data_pid_start = qtd->data_toggle;
  69558. + break;
  69559. + case DWC_OTG_CONTROL_STATUS:
  69560. + /*
  69561. + * Direction is opposite of data direction or IN if no
  69562. + * data.
  69563. + */
  69564. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  69565. + if (urb->length == 0) {
  69566. + hc->ep_is_in = 1;
  69567. + } else {
  69568. + hc->ep_is_in =
  69569. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  69570. + }
  69571. + if (hc->ep_is_in) {
  69572. + hc->do_ping = 0;
  69573. + }
  69574. +
  69575. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  69576. +
  69577. + hc->xfer_len = 0;
  69578. + if (hcd->core_if->dma_enable) {
  69579. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  69580. + } else {
  69581. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  69582. + }
  69583. + ptr = NULL;
  69584. + break;
  69585. + }
  69586. + break;
  69587. + case UE_BULK:
  69588. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  69589. + break;
  69590. + case UE_INTERRUPT:
  69591. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  69592. + break;
  69593. + case UE_ISOCHRONOUS:
  69594. + {
  69595. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  69596. +
  69597. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  69598. +
  69599. + if (hcd->core_if->dma_desc_enable)
  69600. + break;
  69601. +
  69602. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  69603. +
  69604. + frame_desc->status = 0;
  69605. +
  69606. + if (hcd->core_if->dma_enable) {
  69607. + hc->xfer_buff = (uint8_t *) urb->dma;
  69608. + } else {
  69609. + hc->xfer_buff = (uint8_t *) urb->buf;
  69610. + }
  69611. + hc->xfer_buff +=
  69612. + frame_desc->offset + qtd->isoc_split_offset;
  69613. + hc->xfer_len =
  69614. + frame_desc->length - qtd->isoc_split_offset;
  69615. +
  69616. + /* For non-dword aligned buffers */
  69617. + if (((unsigned long)hc->xfer_buff & 0x3)
  69618. + && hcd->core_if->dma_enable) {
  69619. + ptr =
  69620. + (uint8_t *) urb->buf + frame_desc->offset +
  69621. + qtd->isoc_split_offset;
  69622. + } else
  69623. + ptr = NULL;
  69624. +
  69625. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  69626. + if (hc->xfer_len <= 188) {
  69627. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  69628. + } else {
  69629. + hc->xact_pos =
  69630. + DWC_HCSPLIT_XACTPOS_BEGIN;
  69631. + }
  69632. + }
  69633. + }
  69634. + break;
  69635. + }
  69636. + /* non DWORD-aligned buffer case */
  69637. + if (ptr) {
  69638. + uint32_t buf_size;
  69639. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  69640. + buf_size = hcd->core_if->core_params->max_transfer_size;
  69641. + } else {
  69642. + buf_size = 4096;
  69643. + }
  69644. + if (!qh->dw_align_buf) {
  69645. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
  69646. + &qh->dw_align_buf_dma);
  69647. + if (!qh->dw_align_buf) {
  69648. + DWC_ERROR
  69649. + ("%s: Failed to allocate memory to handle "
  69650. + "non-dword aligned buffer case\n",
  69651. + __func__);
  69652. + return;
  69653. + }
  69654. + }
  69655. + if (!hc->ep_is_in) {
  69656. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  69657. + }
  69658. + hc->align_buff = qh->dw_align_buf_dma;
  69659. + } else {
  69660. + hc->align_buff = 0;
  69661. + }
  69662. +
  69663. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  69664. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  69665. + /*
  69666. + * This value may be modified when the transfer is started to
  69667. + * reflect the actual transfer length.
  69668. + */
  69669. + hc->multi_count = dwc_hb_mult(qh->maxp);
  69670. + }
  69671. +
  69672. + if (hcd->core_if->dma_desc_enable)
  69673. + hc->desc_list_addr = qh->desc_list_dma;
  69674. +
  69675. + dwc_otg_hc_init(hcd->core_if, hc);
  69676. + hc->qh = qh;
  69677. +}
  69678. +
  69679. +/*
  69680. +** Check the transaction to see if the port / hub has already been assigned for
  69681. +** a split transaction
  69682. +**
  69683. +** Return 0 - Port is already in use
  69684. +*/
  69685. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh)
  69686. +{
  69687. + uint32_t hub_addr, port_addr;
  69688. +
  69689. + if(!fiq_split_enable)
  69690. + return 0;
  69691. +
  69692. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  69693. +
  69694. + if(hcd->hub_port[hub_addr] & (1 << port_addr))
  69695. + {
  69696. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:S%02d", hub_addr, port_addr, qh->skip_count);
  69697. +
  69698. + qh->skip_count++;
  69699. +
  69700. + if(qh->skip_count > 40000)
  69701. + {
  69702. + printk_once(KERN_ERR "Error: Having to skip port allocation");
  69703. + local_fiq_disable();
  69704. + BUG();
  69705. + return 0;
  69706. + }
  69707. + return 1;
  69708. + }
  69709. + else
  69710. + {
  69711. + qh->skip_count = 0;
  69712. + hcd->hub_port[hub_addr] |= 1 << port_addr;
  69713. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:A %d", hub_addr, port_addr, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->pipe_info.ep_num);
  69714. +#ifdef FIQ_DEBUG
  69715. + hcd->hub_port_alloc[hub_addr * 16 + port_addr] = dwc_otg_hcd_get_frame_number(hcd);
  69716. +#endif
  69717. + return 0;
  69718. + }
  69719. +}
  69720. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh)
  69721. +{
  69722. + uint32_t hub_addr, port_addr;
  69723. +
  69724. + if(!fiq_split_enable)
  69725. + return;
  69726. +
  69727. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  69728. +
  69729. + hcd->hub_port[hub_addr] &= ~(1 << port_addr);
  69730. +#ifdef FIQ_DEBUG
  69731. + hcd->hub_port_alloc[hub_addr * 16 + port_addr] = -1;
  69732. +#endif
  69733. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:RO%d", hub_addr, port_addr, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->pipe_info.ep_num);
  69734. +
  69735. +}
  69736. +
  69737. +
  69738. +/**
  69739. + * This function selects transactions from the HCD transfer schedule and
  69740. + * assigns them to available host channels. It is called from HCD interrupt
  69741. + * handler functions.
  69742. + *
  69743. + * @param hcd The HCD state structure.
  69744. + *
  69745. + * @return The types of new transactions that were assigned to host channels.
  69746. + */
  69747. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  69748. +{
  69749. + dwc_list_link_t *qh_ptr;
  69750. + dwc_otg_qh_t *qh;
  69751. + dwc_otg_qtd_t *qtd;
  69752. + int num_channels;
  69753. + dwc_irqflags_t flags;
  69754. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  69755. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  69756. +
  69757. +#ifdef DEBUG_SOF
  69758. + DWC_DEBUGPL(DBG_HCD, " Select Transactions\n");
  69759. +#endif
  69760. +
  69761. +#ifdef DEBUG_HOST_CHANNELS
  69762. + last_sel_trans_num_per_scheduled = 0;
  69763. + last_sel_trans_num_nonper_scheduled = 0;
  69764. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  69765. +#endif /* DEBUG_HOST_CHANNELS */
  69766. +
  69767. + /* Process entries in the periodic ready list. */
  69768. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  69769. +
  69770. + while (qh_ptr != &hcd->periodic_sched_ready &&
  69771. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  69772. +
  69773. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69774. +
  69775. + if(qh->do_split) {
  69776. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  69777. + if(!(qh->ep_type == UE_ISOCHRONOUS &&
  69778. + (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  69779. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END))) {
  69780. + if(dwc_otg_hcd_allocate_port(hcd, qh))
  69781. + {
  69782. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69783. + g_next_sched_frame = dwc_frame_num_inc(dwc_otg_hcd_get_frame_number(hcd), 1);
  69784. + continue;
  69785. + }
  69786. + }
  69787. + }
  69788. +
  69789. + if (microframe_schedule) {
  69790. + // Make sure we leave one channel for non periodic transactions.
  69791. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69792. + if (hcd->available_host_channels <= 1) {
  69793. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69794. + if(qh->do_split) dwc_otg_hcd_release_port(hcd, qh);
  69795. + break;
  69796. + }
  69797. + hcd->available_host_channels--;
  69798. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69799. +#ifdef DEBUG_HOST_CHANNELS
  69800. + last_sel_trans_num_per_scheduled++;
  69801. +#endif /* DEBUG_HOST_CHANNELS */
  69802. + }
  69803. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69804. + assign_and_init_hc(hcd, qh);
  69805. +
  69806. + /*
  69807. + * Move the QH from the periodic ready schedule to the
  69808. + * periodic assigned schedule.
  69809. + */
  69810. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69811. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69812. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  69813. + &qh->qh_list_entry);
  69814. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69815. + }
  69816. +
  69817. + /*
  69818. + * Process entries in the inactive portion of the non-periodic
  69819. + * schedule. Some free host channels may not be used if they are
  69820. + * reserved for periodic transfers.
  69821. + */
  69822. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  69823. + num_channels = hcd->core_if->core_params->host_channels;
  69824. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  69825. + (microframe_schedule || hcd->non_periodic_channels <
  69826. + num_channels - hcd->periodic_channels) &&
  69827. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  69828. +
  69829. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69830. +
  69831. + /*
  69832. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  69833. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  69834. + * cheeky devices that just hold off using NAKs
  69835. + */
  69836. + if (nak_holdoff_enable && qh->do_split) {
  69837. + if (qh->nak_frame != 0xffff &&
  69838. + dwc_full_frame_num(qh->nak_frame) ==
  69839. + dwc_full_frame_num(dwc_otg_hcd_get_frame_number(hcd))) {
  69840. + /*
  69841. + * Revisit: Need to avoid trampling on periodic scheduling.
  69842. + * Currently we are safe because g_np_count != g_np_sent whenever we hit this,
  69843. + * but if this behaviour is changed then periodic endpoints will get a slower
  69844. + * polling rate.
  69845. + */
  69846. + g_next_sched_frame = ((qh->nak_frame + 8) & ~7) & DWC_HFNUM_MAX_FRNUM;
  69847. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69848. + continue;
  69849. + } else {
  69850. + qh->nak_frame = 0xffff;
  69851. + }
  69852. + }
  69853. +
  69854. + if (microframe_schedule) {
  69855. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69856. + if (hcd->available_host_channels < 1) {
  69857. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69858. + break;
  69859. + }
  69860. + hcd->available_host_channels--;
  69861. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69862. +#ifdef DEBUG_HOST_CHANNELS
  69863. + last_sel_trans_num_nonper_scheduled++;
  69864. +#endif /* DEBUG_HOST_CHANNELS */
  69865. + }
  69866. +
  69867. + assign_and_init_hc(hcd, qh);
  69868. +
  69869. + /*
  69870. + * Move the QH from the non-periodic inactive schedule to the
  69871. + * non-periodic active schedule.
  69872. + */
  69873. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69874. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69875. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  69876. + &qh->qh_list_entry);
  69877. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69878. +
  69879. + g_np_sent++;
  69880. +
  69881. + if (!microframe_schedule)
  69882. + hcd->non_periodic_channels++;
  69883. + }
  69884. +
  69885. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  69886. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  69887. +
  69888. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  69889. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  69890. +
  69891. +
  69892. +#ifdef DEBUG_HOST_CHANNELS
  69893. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  69894. +#endif /* DEBUG_HOST_CHANNELS */
  69895. + return ret_val;
  69896. +}
  69897. +
  69898. +/**
  69899. + * Attempts to queue a single transaction request for a host channel
  69900. + * associated with either a periodic or non-periodic transfer. This function
  69901. + * assumes that there is space available in the appropriate request queue. For
  69902. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  69903. + * is available in the appropriate Tx FIFO.
  69904. + *
  69905. + * @param hcd The HCD state structure.
  69906. + * @param hc Host channel descriptor associated with either a periodic or
  69907. + * non-periodic transfer.
  69908. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  69909. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  69910. + * transfers.
  69911. + *
  69912. + * @return 1 if a request is queued and more requests may be needed to
  69913. + * complete the transfer, 0 if no more requests are required for this
  69914. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  69915. + */
  69916. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  69917. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  69918. +{
  69919. + int retval;
  69920. +
  69921. + if (hcd->core_if->dma_enable) {
  69922. + if (hcd->core_if->dma_desc_enable) {
  69923. + if (!hc->xfer_started
  69924. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  69925. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  69926. + hc->qh->ping_state = 0;
  69927. + }
  69928. + } else if (!hc->xfer_started) {
  69929. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69930. + hc->qh->ping_state = 0;
  69931. + }
  69932. + retval = 0;
  69933. + } else if (hc->halt_pending) {
  69934. + /* Don't queue a request if the channel has been halted. */
  69935. + retval = 0;
  69936. + } else if (hc->halt_on_queue) {
  69937. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  69938. + retval = 0;
  69939. + } else if (hc->do_ping) {
  69940. + if (!hc->xfer_started) {
  69941. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69942. + }
  69943. + retval = 0;
  69944. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  69945. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  69946. + if (!hc->xfer_started) {
  69947. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69948. + retval = 1;
  69949. + } else {
  69950. + retval =
  69951. + dwc_otg_hc_continue_transfer(hcd->core_if,
  69952. + hc);
  69953. + }
  69954. + } else {
  69955. + retval = -1;
  69956. + }
  69957. + } else {
  69958. + if (!hc->xfer_started) {
  69959. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69960. + retval = 1;
  69961. + } else {
  69962. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  69963. + }
  69964. + }
  69965. +
  69966. + return retval;
  69967. +}
  69968. +
  69969. +/**
  69970. + * Processes periodic channels for the next frame and queues transactions for
  69971. + * these channels to the DWC_otg controller. After queueing transactions, the
  69972. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  69973. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  69974. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  69975. + */
  69976. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  69977. +{
  69978. + hptxsts_data_t tx_status;
  69979. + dwc_list_link_t *qh_ptr;
  69980. + dwc_otg_qh_t *qh;
  69981. + int status;
  69982. + int no_queue_space = 0;
  69983. + int no_fifo_space = 0;
  69984. +
  69985. + dwc_otg_host_global_regs_t *host_regs;
  69986. + host_regs = hcd->core_if->host_if->host_global_regs;
  69987. +
  69988. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  69989. +#ifdef DEBUG
  69990. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  69991. + DWC_DEBUGPL(DBG_HCDV,
  69992. + " P Tx Req Queue Space Avail (before queue): %d\n",
  69993. + tx_status.b.ptxqspcavail);
  69994. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  69995. + tx_status.b.ptxfspcavail);
  69996. +#endif
  69997. +
  69998. + qh_ptr = hcd->periodic_sched_assigned.next;
  69999. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  70000. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  70001. + if (tx_status.b.ptxqspcavail == 0) {
  70002. + no_queue_space = 1;
  70003. + break;
  70004. + }
  70005. +
  70006. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  70007. +
  70008. + // Do not send a split start transaction any later than frame .6
  70009. + // Note, we have to schedule a periodic in .5 to make it go in .6
  70010. + if(fiq_split_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  70011. + {
  70012. + qh_ptr = qh_ptr->next;
  70013. + g_next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  70014. + continue;
  70015. + }
  70016. +
  70017. + /*
  70018. + * Set a flag if we're queuing high-bandwidth in slave mode.
  70019. + * The flag prevents any halts to get into the request queue in
  70020. + * the middle of multiple high-bandwidth packets getting queued.
  70021. + */
  70022. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  70023. + hcd->core_if->queuing_high_bandwidth = 1;
  70024. + }
  70025. + status =
  70026. + queue_transaction(hcd, qh->channel,
  70027. + tx_status.b.ptxfspcavail);
  70028. + if (status < 0) {
  70029. + no_fifo_space = 1;
  70030. + break;
  70031. + }
  70032. +
  70033. + /*
  70034. + * In Slave mode, stay on the current transfer until there is
  70035. + * nothing more to do or the high-bandwidth request count is
  70036. + * reached. In DMA mode, only need to queue one request. The
  70037. + * controller automatically handles multiple packets for
  70038. + * high-bandwidth transfers.
  70039. + */
  70040. + if (hcd->core_if->dma_enable || status == 0 ||
  70041. + qh->channel->requests == qh->channel->multi_count) {
  70042. + qh_ptr = qh_ptr->next;
  70043. + /*
  70044. + * Move the QH from the periodic assigned schedule to
  70045. + * the periodic queued schedule.
  70046. + */
  70047. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  70048. + &qh->qh_list_entry);
  70049. +
  70050. + /* done queuing high bandwidth */
  70051. + hcd->core_if->queuing_high_bandwidth = 0;
  70052. + }
  70053. + }
  70054. +
  70055. + if (!hcd->core_if->dma_enable) {
  70056. + dwc_otg_core_global_regs_t *global_regs;
  70057. + gintmsk_data_t intr_mask = {.d32 = 0 };
  70058. +
  70059. + global_regs = hcd->core_if->core_global_regs;
  70060. + intr_mask.b.ptxfempty = 1;
  70061. +#ifdef DEBUG
  70062. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  70063. + DWC_DEBUGPL(DBG_HCDV,
  70064. + " P Tx Req Queue Space Avail (after queue): %d\n",
  70065. + tx_status.b.ptxqspcavail);
  70066. + DWC_DEBUGPL(DBG_HCDV,
  70067. + " P Tx FIFO Space Avail (after queue): %d\n",
  70068. + tx_status.b.ptxfspcavail);
  70069. +#endif
  70070. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  70071. + no_queue_space || no_fifo_space) {
  70072. + /*
  70073. + * May need to queue more transactions as the request
  70074. + * queue or Tx FIFO empties. Enable the periodic Tx
  70075. + * FIFO empty interrupt. (Always use the half-empty
  70076. + * level to ensure that new requests are loaded as
  70077. + * soon as possible.)
  70078. + */
  70079. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  70080. + intr_mask.d32);
  70081. + } else {
  70082. + /*
  70083. + * Disable the Tx FIFO empty interrupt since there are
  70084. + * no more transactions that need to be queued right
  70085. + * now. This function is called from interrupt
  70086. + * handlers to queue more transactions as transfer
  70087. + * states change.
  70088. + */
  70089. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  70090. + 0);
  70091. + }
  70092. + }
  70093. +}
  70094. +
  70095. +/**
  70096. + * Processes active non-periodic channels and queues transactions for these
  70097. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  70098. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  70099. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  70100. + * FIFO Empty interrupt is disabled.
  70101. + */
  70102. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  70103. +{
  70104. + gnptxsts_data_t tx_status;
  70105. + dwc_list_link_t *orig_qh_ptr;
  70106. + dwc_otg_qh_t *qh;
  70107. + int status;
  70108. + int no_queue_space = 0;
  70109. + int no_fifo_space = 0;
  70110. + int more_to_do = 0;
  70111. +
  70112. + dwc_otg_core_global_regs_t *global_regs =
  70113. + hcd->core_if->core_global_regs;
  70114. +
  70115. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  70116. +#ifdef DEBUG
  70117. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  70118. + DWC_DEBUGPL(DBG_HCDV,
  70119. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  70120. + tx_status.b.nptxqspcavail);
  70121. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  70122. + tx_status.b.nptxfspcavail);
  70123. +#endif
  70124. + /*
  70125. + * Keep track of the starting point. Skip over the start-of-list
  70126. + * entry.
  70127. + */
  70128. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  70129. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  70130. + }
  70131. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  70132. +
  70133. + /*
  70134. + * Process once through the active list or until no more space is
  70135. + * available in the request queue or the Tx FIFO.
  70136. + */
  70137. + do {
  70138. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  70139. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  70140. + no_queue_space = 1;
  70141. + break;
  70142. + }
  70143. +
  70144. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  70145. + qh_list_entry);
  70146. +
  70147. + // Do not send a split start transaction any later than frame .5
  70148. + // non periodic transactions will start immediately in this uframe
  70149. + if(fiq_split_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  70150. + {
  70151. + g_next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  70152. + break;
  70153. + }
  70154. +
  70155. + status =
  70156. + queue_transaction(hcd, qh->channel,
  70157. + tx_status.b.nptxfspcavail);
  70158. +
  70159. + if (status > 0) {
  70160. + more_to_do = 1;
  70161. + } else if (status < 0) {
  70162. + no_fifo_space = 1;
  70163. + break;
  70164. + }
  70165. +
  70166. + /* Advance to next QH, skipping start-of-list entry. */
  70167. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  70168. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  70169. + hcd->non_periodic_qh_ptr =
  70170. + hcd->non_periodic_qh_ptr->next;
  70171. + }
  70172. +
  70173. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  70174. +
  70175. + if (!hcd->core_if->dma_enable) {
  70176. + gintmsk_data_t intr_mask = {.d32 = 0 };
  70177. + intr_mask.b.nptxfempty = 1;
  70178. +
  70179. +#ifdef DEBUG
  70180. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  70181. + DWC_DEBUGPL(DBG_HCDV,
  70182. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  70183. + tx_status.b.nptxqspcavail);
  70184. + DWC_DEBUGPL(DBG_HCDV,
  70185. + " NP Tx FIFO Space Avail (after queue): %d\n",
  70186. + tx_status.b.nptxfspcavail);
  70187. +#endif
  70188. + if (more_to_do || no_queue_space || no_fifo_space) {
  70189. + /*
  70190. + * May need to queue more transactions as the request
  70191. + * queue or Tx FIFO empties. Enable the non-periodic
  70192. + * Tx FIFO empty interrupt. (Always use the half-empty
  70193. + * level to ensure that new requests are loaded as
  70194. + * soon as possible.)
  70195. + */
  70196. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  70197. + intr_mask.d32);
  70198. + } else {
  70199. + /*
  70200. + * Disable the Tx FIFO empty interrupt since there are
  70201. + * no more transactions that need to be queued right
  70202. + * now. This function is called from interrupt
  70203. + * handlers to queue more transactions as transfer
  70204. + * states change.
  70205. + */
  70206. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  70207. + 0);
  70208. + }
  70209. + }
  70210. +}
  70211. +
  70212. +/**
  70213. + * This function processes the currently active host channels and queues
  70214. + * transactions for these channels to the DWC_otg controller. It is called
  70215. + * from HCD interrupt handler functions.
  70216. + *
  70217. + * @param hcd The HCD state structure.
  70218. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  70219. + * periodic, or both).
  70220. + */
  70221. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  70222. + dwc_otg_transaction_type_e tr_type)
  70223. +{
  70224. +#ifdef DEBUG_SOF
  70225. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  70226. +#endif
  70227. + /* Process host channels associated with periodic transfers. */
  70228. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  70229. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  70230. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  70231. +
  70232. + process_periodic_channels(hcd);
  70233. + }
  70234. +
  70235. + /* Process host channels associated with non-periodic transfers. */
  70236. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  70237. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  70238. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  70239. + process_non_periodic_channels(hcd);
  70240. + } else {
  70241. + /*
  70242. + * Ensure NP Tx FIFO empty interrupt is disabled when
  70243. + * there are no non-periodic transfers to process.
  70244. + */
  70245. + gintmsk_data_t gintmsk = {.d32 = 0 };
  70246. + gintmsk.b.nptxfempty = 1;
  70247. + DWC_MODIFY_REG32(&hcd->core_if->
  70248. + core_global_regs->gintmsk, gintmsk.d32,
  70249. + 0);
  70250. + }
  70251. + }
  70252. +}
  70253. +
  70254. +#ifdef DWC_HS_ELECT_TST
  70255. +/*
  70256. + * Quick and dirty hack to implement the HS Electrical Test
  70257. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  70258. + *
  70259. + * This code was copied from our userspace app "hset". It sends a
  70260. + * Get Device Descriptor control sequence in two parts, first the
  70261. + * Setup packet by itself, followed some time later by the In and
  70262. + * Ack packets. Rather than trying to figure out how to add this
  70263. + * functionality to the normal driver code, we just hijack the
  70264. + * hardware, using these two function to drive the hardware
  70265. + * directly.
  70266. + */
  70267. +
  70268. +static dwc_otg_core_global_regs_t *global_regs;
  70269. +static dwc_otg_host_global_regs_t *hc_global_regs;
  70270. +static dwc_otg_hc_regs_t *hc_regs;
  70271. +static uint32_t *data_fifo;
  70272. +
  70273. +static void do_setup(void)
  70274. +{
  70275. + gintsts_data_t gintsts;
  70276. + hctsiz_data_t hctsiz;
  70277. + hcchar_data_t hcchar;
  70278. + haint_data_t haint;
  70279. + hcint_data_t hcint;
  70280. +
  70281. + /* Enable HAINTs */
  70282. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  70283. +
  70284. + /* Enable HCINTs */
  70285. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  70286. +
  70287. + /* Read GINTSTS */
  70288. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70289. +
  70290. + /* Read HAINT */
  70291. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70292. +
  70293. + /* Read HCINT */
  70294. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70295. +
  70296. + /* Read HCCHAR */
  70297. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70298. +
  70299. + /* Clear HCINT */
  70300. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70301. +
  70302. + /* Clear HAINT */
  70303. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70304. +
  70305. + /* Clear GINTSTS */
  70306. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70307. +
  70308. + /* Read GINTSTS */
  70309. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70310. +
  70311. + /*
  70312. + * Send Setup packet (Get Device Descriptor)
  70313. + */
  70314. +
  70315. + /* Make sure channel is disabled */
  70316. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70317. + if (hcchar.b.chen) {
  70318. + hcchar.b.chdis = 1;
  70319. +// hcchar.b.chen = 1;
  70320. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70321. + //sleep(1);
  70322. + dwc_mdelay(1000);
  70323. +
  70324. + /* Read GINTSTS */
  70325. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70326. +
  70327. + /* Read HAINT */
  70328. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70329. +
  70330. + /* Read HCINT */
  70331. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70332. +
  70333. + /* Read HCCHAR */
  70334. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70335. +
  70336. + /* Clear HCINT */
  70337. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70338. +
  70339. + /* Clear HAINT */
  70340. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70341. +
  70342. + /* Clear GINTSTS */
  70343. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70344. +
  70345. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70346. + }
  70347. +
  70348. + /* Set HCTSIZ */
  70349. + hctsiz.d32 = 0;
  70350. + hctsiz.b.xfersize = 8;
  70351. + hctsiz.b.pktcnt = 1;
  70352. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  70353. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  70354. +
  70355. + /* Set HCCHAR */
  70356. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70357. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70358. + hcchar.b.epdir = 0;
  70359. + hcchar.b.epnum = 0;
  70360. + hcchar.b.mps = 8;
  70361. + hcchar.b.chen = 1;
  70362. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70363. +
  70364. + /* Fill FIFO with Setup data for Get Device Descriptor */
  70365. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  70366. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  70367. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  70368. +
  70369. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70370. +
  70371. + /* Wait for host channel interrupt */
  70372. + do {
  70373. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70374. + } while (gintsts.b.hcintr == 0);
  70375. +
  70376. + /* Disable HCINTs */
  70377. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  70378. +
  70379. + /* Disable HAINTs */
  70380. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  70381. +
  70382. + /* Read HAINT */
  70383. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70384. +
  70385. + /* Read HCINT */
  70386. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70387. +
  70388. + /* Read HCCHAR */
  70389. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70390. +
  70391. + /* Clear HCINT */
  70392. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70393. +
  70394. + /* Clear HAINT */
  70395. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70396. +
  70397. + /* Clear GINTSTS */
  70398. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70399. +
  70400. + /* Read GINTSTS */
  70401. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70402. +}
  70403. +
  70404. +static void do_in_ack(void)
  70405. +{
  70406. + gintsts_data_t gintsts;
  70407. + hctsiz_data_t hctsiz;
  70408. + hcchar_data_t hcchar;
  70409. + haint_data_t haint;
  70410. + hcint_data_t hcint;
  70411. + host_grxsts_data_t grxsts;
  70412. +
  70413. + /* Enable HAINTs */
  70414. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  70415. +
  70416. + /* Enable HCINTs */
  70417. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  70418. +
  70419. + /* Read GINTSTS */
  70420. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70421. +
  70422. + /* Read HAINT */
  70423. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70424. +
  70425. + /* Read HCINT */
  70426. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70427. +
  70428. + /* Read HCCHAR */
  70429. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70430. +
  70431. + /* Clear HCINT */
  70432. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70433. +
  70434. + /* Clear HAINT */
  70435. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70436. +
  70437. + /* Clear GINTSTS */
  70438. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70439. +
  70440. + /* Read GINTSTS */
  70441. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70442. +
  70443. + /*
  70444. + * Receive Control In packet
  70445. + */
  70446. +
  70447. + /* Make sure channel is disabled */
  70448. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70449. + if (hcchar.b.chen) {
  70450. + hcchar.b.chdis = 1;
  70451. + hcchar.b.chen = 1;
  70452. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70453. + //sleep(1);
  70454. + dwc_mdelay(1000);
  70455. +
  70456. + /* Read GINTSTS */
  70457. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70458. +
  70459. + /* Read HAINT */
  70460. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70461. +
  70462. + /* Read HCINT */
  70463. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70464. +
  70465. + /* Read HCCHAR */
  70466. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70467. +
  70468. + /* Clear HCINT */
  70469. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70470. +
  70471. + /* Clear HAINT */
  70472. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70473. +
  70474. + /* Clear GINTSTS */
  70475. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70476. +
  70477. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70478. + }
  70479. +
  70480. + /* Set HCTSIZ */
  70481. + hctsiz.d32 = 0;
  70482. + hctsiz.b.xfersize = 8;
  70483. + hctsiz.b.pktcnt = 1;
  70484. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  70485. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  70486. +
  70487. + /* Set HCCHAR */
  70488. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70489. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70490. + hcchar.b.epdir = 1;
  70491. + hcchar.b.epnum = 0;
  70492. + hcchar.b.mps = 8;
  70493. + hcchar.b.chen = 1;
  70494. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70495. +
  70496. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70497. +
  70498. + /* Wait for receive status queue interrupt */
  70499. + do {
  70500. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70501. + } while (gintsts.b.rxstsqlvl == 0);
  70502. +
  70503. + /* Read RXSTS */
  70504. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  70505. +
  70506. + /* Clear RXSTSQLVL in GINTSTS */
  70507. + gintsts.d32 = 0;
  70508. + gintsts.b.rxstsqlvl = 1;
  70509. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70510. +
  70511. + switch (grxsts.b.pktsts) {
  70512. + case DWC_GRXSTS_PKTSTS_IN:
  70513. + /* Read the data into the host buffer */
  70514. + if (grxsts.b.bcnt > 0) {
  70515. + int i;
  70516. + int word_count = (grxsts.b.bcnt + 3) / 4;
  70517. +
  70518. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  70519. +
  70520. + for (i = 0; i < word_count; i++) {
  70521. + (void)DWC_READ_REG32(data_fifo++);
  70522. + }
  70523. + }
  70524. + break;
  70525. +
  70526. + default:
  70527. + break;
  70528. + }
  70529. +
  70530. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70531. +
  70532. + /* Wait for receive status queue interrupt */
  70533. + do {
  70534. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70535. + } while (gintsts.b.rxstsqlvl == 0);
  70536. +
  70537. + /* Read RXSTS */
  70538. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  70539. +
  70540. + /* Clear RXSTSQLVL in GINTSTS */
  70541. + gintsts.d32 = 0;
  70542. + gintsts.b.rxstsqlvl = 1;
  70543. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70544. +
  70545. + switch (grxsts.b.pktsts) {
  70546. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  70547. + break;
  70548. +
  70549. + default:
  70550. + break;
  70551. + }
  70552. +
  70553. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70554. +
  70555. + /* Wait for host channel interrupt */
  70556. + do {
  70557. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70558. + } while (gintsts.b.hcintr == 0);
  70559. +
  70560. + /* Read HAINT */
  70561. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70562. +
  70563. + /* Read HCINT */
  70564. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70565. +
  70566. + /* Read HCCHAR */
  70567. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70568. +
  70569. + /* Clear HCINT */
  70570. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70571. +
  70572. + /* Clear HAINT */
  70573. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70574. +
  70575. + /* Clear GINTSTS */
  70576. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70577. +
  70578. + /* Read GINTSTS */
  70579. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70580. +
  70581. +// usleep(100000);
  70582. +// mdelay(100);
  70583. + dwc_mdelay(1);
  70584. +
  70585. + /*
  70586. + * Send handshake packet
  70587. + */
  70588. +
  70589. + /* Read HAINT */
  70590. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70591. +
  70592. + /* Read HCINT */
  70593. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70594. +
  70595. + /* Read HCCHAR */
  70596. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70597. +
  70598. + /* Clear HCINT */
  70599. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70600. +
  70601. + /* Clear HAINT */
  70602. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70603. +
  70604. + /* Clear GINTSTS */
  70605. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70606. +
  70607. + /* Read GINTSTS */
  70608. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70609. +
  70610. + /* Make sure channel is disabled */
  70611. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70612. + if (hcchar.b.chen) {
  70613. + hcchar.b.chdis = 1;
  70614. + hcchar.b.chen = 1;
  70615. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70616. + //sleep(1);
  70617. + dwc_mdelay(1000);
  70618. +
  70619. + /* Read GINTSTS */
  70620. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70621. +
  70622. + /* Read HAINT */
  70623. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70624. +
  70625. + /* Read HCINT */
  70626. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70627. +
  70628. + /* Read HCCHAR */
  70629. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70630. +
  70631. + /* Clear HCINT */
  70632. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70633. +
  70634. + /* Clear HAINT */
  70635. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70636. +
  70637. + /* Clear GINTSTS */
  70638. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70639. +
  70640. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70641. + }
  70642. +
  70643. + /* Set HCTSIZ */
  70644. + hctsiz.d32 = 0;
  70645. + hctsiz.b.xfersize = 0;
  70646. + hctsiz.b.pktcnt = 1;
  70647. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  70648. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  70649. +
  70650. + /* Set HCCHAR */
  70651. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70652. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70653. + hcchar.b.epdir = 0;
  70654. + hcchar.b.epnum = 0;
  70655. + hcchar.b.mps = 8;
  70656. + hcchar.b.chen = 1;
  70657. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70658. +
  70659. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70660. +
  70661. + /* Wait for host channel interrupt */
  70662. + do {
  70663. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70664. + } while (gintsts.b.hcintr == 0);
  70665. +
  70666. + /* Disable HCINTs */
  70667. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  70668. +
  70669. + /* Disable HAINTs */
  70670. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  70671. +
  70672. + /* Read HAINT */
  70673. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70674. +
  70675. + /* Read HCINT */
  70676. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70677. +
  70678. + /* Read HCCHAR */
  70679. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70680. +
  70681. + /* Clear HCINT */
  70682. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70683. +
  70684. + /* Clear HAINT */
  70685. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70686. +
  70687. + /* Clear GINTSTS */
  70688. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70689. +
  70690. + /* Read GINTSTS */
  70691. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70692. +}
  70693. +#endif
  70694. +
  70695. +/** Handles hub class-specific requests. */
  70696. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  70697. + uint16_t typeReq,
  70698. + uint16_t wValue,
  70699. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  70700. +{
  70701. + int retval = 0;
  70702. +
  70703. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  70704. + usb_hub_descriptor_t *hub_desc;
  70705. + hprt0_data_t hprt0 = {.d32 = 0 };
  70706. +
  70707. + uint32_t port_status;
  70708. +
  70709. + switch (typeReq) {
  70710. + case UCR_CLEAR_HUB_FEATURE:
  70711. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70712. + "ClearHubFeature 0x%x\n", wValue);
  70713. + switch (wValue) {
  70714. + case UHF_C_HUB_LOCAL_POWER:
  70715. + case UHF_C_HUB_OVER_CURRENT:
  70716. + /* Nothing required here */
  70717. + break;
  70718. + default:
  70719. + retval = -DWC_E_INVALID;
  70720. + DWC_ERROR("DWC OTG HCD - "
  70721. + "ClearHubFeature request %xh unknown\n",
  70722. + wValue);
  70723. + }
  70724. + break;
  70725. + case UCR_CLEAR_PORT_FEATURE:
  70726. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70727. + if (wValue != UHF_PORT_L1)
  70728. +#endif
  70729. + if (!wIndex || wIndex > 1)
  70730. + goto error;
  70731. +
  70732. + switch (wValue) {
  70733. + case UHF_PORT_ENABLE:
  70734. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  70735. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  70736. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70737. + hprt0.b.prtena = 1;
  70738. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70739. + break;
  70740. + case UHF_PORT_SUSPEND:
  70741. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70742. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  70743. +
  70744. + if (core_if->power_down == 2) {
  70745. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  70746. + } else {
  70747. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  70748. + dwc_mdelay(5);
  70749. +
  70750. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70751. + hprt0.b.prtres = 1;
  70752. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70753. + hprt0.b.prtsusp = 0;
  70754. + /* Clear Resume bit */
  70755. + dwc_mdelay(100);
  70756. + hprt0.b.prtres = 0;
  70757. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70758. + }
  70759. + break;
  70760. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70761. + case UHF_PORT_L1:
  70762. + {
  70763. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70764. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  70765. +
  70766. + lpmcfg.d32 =
  70767. + DWC_READ_REG32(&core_if->
  70768. + core_global_regs->glpmcfg);
  70769. + lpmcfg.b.en_utmi_sleep = 0;
  70770. + lpmcfg.b.hird_thres &= (~(1 << 4));
  70771. + lpmcfg.b.prt_sleep_sts = 1;
  70772. + DWC_WRITE_REG32(&core_if->
  70773. + core_global_regs->glpmcfg,
  70774. + lpmcfg.d32);
  70775. +
  70776. + /* Clear Enbl_L1Gating bit. */
  70777. + pcgcctl.b.enbl_sleep_gating = 1;
  70778. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  70779. + 0);
  70780. +
  70781. + dwc_mdelay(5);
  70782. +
  70783. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70784. + hprt0.b.prtres = 1;
  70785. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  70786. + hprt0.d32);
  70787. + /* This bit will be cleared in wakeup interrupt handle */
  70788. + break;
  70789. + }
  70790. +#endif
  70791. + case UHF_PORT_POWER:
  70792. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70793. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  70794. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70795. + hprt0.b.prtpwr = 0;
  70796. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70797. + break;
  70798. + case UHF_PORT_INDICATOR:
  70799. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70800. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  70801. + /* Port inidicator not supported */
  70802. + break;
  70803. + case UHF_C_PORT_CONNECTION:
  70804. + /* Clears drivers internal connect status change
  70805. + * flag */
  70806. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70807. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  70808. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  70809. + break;
  70810. + case UHF_C_PORT_RESET:
  70811. + /* Clears the driver's internal Port Reset Change
  70812. + * flag */
  70813. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70814. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  70815. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  70816. + break;
  70817. + case UHF_C_PORT_ENABLE:
  70818. + /* Clears the driver's internal Port
  70819. + * Enable/Disable Change flag */
  70820. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70821. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  70822. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  70823. + break;
  70824. + case UHF_C_PORT_SUSPEND:
  70825. + /* Clears the driver's internal Port Suspend
  70826. + * Change flag, which is set when resume signaling on
  70827. + * the host port is complete */
  70828. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70829. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  70830. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  70831. + break;
  70832. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70833. + case UHF_C_PORT_L1:
  70834. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  70835. + break;
  70836. +#endif
  70837. + case UHF_C_PORT_OVER_CURRENT:
  70838. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70839. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  70840. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  70841. + break;
  70842. + default:
  70843. + retval = -DWC_E_INVALID;
  70844. + DWC_ERROR("DWC OTG HCD - "
  70845. + "ClearPortFeature request %xh "
  70846. + "unknown or unsupported\n", wValue);
  70847. + }
  70848. + break;
  70849. + case UCR_GET_HUB_DESCRIPTOR:
  70850. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70851. + "GetHubDescriptor\n");
  70852. + hub_desc = (usb_hub_descriptor_t *) buf;
  70853. + hub_desc->bDescLength = 9;
  70854. + hub_desc->bDescriptorType = 0x29;
  70855. + hub_desc->bNbrPorts = 1;
  70856. + USETW(hub_desc->wHubCharacteristics, 0x08);
  70857. + hub_desc->bPwrOn2PwrGood = 1;
  70858. + hub_desc->bHubContrCurrent = 0;
  70859. + hub_desc->DeviceRemovable[0] = 0;
  70860. + hub_desc->DeviceRemovable[1] = 0xff;
  70861. + break;
  70862. + case UCR_GET_HUB_STATUS:
  70863. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70864. + "GetHubStatus\n");
  70865. + DWC_MEMSET(buf, 0, 4);
  70866. + break;
  70867. + case UCR_GET_PORT_STATUS:
  70868. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70869. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  70870. + wIndex, dwc_otg_hcd->flags.d32);
  70871. + if (!wIndex || wIndex > 1)
  70872. + goto error;
  70873. +
  70874. + port_status = 0;
  70875. +
  70876. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  70877. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  70878. +
  70879. + if (dwc_otg_hcd->flags.b.port_enable_change)
  70880. + port_status |= (1 << UHF_C_PORT_ENABLE);
  70881. +
  70882. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  70883. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  70884. +
  70885. + if (dwc_otg_hcd->flags.b.port_l1_change)
  70886. + port_status |= (1 << UHF_C_PORT_L1);
  70887. +
  70888. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  70889. + port_status |= (1 << UHF_C_PORT_RESET);
  70890. + }
  70891. +
  70892. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  70893. + DWC_WARN("Overcurrent change detected\n");
  70894. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  70895. + }
  70896. +
  70897. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  70898. + /*
  70899. + * The port is disconnected, which means the core is
  70900. + * either in device mode or it soon will be. Just
  70901. + * return 0's for the remainder of the port status
  70902. + * since the port register can't be read if the core
  70903. + * is in device mode.
  70904. + */
  70905. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  70906. + break;
  70907. + }
  70908. +
  70909. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  70910. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  70911. +
  70912. + if (hprt0.b.prtconnsts)
  70913. + port_status |= (1 << UHF_PORT_CONNECTION);
  70914. +
  70915. + if (hprt0.b.prtena)
  70916. + port_status |= (1 << UHF_PORT_ENABLE);
  70917. +
  70918. + if (hprt0.b.prtsusp)
  70919. + port_status |= (1 << UHF_PORT_SUSPEND);
  70920. +
  70921. + if (hprt0.b.prtovrcurract)
  70922. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  70923. +
  70924. + if (hprt0.b.prtrst)
  70925. + port_status |= (1 << UHF_PORT_RESET);
  70926. +
  70927. + if (hprt0.b.prtpwr)
  70928. + port_status |= (1 << UHF_PORT_POWER);
  70929. +
  70930. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  70931. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  70932. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  70933. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  70934. +
  70935. + if (hprt0.b.prttstctl)
  70936. + port_status |= (1 << UHF_PORT_TEST);
  70937. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  70938. + port_status |= (1 << UHF_PORT_L1);
  70939. + }
  70940. + /*
  70941. + For Synopsys HW emulation of Power down wkup_control asserts the
  70942. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  70943. + We intentionally tell the software that port is in L2Suspend state.
  70944. + Only for STE.
  70945. + */
  70946. + if ((core_if->power_down == 2)
  70947. + && (core_if->hibernation_suspend == 1)) {
  70948. + port_status |= (1 << UHF_PORT_SUSPEND);
  70949. + }
  70950. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  70951. +
  70952. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  70953. +
  70954. + break;
  70955. + case UCR_SET_HUB_FEATURE:
  70956. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70957. + "SetHubFeature\n");
  70958. + /* No HUB features supported */
  70959. + break;
  70960. + case UCR_SET_PORT_FEATURE:
  70961. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  70962. + goto error;
  70963. +
  70964. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  70965. + /*
  70966. + * The port is disconnected, which means the core is
  70967. + * either in device mode or it soon will be. Just
  70968. + * return without doing anything since the port
  70969. + * register can't be written if the core is in device
  70970. + * mode.
  70971. + */
  70972. + break;
  70973. + }
  70974. +
  70975. + switch (wValue) {
  70976. + case UHF_PORT_SUSPEND:
  70977. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70978. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  70979. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  70980. + goto error;
  70981. + }
  70982. + if (core_if->power_down == 2) {
  70983. + int timeout = 300;
  70984. + dwc_irqflags_t flags;
  70985. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70986. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  70987. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  70988. +#ifdef DWC_DEV_SRPCAP
  70989. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  70990. +#endif
  70991. + DWC_PRINTF("Preparing for complete power-off\n");
  70992. +
  70993. + /* Save registers before hibernation */
  70994. + dwc_otg_save_global_regs(core_if);
  70995. + dwc_otg_save_host_regs(core_if);
  70996. +
  70997. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70998. + hprt0.b.prtsusp = 1;
  70999. + hprt0.b.prtena = 0;
  71000. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71001. + /* Spin hprt0.b.prtsusp to became 1 */
  71002. + do {
  71003. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71004. + if (hprt0.b.prtsusp) {
  71005. + break;
  71006. + }
  71007. + dwc_mdelay(1);
  71008. + } while (--timeout);
  71009. + if (!timeout) {
  71010. + DWC_WARN("Suspend wasn't genereted\n");
  71011. + }
  71012. + dwc_udelay(10);
  71013. +
  71014. + /*
  71015. + * We need to disable interrupts to prevent servicing of any IRQ
  71016. + * during going to hibernation
  71017. + */
  71018. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  71019. + core_if->lx_state = DWC_OTG_L2;
  71020. +#ifdef DWC_DEV_SRPCAP
  71021. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71022. + hprt0.b.prtpwr = 0;
  71023. + hprt0.b.prtena = 0;
  71024. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  71025. + hprt0.d32);
  71026. +#endif
  71027. + gusbcfg.d32 =
  71028. + DWC_READ_REG32(&core_if->core_global_regs->
  71029. + gusbcfg);
  71030. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  71031. + /* ULPI interface */
  71032. + /* Suspend the Phy Clock */
  71033. + pcgcctl.d32 = 0;
  71034. + pcgcctl.b.stoppclk = 1;
  71035. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71036. + pcgcctl.d32);
  71037. + dwc_udelay(10);
  71038. + gpwrdn.b.pmuactv = 1;
  71039. + DWC_MODIFY_REG32(&core_if->
  71040. + core_global_regs->
  71041. + gpwrdn, 0, gpwrdn.d32);
  71042. + } else {
  71043. + /* UTMI+ Interface */
  71044. + gpwrdn.b.pmuactv = 1;
  71045. + DWC_MODIFY_REG32(&core_if->
  71046. + core_global_regs->
  71047. + gpwrdn, 0, gpwrdn.d32);
  71048. + dwc_udelay(10);
  71049. + pcgcctl.b.stoppclk = 1;
  71050. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  71051. + dwc_udelay(10);
  71052. + }
  71053. +#ifdef DWC_DEV_SRPCAP
  71054. + gpwrdn.d32 = 0;
  71055. + gpwrdn.b.dis_vbus = 1;
  71056. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71057. + gpwrdn, 0, gpwrdn.d32);
  71058. +#endif
  71059. + gpwrdn.d32 = 0;
  71060. + gpwrdn.b.pmuintsel = 1;
  71061. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71062. + gpwrdn, 0, gpwrdn.d32);
  71063. + dwc_udelay(10);
  71064. +
  71065. + gpwrdn.d32 = 0;
  71066. +#ifdef DWC_DEV_SRPCAP
  71067. + gpwrdn.b.srp_det_msk = 1;
  71068. +#endif
  71069. + gpwrdn.b.disconn_det_msk = 1;
  71070. + gpwrdn.b.lnstchng_msk = 1;
  71071. + gpwrdn.b.sts_chngint_msk = 1;
  71072. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71073. + gpwrdn, 0, gpwrdn.d32);
  71074. + dwc_udelay(10);
  71075. +
  71076. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  71077. + gpwrdn.d32 = 0;
  71078. + gpwrdn.b.pwrdnclmp = 1;
  71079. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71080. + gpwrdn, 0, gpwrdn.d32);
  71081. + dwc_udelay(10);
  71082. +
  71083. + /* Switch off VDD */
  71084. + gpwrdn.d32 = 0;
  71085. + gpwrdn.b.pwrdnswtch = 1;
  71086. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71087. + gpwrdn, 0, gpwrdn.d32);
  71088. +
  71089. +#ifdef DWC_DEV_SRPCAP
  71090. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  71091. + {
  71092. + core_if->pwron_timer_started = 1;
  71093. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  71094. + }
  71095. +#endif
  71096. + /* Save gpwrdn register for further usage if stschng interrupt */
  71097. + core_if->gr_backup->gpwrdn_local =
  71098. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71099. +
  71100. + /* Set flag to indicate that we are in hibernation */
  71101. + core_if->hibernation_suspend = 1;
  71102. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  71103. +
  71104. + DWC_PRINTF("Host hibernation completed\n");
  71105. + // Exit from case statement
  71106. + break;
  71107. +
  71108. + }
  71109. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  71110. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  71111. + gotgctl_data_t gotgctl = {.d32 = 0 };
  71112. + gotgctl.b.hstsethnpen = 1;
  71113. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71114. + gotgctl, 0, gotgctl.d32);
  71115. + core_if->op_state = A_SUSPEND;
  71116. + }
  71117. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71118. + hprt0.b.prtsusp = 1;
  71119. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71120. + {
  71121. + dwc_irqflags_t flags;
  71122. + /* Update lx_state */
  71123. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  71124. + core_if->lx_state = DWC_OTG_L2;
  71125. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  71126. + }
  71127. + /* Suspend the Phy Clock */
  71128. + {
  71129. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71130. + pcgcctl.b.stoppclk = 1;
  71131. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71132. + pcgcctl.d32);
  71133. + dwc_udelay(10);
  71134. + }
  71135. +
  71136. + /* For HNP the bus must be suspended for at least 200ms. */
  71137. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  71138. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71139. + pcgcctl.b.stoppclk = 1;
  71140. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71141. + dwc_mdelay(200);
  71142. + }
  71143. +
  71144. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  71145. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  71146. + if (core_if->adp_enable) {
  71147. + gotgctl_data_t gotgctl = {.d32 = 0 };
  71148. + gpwrdn_data_t gpwrdn;
  71149. +
  71150. + while (gotgctl.b.asesvld == 1) {
  71151. + gotgctl.d32 =
  71152. + DWC_READ_REG32(&core_if->
  71153. + core_global_regs->
  71154. + gotgctl);
  71155. + dwc_mdelay(100);
  71156. + }
  71157. +
  71158. + /* Enable Power Down Logic */
  71159. + gpwrdn.d32 = 0;
  71160. + gpwrdn.b.pmuactv = 1;
  71161. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71162. + gpwrdn, 0, gpwrdn.d32);
  71163. +
  71164. + /* Unmask SRP detected interrupt from Power Down Logic */
  71165. + gpwrdn.d32 = 0;
  71166. + gpwrdn.b.srp_det_msk = 1;
  71167. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71168. + gpwrdn, 0, gpwrdn.d32);
  71169. +
  71170. + dwc_otg_adp_probe_start(core_if);
  71171. + }
  71172. +#endif
  71173. + break;
  71174. + case UHF_PORT_POWER:
  71175. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  71176. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  71177. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71178. + hprt0.b.prtpwr = 1;
  71179. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71180. + break;
  71181. + case UHF_PORT_RESET:
  71182. + if ((core_if->power_down == 2)
  71183. + && (core_if->hibernation_suspend == 1)) {
  71184. + /* If we are going to exit from Hibernated
  71185. + * state via USB RESET.
  71186. + */
  71187. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  71188. + } else {
  71189. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71190. +
  71191. + DWC_DEBUGPL(DBG_HCD,
  71192. + "DWC OTG HCD HUB CONTROL - "
  71193. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  71194. + {
  71195. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71196. + pcgcctl.b.enbl_sleep_gating = 1;
  71197. + pcgcctl.b.stoppclk = 1;
  71198. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71199. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  71200. + }
  71201. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71202. + {
  71203. + glpmcfg_data_t lpmcfg;
  71204. + lpmcfg.d32 =
  71205. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71206. + if (lpmcfg.b.prt_sleep_sts) {
  71207. + lpmcfg.b.en_utmi_sleep = 0;
  71208. + lpmcfg.b.hird_thres &= (~(1 << 4));
  71209. + DWC_WRITE_REG32
  71210. + (&core_if->core_global_regs->glpmcfg,
  71211. + lpmcfg.d32);
  71212. + dwc_mdelay(1);
  71213. + }
  71214. + }
  71215. +#endif
  71216. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71217. + /* Clear suspend bit if resetting from suspended state. */
  71218. + hprt0.b.prtsusp = 0;
  71219. + /* When B-Host the Port reset bit is set in
  71220. + * the Start HCD Callback function, so that
  71221. + * the reset is started within 1ms of the HNP
  71222. + * success interrupt. */
  71223. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  71224. + hprt0.b.prtpwr = 1;
  71225. + hprt0.b.prtrst = 1;
  71226. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  71227. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  71228. + hprt0.d32);
  71229. + }
  71230. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  71231. + dwc_mdelay(60);
  71232. + hprt0.b.prtrst = 0;
  71233. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71234. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  71235. + }
  71236. + break;
  71237. +#ifdef DWC_HS_ELECT_TST
  71238. + case UHF_PORT_TEST:
  71239. + {
  71240. + uint32_t t;
  71241. + gintmsk_data_t gintmsk;
  71242. +
  71243. + t = (wIndex >> 8); /* MSB wIndex USB */
  71244. + DWC_DEBUGPL(DBG_HCD,
  71245. + "DWC OTG HCD HUB CONTROL - "
  71246. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  71247. + t);
  71248. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  71249. + if (t < 6) {
  71250. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71251. + hprt0.b.prttstctl = t;
  71252. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  71253. + hprt0.d32);
  71254. + } else {
  71255. + /* Setup global vars with reg addresses (quick and
  71256. + * dirty hack, should be cleaned up)
  71257. + */
  71258. + global_regs = core_if->core_global_regs;
  71259. + hc_global_regs =
  71260. + core_if->host_if->host_global_regs;
  71261. + hc_regs =
  71262. + (dwc_otg_hc_regs_t *) ((char *)
  71263. + global_regs +
  71264. + 0x500);
  71265. + data_fifo =
  71266. + (uint32_t *) ((char *)global_regs +
  71267. + 0x1000);
  71268. +
  71269. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  71270. + /* Save current interrupt mask */
  71271. + gintmsk.d32 =
  71272. + DWC_READ_REG32
  71273. + (&global_regs->gintmsk);
  71274. +
  71275. + /* Disable all interrupts while we muck with
  71276. + * the hardware directly
  71277. + */
  71278. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  71279. +
  71280. + /* 15 second delay per the test spec */
  71281. + dwc_mdelay(15000);
  71282. +
  71283. + /* Drive suspend on the root port */
  71284. + hprt0.d32 =
  71285. + dwc_otg_read_hprt0(core_if);
  71286. + hprt0.b.prtsusp = 1;
  71287. + hprt0.b.prtres = 0;
  71288. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71289. +
  71290. + /* 15 second delay per the test spec */
  71291. + dwc_mdelay(15000);
  71292. +
  71293. + /* Drive resume on the root port */
  71294. + hprt0.d32 =
  71295. + dwc_otg_read_hprt0(core_if);
  71296. + hprt0.b.prtsusp = 0;
  71297. + hprt0.b.prtres = 1;
  71298. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71299. + dwc_mdelay(100);
  71300. +
  71301. + /* Clear the resume bit */
  71302. + hprt0.b.prtres = 0;
  71303. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71304. +
  71305. + /* Restore interrupts */
  71306. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  71307. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  71308. + /* Save current interrupt mask */
  71309. + gintmsk.d32 =
  71310. + DWC_READ_REG32
  71311. + (&global_regs->gintmsk);
  71312. +
  71313. + /* Disable all interrupts while we muck with
  71314. + * the hardware directly
  71315. + */
  71316. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  71317. +
  71318. + /* 15 second delay per the test spec */
  71319. + dwc_mdelay(15000);
  71320. +
  71321. + /* Send the Setup packet */
  71322. + do_setup();
  71323. +
  71324. + /* 15 second delay so nothing else happens for awhile */
  71325. + dwc_mdelay(15000);
  71326. +
  71327. + /* Restore interrupts */
  71328. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  71329. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  71330. + /* Save current interrupt mask */
  71331. + gintmsk.d32 =
  71332. + DWC_READ_REG32
  71333. + (&global_regs->gintmsk);
  71334. +
  71335. + /* Disable all interrupts while we muck with
  71336. + * the hardware directly
  71337. + */
  71338. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  71339. +
  71340. + /* Send the Setup packet */
  71341. + do_setup();
  71342. +
  71343. + /* 15 second delay so nothing else happens for awhile */
  71344. + dwc_mdelay(15000);
  71345. +
  71346. + /* Send the In and Ack packets */
  71347. + do_in_ack();
  71348. +
  71349. + /* 15 second delay so nothing else happens for awhile */
  71350. + dwc_mdelay(15000);
  71351. +
  71352. + /* Restore interrupts */
  71353. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  71354. + }
  71355. + }
  71356. + break;
  71357. + }
  71358. +#endif /* DWC_HS_ELECT_TST */
  71359. +
  71360. + case UHF_PORT_INDICATOR:
  71361. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  71362. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  71363. + /* Not supported */
  71364. + break;
  71365. + default:
  71366. + retval = -DWC_E_INVALID;
  71367. + DWC_ERROR("DWC OTG HCD - "
  71368. + "SetPortFeature request %xh "
  71369. + "unknown or unsupported\n", wValue);
  71370. + break;
  71371. + }
  71372. + break;
  71373. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71374. + case UCR_SET_AND_TEST_PORT_FEATURE:
  71375. + if (wValue != UHF_PORT_L1) {
  71376. + goto error;
  71377. + }
  71378. + {
  71379. + int portnum, hird, devaddr, remwake;
  71380. + glpmcfg_data_t lpmcfg;
  71381. + uint32_t time_usecs;
  71382. + gintsts_data_t gintsts;
  71383. + gintmsk_data_t gintmsk;
  71384. +
  71385. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  71386. + goto error;
  71387. + }
  71388. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  71389. + goto error;
  71390. + }
  71391. + /* Check if the port currently is in SLEEP state */
  71392. + lpmcfg.d32 =
  71393. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71394. + if (lpmcfg.b.prt_sleep_sts) {
  71395. + DWC_INFO("Port is already in sleep mode\n");
  71396. + buf[0] = 0; /* Return success */
  71397. + break;
  71398. + }
  71399. +
  71400. + portnum = wIndex & 0xf;
  71401. + hird = (wIndex >> 4) & 0xf;
  71402. + devaddr = (wIndex >> 8) & 0x7f;
  71403. + remwake = (wIndex >> 15);
  71404. +
  71405. + if (portnum != 1) {
  71406. + retval = -DWC_E_INVALID;
  71407. + DWC_WARN
  71408. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  71409. + portnum);
  71410. + break;
  71411. + }
  71412. +
  71413. + DWC_PRINTF
  71414. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  71415. + portnum, hird, devaddr, remwake);
  71416. + /* Disable LPM interrupt */
  71417. + gintmsk.d32 = 0;
  71418. + gintmsk.b.lpmtranrcvd = 1;
  71419. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  71420. + gintmsk.d32, 0);
  71421. +
  71422. + if (dwc_otg_hcd_send_lpm
  71423. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  71424. + retval = -DWC_E_INVALID;
  71425. + break;
  71426. + }
  71427. +
  71428. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  71429. + /* We will consider timeout if time_usecs microseconds pass,
  71430. + * and we don't receive LPM transaction status.
  71431. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  71432. + * core will set lpmtranrcvd bit.
  71433. + */
  71434. + do {
  71435. + gintsts.d32 =
  71436. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  71437. + if (gintsts.b.lpmtranrcvd) {
  71438. + break;
  71439. + }
  71440. + dwc_udelay(1);
  71441. + } while (--time_usecs);
  71442. + /* lpm_int bit will be cleared in LPM interrupt handler */
  71443. +
  71444. + /* Now fill status
  71445. + * 0x00 - Success
  71446. + * 0x10 - NYET
  71447. + * 0x11 - Timeout
  71448. + */
  71449. + if (!gintsts.b.lpmtranrcvd) {
  71450. + buf[0] = 0x3; /* Completion code is Timeout */
  71451. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  71452. + } else {
  71453. + lpmcfg.d32 =
  71454. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71455. + if (lpmcfg.b.lpm_resp == 0x3) {
  71456. + /* ACK responce from the device */
  71457. + buf[0] = 0x00; /* Success */
  71458. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  71459. + /* NYET responce from the device */
  71460. + buf[0] = 0x2;
  71461. + } else {
  71462. + /* Otherwise responce with Timeout */
  71463. + buf[0] = 0x3;
  71464. + }
  71465. + }
  71466. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  71467. + lpmcfg.b.lpm_resp);
  71468. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  71469. + gintmsk.d32);
  71470. +
  71471. + break;
  71472. + }
  71473. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  71474. + default:
  71475. +error:
  71476. + retval = -DWC_E_INVALID;
  71477. + DWC_WARN("DWC OTG HCD - "
  71478. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  71479. + typeReq, wIndex, wValue);
  71480. + break;
  71481. + }
  71482. +
  71483. + return retval;
  71484. +}
  71485. +
  71486. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71487. +/** Returns index of host channel to perform LPM transaction. */
  71488. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  71489. +{
  71490. + dwc_otg_core_if_t *core_if = hcd->core_if;
  71491. + dwc_hc_t *hc;
  71492. + hcchar_data_t hcchar;
  71493. + gintmsk_data_t gintmsk = {.d32 = 0 };
  71494. +
  71495. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  71496. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  71497. + return -1;
  71498. + }
  71499. +
  71500. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  71501. +
  71502. + /* Mask host channel interrupts. */
  71503. + gintmsk.b.hcintr = 1;
  71504. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  71505. +
  71506. + /* Fill fields that core needs for LPM transaction */
  71507. + hcchar.b.devaddr = devaddr;
  71508. + hcchar.b.epnum = 0;
  71509. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  71510. + hcchar.b.mps = 64;
  71511. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  71512. + hcchar.b.epdir = 0; /* OUT */
  71513. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  71514. + hcchar.d32);
  71515. +
  71516. + /* Remove the host channel from the free list. */
  71517. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  71518. +
  71519. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  71520. +
  71521. + return hc->hc_num;
  71522. +}
  71523. +
  71524. +/** Release hc after performing LPM transaction */
  71525. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  71526. +{
  71527. + dwc_hc_t *hc;
  71528. + glpmcfg_data_t lpmcfg;
  71529. + uint8_t hc_num;
  71530. +
  71531. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  71532. + hc_num = lpmcfg.b.lpm_chan_index;
  71533. +
  71534. + hc = hcd->hc_ptr_array[hc_num];
  71535. +
  71536. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  71537. + /* Return host channel to free list */
  71538. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  71539. +}
  71540. +
  71541. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  71542. + uint8_t bRemoteWake)
  71543. +{
  71544. + glpmcfg_data_t lpmcfg;
  71545. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71546. + int channel;
  71547. +
  71548. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  71549. + if (channel < 0) {
  71550. + return channel;
  71551. + }
  71552. +
  71553. + pcgcctl.b.enbl_sleep_gating = 1;
  71554. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  71555. +
  71556. + /* Read LPM config register */
  71557. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  71558. +
  71559. + /* Program LPM transaction fields */
  71560. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  71561. + lpmcfg.b.hird = hird;
  71562. + lpmcfg.b.hird_thres = 0x1c;
  71563. + lpmcfg.b.lpm_chan_index = channel;
  71564. + lpmcfg.b.en_utmi_sleep = 1;
  71565. + /* Program LPM config register */
  71566. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  71567. +
  71568. + /* Send LPM transaction */
  71569. + lpmcfg.b.send_lpm = 1;
  71570. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  71571. +
  71572. + return 0;
  71573. +}
  71574. +
  71575. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  71576. +
  71577. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  71578. +{
  71579. + int retval;
  71580. +
  71581. + if (port != 1) {
  71582. + return -DWC_E_INVALID;
  71583. + }
  71584. +
  71585. + retval = (hcd->flags.b.port_connect_status_change ||
  71586. + hcd->flags.b.port_reset_change ||
  71587. + hcd->flags.b.port_enable_change ||
  71588. + hcd->flags.b.port_suspend_change ||
  71589. + hcd->flags.b.port_over_current_change);
  71590. +#ifdef DEBUG
  71591. + if (retval) {
  71592. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  71593. + " Root port status changed\n");
  71594. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  71595. + hcd->flags.b.port_connect_status_change);
  71596. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  71597. + hcd->flags.b.port_reset_change);
  71598. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  71599. + hcd->flags.b.port_enable_change);
  71600. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  71601. + hcd->flags.b.port_suspend_change);
  71602. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  71603. + hcd->flags.b.port_over_current_change);
  71604. + }
  71605. +#endif
  71606. + return retval;
  71607. +}
  71608. +
  71609. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  71610. +{
  71611. + hfnum_data_t hfnum;
  71612. + hfnum.d32 =
  71613. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  71614. + hfnum);
  71615. +
  71616. +#ifdef DEBUG_SOF
  71617. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  71618. + hfnum.b.frnum);
  71619. +#endif
  71620. + return hfnum.b.frnum;
  71621. +}
  71622. +
  71623. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  71624. + struct dwc_otg_hcd_function_ops *fops)
  71625. +{
  71626. + int retval = 0;
  71627. +
  71628. + hcd->fops = fops;
  71629. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  71630. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  71631. + dwc_otg_hcd_reinit(hcd);
  71632. + } else {
  71633. + retval = -DWC_E_NO_DEVICE;
  71634. + }
  71635. +
  71636. + return retval;
  71637. +}
  71638. +
  71639. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  71640. +{
  71641. + return hcd->priv;
  71642. +}
  71643. +
  71644. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  71645. +{
  71646. + hcd->priv = priv_data;
  71647. +}
  71648. +
  71649. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  71650. +{
  71651. + return hcd->otg_port;
  71652. +}
  71653. +
  71654. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  71655. +{
  71656. + uint32_t is_b_host;
  71657. + if (hcd->core_if->op_state == B_HOST) {
  71658. + is_b_host = 1;
  71659. + } else {
  71660. + is_b_host = 0;
  71661. + }
  71662. +
  71663. + return is_b_host;
  71664. +}
  71665. +
  71666. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  71667. + int iso_desc_count, int atomic_alloc)
  71668. +{
  71669. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  71670. + uint32_t size;
  71671. +
  71672. + size =
  71673. + sizeof(*dwc_otg_urb) +
  71674. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  71675. + if (atomic_alloc)
  71676. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  71677. + else
  71678. + dwc_otg_urb = DWC_ALLOC(size);
  71679. +
  71680. + if (dwc_otg_urb)
  71681. + dwc_otg_urb->packet_count = iso_desc_count;
  71682. + else {
  71683. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  71684. + "%salloc of %db failed\n",
  71685. + atomic_alloc?"atomic ":"", size);
  71686. + }
  71687. + return dwc_otg_urb;
  71688. +}
  71689. +
  71690. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71691. + uint8_t dev_addr, uint8_t ep_num,
  71692. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  71693. +{
  71694. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  71695. + ep_type, ep_dir, mps);
  71696. +#if 0
  71697. + DWC_PRINTF
  71698. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  71699. + dev_addr, ep_num, ep_dir, ep_type, mps);
  71700. +#endif
  71701. +}
  71702. +
  71703. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71704. + void *urb_handle, void *buf, dwc_dma_t dma,
  71705. + uint32_t buflen, void *setup_packet,
  71706. + dwc_dma_t setup_dma, uint32_t flags,
  71707. + uint16_t interval)
  71708. +{
  71709. + dwc_otg_urb->priv = urb_handle;
  71710. + dwc_otg_urb->buf = buf;
  71711. + dwc_otg_urb->dma = dma;
  71712. + dwc_otg_urb->length = buflen;
  71713. + dwc_otg_urb->setup_packet = setup_packet;
  71714. + dwc_otg_urb->setup_dma = setup_dma;
  71715. + dwc_otg_urb->flags = flags;
  71716. + dwc_otg_urb->interval = interval;
  71717. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  71718. +}
  71719. +
  71720. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71721. +{
  71722. + return dwc_otg_urb->status;
  71723. +}
  71724. +
  71725. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71726. +{
  71727. + return dwc_otg_urb->actual_length;
  71728. +}
  71729. +
  71730. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71731. +{
  71732. + return dwc_otg_urb->error_count;
  71733. +}
  71734. +
  71735. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71736. + int desc_num, uint32_t offset,
  71737. + uint32_t length)
  71738. +{
  71739. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  71740. + dwc_otg_urb->iso_descs[desc_num].length = length;
  71741. +}
  71742. +
  71743. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71744. + int desc_num)
  71745. +{
  71746. + return dwc_otg_urb->iso_descs[desc_num].status;
  71747. +}
  71748. +
  71749. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  71750. + dwc_otg_urb, int desc_num)
  71751. +{
  71752. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  71753. +}
  71754. +
  71755. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  71756. +{
  71757. + int allocated = 0;
  71758. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71759. +
  71760. + if (qh) {
  71761. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  71762. + allocated = 1;
  71763. + }
  71764. + }
  71765. + return allocated;
  71766. +}
  71767. +
  71768. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  71769. +{
  71770. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71771. + int freed = 0;
  71772. + DWC_ASSERT(qh, "qh is not allocated\n");
  71773. +
  71774. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  71775. + freed = 1;
  71776. + }
  71777. +
  71778. + return freed;
  71779. +}
  71780. +
  71781. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  71782. +{
  71783. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71784. + DWC_ASSERT(qh, "qh is not allocated\n");
  71785. + return qh->usecs;
  71786. +}
  71787. +
  71788. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  71789. +{
  71790. +#ifdef DEBUG
  71791. + int num_channels;
  71792. + int i;
  71793. + gnptxsts_data_t np_tx_status;
  71794. + hptxsts_data_t p_tx_status;
  71795. +
  71796. + num_channels = hcd->core_if->core_params->host_channels;
  71797. + DWC_PRINTF("\n");
  71798. + DWC_PRINTF
  71799. + ("************************************************************\n");
  71800. + DWC_PRINTF("HCD State:\n");
  71801. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  71802. + for (i = 0; i < num_channels; i++) {
  71803. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  71804. + DWC_PRINTF(" Channel %d:\n", i);
  71805. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  71806. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  71807. + DWC_PRINTF(" speed: %d\n", hc->speed);
  71808. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  71809. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  71810. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  71811. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  71812. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  71813. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  71814. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  71815. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  71816. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  71817. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  71818. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  71819. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  71820. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  71821. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  71822. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  71823. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  71824. + DWC_PRINTF(" requests: %d\n", hc->requests);
  71825. + DWC_PRINTF(" qh: %p\n", hc->qh);
  71826. + if (hc->xfer_started) {
  71827. + hfnum_data_t hfnum;
  71828. + hcchar_data_t hcchar;
  71829. + hctsiz_data_t hctsiz;
  71830. + hcint_data_t hcint;
  71831. + hcintmsk_data_t hcintmsk;
  71832. + hfnum.d32 =
  71833. + DWC_READ_REG32(&hcd->core_if->
  71834. + host_if->host_global_regs->hfnum);
  71835. + hcchar.d32 =
  71836. + DWC_READ_REG32(&hcd->core_if->host_if->
  71837. + hc_regs[i]->hcchar);
  71838. + hctsiz.d32 =
  71839. + DWC_READ_REG32(&hcd->core_if->host_if->
  71840. + hc_regs[i]->hctsiz);
  71841. + hcint.d32 =
  71842. + DWC_READ_REG32(&hcd->core_if->host_if->
  71843. + hc_regs[i]->hcint);
  71844. + hcintmsk.d32 =
  71845. + DWC_READ_REG32(&hcd->core_if->host_if->
  71846. + hc_regs[i]->hcintmsk);
  71847. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  71848. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  71849. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  71850. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  71851. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  71852. + }
  71853. + if (hc->xfer_started && hc->qh) {
  71854. + dwc_otg_qtd_t *qtd;
  71855. + dwc_otg_hcd_urb_t *urb;
  71856. +
  71857. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  71858. + if (!qtd->in_process)
  71859. + break;
  71860. +
  71861. + urb = qtd->urb;
  71862. + DWC_PRINTF(" URB Info:\n");
  71863. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  71864. + if (urb) {
  71865. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  71866. + dwc_otg_hcd_get_dev_addr(&urb->
  71867. + pipe_info),
  71868. + dwc_otg_hcd_get_ep_num(&urb->
  71869. + pipe_info),
  71870. + dwc_otg_hcd_is_pipe_in(&urb->
  71871. + pipe_info) ?
  71872. + "IN" : "OUT");
  71873. + DWC_PRINTF(" Max packet size: %d\n",
  71874. + dwc_otg_hcd_get_mps(&urb->
  71875. + pipe_info));
  71876. + DWC_PRINTF(" transfer_buffer: %p\n",
  71877. + urb->buf);
  71878. + DWC_PRINTF(" transfer_dma: %p\n",
  71879. + (void *)urb->dma);
  71880. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  71881. + urb->length);
  71882. + DWC_PRINTF(" actual_length: %d\n",
  71883. + urb->actual_length);
  71884. + }
  71885. + }
  71886. + }
  71887. + }
  71888. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  71889. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  71890. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  71891. + np_tx_status.d32 =
  71892. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  71893. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  71894. + np_tx_status.b.nptxqspcavail);
  71895. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  71896. + np_tx_status.b.nptxfspcavail);
  71897. + p_tx_status.d32 =
  71898. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  71899. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  71900. + p_tx_status.b.ptxqspcavail);
  71901. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  71902. + dwc_otg_hcd_dump_frrem(hcd);
  71903. + dwc_otg_dump_global_registers(hcd->core_if);
  71904. + dwc_otg_dump_host_registers(hcd->core_if);
  71905. + DWC_PRINTF
  71906. + ("************************************************************\n");
  71907. + DWC_PRINTF("\n");
  71908. +#endif
  71909. +}
  71910. +
  71911. +#ifdef DEBUG
  71912. +void dwc_print_setup_data(uint8_t * setup)
  71913. +{
  71914. + int i;
  71915. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  71916. + DWC_PRINTF("Setup Data = MSB ");
  71917. + for (i = 7; i >= 0; i--)
  71918. + DWC_PRINTF("%02x ", setup[i]);
  71919. + DWC_PRINTF("\n");
  71920. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  71921. + (setup[0] & 0x80) ? "Device-to-Host" :
  71922. + "Host-to-Device");
  71923. + DWC_PRINTF(" bmRequestType Type = ");
  71924. + switch ((setup[0] & 0x60) >> 5) {
  71925. + case 0:
  71926. + DWC_PRINTF("Standard\n");
  71927. + break;
  71928. + case 1:
  71929. + DWC_PRINTF("Class\n");
  71930. + break;
  71931. + case 2:
  71932. + DWC_PRINTF("Vendor\n");
  71933. + break;
  71934. + case 3:
  71935. + DWC_PRINTF("Reserved\n");
  71936. + break;
  71937. + }
  71938. + DWC_PRINTF(" bmRequestType Recipient = ");
  71939. + switch (setup[0] & 0x1f) {
  71940. + case 0:
  71941. + DWC_PRINTF("Device\n");
  71942. + break;
  71943. + case 1:
  71944. + DWC_PRINTF("Interface\n");
  71945. + break;
  71946. + case 2:
  71947. + DWC_PRINTF("Endpoint\n");
  71948. + break;
  71949. + case 3:
  71950. + DWC_PRINTF("Other\n");
  71951. + break;
  71952. + default:
  71953. + DWC_PRINTF("Reserved\n");
  71954. + break;
  71955. + }
  71956. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  71957. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  71958. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  71959. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  71960. + }
  71961. +}
  71962. +#endif
  71963. +
  71964. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  71965. +{
  71966. +#if 0
  71967. + DWC_PRINTF("Frame remaining at SOF:\n");
  71968. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71969. + hcd->frrem_samples, hcd->frrem_accum,
  71970. + (hcd->frrem_samples > 0) ?
  71971. + hcd->frrem_accum / hcd->frrem_samples : 0);
  71972. +
  71973. + DWC_PRINTF("\n");
  71974. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  71975. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71976. + hcd->core_if->hfnum_7_samples,
  71977. + hcd->core_if->hfnum_7_frrem_accum,
  71978. + (hcd->core_if->hfnum_7_samples >
  71979. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  71980. + hcd->core_if->hfnum_7_samples : 0);
  71981. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  71982. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71983. + hcd->core_if->hfnum_0_samples,
  71984. + hcd->core_if->hfnum_0_frrem_accum,
  71985. + (hcd->core_if->hfnum_0_samples >
  71986. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  71987. + hcd->core_if->hfnum_0_samples : 0);
  71988. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  71989. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71990. + hcd->core_if->hfnum_other_samples,
  71991. + hcd->core_if->hfnum_other_frrem_accum,
  71992. + (hcd->core_if->hfnum_other_samples >
  71993. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  71994. + hcd->core_if->hfnum_other_samples : 0);
  71995. +
  71996. + DWC_PRINTF("\n");
  71997. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  71998. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71999. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  72000. + (hcd->hfnum_7_samples_a > 0) ?
  72001. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  72002. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  72003. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72004. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  72005. + (hcd->hfnum_0_samples_a > 0) ?
  72006. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  72007. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  72008. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72009. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  72010. + (hcd->hfnum_other_samples_a > 0) ?
  72011. + hcd->hfnum_other_frrem_accum_a /
  72012. + hcd->hfnum_other_samples_a : 0);
  72013. +
  72014. + DWC_PRINTF("\n");
  72015. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  72016. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72017. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  72018. + (hcd->hfnum_7_samples_b > 0) ?
  72019. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  72020. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  72021. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72022. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  72023. + (hcd->hfnum_0_samples_b > 0) ?
  72024. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  72025. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  72026. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72027. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  72028. + (hcd->hfnum_other_samples_b > 0) ?
  72029. + hcd->hfnum_other_frrem_accum_b /
  72030. + hcd->hfnum_other_samples_b : 0);
  72031. +#endif
  72032. +}
  72033. +
  72034. +#endif /* DWC_DEVICE_ONLY */
  72035. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  72036. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 1970-01-01 01:00:00.000000000 +0100
  72037. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 2014-03-11 17:51:27.000000000 +0100
  72038. @@ -0,0 +1,1132 @@
  72039. +/*==========================================================================
  72040. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  72041. + * $Revision: #10 $
  72042. + * $Date: 2011/10/20 $
  72043. + * $Change: 1869464 $
  72044. + *
  72045. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  72046. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  72047. + * otherwise expressly agreed to in writing between Synopsys and you.
  72048. + *
  72049. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  72050. + * any End User Software License Agreement or Agreement for Licensed Product
  72051. + * with Synopsys or any supplement thereto. You are permitted to use and
  72052. + * redistribute this Software in source and binary forms, with or without
  72053. + * modification, provided that redistributions of source code must retain this
  72054. + * notice. You may not view, use, disclose, copy or distribute this file or
  72055. + * any information contained herein except pursuant to this license grant from
  72056. + * Synopsys. If you do not agree with this notice, including the disclaimer
  72057. + * below, then you are not authorized to use the Software.
  72058. + *
  72059. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  72060. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72061. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  72062. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  72063. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72064. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72065. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72066. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72067. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72068. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  72069. + * DAMAGE.
  72070. + * ========================================================================== */
  72071. +#ifndef DWC_DEVICE_ONLY
  72072. +
  72073. +/** @file
  72074. + * This file contains Descriptor DMA support implementation for host mode.
  72075. + */
  72076. +
  72077. +#include "dwc_otg_hcd.h"
  72078. +#include "dwc_otg_regs.h"
  72079. +
  72080. +extern bool microframe_schedule;
  72081. +
  72082. +static inline uint8_t frame_list_idx(uint16_t frame)
  72083. +{
  72084. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  72085. +}
  72086. +
  72087. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  72088. +{
  72089. + return (idx + inc) &
  72090. + (((speed ==
  72091. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  72092. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  72093. +}
  72094. +
  72095. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  72096. +{
  72097. + return (idx - inc) &
  72098. + (((speed ==
  72099. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  72100. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  72101. +}
  72102. +
  72103. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  72104. +{
  72105. + return (((qh->ep_type == UE_ISOCHRONOUS)
  72106. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  72107. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  72108. +}
  72109. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  72110. +{
  72111. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  72112. + ? ((qh->interval + 8 - 1) / 8)
  72113. + : qh->interval);
  72114. +}
  72115. +
  72116. +static int desc_list_alloc(dwc_otg_qh_t * qh)
  72117. +{
  72118. + int retval = 0;
  72119. +
  72120. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  72121. + DWC_DMA_ALLOC(sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  72122. + &qh->desc_list_dma);
  72123. +
  72124. + if (!qh->desc_list) {
  72125. + retval = -DWC_E_NO_MEMORY;
  72126. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  72127. +
  72128. + }
  72129. +
  72130. + dwc_memset(qh->desc_list, 0x00,
  72131. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  72132. +
  72133. + qh->n_bytes =
  72134. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  72135. +
  72136. + if (!qh->n_bytes) {
  72137. + retval = -DWC_E_NO_MEMORY;
  72138. + DWC_ERROR
  72139. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  72140. + __func__);
  72141. +
  72142. + }
  72143. + return retval;
  72144. +
  72145. +}
  72146. +
  72147. +static void desc_list_free(dwc_otg_qh_t * qh)
  72148. +{
  72149. + if (qh->desc_list) {
  72150. + DWC_DMA_FREE(max_desc_num(qh), qh->desc_list,
  72151. + qh->desc_list_dma);
  72152. + qh->desc_list = NULL;
  72153. + }
  72154. +
  72155. + if (qh->n_bytes) {
  72156. + DWC_FREE(qh->n_bytes);
  72157. + qh->n_bytes = NULL;
  72158. + }
  72159. +}
  72160. +
  72161. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  72162. +{
  72163. + int retval = 0;
  72164. + if (hcd->frame_list)
  72165. + return 0;
  72166. +
  72167. + hcd->frame_list = DWC_DMA_ALLOC(4 * MAX_FRLIST_EN_NUM,
  72168. + &hcd->frame_list_dma);
  72169. + if (!hcd->frame_list) {
  72170. + retval = -DWC_E_NO_MEMORY;
  72171. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  72172. + }
  72173. +
  72174. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  72175. +
  72176. + return retval;
  72177. +}
  72178. +
  72179. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  72180. +{
  72181. + if (!hcd->frame_list)
  72182. + return;
  72183. +
  72184. + DWC_DMA_FREE(4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  72185. + hcd->frame_list = NULL;
  72186. +}
  72187. +
  72188. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  72189. +{
  72190. +
  72191. + hcfg_data_t hcfg;
  72192. +
  72193. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  72194. +
  72195. + if (hcfg.b.perschedena) {
  72196. + /* already enabled */
  72197. + return;
  72198. + }
  72199. +
  72200. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  72201. + hcd->frame_list_dma);
  72202. +
  72203. + switch (fr_list_en) {
  72204. + case 64:
  72205. + hcfg.b.frlisten = 3;
  72206. + break;
  72207. + case 32:
  72208. + hcfg.b.frlisten = 2;
  72209. + break;
  72210. + case 16:
  72211. + hcfg.b.frlisten = 1;
  72212. + break;
  72213. + case 8:
  72214. + hcfg.b.frlisten = 0;
  72215. + break;
  72216. + default:
  72217. + break;
  72218. + }
  72219. +
  72220. + hcfg.b.perschedena = 1;
  72221. +
  72222. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  72223. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  72224. +
  72225. +}
  72226. +
  72227. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  72228. +{
  72229. + hcfg_data_t hcfg;
  72230. +
  72231. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  72232. +
  72233. + if (!hcfg.b.perschedena) {
  72234. + /* already disabled */
  72235. + return;
  72236. + }
  72237. + hcfg.b.perschedena = 0;
  72238. +
  72239. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  72240. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  72241. +}
  72242. +
  72243. +/*
  72244. + * Activates/Deactivates FrameList entries for the channel
  72245. + * based on endpoint servicing period.
  72246. + */
  72247. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  72248. +{
  72249. + uint16_t i, j, inc;
  72250. + dwc_hc_t *hc = NULL;
  72251. +
  72252. + if (!qh->channel) {
  72253. + DWC_ERROR("qh->channel = %p", qh->channel);
  72254. + return;
  72255. + }
  72256. +
  72257. + if (!hcd) {
  72258. + DWC_ERROR("------hcd = %p", hcd);
  72259. + return;
  72260. + }
  72261. +
  72262. + if (!hcd->frame_list) {
  72263. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  72264. + return;
  72265. + }
  72266. +
  72267. + hc = qh->channel;
  72268. + inc = frame_incr_val(qh);
  72269. + if (qh->ep_type == UE_ISOCHRONOUS)
  72270. + i = frame_list_idx(qh->sched_frame);
  72271. + else
  72272. + i = 0;
  72273. +
  72274. + j = i;
  72275. + do {
  72276. + if (enable)
  72277. + hcd->frame_list[j] |= (1 << hc->hc_num);
  72278. + else
  72279. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  72280. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  72281. + }
  72282. + while (j != i);
  72283. + if (!enable)
  72284. + return;
  72285. + hc->schinfo = 0;
  72286. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  72287. + j = 1;
  72288. + /* TODO - check this */
  72289. + inc = (8 + qh->interval - 1) / qh->interval;
  72290. + for (i = 0; i < inc; i++) {
  72291. + hc->schinfo |= j;
  72292. + j = j << qh->interval;
  72293. + }
  72294. + } else {
  72295. + hc->schinfo = 0xff;
  72296. + }
  72297. +}
  72298. +
  72299. +#if 1
  72300. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  72301. +{
  72302. + int i = 0;
  72303. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  72304. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  72305. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  72306. + if (!(i % 8) && i)
  72307. + DWC_PRINTF("\n");
  72308. + }
  72309. + DWC_PRINTF("\n----\n");
  72310. +
  72311. +}
  72312. +#endif
  72313. +
  72314. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72315. +{
  72316. + dwc_irqflags_t flags;
  72317. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  72318. +
  72319. + dwc_hc_t *hc = qh->channel;
  72320. + if (dwc_qh_is_non_per(qh)) {
  72321. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  72322. + if (!microframe_schedule)
  72323. + hcd->non_periodic_channels--;
  72324. + else
  72325. + hcd->available_host_channels++;
  72326. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  72327. + } else
  72328. + update_frame_list(hcd, qh, 0);
  72329. +
  72330. + /*
  72331. + * The condition is added to prevent double cleanup try in case of device
  72332. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  72333. + */
  72334. + if (hc->qh) {
  72335. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  72336. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  72337. + hc->qh = NULL;
  72338. + }
  72339. +
  72340. + qh->channel = NULL;
  72341. + qh->ntd = 0;
  72342. +
  72343. + if (qh->desc_list) {
  72344. + dwc_memset(qh->desc_list, 0x00,
  72345. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  72346. + }
  72347. +}
  72348. +
  72349. +/**
  72350. + * Initializes a QH structure's Descriptor DMA related members.
  72351. + * Allocates memory for descriptor list.
  72352. + * On first periodic QH, allocates memory for FrameList
  72353. + * and enables periodic scheduling.
  72354. + *
  72355. + * @param hcd The HCD state structure for the DWC OTG controller.
  72356. + * @param qh The QH to init.
  72357. + *
  72358. + * @return 0 if successful, negative error code otherwise.
  72359. + */
  72360. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72361. +{
  72362. + int retval = 0;
  72363. +
  72364. + if (qh->do_split) {
  72365. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  72366. + return -1;
  72367. + }
  72368. +
  72369. + retval = desc_list_alloc(qh);
  72370. +
  72371. + if ((retval == 0)
  72372. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  72373. + if (!hcd->frame_list) {
  72374. + retval = frame_list_alloc(hcd);
  72375. + /* Enable periodic schedule on first periodic QH */
  72376. + if (retval == 0)
  72377. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  72378. + }
  72379. + }
  72380. +
  72381. + qh->ntd = 0;
  72382. +
  72383. + return retval;
  72384. +}
  72385. +
  72386. +/**
  72387. + * Frees descriptor list memory associated with the QH.
  72388. + * If QH is periodic and the last, frees FrameList memory
  72389. + * and disables periodic scheduling.
  72390. + *
  72391. + * @param hcd The HCD state structure for the DWC OTG controller.
  72392. + * @param qh The QH to init.
  72393. + */
  72394. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72395. +{
  72396. + desc_list_free(qh);
  72397. +
  72398. + /*
  72399. + * Channel still assigned due to some reasons.
  72400. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  72401. + * ChHalted interrupt to release the channel. Afterwards
  72402. + * when it comes here from endpoint disable routine
  72403. + * channel remains assigned.
  72404. + */
  72405. + if (qh->channel)
  72406. + release_channel_ddma(hcd, qh);
  72407. +
  72408. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  72409. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  72410. +
  72411. + per_sched_disable(hcd);
  72412. + frame_list_free(hcd);
  72413. + }
  72414. +}
  72415. +
  72416. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  72417. +{
  72418. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  72419. + /*
  72420. + * Descriptor set(8 descriptors) index
  72421. + * which is 8-aligned.
  72422. + */
  72423. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  72424. + } else {
  72425. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  72426. + }
  72427. +}
  72428. +
  72429. +/*
  72430. + * Determine starting frame for Isochronous transfer.
  72431. + * Few frames skipped to prevent race condition with HC.
  72432. + */
  72433. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  72434. + uint8_t * skip_frames)
  72435. +{
  72436. + uint16_t frame = 0;
  72437. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  72438. +
  72439. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  72440. +
  72441. + /*
  72442. + * skip_frames is used to limit activated descriptors number
  72443. + * to avoid the situation when HC services the last activated
  72444. + * descriptor firstly.
  72445. + * Example for FS:
  72446. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  72447. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  72448. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  72449. + * list will be fully programmed with Active descriptors and it is possible
  72450. + * case(rare) that the latest descriptor(considering rollback) corresponding
  72451. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  72452. + * up to 11 uframes(16 in the code) may be skipped.
  72453. + */
  72454. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  72455. + /*
  72456. + * Consider uframe counter also, to start xfer asap.
  72457. + * If half of the frame elapsed skip 2 frames otherwise
  72458. + * just 1 frame.
  72459. + * Starting descriptor index must be 8-aligned, so
  72460. + * if the current frame is near to complete the next one
  72461. + * is skipped as well.
  72462. + */
  72463. +
  72464. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  72465. + *skip_frames = 2 * 8;
  72466. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  72467. + } else {
  72468. + *skip_frames = 1 * 8;
  72469. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  72470. + }
  72471. +
  72472. + frame = dwc_full_frame_num(frame);
  72473. + } else {
  72474. + /*
  72475. + * Two frames are skipped for FS - the current and the next.
  72476. + * But for descriptor programming, 1 frame(descriptor) is enough,
  72477. + * see example above.
  72478. + */
  72479. + *skip_frames = 1;
  72480. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  72481. + }
  72482. +
  72483. + return frame;
  72484. +}
  72485. +
  72486. +/*
  72487. + * Calculate initial descriptor index for isochronous transfer
  72488. + * based on scheduled frame.
  72489. + */
  72490. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72491. +{
  72492. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  72493. + uint8_t skip_frames = 0;
  72494. + /*
  72495. + * With current ISOC processing algorithm the channel is being
  72496. + * released when no more QTDs in the list(qh->ntd == 0).
  72497. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  72498. + *
  72499. + * So qh->channel != NULL branch is not used and just not removed from the
  72500. + * source file. It is required for another possible approach which is,
  72501. + * do not disable and release the channel when ISOC session completed,
  72502. + * just move QH to inactive schedule until new QTD arrives.
  72503. + * On new QTD, the QH moved back to 'ready' schedule,
  72504. + * starting frame and therefore starting desc_index are recalculated.
  72505. + * In this case channel is released only on ep_disable.
  72506. + */
  72507. +
  72508. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  72509. + if (qh->channel) {
  72510. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  72511. + /*
  72512. + * Calculate initial descriptor index based on FrameList current bitmap
  72513. + * and servicing period.
  72514. + */
  72515. + fr_idx_tmp = frame_list_idx(frame);
  72516. + fr_idx =
  72517. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  72518. + fr_idx_tmp)
  72519. + % frame_incr_val(qh);
  72520. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  72521. + } else {
  72522. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  72523. + fr_idx = frame_list_idx(qh->sched_frame);
  72524. + }
  72525. +
  72526. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  72527. +
  72528. + return skip_frames;
  72529. +}
  72530. +
  72531. +#define ISOC_URB_GIVEBACK_ASAP
  72532. +
  72533. +#define MAX_ISOC_XFER_SIZE_FS 1023
  72534. +#define MAX_ISOC_XFER_SIZE_HS 3072
  72535. +#define DESCNUM_THRESHOLD 4
  72536. +
  72537. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  72538. + uint8_t skip_frames)
  72539. +{
  72540. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  72541. + dwc_otg_qtd_t *qtd;
  72542. + dwc_otg_host_dma_desc_t *dma_desc;
  72543. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  72544. +
  72545. + idx = qh->td_last;
  72546. + inc = qh->interval;
  72547. + n_desc = 0;
  72548. +
  72549. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  72550. + if (skip_frames && !qh->channel)
  72551. + ntd_max = ntd_max - skip_frames / qh->interval;
  72552. +
  72553. + max_xfer_size =
  72554. + (qh->dev_speed ==
  72555. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  72556. + MAX_ISOC_XFER_SIZE_FS;
  72557. +
  72558. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  72559. + while ((qh->ntd < ntd_max)
  72560. + && (qtd->isoc_frame_index_last <
  72561. + qtd->urb->packet_count)) {
  72562. +
  72563. + dma_desc = &qh->desc_list[idx];
  72564. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  72565. +
  72566. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  72567. +
  72568. + if (frame_desc->length > max_xfer_size)
  72569. + qh->n_bytes[idx] = max_xfer_size;
  72570. + else
  72571. + qh->n_bytes[idx] = frame_desc->length;
  72572. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  72573. + dma_desc->status.b_isoc.a = 1;
  72574. + dma_desc->status.b_isoc.sts = 0;
  72575. +
  72576. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  72577. +
  72578. + qh->ntd++;
  72579. +
  72580. + qtd->isoc_frame_index_last++;
  72581. +
  72582. +#ifdef ISOC_URB_GIVEBACK_ASAP
  72583. + /*
  72584. + * Set IOC for each descriptor corresponding to the
  72585. + * last frame of the URB.
  72586. + */
  72587. + if (qtd->isoc_frame_index_last ==
  72588. + qtd->urb->packet_count)
  72589. + dma_desc->status.b_isoc.ioc = 1;
  72590. +
  72591. +#endif
  72592. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  72593. + n_desc++;
  72594. +
  72595. + }
  72596. + qtd->in_process = 1;
  72597. + }
  72598. +
  72599. + qh->td_last = idx;
  72600. +
  72601. +#ifdef ISOC_URB_GIVEBACK_ASAP
  72602. + /* Set IOC for the last descriptor if descriptor list is full */
  72603. + if (qh->ntd == ntd_max) {
  72604. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  72605. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  72606. + }
  72607. +#else
  72608. + /*
  72609. + * Set IOC bit only for one descriptor.
  72610. + * Always try to be ahead of HW processing,
  72611. + * i.e. on IOC generation driver activates next descriptors but
  72612. + * core continues to process descriptors followed the one with IOC set.
  72613. + */
  72614. +
  72615. + if (n_desc > DESCNUM_THRESHOLD) {
  72616. + /*
  72617. + * Move IOC "up". Required even if there is only one QTD
  72618. + * in the list, cause QTDs migth continue to be queued,
  72619. + * but during the activation it was only one queued.
  72620. + * Actually more than one QTD might be in the list if this function called
  72621. + * from XferCompletion - QTDs was queued during HW processing of the previous
  72622. + * descriptor chunk.
  72623. + */
  72624. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  72625. + } else {
  72626. + /*
  72627. + * Set the IOC for the latest descriptor
  72628. + * if either number of descriptor is not greather than threshold
  72629. + * or no more new descriptors activated.
  72630. + */
  72631. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  72632. + }
  72633. +
  72634. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  72635. +#endif
  72636. +}
  72637. +
  72638. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72639. +{
  72640. +
  72641. + dwc_hc_t *hc;
  72642. + dwc_otg_host_dma_desc_t *dma_desc;
  72643. + dwc_otg_qtd_t *qtd;
  72644. + int num_packets, len, n_desc = 0;
  72645. +
  72646. + hc = qh->channel;
  72647. +
  72648. + /*
  72649. + * Start with hc->xfer_buff initialized in
  72650. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  72651. + * this pointer re-assigned to the buffer of the currently processed QTD.
  72652. + * For non-SG request there is always one QTD active.
  72653. + */
  72654. +
  72655. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  72656. +
  72657. + if (n_desc) {
  72658. + /* SG request - more than 1 QTDs */
  72659. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  72660. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  72661. + }
  72662. +
  72663. + qtd->n_desc = 0;
  72664. +
  72665. + do {
  72666. + dma_desc = &qh->desc_list[n_desc];
  72667. + len = hc->xfer_len;
  72668. +
  72669. + if (len > MAX_DMA_DESC_SIZE)
  72670. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  72671. +
  72672. + if (hc->ep_is_in) {
  72673. + if (len > 0) {
  72674. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  72675. + } else {
  72676. + /* Need 1 packet for transfer length of 0. */
  72677. + num_packets = 1;
  72678. + }
  72679. + /* Always program an integral # of max packets for IN transfers. */
  72680. + len = num_packets * hc->max_packet;
  72681. + }
  72682. +
  72683. + dma_desc->status.b.n_bytes = len;
  72684. +
  72685. + qh->n_bytes[n_desc] = len;
  72686. +
  72687. + if ((qh->ep_type == UE_CONTROL)
  72688. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  72689. + dma_desc->status.b.sup = 1; /* Setup Packet */
  72690. +
  72691. + dma_desc->status.b.a = 1; /* Active descriptor */
  72692. + dma_desc->status.b.sts = 0;
  72693. +
  72694. + dma_desc->buf =
  72695. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  72696. +
  72697. + /*
  72698. + * Last descriptor(or single) of IN transfer
  72699. + * with actual size less than MaxPacket.
  72700. + */
  72701. + if (len > hc->xfer_len) {
  72702. + hc->xfer_len = 0;
  72703. + } else {
  72704. + hc->xfer_buff += len;
  72705. + hc->xfer_len -= len;
  72706. + }
  72707. +
  72708. + qtd->n_desc++;
  72709. + n_desc++;
  72710. + }
  72711. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  72712. +
  72713. +
  72714. + qtd->in_process = 1;
  72715. +
  72716. + if (qh->ep_type == UE_CONTROL)
  72717. + break;
  72718. +
  72719. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  72720. + break;
  72721. + }
  72722. +
  72723. + if (n_desc) {
  72724. + /* Request Transfer Complete interrupt for the last descriptor */
  72725. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  72726. + /* End of List indicator */
  72727. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  72728. +
  72729. + hc->ntd = n_desc;
  72730. + }
  72731. +}
  72732. +
  72733. +/**
  72734. + * For Control and Bulk endpoints initializes descriptor list
  72735. + * and starts the transfer.
  72736. + *
  72737. + * For Interrupt and Isochronous endpoints initializes descriptor list
  72738. + * then updates FrameList, marking appropriate entries as active.
  72739. + * In case of Isochronous, the starting descriptor index is calculated based
  72740. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  72741. + * Then starts the transfer via enabling the channel.
  72742. + * For Isochronous endpoint the channel is not halted on XferComplete
  72743. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  72744. + *
  72745. + * @param hcd The HCD state structure for the DWC OTG controller.
  72746. + * @param qh The QH to init.
  72747. + *
  72748. + * @return 0 if successful, negative error code otherwise.
  72749. + */
  72750. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72751. +{
  72752. + /* Channel is already assigned */
  72753. + dwc_hc_t *hc = qh->channel;
  72754. + uint8_t skip_frames = 0;
  72755. +
  72756. + switch (hc->ep_type) {
  72757. + case DWC_OTG_EP_TYPE_CONTROL:
  72758. + case DWC_OTG_EP_TYPE_BULK:
  72759. + init_non_isoc_dma_desc(hcd, qh);
  72760. +
  72761. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72762. + break;
  72763. + case DWC_OTG_EP_TYPE_INTR:
  72764. + init_non_isoc_dma_desc(hcd, qh);
  72765. +
  72766. + update_frame_list(hcd, qh, 1);
  72767. +
  72768. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72769. + break;
  72770. + case DWC_OTG_EP_TYPE_ISOC:
  72771. +
  72772. + if (!qh->ntd)
  72773. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  72774. +
  72775. + init_isoc_dma_desc(hcd, qh, skip_frames);
  72776. +
  72777. + if (!hc->xfer_started) {
  72778. +
  72779. + update_frame_list(hcd, qh, 1);
  72780. +
  72781. + /*
  72782. + * Always set to max, instead of actual size.
  72783. + * Otherwise ntd will be changed with
  72784. + * channel being enabled. Not recommended.
  72785. + *
  72786. + */
  72787. + hc->ntd = max_desc_num(qh);
  72788. + /* Enable channel only once for ISOC */
  72789. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72790. + }
  72791. +
  72792. + break;
  72793. + default:
  72794. +
  72795. + break;
  72796. + }
  72797. +}
  72798. +
  72799. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  72800. + dwc_hc_t * hc,
  72801. + dwc_otg_hc_regs_t * hc_regs,
  72802. + dwc_otg_halt_status_e halt_status)
  72803. +{
  72804. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  72805. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  72806. + dwc_otg_qh_t *qh;
  72807. + dwc_otg_host_dma_desc_t *dma_desc;
  72808. + uint16_t idx, remain;
  72809. + uint8_t urb_compl;
  72810. +
  72811. + qh = hc->qh;
  72812. + idx = qh->td_first;
  72813. +
  72814. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  72815. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  72816. + qtd->in_process = 0;
  72817. + return;
  72818. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  72819. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  72820. + /*
  72821. + * Channel is halted in these error cases.
  72822. + * Considered as serious issues.
  72823. + * Complete all URBs marking all frames as failed,
  72824. + * irrespective whether some of the descriptors(frames) succeeded or no.
  72825. + * Pass error code to completion routine as well, to
  72826. + * update urb->status, some of class drivers might use it to stop
  72827. + * queing transfer requests.
  72828. + */
  72829. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  72830. + ? (-DWC_E_IO)
  72831. + : (-DWC_E_OVERFLOW);
  72832. +
  72833. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  72834. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  72835. + frame_desc = &qtd->urb->iso_descs[idx];
  72836. + frame_desc->status = err;
  72837. + }
  72838. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  72839. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  72840. + }
  72841. + return;
  72842. + }
  72843. +
  72844. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  72845. +
  72846. + if (!qtd->in_process)
  72847. + break;
  72848. +
  72849. + urb_compl = 0;
  72850. +
  72851. + do {
  72852. +
  72853. + dma_desc = &qh->desc_list[idx];
  72854. +
  72855. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  72856. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  72857. +
  72858. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  72859. + /*
  72860. + * XactError or, unable to complete all the transactions
  72861. + * in the scheduled micro-frame/frame,
  72862. + * both indicated by DMA_DESC_STS_PKTERR.
  72863. + */
  72864. + qtd->urb->error_count++;
  72865. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  72866. + frame_desc->status = -DWC_E_PROTOCOL;
  72867. + } else {
  72868. + /* Success */
  72869. +
  72870. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  72871. + frame_desc->status = 0;
  72872. + }
  72873. +
  72874. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  72875. + /*
  72876. + * urb->status is not used for isoc transfers here.
  72877. + * The individual frame_desc status are used instead.
  72878. + */
  72879. +
  72880. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  72881. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  72882. +
  72883. + /*
  72884. + * This check is necessary because urb_dequeue can be called
  72885. + * from urb complete callback(sound driver example).
  72886. + * All pending URBs are dequeued there, so no need for
  72887. + * further processing.
  72888. + */
  72889. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  72890. + return;
  72891. + }
  72892. +
  72893. + urb_compl = 1;
  72894. +
  72895. + }
  72896. +
  72897. + qh->ntd--;
  72898. +
  72899. + /* Stop if IOC requested descriptor reached */
  72900. + if (dma_desc->status.b_isoc.ioc) {
  72901. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  72902. + goto stop_scan;
  72903. + }
  72904. +
  72905. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  72906. +
  72907. + if (urb_compl)
  72908. + break;
  72909. + }
  72910. + while (idx != qh->td_first);
  72911. + }
  72912. +stop_scan:
  72913. + qh->td_first = idx;
  72914. +}
  72915. +
  72916. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  72917. + dwc_hc_t * hc,
  72918. + dwc_otg_qtd_t * qtd,
  72919. + dwc_otg_host_dma_desc_t * dma_desc,
  72920. + dwc_otg_halt_status_e halt_status,
  72921. + uint32_t n_bytes, uint8_t * xfer_done)
  72922. +{
  72923. +
  72924. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  72925. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  72926. +
  72927. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  72928. + urb->status = -DWC_E_IO;
  72929. + return 1;
  72930. + }
  72931. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  72932. + switch (halt_status) {
  72933. + case DWC_OTG_HC_XFER_STALL:
  72934. + urb->status = -DWC_E_PIPE;
  72935. + break;
  72936. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  72937. + urb->status = -DWC_E_OVERFLOW;
  72938. + break;
  72939. + case DWC_OTG_HC_XFER_XACT_ERR:
  72940. + urb->status = -DWC_E_PROTOCOL;
  72941. + break;
  72942. + default:
  72943. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  72944. + halt_status);
  72945. + break;
  72946. + }
  72947. + return 1;
  72948. + }
  72949. +
  72950. + if (dma_desc->status.b.a == 1) {
  72951. + DWC_DEBUGPL(DBG_HCDV,
  72952. + "Active descriptor encountered on channel %d\n",
  72953. + hc->hc_num);
  72954. + return 0;
  72955. + }
  72956. +
  72957. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  72958. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  72959. + urb->actual_length += n_bytes - remain;
  72960. + if (remain || urb->actual_length == urb->length) {
  72961. + /*
  72962. + * For Control Data stage do not set urb->status=0 to prevent
  72963. + * URB callback. Set it when Status phase done. See below.
  72964. + */
  72965. + *xfer_done = 1;
  72966. + }
  72967. +
  72968. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  72969. + urb->status = 0;
  72970. + *xfer_done = 1;
  72971. + }
  72972. + /* No handling for SETUP stage */
  72973. + } else {
  72974. + /* BULK and INTR */
  72975. + urb->actual_length += n_bytes - remain;
  72976. + if (remain || urb->actual_length == urb->length) {
  72977. + urb->status = 0;
  72978. + *xfer_done = 1;
  72979. + }
  72980. + }
  72981. +
  72982. + return 0;
  72983. +}
  72984. +
  72985. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  72986. + dwc_hc_t * hc,
  72987. + dwc_otg_hc_regs_t * hc_regs,
  72988. + dwc_otg_halt_status_e halt_status)
  72989. +{
  72990. + dwc_otg_hcd_urb_t *urb = NULL;
  72991. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  72992. + dwc_otg_qh_t *qh;
  72993. + dwc_otg_host_dma_desc_t *dma_desc;
  72994. + uint32_t n_bytes, n_desc, i;
  72995. + uint8_t failed = 0, xfer_done;
  72996. +
  72997. + n_desc = 0;
  72998. +
  72999. + qh = hc->qh;
  73000. +
  73001. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  73002. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  73003. + qtd->in_process = 0;
  73004. + }
  73005. + return;
  73006. + }
  73007. +
  73008. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  73009. +
  73010. + urb = qtd->urb;
  73011. +
  73012. + n_bytes = 0;
  73013. + xfer_done = 0;
  73014. +
  73015. + for (i = 0; i < qtd->n_desc; i++) {
  73016. + dma_desc = &qh->desc_list[n_desc];
  73017. +
  73018. + n_bytes = qh->n_bytes[n_desc];
  73019. +
  73020. + failed =
  73021. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  73022. + dma_desc,
  73023. + halt_status, n_bytes,
  73024. + &xfer_done);
  73025. +
  73026. + if (failed
  73027. + || (xfer_done
  73028. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  73029. +
  73030. + hcd->fops->complete(hcd, urb->priv, urb,
  73031. + urb->status);
  73032. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  73033. +
  73034. + if (failed)
  73035. + goto stop_scan;
  73036. + } else if (qh->ep_type == UE_CONTROL) {
  73037. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  73038. + if (urb->length > 0) {
  73039. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  73040. + } else {
  73041. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  73042. + }
  73043. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  73044. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  73045. + if (xfer_done) {
  73046. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  73047. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  73048. + } else if (i + 1 == qtd->n_desc) {
  73049. + /*
  73050. + * Last descriptor for Control data stage which is
  73051. + * not completed yet.
  73052. + */
  73053. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  73054. + }
  73055. + }
  73056. + }
  73057. +
  73058. + n_desc++;
  73059. + }
  73060. +
  73061. + }
  73062. +
  73063. +stop_scan:
  73064. +
  73065. + if (qh->ep_type != UE_CONTROL) {
  73066. + /*
  73067. + * Resetting the data toggle for bulk
  73068. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  73069. + */
  73070. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  73071. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  73072. + else
  73073. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  73074. + }
  73075. +
  73076. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  73077. + hcint_data_t hcint;
  73078. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  73079. + if (hcint.b.nyet) {
  73080. + /*
  73081. + * Got a NYET on the last transaction of the transfer. It
  73082. + * means that the endpoint should be in the PING state at the
  73083. + * beginning of the next transfer.
  73084. + */
  73085. + qh->ping_state = 1;
  73086. + clear_hc_int(hc_regs, nyet);
  73087. + }
  73088. +
  73089. + }
  73090. +
  73091. +}
  73092. +
  73093. +/**
  73094. + * This function is called from interrupt handlers.
  73095. + * Scans the descriptor list, updates URB's status and
  73096. + * calls completion routine for the URB if it's done.
  73097. + * Releases the channel to be used by other transfers.
  73098. + * In case of Isochronous endpoint the channel is not halted until
  73099. + * the end of the session, i.e. QTD list is empty.
  73100. + * If periodic channel released the FrameList is updated accordingly.
  73101. + *
  73102. + * Calls transaction selection routines to activate pending transfers.
  73103. + *
  73104. + * @param hcd The HCD state structure for the DWC OTG controller.
  73105. + * @param hc Host channel, the transfer is completed on.
  73106. + * @param hc_regs Host channel registers.
  73107. + * @param halt_status Reason the channel is being halted,
  73108. + * or just XferComplete for isochronous transfer
  73109. + */
  73110. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  73111. + dwc_hc_t * hc,
  73112. + dwc_otg_hc_regs_t * hc_regs,
  73113. + dwc_otg_halt_status_e halt_status)
  73114. +{
  73115. + uint8_t continue_isoc_xfer = 0;
  73116. + dwc_otg_transaction_type_e tr_type;
  73117. + dwc_otg_qh_t *qh = hc->qh;
  73118. +
  73119. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  73120. +
  73121. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  73122. +
  73123. + /* Release the channel if halted or session completed */
  73124. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  73125. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  73126. +
  73127. + /* Halt the channel if session completed */
  73128. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  73129. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  73130. + }
  73131. +
  73132. + release_channel_ddma(hcd, qh);
  73133. + dwc_otg_hcd_qh_remove(hcd, qh);
  73134. + } else {
  73135. + /* Keep in assigned schedule to continue transfer */
  73136. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  73137. + &qh->qh_list_entry);
  73138. + continue_isoc_xfer = 1;
  73139. +
  73140. + }
  73141. + /** @todo Consider the case when period exceeds FrameList size.
  73142. + * Frame Rollover interrupt should be used.
  73143. + */
  73144. + } else {
  73145. + /* Scan descriptor list to complete the URB(s), then release the channel */
  73146. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  73147. +
  73148. + release_channel_ddma(hcd, qh);
  73149. + dwc_otg_hcd_qh_remove(hcd, qh);
  73150. +
  73151. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  73152. + /* Add back to inactive non-periodic schedule on normal completion */
  73153. + dwc_otg_hcd_qh_add(hcd, qh);
  73154. + }
  73155. +
  73156. + }
  73157. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  73158. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  73159. + if (continue_isoc_xfer) {
  73160. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  73161. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  73162. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  73163. + tr_type = DWC_OTG_TRANSACTION_ALL;
  73164. + }
  73165. + }
  73166. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  73167. + }
  73168. +}
  73169. +
  73170. +#endif /* DWC_DEVICE_ONLY */
  73171. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  73172. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 1970-01-01 01:00:00.000000000 +0100
  73173. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 2014-03-11 17:51:27.000000000 +0100
  73174. @@ -0,0 +1,851 @@
  73175. +/* ==========================================================================
  73176. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  73177. + * $Revision: #58 $
  73178. + * $Date: 2011/09/15 $
  73179. + * $Change: 1846647 $
  73180. + *
  73181. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  73182. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  73183. + * otherwise expressly agreed to in writing between Synopsys and you.
  73184. + *
  73185. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  73186. + * any End User Software License Agreement or Agreement for Licensed Product
  73187. + * with Synopsys or any supplement thereto. You are permitted to use and
  73188. + * redistribute this Software in source and binary forms, with or without
  73189. + * modification, provided that redistributions of source code must retain this
  73190. + * notice. You may not view, use, disclose, copy or distribute this file or
  73191. + * any information contained herein except pursuant to this license grant from
  73192. + * Synopsys. If you do not agree with this notice, including the disclaimer
  73193. + * below, then you are not authorized to use the Software.
  73194. + *
  73195. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  73196. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73197. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  73198. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  73199. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73200. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73201. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73202. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73203. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73204. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  73205. + * DAMAGE.
  73206. + * ========================================================================== */
  73207. +#ifndef DWC_DEVICE_ONLY
  73208. +#ifndef __DWC_HCD_H__
  73209. +#define __DWC_HCD_H__
  73210. +
  73211. +#include "dwc_otg_os_dep.h"
  73212. +#include "usb.h"
  73213. +#include "dwc_otg_hcd_if.h"
  73214. +#include "dwc_otg_core_if.h"
  73215. +#include "dwc_list.h"
  73216. +#include "dwc_otg_cil.h"
  73217. +
  73218. +/**
  73219. + * @file
  73220. + *
  73221. + * This file contains the structures, constants, and interfaces for
  73222. + * the Host Contoller Driver (HCD).
  73223. + *
  73224. + * The Host Controller Driver (HCD) is responsible for translating requests
  73225. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  73226. + * It isolates the USBD from the specifics of the controller by providing an
  73227. + * API to the USBD.
  73228. + */
  73229. +
  73230. +struct dwc_otg_hcd_pipe_info {
  73231. + uint8_t dev_addr;
  73232. + uint8_t ep_num;
  73233. + uint8_t pipe_type;
  73234. + uint8_t pipe_dir;
  73235. + uint16_t mps;
  73236. +};
  73237. +
  73238. +struct dwc_otg_hcd_iso_packet_desc {
  73239. + uint32_t offset;
  73240. + uint32_t length;
  73241. + uint32_t actual_length;
  73242. + uint32_t status;
  73243. +};
  73244. +
  73245. +struct dwc_otg_qtd;
  73246. +
  73247. +struct dwc_otg_hcd_urb {
  73248. + void *priv;
  73249. + struct dwc_otg_qtd *qtd;
  73250. + void *buf;
  73251. + dwc_dma_t dma;
  73252. + void *setup_packet;
  73253. + dwc_dma_t setup_dma;
  73254. + uint32_t length;
  73255. + uint32_t actual_length;
  73256. + uint32_t status;
  73257. + uint32_t error_count;
  73258. + uint32_t packet_count;
  73259. + uint32_t flags;
  73260. + uint16_t interval;
  73261. + struct dwc_otg_hcd_pipe_info pipe_info;
  73262. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  73263. +};
  73264. +
  73265. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  73266. +{
  73267. + return pipe->ep_num;
  73268. +}
  73269. +
  73270. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  73271. + *pipe)
  73272. +{
  73273. + return pipe->pipe_type;
  73274. +}
  73275. +
  73276. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  73277. +{
  73278. + return pipe->mps;
  73279. +}
  73280. +
  73281. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  73282. + *pipe)
  73283. +{
  73284. + return pipe->dev_addr;
  73285. +}
  73286. +
  73287. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  73288. + *pipe)
  73289. +{
  73290. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  73291. +}
  73292. +
  73293. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  73294. + *pipe)
  73295. +{
  73296. + return (pipe->pipe_type == UE_INTERRUPT);
  73297. +}
  73298. +
  73299. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  73300. + *pipe)
  73301. +{
  73302. + return (pipe->pipe_type == UE_BULK);
  73303. +}
  73304. +
  73305. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  73306. + *pipe)
  73307. +{
  73308. + return (pipe->pipe_type == UE_CONTROL);
  73309. +}
  73310. +
  73311. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  73312. +{
  73313. + return (pipe->pipe_dir == UE_DIR_IN);
  73314. +}
  73315. +
  73316. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  73317. + *pipe)
  73318. +{
  73319. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  73320. +}
  73321. +
  73322. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  73323. + uint8_t devaddr, uint8_t ep_num,
  73324. + uint8_t pipe_type, uint8_t pipe_dir,
  73325. + uint16_t mps)
  73326. +{
  73327. + pipe->dev_addr = devaddr;
  73328. + pipe->ep_num = ep_num;
  73329. + pipe->pipe_type = pipe_type;
  73330. + pipe->pipe_dir = pipe_dir;
  73331. + pipe->mps = mps;
  73332. +}
  73333. +
  73334. +/**
  73335. + * Phases for control transfers.
  73336. + */
  73337. +typedef enum dwc_otg_control_phase {
  73338. + DWC_OTG_CONTROL_SETUP,
  73339. + DWC_OTG_CONTROL_DATA,
  73340. + DWC_OTG_CONTROL_STATUS
  73341. +} dwc_otg_control_phase_e;
  73342. +
  73343. +/** Transaction types. */
  73344. +typedef enum dwc_otg_transaction_type {
  73345. + DWC_OTG_TRANSACTION_NONE = 0,
  73346. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  73347. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  73348. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  73349. +} dwc_otg_transaction_type_e;
  73350. +
  73351. +struct dwc_otg_qh;
  73352. +
  73353. +/**
  73354. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  73355. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  73356. + * (of one of these types) submitted to the HCD. The transfer associated with
  73357. + * a QTD may require one or multiple transactions.
  73358. + *
  73359. + * A QTD is linked to a Queue Head, which is entered in either the
  73360. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  73361. + * execution, some or all of its transactions may be executed. After
  73362. + * execution, the state of the QTD is updated. The QTD may be retired if all
  73363. + * its transactions are complete or if an error occurred. Otherwise, it
  73364. + * remains in the schedule so more transactions can be executed later.
  73365. + */
  73366. +typedef struct dwc_otg_qtd {
  73367. + /**
  73368. + * Determines the PID of the next data packet for the data phase of
  73369. + * control transfers. Ignored for other transfer types.<br>
  73370. + * One of the following values:
  73371. + * - DWC_OTG_HC_PID_DATA0
  73372. + * - DWC_OTG_HC_PID_DATA1
  73373. + */
  73374. + uint8_t data_toggle;
  73375. +
  73376. + /** Current phase for control transfers (Setup, Data, or Status). */
  73377. + dwc_otg_control_phase_e control_phase;
  73378. +
  73379. + /** Keep track of the current split type
  73380. + * for FS/LS endpoints on a HS Hub */
  73381. + uint8_t complete_split;
  73382. +
  73383. + /** How many bytes transferred during SSPLIT OUT */
  73384. + uint32_t ssplit_out_xfer_count;
  73385. +
  73386. + /**
  73387. + * Holds the number of bus errors that have occurred for a transaction
  73388. + * within this transfer.
  73389. + */
  73390. + uint8_t error_count;
  73391. +
  73392. + /**
  73393. + * Index of the next frame descriptor for an isochronous transfer. A
  73394. + * frame descriptor describes the buffer position and length of the
  73395. + * data to be transferred in the next scheduled (micro)frame of an
  73396. + * isochronous transfer. It also holds status for that transaction.
  73397. + * The frame index starts at 0.
  73398. + */
  73399. + uint16_t isoc_frame_index;
  73400. +
  73401. + /** Position of the ISOC split on full/low speed */
  73402. + uint8_t isoc_split_pos;
  73403. +
  73404. + /** Position of the ISOC split in the buffer for the current frame */
  73405. + uint16_t isoc_split_offset;
  73406. +
  73407. + /** URB for this transfer */
  73408. + struct dwc_otg_hcd_urb *urb;
  73409. +
  73410. + struct dwc_otg_qh *qh;
  73411. +
  73412. + /** This list of QTDs */
  73413. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  73414. +
  73415. + /** Indicates if this QTD is currently processed by HW. */
  73416. + uint8_t in_process;
  73417. +
  73418. + /** Number of DMA descriptors for this QTD */
  73419. + uint8_t n_desc;
  73420. +
  73421. + /**
  73422. + * Last activated frame(packet) index.
  73423. + * Used in Descriptor DMA mode only.
  73424. + */
  73425. + uint16_t isoc_frame_index_last;
  73426. +
  73427. +} dwc_otg_qtd_t;
  73428. +
  73429. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  73430. +
  73431. +/**
  73432. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  73433. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  73434. + * be entered in either the non-periodic or periodic schedule.
  73435. + */
  73436. +typedef struct dwc_otg_qh {
  73437. + /**
  73438. + * Endpoint type.
  73439. + * One of the following values:
  73440. + * - UE_CONTROL
  73441. + * - UE_BULK
  73442. + * - UE_INTERRUPT
  73443. + * - UE_ISOCHRONOUS
  73444. + */
  73445. + uint8_t ep_type;
  73446. + uint8_t ep_is_in;
  73447. +
  73448. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  73449. + uint16_t maxp;
  73450. +
  73451. + /**
  73452. + * Device speed.
  73453. + * One of the following values:
  73454. + * - DWC_OTG_EP_SPEED_LOW
  73455. + * - DWC_OTG_EP_SPEED_FULL
  73456. + * - DWC_OTG_EP_SPEED_HIGH
  73457. + */
  73458. + uint8_t dev_speed;
  73459. +
  73460. + /**
  73461. + * Determines the PID of the next data packet for non-control
  73462. + * transfers. Ignored for control transfers.<br>
  73463. + * One of the following values:
  73464. + * - DWC_OTG_HC_PID_DATA0
  73465. + * - DWC_OTG_HC_PID_DATA1
  73466. + */
  73467. + uint8_t data_toggle;
  73468. +
  73469. + /** Ping state if 1. */
  73470. + uint8_t ping_state;
  73471. +
  73472. + /**
  73473. + * List of QTDs for this QH.
  73474. + */
  73475. + struct dwc_otg_qtd_list qtd_list;
  73476. +
  73477. + /** Host channel currently processing transfers for this QH. */
  73478. + struct dwc_hc *channel;
  73479. +
  73480. + /** Full/low speed endpoint on high-speed hub requires split. */
  73481. + uint8_t do_split;
  73482. +
  73483. + /** @name Periodic schedule information */
  73484. + /** @{ */
  73485. +
  73486. + /** Bandwidth in microseconds per (micro)frame. */
  73487. + uint16_t usecs;
  73488. +
  73489. + /** Interval between transfers in (micro)frames. */
  73490. + uint16_t interval;
  73491. +
  73492. + /**
  73493. + * (micro)frame to initialize a periodic transfer. The transfer
  73494. + * executes in the following (micro)frame.
  73495. + */
  73496. + uint16_t sched_frame;
  73497. +
  73498. + /*
  73499. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  73500. + */
  73501. + uint16_t nak_frame;
  73502. +
  73503. + /** (micro)frame at which last start split was initialized. */
  73504. + uint16_t start_split_frame;
  73505. +
  73506. + /** @} */
  73507. +
  73508. + /**
  73509. + * Used instead of original buffer if
  73510. + * it(physical address) is not dword-aligned.
  73511. + */
  73512. + uint8_t *dw_align_buf;
  73513. + dwc_dma_t dw_align_buf_dma;
  73514. +
  73515. + /** Entry for QH in either the periodic or non-periodic schedule. */
  73516. + dwc_list_link_t qh_list_entry;
  73517. +
  73518. + /** @name Descriptor DMA support */
  73519. + /** @{ */
  73520. +
  73521. + /** Descriptor List. */
  73522. + dwc_otg_host_dma_desc_t *desc_list;
  73523. +
  73524. + /** Descriptor List physical address. */
  73525. + dwc_dma_t desc_list_dma;
  73526. +
  73527. + /**
  73528. + * Xfer Bytes array.
  73529. + * Each element corresponds to a descriptor and indicates
  73530. + * original XferSize size value for the descriptor.
  73531. + */
  73532. + uint32_t *n_bytes;
  73533. +
  73534. + /** Actual number of transfer descriptors in a list. */
  73535. + uint16_t ntd;
  73536. +
  73537. + /** First activated isochronous transfer descriptor index. */
  73538. + uint8_t td_first;
  73539. + /** Last activated isochronous transfer descriptor index. */
  73540. + uint8_t td_last;
  73541. +
  73542. + /** @} */
  73543. +
  73544. +
  73545. + uint16_t speed;
  73546. + uint16_t frame_usecs[8];
  73547. +
  73548. + uint32_t skip_count;
  73549. +} dwc_otg_qh_t;
  73550. +
  73551. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  73552. +
  73553. +typedef struct urb_tq_entry {
  73554. + struct urb *urb;
  73555. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  73556. +} urb_tq_entry_t;
  73557. +
  73558. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  73559. +
  73560. +/**
  73561. + * This structure holds the state of the HCD, including the non-periodic and
  73562. + * periodic schedules.
  73563. + */
  73564. +struct dwc_otg_hcd {
  73565. + /** The DWC otg device pointer */
  73566. + struct dwc_otg_device *otg_dev;
  73567. + /** DWC OTG Core Interface Layer */
  73568. + dwc_otg_core_if_t *core_if;
  73569. +
  73570. + /** Function HCD driver callbacks */
  73571. + struct dwc_otg_hcd_function_ops *fops;
  73572. +
  73573. + /** Internal DWC HCD Flags */
  73574. + volatile union dwc_otg_hcd_internal_flags {
  73575. + uint32_t d32;
  73576. + struct {
  73577. + unsigned port_connect_status_change:1;
  73578. + unsigned port_connect_status:1;
  73579. + unsigned port_reset_change:1;
  73580. + unsigned port_enable_change:1;
  73581. + unsigned port_suspend_change:1;
  73582. + unsigned port_over_current_change:1;
  73583. + unsigned port_l1_change:1;
  73584. + unsigned reserved:26;
  73585. + } b;
  73586. + } flags;
  73587. +
  73588. + /**
  73589. + * Inactive items in the non-periodic schedule. This is a list of
  73590. + * Queue Heads. Transfers associated with these Queue Heads are not
  73591. + * currently assigned to a host channel.
  73592. + */
  73593. + dwc_list_link_t non_periodic_sched_inactive;
  73594. +
  73595. + /**
  73596. + * Active items in the non-periodic schedule. This is a list of
  73597. + * Queue Heads. Transfers associated with these Queue Heads are
  73598. + * currently assigned to a host channel.
  73599. + */
  73600. + dwc_list_link_t non_periodic_sched_active;
  73601. +
  73602. + /**
  73603. + * Pointer to the next Queue Head to process in the active
  73604. + * non-periodic schedule.
  73605. + */
  73606. + dwc_list_link_t *non_periodic_qh_ptr;
  73607. +
  73608. + /**
  73609. + * Inactive items in the periodic schedule. This is a list of QHs for
  73610. + * periodic transfers that are _not_ scheduled for the next frame.
  73611. + * Each QH in the list has an interval counter that determines when it
  73612. + * needs to be scheduled for execution. This scheduling mechanism
  73613. + * allows only a simple calculation for periodic bandwidth used (i.e.
  73614. + * must assume that all periodic transfers may need to execute in the
  73615. + * same frame). However, it greatly simplifies scheduling and should
  73616. + * be sufficient for the vast majority of OTG hosts, which need to
  73617. + * connect to a small number of peripherals at one time.
  73618. + *
  73619. + * Items move from this list to periodic_sched_ready when the QH
  73620. + * interval counter is 0 at SOF.
  73621. + */
  73622. + dwc_list_link_t periodic_sched_inactive;
  73623. +
  73624. + /**
  73625. + * List of periodic QHs that are ready for execution in the next
  73626. + * frame, but have not yet been assigned to host channels.
  73627. + *
  73628. + * Items move from this list to periodic_sched_assigned as host
  73629. + * channels become available during the current frame.
  73630. + */
  73631. + dwc_list_link_t periodic_sched_ready;
  73632. +
  73633. + /**
  73634. + * List of periodic QHs to be executed in the next frame that are
  73635. + * assigned to host channels.
  73636. + *
  73637. + * Items move from this list to periodic_sched_queued as the
  73638. + * transactions for the QH are queued to the DWC_otg controller.
  73639. + */
  73640. + dwc_list_link_t periodic_sched_assigned;
  73641. +
  73642. + /**
  73643. + * List of periodic QHs that have been queued for execution.
  73644. + *
  73645. + * Items move from this list to either periodic_sched_inactive or
  73646. + * periodic_sched_ready when the channel associated with the transfer
  73647. + * is released. If the interval for the QH is 1, the item moves to
  73648. + * periodic_sched_ready because it must be rescheduled for the next
  73649. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  73650. + */
  73651. + dwc_list_link_t periodic_sched_queued;
  73652. +
  73653. + /**
  73654. + * Total bandwidth claimed so far for periodic transfers. This value
  73655. + * is in microseconds per (micro)frame. The assumption is that all
  73656. + * periodic transfers may occur in the same (micro)frame.
  73657. + */
  73658. + uint16_t periodic_usecs;
  73659. +
  73660. + /**
  73661. + * Total bandwidth claimed so far for all periodic transfers
  73662. + * in a frame.
  73663. + * This will include a mixture of HS and FS transfers.
  73664. + * Units are microseconds per (micro)frame.
  73665. + * We have a budget per frame and have to schedule
  73666. + * transactions accordingly.
  73667. + * Watch out for the fact that things are actually scheduled for the
  73668. + * "next frame".
  73669. + */
  73670. + uint16_t frame_usecs[8];
  73671. +
  73672. +
  73673. + /**
  73674. + * Frame number read from the core at SOF. The value ranges from 0 to
  73675. + * DWC_HFNUM_MAX_FRNUM.
  73676. + */
  73677. + uint16_t frame_number;
  73678. +
  73679. + /**
  73680. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  73681. + */
  73682. + uint16_t periodic_qh_count;
  73683. +
  73684. + /**
  73685. + * Free host channels in the controller. This is a list of
  73686. + * dwc_hc_t items.
  73687. + */
  73688. + struct hc_list free_hc_list;
  73689. + /**
  73690. + * Number of host channels assigned to periodic transfers. Currently
  73691. + * assuming that there is a dedicated host channel for each periodic
  73692. + * transaction and at least one host channel available for
  73693. + * non-periodic transactions.
  73694. + */
  73695. + int periodic_channels; /* microframe_schedule==0 */
  73696. +
  73697. + /**
  73698. + * Number of host channels assigned to non-periodic transfers.
  73699. + */
  73700. + int non_periodic_channels; /* microframe_schedule==0 */
  73701. +
  73702. + /**
  73703. + * Number of host channels assigned to non-periodic transfers.
  73704. + */
  73705. + int available_host_channels;
  73706. +
  73707. + /**
  73708. + * Array of pointers to the host channel descriptors. Allows accessing
  73709. + * a host channel descriptor given the host channel number. This is
  73710. + * useful in interrupt handlers.
  73711. + */
  73712. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  73713. +
  73714. + /**
  73715. + * Buffer to use for any data received during the status phase of a
  73716. + * control transfer. Normally no data is transferred during the status
  73717. + * phase. This buffer is used as a bit bucket.
  73718. + */
  73719. + uint8_t *status_buf;
  73720. +
  73721. + /**
  73722. + * DMA address for status_buf.
  73723. + */
  73724. + dma_addr_t status_buf_dma;
  73725. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  73726. +
  73727. + /**
  73728. + * Connection timer. An OTG host must display a message if the device
  73729. + * does not connect. Started when the VBus power is turned on via
  73730. + * sysfs attribute "buspower".
  73731. + */
  73732. + dwc_timer_t *conn_timer;
  73733. +
  73734. + /* Tasket to do a reset */
  73735. + dwc_tasklet_t *reset_tasklet;
  73736. +
  73737. + dwc_tasklet_t *completion_tasklet;
  73738. + struct urb_list completed_urb_list;
  73739. +
  73740. + /* */
  73741. + dwc_spinlock_t *lock;
  73742. + dwc_spinlock_t *channel_lock;
  73743. + /**
  73744. + * Private data that could be used by OS wrapper.
  73745. + */
  73746. + void *priv;
  73747. +
  73748. + uint8_t otg_port;
  73749. +
  73750. + /** Frame List */
  73751. + uint32_t *frame_list;
  73752. +
  73753. + /** Hub - Port assignment */
  73754. + int hub_port[128];
  73755. +#ifdef FIQ_DEBUG
  73756. + int hub_port_alloc[2048];
  73757. +#endif
  73758. +
  73759. + /** Frame List DMA address */
  73760. + dma_addr_t frame_list_dma;
  73761. +
  73762. +#ifdef DEBUG
  73763. + uint32_t frrem_samples;
  73764. + uint64_t frrem_accum;
  73765. +
  73766. + uint32_t hfnum_7_samples_a;
  73767. + uint64_t hfnum_7_frrem_accum_a;
  73768. + uint32_t hfnum_0_samples_a;
  73769. + uint64_t hfnum_0_frrem_accum_a;
  73770. + uint32_t hfnum_other_samples_a;
  73771. + uint64_t hfnum_other_frrem_accum_a;
  73772. +
  73773. + uint32_t hfnum_7_samples_b;
  73774. + uint64_t hfnum_7_frrem_accum_b;
  73775. + uint32_t hfnum_0_samples_b;
  73776. + uint64_t hfnum_0_frrem_accum_b;
  73777. + uint32_t hfnum_other_samples_b;
  73778. + uint64_t hfnum_other_frrem_accum_b;
  73779. +#endif
  73780. +};
  73781. +
  73782. +/** @name Transaction Execution Functions */
  73783. +/** @{ */
  73784. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  73785. + * hcd);
  73786. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  73787. + dwc_otg_transaction_type_e tr_type);
  73788. +
  73789. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  73790. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  73791. +
  73792. +
  73793. +/** @} */
  73794. +
  73795. +/** @name Interrupt Handler Functions */
  73796. +/** @{ */
  73797. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73798. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73799. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  73800. + dwc_otg_hcd);
  73801. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  73802. + dwc_otg_hcd);
  73803. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  73804. + dwc_otg_hcd);
  73805. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  73806. + dwc_otg_hcd);
  73807. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73808. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  73809. + dwc_otg_hcd);
  73810. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73811. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73812. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  73813. + uint32_t num);
  73814. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73815. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  73816. + dwc_otg_hcd);
  73817. +/** @} */
  73818. +
  73819. +/** @name Schedule Queue Functions */
  73820. +/** @{ */
  73821. +
  73822. +/* Implemented in dwc_otg_hcd_queue.c */
  73823. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  73824. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  73825. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73826. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73827. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73828. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  73829. + int sched_csplit);
  73830. +
  73831. +/** Remove and free a QH */
  73832. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  73833. + dwc_otg_qh_t * qh)
  73834. +{
  73835. + dwc_irqflags_t flags;
  73836. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  73837. + dwc_otg_hcd_qh_remove(hcd, qh);
  73838. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  73839. + dwc_otg_hcd_qh_free(hcd, qh);
  73840. +}
  73841. +
  73842. +/** Allocates memory for a QH structure.
  73843. + * @return Returns the memory allocate or NULL on error. */
  73844. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  73845. +{
  73846. + if (atomic_alloc)
  73847. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  73848. + else
  73849. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  73850. +}
  73851. +
  73852. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  73853. + int atomic_alloc);
  73854. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  73855. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  73856. + dwc_otg_qh_t ** qh, int atomic_alloc);
  73857. +
  73858. +/** Allocates memory for a QTD structure.
  73859. + * @return Returns the memory allocate or NULL on error. */
  73860. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  73861. +{
  73862. + if (atomic_alloc)
  73863. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  73864. + else
  73865. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  73866. +}
  73867. +
  73868. +/** Frees the memory for a QTD structure. QTD should already be removed from
  73869. + * list.
  73870. + * @param qtd QTD to free.*/
  73871. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  73872. +{
  73873. + DWC_FREE(qtd);
  73874. +}
  73875. +
  73876. +/** Removes a QTD from list.
  73877. + * @param hcd HCD instance.
  73878. + * @param qtd QTD to remove from list.
  73879. + * @param qh QTD belongs to.
  73880. + */
  73881. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  73882. + dwc_otg_qtd_t * qtd,
  73883. + dwc_otg_qh_t * qh)
  73884. +{
  73885. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  73886. +}
  73887. +
  73888. +/** Remove and free a QTD
  73889. + * Need to disable IRQ and hold hcd lock while calling this function out of
  73890. + * interrupt servicing chain */
  73891. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  73892. + dwc_otg_qtd_t * qtd,
  73893. + dwc_otg_qh_t * qh)
  73894. +{
  73895. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  73896. + dwc_otg_hcd_qtd_free(qtd);
  73897. +}
  73898. +
  73899. +/** @} */
  73900. +
  73901. +/** @name Descriptor DMA Supporting Functions */
  73902. +/** @{ */
  73903. +
  73904. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73905. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  73906. + dwc_hc_t * hc,
  73907. + dwc_otg_hc_regs_t * hc_regs,
  73908. + dwc_otg_halt_status_e halt_status);
  73909. +
  73910. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73911. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73912. +
  73913. +/** @} */
  73914. +
  73915. +/** @name Internal Functions */
  73916. +/** @{ */
  73917. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  73918. +/** @} */
  73919. +
  73920. +#ifdef CONFIG_USB_DWC_OTG_LPM
  73921. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  73922. + uint8_t devaddr);
  73923. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  73924. +#endif
  73925. +
  73926. +/** Gets the QH that contains the list_head */
  73927. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  73928. +
  73929. +/** Gets the QTD that contains the list_head */
  73930. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  73931. +
  73932. +/** Check if QH is non-periodic */
  73933. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  73934. + (_qh_ptr_->ep_type == UE_CONTROL))
  73935. +
  73936. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  73937. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  73938. +
  73939. +/** Packet size for any kind of endpoint descriptor */
  73940. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  73941. +
  73942. +/**
  73943. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  73944. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  73945. + * frame number when the max frame number is reached.
  73946. + */
  73947. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  73948. +{
  73949. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  73950. + (DWC_HFNUM_MAX_FRNUM >> 1);
  73951. +}
  73952. +
  73953. +/**
  73954. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  73955. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  73956. + * number when the max frame number is reached.
  73957. + */
  73958. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  73959. +{
  73960. + return (frame1 != frame2) &&
  73961. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  73962. + (DWC_HFNUM_MAX_FRNUM >> 1));
  73963. +}
  73964. +
  73965. +/**
  73966. + * Increments _frame by the amount specified by _inc. The addition is done
  73967. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  73968. + */
  73969. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  73970. +{
  73971. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  73972. +}
  73973. +
  73974. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  73975. +{
  73976. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  73977. +}
  73978. +
  73979. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  73980. +{
  73981. + return frame & 0x7;
  73982. +}
  73983. +
  73984. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  73985. + dwc_otg_hc_regs_t * hc_regs,
  73986. + dwc_otg_qtd_t * qtd);
  73987. +
  73988. +#ifdef DEBUG
  73989. +/**
  73990. + * Macro to sample the remaining PHY clocks left in the current frame. This
  73991. + * may be used during debugging to determine the average time it takes to
  73992. + * execute sections of code. There are two possible sample points, "a" and
  73993. + * "b", so the _letter argument must be one of these values.
  73994. + *
  73995. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  73996. + * example, "cat /sys/devices/lm0/hcd_frrem".
  73997. + */
  73998. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  73999. +{ \
  74000. + hfnum_data_t hfnum; \
  74001. + dwc_otg_qtd_t *qtd; \
  74002. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  74003. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  74004. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  74005. + switch (hfnum.b.frnum & 0x7) { \
  74006. + case 7: \
  74007. + _hcd->hfnum_7_samples_##_letter++; \
  74008. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  74009. + break; \
  74010. + case 0: \
  74011. + _hcd->hfnum_0_samples_##_letter++; \
  74012. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  74013. + break; \
  74014. + default: \
  74015. + _hcd->hfnum_other_samples_##_letter++; \
  74016. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  74017. + break; \
  74018. + } \
  74019. + } \
  74020. +}
  74021. +#else
  74022. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  74023. +#endif
  74024. +#endif
  74025. +#endif /* DWC_DEVICE_ONLY */
  74026. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  74027. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 1970-01-01 01:00:00.000000000 +0100
  74028. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 2014-03-11 17:51:27.000000000 +0100
  74029. @@ -0,0 +1,417 @@
  74030. +/* ==========================================================================
  74031. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  74032. + * $Revision: #12 $
  74033. + * $Date: 2011/10/26 $
  74034. + * $Change: 1873028 $
  74035. + *
  74036. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  74037. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  74038. + * otherwise expressly agreed to in writing between Synopsys and you.
  74039. + *
  74040. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  74041. + * any End User Software License Agreement or Agreement for Licensed Product
  74042. + * with Synopsys or any supplement thereto. You are permitted to use and
  74043. + * redistribute this Software in source and binary forms, with or without
  74044. + * modification, provided that redistributions of source code must retain this
  74045. + * notice. You may not view, use, disclose, copy or distribute this file or
  74046. + * any information contained herein except pursuant to this license grant from
  74047. + * Synopsys. If you do not agree with this notice, including the disclaimer
  74048. + * below, then you are not authorized to use the Software.
  74049. + *
  74050. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  74051. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74052. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74053. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  74054. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74055. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74056. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74057. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  74058. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74059. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  74060. + * DAMAGE.
  74061. + * ========================================================================== */
  74062. +#ifndef DWC_DEVICE_ONLY
  74063. +#ifndef __DWC_HCD_IF_H__
  74064. +#define __DWC_HCD_IF_H__
  74065. +
  74066. +#include "dwc_otg_core_if.h"
  74067. +
  74068. +/** @file
  74069. + * This file defines DWC_OTG HCD Core API.
  74070. + */
  74071. +
  74072. +struct dwc_otg_hcd;
  74073. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  74074. +
  74075. +struct dwc_otg_hcd_urb;
  74076. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  74077. +
  74078. +/** @name HCD Function Driver Callbacks */
  74079. +/** @{ */
  74080. +
  74081. +/** This function is called whenever core switches to host mode. */
  74082. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  74083. +
  74084. +/** This function is called when device has been disconnected */
  74085. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  74086. +
  74087. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  74088. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  74089. + void *urb_handle,
  74090. + uint32_t * hub_addr,
  74091. + uint32_t * port_addr);
  74092. +/** Via this function HCD core gets device speed */
  74093. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  74094. + void *urb_handle);
  74095. +
  74096. +/** This function is called when urb is completed */
  74097. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  74098. + void *urb_handle,
  74099. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  74100. + int32_t status);
  74101. +
  74102. +/** Via this function HCD core gets b_hnp_enable parameter */
  74103. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  74104. +
  74105. +struct dwc_otg_hcd_function_ops {
  74106. + dwc_otg_hcd_start_cb_t start;
  74107. + dwc_otg_hcd_disconnect_cb_t disconnect;
  74108. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  74109. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  74110. + dwc_otg_hcd_complete_urb_cb_t complete;
  74111. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  74112. +};
  74113. +/** @} */
  74114. +
  74115. +/** @name HCD Core API */
  74116. +/** @{ */
  74117. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  74118. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  74119. +
  74120. +/** This function should be called to initiate HCD Core.
  74121. + *
  74122. + * @param hcd The HCD
  74123. + * @param core_if The DWC_OTG Core
  74124. + *
  74125. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  74126. + * Returns 0 on success
  74127. + */
  74128. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  74129. +
  74130. +/** Frees HCD
  74131. + *
  74132. + * @param hcd The HCD
  74133. + */
  74134. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  74135. +
  74136. +/** This function should be called on every hardware interrupt.
  74137. + *
  74138. + * @param dwc_otg_hcd The HCD
  74139. + *
  74140. + * Returns non zero if interrupt is handled
  74141. + * Return 0 if interrupt is not handled
  74142. + */
  74143. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  74144. +
  74145. +/** This function is used to handle the fast interrupt
  74146. + *
  74147. + */
  74148. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  74149. +
  74150. +/**
  74151. + * Returns private data set by
  74152. + * dwc_otg_hcd_set_priv_data function.
  74153. + *
  74154. + * @param hcd The HCD
  74155. + */
  74156. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  74157. +
  74158. +/**
  74159. + * Set private data.
  74160. + *
  74161. + * @param hcd The HCD
  74162. + * @param priv_data pointer to be stored in private data
  74163. + */
  74164. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  74165. +
  74166. +/**
  74167. + * This function initializes the HCD Core.
  74168. + *
  74169. + * @param hcd The HCD
  74170. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  74171. + *
  74172. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  74173. + * Returns 0 on success
  74174. + */
  74175. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  74176. + struct dwc_otg_hcd_function_ops *fops);
  74177. +
  74178. +/**
  74179. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  74180. + * stopped.
  74181. + *
  74182. + * @param hcd The HCD
  74183. + */
  74184. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  74185. +
  74186. +/**
  74187. + * Handles hub class-specific requests.
  74188. + *
  74189. + * @param dwc_otg_hcd The HCD
  74190. + * @param typeReq Request Type
  74191. + * @param wValue wValue from control request
  74192. + * @param wIndex wIndex from control request
  74193. + * @param buf data buffer
  74194. + * @param wLength data buffer length
  74195. + *
  74196. + * Returns -DWC_E_INVALID if invalid argument is passed
  74197. + * Returns 0 on success
  74198. + */
  74199. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  74200. + uint16_t typeReq, uint16_t wValue,
  74201. + uint16_t wIndex, uint8_t * buf,
  74202. + uint16_t wLength);
  74203. +
  74204. +/**
  74205. + * Returns otg port number.
  74206. + *
  74207. + * @param hcd The HCD
  74208. + */
  74209. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  74210. +
  74211. +/**
  74212. + * Returns OTG version - either 1.3 or 2.0.
  74213. + *
  74214. + * @param core_if The core_if structure pointer
  74215. + */
  74216. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  74217. +
  74218. +/**
  74219. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  74220. + *
  74221. + * @param hcd The HCD
  74222. + */
  74223. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  74224. +
  74225. +/**
  74226. + * Returns current frame number.
  74227. + *
  74228. + * @param hcd The HCD
  74229. + */
  74230. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  74231. +
  74232. +/**
  74233. + * Dumps hcd state.
  74234. + *
  74235. + * @param hcd The HCD
  74236. + */
  74237. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  74238. +
  74239. +/**
  74240. + * Dump the average frame remaining at SOF. This can be used to
  74241. + * determine average interrupt latency. Frame remaining is also shown for
  74242. + * start transfer and two additional sample points.
  74243. + * Currently this function is not implemented.
  74244. + *
  74245. + * @param hcd The HCD
  74246. + */
  74247. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  74248. +
  74249. +/**
  74250. + * Sends LPM transaction to the local device.
  74251. + *
  74252. + * @param hcd The HCD
  74253. + * @param devaddr Device Address
  74254. + * @param hird Host initiated resume duration
  74255. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  74256. + *
  74257. + * Returns negative value if sending LPM transaction was not succeeded.
  74258. + * Returns 0 on success.
  74259. + */
  74260. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  74261. + uint8_t hird, uint8_t bRemoteWake);
  74262. +
  74263. +/* URB interface */
  74264. +
  74265. +/**
  74266. + * Allocates memory for dwc_otg_hcd_urb structure.
  74267. + * Allocated memory should be freed by call of DWC_FREE.
  74268. + *
  74269. + * @param hcd The HCD
  74270. + * @param iso_desc_count Count of ISOC descriptors
  74271. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  74272. + */
  74273. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  74274. + int iso_desc_count,
  74275. + int atomic_alloc);
  74276. +
  74277. +/**
  74278. + * Set pipe information in URB.
  74279. + *
  74280. + * @param hcd_urb DWC_OTG URB
  74281. + * @param devaddr Device Address
  74282. + * @param ep_num Endpoint Number
  74283. + * @param ep_type Endpoint Type
  74284. + * @param ep_dir Endpoint Direction
  74285. + * @param mps Max Packet Size
  74286. + */
  74287. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  74288. + uint8_t devaddr, uint8_t ep_num,
  74289. + uint8_t ep_type, uint8_t ep_dir,
  74290. + uint16_t mps);
  74291. +
  74292. +/* Transfer flags */
  74293. +#define URB_GIVEBACK_ASAP 0x1
  74294. +#define URB_SEND_ZERO_PACKET 0x2
  74295. +
  74296. +/**
  74297. + * Sets dwc_otg_hcd_urb parameters.
  74298. + *
  74299. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  74300. + * @param urb_handle Unique handle for request, this will be passed back
  74301. + * to function driver in completion callback.
  74302. + * @param buf The buffer for the data
  74303. + * @param dma The DMA buffer for the data
  74304. + * @param buflen Transfer length
  74305. + * @param sp Buffer for setup data
  74306. + * @param sp_dma DMA address of setup data buffer
  74307. + * @param flags Transfer flags
  74308. + * @param interval Polling interval for interrupt or isochronous transfers.
  74309. + */
  74310. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  74311. + void *urb_handle, void *buf,
  74312. + dwc_dma_t dma, uint32_t buflen, void *sp,
  74313. + dwc_dma_t sp_dma, uint32_t flags,
  74314. + uint16_t interval);
  74315. +
  74316. +/** Gets status from dwc_otg_hcd_urb
  74317. + *
  74318. + * @param dwc_otg_urb DWC_OTG URB
  74319. + */
  74320. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  74321. +
  74322. +/** Gets actual length from dwc_otg_hcd_urb
  74323. + *
  74324. + * @param dwc_otg_urb DWC_OTG URB
  74325. + */
  74326. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  74327. + dwc_otg_urb);
  74328. +
  74329. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  74330. + *
  74331. + * @param dwc_otg_urb DWC_OTG URB
  74332. + */
  74333. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  74334. + dwc_otg_urb);
  74335. +
  74336. +/** Set ISOC descriptor offset and length
  74337. + *
  74338. + * @param dwc_otg_urb DWC_OTG URB
  74339. + * @param desc_num ISOC descriptor number
  74340. + * @param offset Offset from beginig of buffer.
  74341. + * @param length Transaction length
  74342. + */
  74343. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  74344. + int desc_num, uint32_t offset,
  74345. + uint32_t length);
  74346. +
  74347. +/** Get status of ISOC descriptor, specified by desc_num
  74348. + *
  74349. + * @param dwc_otg_urb DWC_OTG URB
  74350. + * @param desc_num ISOC descriptor number
  74351. + */
  74352. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  74353. + dwc_otg_urb, int desc_num);
  74354. +
  74355. +/** Get actual length of ISOC descriptor, specified by desc_num
  74356. + *
  74357. + * @param dwc_otg_urb DWC_OTG URB
  74358. + * @param desc_num ISOC descriptor number
  74359. + */
  74360. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  74361. + dwc_otg_urb,
  74362. + int desc_num);
  74363. +
  74364. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  74365. + *
  74366. + * @param dwc_otg_hcd The HCD
  74367. + * @param dwc_otg_urb DWC_OTG URB
  74368. + * @param ep_handle Out parameter for returning endpoint handle
  74369. + * @param atomic_alloc Flag to do atomic allocation if needed
  74370. + *
  74371. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  74372. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  74373. + * Returns 0 on success.
  74374. + */
  74375. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  74376. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  74377. + void **ep_handle, int atomic_alloc);
  74378. +
  74379. +/** De-queue the specified URB
  74380. + *
  74381. + * @param dwc_otg_hcd The HCD
  74382. + * @param dwc_otg_urb DWC_OTG URB
  74383. + */
  74384. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  74385. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  74386. +
  74387. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  74388. + * Any URBs for the endpoint must already be dequeued.
  74389. + *
  74390. + * @param hcd The HCD
  74391. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  74392. + * @param retry Number of retries if there are queued transfers.
  74393. + *
  74394. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  74395. + * Returns 0 on success
  74396. + */
  74397. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  74398. + int retry);
  74399. +
  74400. +/* Resets the data toggle in qh structure. This function can be called from
  74401. + * usb_clear_halt routine.
  74402. + *
  74403. + * @param hcd The HCD
  74404. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  74405. + *
  74406. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  74407. + * Returns 0 on success
  74408. + */
  74409. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  74410. +
  74411. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  74412. + *
  74413. + * @param hcd The HCD
  74414. + * @param port Port number
  74415. + */
  74416. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  74417. +
  74418. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  74419. + * Only for ISOC and INTERRUPT endpoints.
  74420. + *
  74421. + * @param hcd The HCD
  74422. + * @param ep_handle Endpoint handle
  74423. + */
  74424. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  74425. + void *ep_handle);
  74426. +
  74427. +/** Call this function to check if bandwidth was freed for specified endpoint.
  74428. + *
  74429. + * @param hcd The HCD
  74430. + * @param ep_handle Endpoint handle
  74431. + */
  74432. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  74433. +
  74434. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  74435. + * Only for ISOC and INTERRUPT endpoints.
  74436. + *
  74437. + * @param hcd The HCD
  74438. + * @param ep_handle Endpoint handle
  74439. + */
  74440. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  74441. + void *ep_handle);
  74442. +
  74443. +/** @} */
  74444. +
  74445. +#endif /* __DWC_HCD_IF_H__ */
  74446. +#endif /* DWC_DEVICE_ONLY */
  74447. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  74448. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  74449. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 2014-03-11 17:51:27.000000000 +0100
  74450. @@ -0,0 +1,2741 @@
  74451. +/* ==========================================================================
  74452. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  74453. + * $Revision: #89 $
  74454. + * $Date: 2011/10/20 $
  74455. + * $Change: 1869487 $
  74456. + *
  74457. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  74458. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  74459. + * otherwise expressly agreed to in writing between Synopsys and you.
  74460. + *
  74461. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  74462. + * any End User Software License Agreement or Agreement for Licensed Product
  74463. + * with Synopsys or any supplement thereto. You are permitted to use and
  74464. + * redistribute this Software in source and binary forms, with or without
  74465. + * modification, provided that redistributions of source code must retain this
  74466. + * notice. You may not view, use, disclose, copy or distribute this file or
  74467. + * any information contained herein except pursuant to this license grant from
  74468. + * Synopsys. If you do not agree with this notice, including the disclaimer
  74469. + * below, then you are not authorized to use the Software.
  74470. + *
  74471. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  74472. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74473. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74474. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  74475. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74476. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74477. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74478. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  74479. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74480. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  74481. + * DAMAGE.
  74482. + * ========================================================================== */
  74483. +#ifndef DWC_DEVICE_ONLY
  74484. +
  74485. +#include "dwc_otg_hcd.h"
  74486. +#include "dwc_otg_regs.h"
  74487. +#include "dwc_otg_mphi_fix.h"
  74488. +
  74489. +#include <linux/jiffies.h>
  74490. +#include <mach/hardware.h>
  74491. +#include <asm/fiq.h>
  74492. +
  74493. +
  74494. +extern bool microframe_schedule;
  74495. +
  74496. +/** @file
  74497. + * This file contains the implementation of the HCD Interrupt handlers.
  74498. + */
  74499. +
  74500. +/*
  74501. + * Some globals to communicate between the FIQ and INTERRUPT
  74502. + */
  74503. +
  74504. +void * dummy_send;
  74505. +mphi_regs_t c_mphi_regs;
  74506. +volatile void *dwc_regs_base;
  74507. +int fiq_done, int_done;
  74508. +
  74509. +gintsts_data_t gintsts_saved = {.d32 = 0};
  74510. +hcint_data_t hcint_saved[MAX_EPS_CHANNELS];
  74511. +hcintmsk_data_t hcintmsk_saved[MAX_EPS_CHANNELS];
  74512. +int split_out_xfersize[MAX_EPS_CHANNELS];
  74513. +haint_data_t haint_saved;
  74514. +
  74515. +int g_next_sched_frame, g_np_count, g_np_sent;
  74516. +static int mphi_int_count = 0 ;
  74517. +
  74518. +hcchar_data_t nak_hcchar;
  74519. +hctsiz_data_t nak_hctsiz;
  74520. +hcsplt_data_t nak_hcsplt;
  74521. +int nak_count;
  74522. +
  74523. +int complete_sched[MAX_EPS_CHANNELS] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  74524. +int split_start_frame[MAX_EPS_CHANNELS];
  74525. +int queued_port[MAX_EPS_CHANNELS];
  74526. +
  74527. +#ifdef FIQ_DEBUG
  74528. +char buffer[1000*16];
  74529. +int wptr;
  74530. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  74531. +{
  74532. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  74533. + va_list args;
  74534. + char text[17];
  74535. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  74536. + unsigned long flags;
  74537. +
  74538. + local_irq_save(flags);
  74539. + local_fiq_disable();
  74540. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  74541. + {
  74542. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  74543. + va_start(args, fmt);
  74544. + vsnprintf(text+8, 9, fmt, args);
  74545. + va_end(args);
  74546. +
  74547. + memcpy(buffer + wptr, text, 16);
  74548. + wptr = (wptr + 16) % sizeof(buffer);
  74549. + }
  74550. + local_irq_restore(flags);
  74551. +}
  74552. +#endif
  74553. +
  74554. +void notrace fiq_queue_request(int channel, int odd_frame)
  74555. +{
  74556. + hcchar_data_t hcchar = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x0) };
  74557. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x4) };
  74558. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x10) };
  74559. +
  74560. + if(hcsplt.b.spltena == 0)
  74561. + {
  74562. + fiq_print(FIQDBG_ERR, "SPLTENA ");
  74563. + BUG();
  74564. + }
  74565. +
  74566. + if(hcchar.b.epdir == 1)
  74567. + {
  74568. + fiq_print(FIQDBG_SCHED, "IN Ch %d", channel);
  74569. + }
  74570. + else
  74571. + {
  74572. + hctsiz.b.xfersize = 0;
  74573. + fiq_print(FIQDBG_SCHED, "OUT Ch %d", channel);
  74574. + }
  74575. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x10), hctsiz.d32);
  74576. +
  74577. + hcsplt.b.compsplt = 1;
  74578. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x4), hcsplt.d32);
  74579. +
  74580. + // Send the Split complete
  74581. + hcchar.b.chen = 1;
  74582. + hcchar.b.oddfrm = odd_frame ? 1 : 0;
  74583. +
  74584. + // Post this for transmit on the next frame for periodic or this frame for non-periodic
  74585. + fiq_print(FIQDBG_SCHED, "SND_%s", odd_frame ? "ODD " : "EVEN");
  74586. +
  74587. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x0), hcchar.d32);
  74588. +}
  74589. +
  74590. +static int last_sof = -1;
  74591. +
  74592. +/*
  74593. +** Function to handle the start of frame interrupt, choose whether we need to do anything and
  74594. +** therefore trigger the main interrupt
  74595. +**
  74596. +** returns int != 0 - interrupt has been handled
  74597. +*/
  74598. +int diff;
  74599. +
  74600. +int notrace fiq_sof_handle(hfnum_data_t hfnum)
  74601. +{
  74602. + int handled = 0;
  74603. + int i;
  74604. +
  74605. + // Just check that once we're running we don't miss a SOF
  74606. + /*if(last_sof != -1 && (hfnum.b.frnum != ((last_sof + 1) & 0x3fff)))
  74607. + {
  74608. + fiq_print(FIQDBG_ERR, "LASTSOF ");
  74609. + fiq_print(FIQDBG_ERR, "%4d%d ", last_sof / 8, last_sof & 7);
  74610. + fiq_print(FIQDBG_ERR, "%4d%d ", hfnum.b.frnum / 8, hfnum.b.frnum & 7);
  74611. + BUG();
  74612. + }*/
  74613. +
  74614. + // Only start remembering the last sof when the interrupt has been
  74615. + // enabled (we don't check the mask to come in here...)
  74616. + if(last_sof != -1 || FIQ_READ(dwc_regs_base + 0x18) & (1<<3))
  74617. + last_sof = hfnum.b.frnum;
  74618. +
  74619. + for(i = 0; i < MAX_EPS_CHANNELS; i++)
  74620. + {
  74621. + if(complete_sched[i] != -1)
  74622. + {
  74623. + if(complete_sched[i] <= hfnum.b.frnum || (complete_sched[i] > 0x3f00 && hfnum.b.frnum < 0xf0))
  74624. + {
  74625. + fiq_queue_request(i, hfnum.b.frnum & 1);
  74626. + complete_sched[i] = -1;
  74627. + }
  74628. + }
  74629. +
  74630. + if(complete_sched[i] != -1)
  74631. + {
  74632. + // This is because we've seen a split complete occur with no start...
  74633. + // most likely because missed the complete 0x3fff frames ago!
  74634. +
  74635. + diff = (hfnum.b.frnum + 0x3fff - complete_sched[i]) & 0x3fff ;
  74636. + if(diff > 32 && diff < 0x3f00)
  74637. + {
  74638. + fiq_print(FIQDBG_ERR, "SPLTMISS");
  74639. + BUG();
  74640. + }
  74641. + }
  74642. + }
  74643. +
  74644. + if(g_np_count == g_np_sent && dwc_frame_num_gt(g_next_sched_frame, hfnum.b.frnum))
  74645. + {
  74646. + /*
  74647. + * If np_count != np_sent that means we need to queue non-periodic (bulk) packets this packet
  74648. + * g_next_sched_frame is the next frame we have periodic packets for
  74649. + *
  74650. + * if neither of these are required for this frame then just clear the interrupt
  74651. + */
  74652. + handled = 1;
  74653. +
  74654. + }
  74655. +
  74656. + return handled;
  74657. +}
  74658. +
  74659. +int notrace port_id(hcsplt_data_t hcsplt)
  74660. +{
  74661. + return hcsplt.b.prtaddr + (hcsplt.b.hubaddr << 8);
  74662. +}
  74663. +
  74664. +int notrace fiq_hcintr_handle(int channel, hfnum_data_t hfnum)
  74665. +{
  74666. + hcchar_data_t hcchar = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x0) };
  74667. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x4) };
  74668. + hcint_data_t hcint = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x8) };
  74669. + hcintmsk_data_t hcintmsk = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0xc) };
  74670. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x10)};
  74671. +
  74672. + hcint_saved[channel].d32 |= hcint.d32;
  74673. + hcintmsk_saved[channel].d32 = hcintmsk.d32;
  74674. +
  74675. + if(hcsplt.b.spltena)
  74676. + {
  74677. + fiq_print(FIQDBG_PORTHUB, "ph: %4x", port_id(hcsplt));
  74678. + if(hcint.b.chhltd)
  74679. + {
  74680. + fiq_print(FIQDBG_SCHED, "CH HLT %d", channel);
  74681. + fiq_print(FIQDBG_SCHED, "%08x", hcint_saved[channel]);
  74682. + }
  74683. + if(hcint.b.stall || hcint.b.xacterr || hcint.b.bblerr || hcint.b.frmovrun || hcint.b.datatglerr)
  74684. + {
  74685. + queued_port[channel] = 0;
  74686. + fiq_print(FIQDBG_ERR, "CHAN ERR");
  74687. + }
  74688. + if(hcint.b.xfercomp)
  74689. + {
  74690. + // Clear the port allocation and transmit anything also on this port
  74691. + queued_port[channel] = 0;
  74692. + fiq_print(FIQDBG_SCHED, "XFERCOMP");
  74693. + }
  74694. + if(hcint.b.nak)
  74695. + {
  74696. + queued_port[channel] = 0;
  74697. + fiq_print(FIQDBG_SCHED, "NAK");
  74698. + }
  74699. + if(hcint.b.ack && !hcsplt.b.compsplt)
  74700. + {
  74701. + int i;
  74702. +
  74703. + // Do not complete isochronous out transactions
  74704. + if(hcchar.b.eptype == 1 && hcchar.b.epdir == 0)
  74705. + {
  74706. + queued_port[channel] = 0;
  74707. + fiq_print(FIQDBG_SCHED, "ISOC_OUT");
  74708. + }
  74709. + else
  74710. + {
  74711. + // Make sure we check the port / hub combination that we sent this split on.
  74712. + // Do not queue a second request to the same port
  74713. + for(i = 0; i < MAX_EPS_CHANNELS; i++)
  74714. + {
  74715. + if(port_id(hcsplt) == queued_port[i])
  74716. + {
  74717. + fiq_print(FIQDBG_ERR, "PORTERR ");
  74718. + //BUG();
  74719. + }
  74720. + }
  74721. +
  74722. + split_start_frame[channel] = (hfnum.b.frnum + 1) & ~7;
  74723. +
  74724. + // Note, the size of an OUT is in the start split phase, not
  74725. + // the complete split
  74726. + split_out_xfersize[channel] = hctsiz.b.xfersize;
  74727. +
  74728. + hcint_saved[channel].b.chhltd = 0;
  74729. + hcint_saved[channel].b.ack = 0;
  74730. +
  74731. + queued_port[channel] = port_id(hcsplt);
  74732. +
  74733. + if(hcchar.b.eptype & 1)
  74734. + {
  74735. + // Send the periodic complete in the same oddness frame as the ACK went...
  74736. + fiq_queue_request(channel, !(hfnum.b.frnum & 1));
  74737. + // complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 1);
  74738. + }
  74739. + else
  74740. + {
  74741. + // Schedule the split complete to occur later
  74742. + complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 2);
  74743. + fiq_print(FIQDBG_SCHED, "ACK%04d%d", complete_sched[channel]/8, complete_sched[channel]%8);
  74744. + }
  74745. + }
  74746. + }
  74747. + if(hcint.b.nyet)
  74748. + {
  74749. + fiq_print(FIQDBG_ERR, "NYETERR1");
  74750. + //BUG();
  74751. + // Can transmit a split complete up to uframe .0 of the next frame
  74752. + if(hfnum.b.frnum <= dwc_frame_num_inc(split_start_frame[channel], 8))
  74753. + {
  74754. + // Send it next frame
  74755. + if(hcchar.b.eptype & 1) // type 1 & 3 are interrupt & isoc
  74756. + {
  74757. + fiq_print(FIQDBG_SCHED, "NYT:SEND");
  74758. + fiq_queue_request(channel, !(hfnum.b.frnum & 1));
  74759. + }
  74760. + else
  74761. + {
  74762. + // Schedule non-periodic access for next frame (the odd-even bit doesn't effect NP)
  74763. + complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 1);
  74764. + fiq_print(FIQDBG_SCHED, "NYT%04d%d", complete_sched[channel]/8, complete_sched[channel]%8);
  74765. + }
  74766. + hcint_saved[channel].b.chhltd = 0;
  74767. + hcint_saved[channel].b.nyet = 0;
  74768. + }
  74769. + else
  74770. + {
  74771. + queued_port[channel] = 0;
  74772. + fiq_print(FIQDBG_ERR, "NYETERR2");
  74773. + //BUG();
  74774. + }
  74775. + }
  74776. + }
  74777. + else
  74778. + {
  74779. + /*
  74780. + * If we have any of NAK, ACK, Datatlgerr active on a
  74781. + * non-split channel, the sole reason is to reset error
  74782. + * counts for a previously broken transaction. The FIQ
  74783. + * will thrash on NAK IN and ACK OUT in particular so
  74784. + * handle it "once" and allow the IRQ to do the rest.
  74785. + */
  74786. + hcint.d32 &= hcintmsk.d32;
  74787. + if(hcint.b.nak)
  74788. + {
  74789. + hcintmsk.b.nak = 0;
  74790. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0xc), hcintmsk.d32);
  74791. + }
  74792. + if (hcint.b.ack)
  74793. + {
  74794. + hcintmsk.b.ack = 0;
  74795. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0xc), hcintmsk.d32);
  74796. + }
  74797. + }
  74798. +
  74799. + // Clear the interrupt, this will also clear the HAINT bit
  74800. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x8), hcint.d32);
  74801. + return hcint_saved[channel].d32 == 0;
  74802. +}
  74803. +
  74804. +gintsts_data_t gintsts;
  74805. +gintmsk_data_t gintmsk;
  74806. +// triggered: The set of interrupts that were triggered
  74807. +// handled: The set of interrupts that have been handled (no IRQ is
  74808. +// required)
  74809. +// keep: The set of interrupts we want to keep unmasked even though we
  74810. +// want to trigger an IRQ to handle it (SOF and HCINTR)
  74811. +gintsts_data_t triggered, handled, keep;
  74812. +hfnum_data_t hfnum;
  74813. +
  74814. +void __attribute__ ((naked)) notrace dwc_otg_hcd_handle_fiq(void)
  74815. +{
  74816. +
  74817. + /* entry takes care to store registers we will be treading on here */
  74818. + asm __volatile__ (
  74819. + "mov ip, sp ;"
  74820. + /* stash FIQ and normal regs */
  74821. + "stmdb sp!, {r0-r12, lr};"
  74822. + /* !! THIS SETS THE FRAME, adjust to > sizeof locals */
  74823. + "sub fp, ip, #512 ;"
  74824. + );
  74825. +
  74826. + // Cannot put local variables at the beginning of the function
  74827. + // because otherwise 'C' will play with the stack pointer. any locals
  74828. + // need to be inside the following block
  74829. + do
  74830. + {
  74831. + fiq_done++;
  74832. + gintsts.d32 = FIQ_READ(dwc_regs_base + 0x14);
  74833. + gintmsk.d32 = FIQ_READ(dwc_regs_base + 0x18);
  74834. + hfnum.d32 = FIQ_READ(dwc_regs_base + 0x408);
  74835. + triggered.d32 = gintsts.d32 & gintmsk.d32;
  74836. + handled.d32 = 0;
  74837. + keep.d32 = 0;
  74838. + fiq_print(FIQDBG_INT, "FIQ ");
  74839. + fiq_print(FIQDBG_INT, "%08x", gintsts.d32);
  74840. + fiq_print(FIQDBG_INT, "%08x", gintmsk.d32);
  74841. + if(gintsts.d32)
  74842. + {
  74843. + // If port enabled
  74844. + if((FIQ_READ(dwc_regs_base + 0x440) & 0xf) == 0x5)
  74845. + {
  74846. + if(gintsts.b.sofintr)
  74847. + {
  74848. + if(fiq_sof_handle(hfnum))
  74849. + {
  74850. + handled.b.sofintr = 1; /* Handled in FIQ */
  74851. + }
  74852. + else
  74853. + {
  74854. + /* Keer interrupt unmasked */
  74855. + keep.b.sofintr = 1;
  74856. + }
  74857. + {
  74858. + // Need to make sure the read and clearing of the SOF interrupt is as close as possible to avoid the possibility of missing
  74859. + // a start of frame interrupt
  74860. + gintsts_data_t gintsts = { .b.sofintr = 1 };
  74861. + FIQ_WRITE((dwc_regs_base + 0x14), gintsts.d32);
  74862. + }
  74863. + }
  74864. +
  74865. + if(fiq_split_enable && gintsts.b.hcintr)
  74866. + {
  74867. + int i;
  74868. + haint_data_t haint;
  74869. + haintmsk_data_t haintmsk;
  74870. +
  74871. + haint.d32 = FIQ_READ(dwc_regs_base + 0x414);
  74872. + haintmsk.d32 = FIQ_READ(dwc_regs_base + 0x418);
  74873. + haint.d32 &= haintmsk.d32;
  74874. + haint_saved.d32 |= haint.d32;
  74875. +
  74876. + fiq_print(FIQDBG_INT, "hcintr");
  74877. + fiq_print(FIQDBG_INT, "%08x", FIQ_READ(dwc_regs_base + 0x414));
  74878. +
  74879. + // Go through each channel that has an enabled interrupt
  74880. + for(i = 0; i < 16; i++)
  74881. + if((haint.d32 >> i) & 1)
  74882. + if(fiq_hcintr_handle(i, hfnum))
  74883. + haint_saved.d32 &= ~(1 << i); /* this was handled */
  74884. +
  74885. + /* If we've handled all host channel interrupts then don't trigger the interrupt */
  74886. + if(haint_saved.d32 == 0)
  74887. + {
  74888. + handled.b.hcintr = 1;
  74889. + }
  74890. + else
  74891. + {
  74892. + /* Make sure we keep the channel interrupt unmasked when triggering the IRQ */
  74893. + keep.b.hcintr = 1;
  74894. + }
  74895. +
  74896. + {
  74897. + gintsts_data_t gintsts = { .b.hcintr = 1 };
  74898. +
  74899. + // Always clear the channel interrupt
  74900. + FIQ_WRITE((dwc_regs_base + 0x14), gintsts.d32);
  74901. + }
  74902. + }
  74903. + }
  74904. + else
  74905. + {
  74906. + last_sof = -1;
  74907. + }
  74908. + }
  74909. +
  74910. + // Mask out the interrupts triggered - those handled - don't mask out the ones we want to keep
  74911. + gintmsk.d32 = keep.d32 | (gintmsk.d32 & ~(triggered.d32 & ~handled.d32));
  74912. + // Save those that were triggered but not handled
  74913. + gintsts_saved.d32 |= triggered.d32 & ~handled.d32;
  74914. + FIQ_WRITE(dwc_regs_base + 0x18, gintmsk.d32);
  74915. +
  74916. + // Clear and save any unhandled interrupts and trigger the interrupt
  74917. + if(gintsts_saved.d32)
  74918. + {
  74919. + /* To enable the MPHI interrupt (INT 32)
  74920. + */
  74921. + FIQ_WRITE( c_mphi_regs.outdda, (int) dummy_send);
  74922. + FIQ_WRITE( c_mphi_regs.outddb, (1 << 29));
  74923. +
  74924. + mphi_int_count++;
  74925. + }
  74926. + }
  74927. + while(0);
  74928. +
  74929. + mb();
  74930. +
  74931. + /* exit back to normal mode restoring everything */
  74932. + asm __volatile__ (
  74933. + /* return FIQ regs back to pristine state
  74934. + * and get normal regs back
  74935. + */
  74936. + "ldmia sp!, {r0-r12, lr};"
  74937. +
  74938. + /* return */
  74939. + "subs pc, lr, #4;"
  74940. + );
  74941. +}
  74942. +
  74943. +/** This function handles interrupts for the HCD. */
  74944. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74945. +{
  74946. + int retval = 0;
  74947. + static int last_time;
  74948. +
  74949. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  74950. + gintsts_data_t gintsts;
  74951. + gintmsk_data_t gintmsk;
  74952. + hfnum_data_t hfnum;
  74953. +
  74954. +#ifdef DEBUG
  74955. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  74956. +
  74957. +#endif
  74958. +
  74959. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  74960. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  74961. +
  74962. + /* Exit from ISR if core is hibernated */
  74963. + if (core_if->hibernation_suspend == 1) {
  74964. + goto exit_handler_routine;
  74965. + }
  74966. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  74967. + /* Check if HOST Mode */
  74968. + if (dwc_otg_is_host_mode(core_if)) {
  74969. + local_fiq_disable();
  74970. + gintmsk.d32 |= gintsts_saved.d32;
  74971. + gintsts.d32 |= gintsts_saved.d32;
  74972. + gintsts_saved.d32 = 0;
  74973. + local_fiq_enable();
  74974. + if (!gintsts.d32) {
  74975. + goto exit_handler_routine;
  74976. + }
  74977. + gintsts.d32 &= gintmsk.d32;
  74978. +
  74979. +#ifdef DEBUG
  74980. + // We should be OK doing this because the common interrupts should already have been serviced
  74981. + /* Don't print debug message in the interrupt handler on SOF */
  74982. +#ifndef DEBUG_SOF
  74983. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  74984. +#endif
  74985. + DWC_DEBUGPL(DBG_HCDI, "\n");
  74986. +#endif
  74987. +
  74988. +#ifdef DEBUG
  74989. +#ifndef DEBUG_SOF
  74990. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  74991. +#endif
  74992. + DWC_DEBUGPL(DBG_HCDI,
  74993. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  74994. + gintsts.d32, core_if);
  74995. +#endif
  74996. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  74997. + if (gintsts.b.sofintr && g_np_count == g_np_sent && dwc_frame_num_gt(g_next_sched_frame, hfnum.b.frnum))
  74998. + {
  74999. + /* Note, we should never get here if the FIQ is doing it's job properly*/
  75000. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  75001. + }
  75002. + else if (gintsts.b.sofintr) {
  75003. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  75004. + }
  75005. +
  75006. + if (gintsts.b.rxstsqlvl) {
  75007. + retval |=
  75008. + dwc_otg_hcd_handle_rx_status_q_level_intr
  75009. + (dwc_otg_hcd);
  75010. + }
  75011. + if (gintsts.b.nptxfempty) {
  75012. + retval |=
  75013. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  75014. + (dwc_otg_hcd);
  75015. + }
  75016. + if (gintsts.b.i2cintr) {
  75017. + /** @todo Implement i2cintr handler. */
  75018. + }
  75019. + if (gintsts.b.portintr) {
  75020. +
  75021. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  75022. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  75023. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  75024. + }
  75025. + if (gintsts.b.hcintr) {
  75026. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  75027. + }
  75028. + if (gintsts.b.ptxfempty) {
  75029. + retval |=
  75030. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  75031. + (dwc_otg_hcd);
  75032. + }
  75033. +#ifdef DEBUG
  75034. +#ifndef DEBUG_SOF
  75035. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  75036. +#endif
  75037. + {
  75038. + DWC_DEBUGPL(DBG_HCDI,
  75039. + "DWC OTG HCD Finished Servicing Interrupts\n");
  75040. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  75041. + DWC_READ_REG32(&global_regs->gintsts));
  75042. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  75043. + DWC_READ_REG32(&global_regs->gintmsk));
  75044. + }
  75045. +#endif
  75046. +
  75047. +#ifdef DEBUG
  75048. +#ifndef DEBUG_SOF
  75049. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  75050. +#endif
  75051. + DWC_DEBUGPL(DBG_HCDI, "\n");
  75052. +#endif
  75053. +
  75054. + }
  75055. +
  75056. +exit_handler_routine:
  75057. +
  75058. + if (fiq_fix_enable)
  75059. + {
  75060. + local_fiq_disable();
  75061. + // Make sure that we don't clear the interrupt if we've still got pending work to do
  75062. + if(gintsts_saved.d32 == 0)
  75063. + {
  75064. + /* Clear the MPHI interrupt */
  75065. + DWC_WRITE_REG32(c_mphi_regs.intstat, (1<<16));
  75066. + if (mphi_int_count >= 60)
  75067. + {
  75068. + DWC_WRITE_REG32(c_mphi_regs.ctrl, ((1<<31) + (1<<16)));
  75069. + while(!(DWC_READ_REG32(c_mphi_regs.ctrl) & (1 << 17)))
  75070. + ;
  75071. + DWC_WRITE_REG32(c_mphi_regs.ctrl, (1<<31));
  75072. + mphi_int_count = 0;
  75073. + }
  75074. + int_done++;
  75075. + }
  75076. +
  75077. + // Unmask handled interrupts
  75078. + FIQ_WRITE(dwc_regs_base + 0x18, gintmsk.d32);
  75079. + //DWC_MODIFY_REG32((uint32_t *)IO_ADDRESS(USB_BASE + 0x8), 0 , 1);
  75080. +
  75081. + local_fiq_enable();
  75082. +
  75083. + if((jiffies / HZ) > last_time)
  75084. + {
  75085. + /* Once a second output the fiq and irq numbers, useful for debug */
  75086. + last_time = jiffies / HZ;
  75087. + DWC_DEBUGPL(DBG_USER, "int_done = %d fiq_done = %d\n", int_done, fiq_done);
  75088. + }
  75089. + }
  75090. +
  75091. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  75092. + return retval;
  75093. +}
  75094. +
  75095. +#ifdef DWC_TRACK_MISSED_SOFS
  75096. +
  75097. +#warning Compiling code to track missed SOFs
  75098. +#define FRAME_NUM_ARRAY_SIZE 1000
  75099. +/**
  75100. + * This function is for debug only.
  75101. + */
  75102. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  75103. +{
  75104. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  75105. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  75106. + static int frame_num_idx = 0;
  75107. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  75108. + static int dumped_frame_num_array = 0;
  75109. +
  75110. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  75111. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  75112. + curr_frame_number) {
  75113. + frame_num_array[frame_num_idx] = curr_frame_number;
  75114. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  75115. + }
  75116. + } else if (!dumped_frame_num_array) {
  75117. + int i;
  75118. + DWC_PRINTF("Frame Last Frame\n");
  75119. + DWC_PRINTF("----- ----------\n");
  75120. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  75121. + DWC_PRINTF("0x%04x 0x%04x\n",
  75122. + frame_num_array[i], last_frame_num_array[i]);
  75123. + }
  75124. + dumped_frame_num_array = 1;
  75125. + }
  75126. + last_frame_num = curr_frame_number;
  75127. +}
  75128. +#endif
  75129. +
  75130. +/**
  75131. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  75132. + * transactions may be queued to the DWC_otg controller for the current
  75133. + * (micro)frame. Periodic transactions may be queued to the controller for the
  75134. + * next (micro)frame.
  75135. + */
  75136. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  75137. +{
  75138. + hfnum_data_t hfnum;
  75139. + dwc_list_link_t *qh_entry;
  75140. + dwc_otg_qh_t *qh;
  75141. + dwc_otg_transaction_type_e tr_type;
  75142. + int did_something = 0;
  75143. + int32_t next_sched_frame = -1;
  75144. +
  75145. + hfnum.d32 =
  75146. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  75147. +
  75148. +#ifdef DEBUG_SOF
  75149. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  75150. +#endif
  75151. + hcd->frame_number = hfnum.b.frnum;
  75152. +
  75153. +#ifdef DEBUG
  75154. + hcd->frrem_accum += hfnum.b.frrem;
  75155. + hcd->frrem_samples++;
  75156. +#endif
  75157. +
  75158. +#ifdef DWC_TRACK_MISSED_SOFS
  75159. + track_missed_sofs(hcd->frame_number);
  75160. +#endif
  75161. + /* Determine whether any periodic QHs should be executed. */
  75162. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  75163. + while (qh_entry != &hcd->periodic_sched_inactive) {
  75164. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  75165. + qh_entry = qh_entry->next;
  75166. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  75167. +
  75168. + /*
  75169. + * Move QH to the ready list to be executed next
  75170. + * (micro)frame.
  75171. + */
  75172. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  75173. + &qh->qh_list_entry);
  75174. +
  75175. + did_something = 1;
  75176. + }
  75177. + else
  75178. + {
  75179. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  75180. + {
  75181. + next_sched_frame = qh->sched_frame;
  75182. + }
  75183. + }
  75184. + }
  75185. +
  75186. + g_next_sched_frame = next_sched_frame;
  75187. +
  75188. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  75189. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  75190. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  75191. + did_something = 1;
  75192. + }
  75193. +
  75194. + /* Clear interrupt */
  75195. + gintsts.b.sofintr = 1;
  75196. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  75197. +
  75198. + return 1;
  75199. +}
  75200. +
  75201. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  75202. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  75203. + * memory if the DWC_otg controller is operating in Slave mode. */
  75204. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75205. +{
  75206. + host_grxsts_data_t grxsts;
  75207. + dwc_hc_t *hc = NULL;
  75208. +
  75209. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  75210. +
  75211. + grxsts.d32 =
  75212. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  75213. +
  75214. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  75215. + if (!hc) {
  75216. + DWC_ERROR("Unable to get corresponding channel\n");
  75217. + return 0;
  75218. + }
  75219. +
  75220. + /* Packet Status */
  75221. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  75222. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  75223. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  75224. + hc->data_pid_start);
  75225. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  75226. +
  75227. + switch (grxsts.b.pktsts) {
  75228. + case DWC_GRXSTS_PKTSTS_IN:
  75229. + /* Read the data into the host buffer. */
  75230. + if (grxsts.b.bcnt > 0) {
  75231. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  75232. + hc->xfer_buff, grxsts.b.bcnt);
  75233. +
  75234. + /* Update the HC fields for the next packet received. */
  75235. + hc->xfer_count += grxsts.b.bcnt;
  75236. + hc->xfer_buff += grxsts.b.bcnt;
  75237. + }
  75238. +
  75239. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  75240. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  75241. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  75242. + /* Handled in interrupt, just ignore data */
  75243. + break;
  75244. + default:
  75245. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  75246. + grxsts.b.pktsts);
  75247. + break;
  75248. + }
  75249. +
  75250. + return 1;
  75251. +}
  75252. +
  75253. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  75254. + * data packets may be written to the FIFO for OUT transfers. More requests
  75255. + * may be written to the non-periodic request queue for IN transfers. This
  75256. + * interrupt is enabled only in Slave mode. */
  75257. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75258. +{
  75259. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  75260. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  75261. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  75262. + return 1;
  75263. +}
  75264. +
  75265. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  75266. + * packets may be written to the FIFO for OUT transfers. More requests may be
  75267. + * written to the periodic request queue for IN transfers. This interrupt is
  75268. + * enabled only in Slave mode. */
  75269. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75270. +{
  75271. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  75272. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  75273. + DWC_OTG_TRANSACTION_PERIODIC);
  75274. + return 1;
  75275. +}
  75276. +
  75277. +/** There are multiple conditions that can cause a port interrupt. This function
  75278. + * determines which interrupt conditions have occurred and handles them
  75279. + * appropriately. */
  75280. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75281. +{
  75282. + int retval = 0;
  75283. + hprt0_data_t hprt0;
  75284. + hprt0_data_t hprt0_modify;
  75285. +
  75286. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  75287. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  75288. +
  75289. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  75290. + * GINTSTS */
  75291. +
  75292. + hprt0_modify.b.prtena = 0;
  75293. + hprt0_modify.b.prtconndet = 0;
  75294. + hprt0_modify.b.prtenchng = 0;
  75295. + hprt0_modify.b.prtovrcurrchng = 0;
  75296. +
  75297. + /* Port Connect Detected
  75298. + * Set flag and clear if detected */
  75299. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  75300. + // Dont modify port status if we are in hibernation state
  75301. + hprt0_modify.b.prtconndet = 1;
  75302. + hprt0_modify.b.prtenchng = 1;
  75303. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  75304. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  75305. + return retval;
  75306. + }
  75307. +
  75308. + if (hprt0.b.prtconndet) {
  75309. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  75310. + if (dwc_otg_hcd->core_if->adp_enable &&
  75311. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  75312. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  75313. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  75314. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  75315. + /* TODO - check if this is required, as
  75316. + * host initialization was already performed
  75317. + * after initial ADP probing
  75318. + */
  75319. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  75320. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  75321. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  75322. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  75323. + } else {
  75324. +
  75325. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  75326. + "Port Connect Detected--\n", hprt0.d32);
  75327. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  75328. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  75329. + hprt0_modify.b.prtconndet = 1;
  75330. +
  75331. + /* B-Device has connected, Delete the connection timer. */
  75332. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  75333. + }
  75334. + /* The Hub driver asserts a reset when it sees port connect
  75335. + * status change flag */
  75336. + retval |= 1;
  75337. + }
  75338. +
  75339. + /* Port Enable Changed
  75340. + * Clear if detected - Set internal flag if disabled */
  75341. + if (hprt0.b.prtenchng) {
  75342. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  75343. + "Port Enable Changed--\n", hprt0.d32);
  75344. + hprt0_modify.b.prtenchng = 1;
  75345. + if (hprt0.b.prtena == 1) {
  75346. + hfir_data_t hfir;
  75347. + int do_reset = 0;
  75348. + dwc_otg_core_params_t *params =
  75349. + dwc_otg_hcd->core_if->core_params;
  75350. + dwc_otg_core_global_regs_t *global_regs =
  75351. + dwc_otg_hcd->core_if->core_global_regs;
  75352. + dwc_otg_host_if_t *host_if =
  75353. + dwc_otg_hcd->core_if->host_if;
  75354. +
  75355. + /* Every time when port enables calculate
  75356. + * HFIR.FrInterval
  75357. + */
  75358. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  75359. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  75360. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  75361. +
  75362. + /* Check if we need to adjust the PHY clock speed for
  75363. + * low power and adjust it */
  75364. + if (params->host_support_fs_ls_low_power) {
  75365. + gusbcfg_data_t usbcfg;
  75366. +
  75367. + usbcfg.d32 =
  75368. + DWC_READ_REG32(&global_regs->gusbcfg);
  75369. +
  75370. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  75371. + || hprt0.b.prtspd ==
  75372. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  75373. + /*
  75374. + * Low power
  75375. + */
  75376. + hcfg_data_t hcfg;
  75377. + if (usbcfg.b.phylpwrclksel == 0) {
  75378. + /* Set PHY low power clock select for FS/LS devices */
  75379. + usbcfg.b.phylpwrclksel = 1;
  75380. + DWC_WRITE_REG32
  75381. + (&global_regs->gusbcfg,
  75382. + usbcfg.d32);
  75383. + do_reset = 1;
  75384. + }
  75385. +
  75386. + hcfg.d32 =
  75387. + DWC_READ_REG32
  75388. + (&host_if->host_global_regs->hcfg);
  75389. +
  75390. + if (hprt0.b.prtspd ==
  75391. + DWC_HPRT0_PRTSPD_LOW_SPEED
  75392. + && params->host_ls_low_power_phy_clk
  75393. + ==
  75394. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  75395. + {
  75396. + /* 6 MHZ */
  75397. + DWC_DEBUGPL(DBG_CIL,
  75398. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  75399. + if (hcfg.b.fslspclksel !=
  75400. + DWC_HCFG_6_MHZ) {
  75401. + hcfg.b.fslspclksel =
  75402. + DWC_HCFG_6_MHZ;
  75403. + DWC_WRITE_REG32
  75404. + (&host_if->host_global_regs->hcfg,
  75405. + hcfg.d32);
  75406. + do_reset = 1;
  75407. + }
  75408. + } else {
  75409. + /* 48 MHZ */
  75410. + DWC_DEBUGPL(DBG_CIL,
  75411. + "FS_PHY programming HCFG to 48 MHz ()\n");
  75412. + if (hcfg.b.fslspclksel !=
  75413. + DWC_HCFG_48_MHZ) {
  75414. + hcfg.b.fslspclksel =
  75415. + DWC_HCFG_48_MHZ;
  75416. + DWC_WRITE_REG32
  75417. + (&host_if->host_global_regs->hcfg,
  75418. + hcfg.d32);
  75419. + do_reset = 1;
  75420. + }
  75421. + }
  75422. + } else {
  75423. + /*
  75424. + * Not low power
  75425. + */
  75426. + if (usbcfg.b.phylpwrclksel == 1) {
  75427. + usbcfg.b.phylpwrclksel = 0;
  75428. + DWC_WRITE_REG32
  75429. + (&global_regs->gusbcfg,
  75430. + usbcfg.d32);
  75431. + do_reset = 1;
  75432. + }
  75433. + }
  75434. +
  75435. + if (do_reset) {
  75436. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  75437. + }
  75438. + }
  75439. +
  75440. + if (!do_reset) {
  75441. + /* Port has been enabled set the reset change flag */
  75442. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  75443. + }
  75444. + } else {
  75445. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  75446. + }
  75447. + retval |= 1;
  75448. + }
  75449. +
  75450. + /** Overcurrent Change Interrupt */
  75451. + if (hprt0.b.prtovrcurrchng) {
  75452. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  75453. + "Port Overcurrent Changed--\n", hprt0.d32);
  75454. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  75455. + hprt0_modify.b.prtovrcurrchng = 1;
  75456. + retval |= 1;
  75457. + }
  75458. +
  75459. + /* Clear Port Interrupts */
  75460. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  75461. +
  75462. + return retval;
  75463. +}
  75464. +
  75465. +/** This interrupt indicates that one or more host channels has a pending
  75466. + * interrupt. There are multiple conditions that can cause each host channel
  75467. + * interrupt. This function determines which conditions have occurred for each
  75468. + * host channel interrupt and handles them appropriately. */
  75469. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75470. +{
  75471. + int i;
  75472. + int retval = 0;
  75473. + haint_data_t haint;
  75474. +
  75475. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  75476. + * GINTSTS */
  75477. +
  75478. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  75479. +
  75480. + // Overwrite with saved interrupts from fiq handler
  75481. + if(fiq_split_enable)
  75482. + {
  75483. + local_fiq_disable();
  75484. + haint.d32 = haint_saved.d32;
  75485. + haint_saved.d32 = 0;
  75486. + local_fiq_enable();
  75487. + }
  75488. +
  75489. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  75490. + if (haint.b2.chint & (1 << i)) {
  75491. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  75492. + }
  75493. + }
  75494. +
  75495. + return retval;
  75496. +}
  75497. +
  75498. +/**
  75499. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  75500. + * holds the reason for the halt.
  75501. + *
  75502. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  75503. + * *short_read is set to 1 upon return if less than the requested
  75504. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  75505. + * return. short_read may also be NULL on entry, in which case it remains
  75506. + * unchanged.
  75507. + */
  75508. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  75509. + dwc_otg_hc_regs_t * hc_regs,
  75510. + dwc_otg_qtd_t * qtd,
  75511. + dwc_otg_halt_status_e halt_status,
  75512. + int *short_read)
  75513. +{
  75514. + hctsiz_data_t hctsiz;
  75515. + uint32_t length;
  75516. +
  75517. + if (short_read != NULL) {
  75518. + *short_read = 0;
  75519. + }
  75520. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75521. +
  75522. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  75523. + if (hc->ep_is_in) {
  75524. + length = hc->xfer_len - hctsiz.b.xfersize;
  75525. + if (short_read != NULL) {
  75526. + *short_read = (hctsiz.b.xfersize != 0);
  75527. + }
  75528. + } else if (hc->qh->do_split) {
  75529. + if(fiq_split_enable)
  75530. + length = split_out_xfersize[hc->hc_num];
  75531. + else
  75532. + length = qtd->ssplit_out_xfer_count;
  75533. + } else {
  75534. + length = hc->xfer_len;
  75535. + }
  75536. + } else {
  75537. + /*
  75538. + * Must use the hctsiz.pktcnt field to determine how much data
  75539. + * has been transferred. This field reflects the number of
  75540. + * packets that have been transferred via the USB. This is
  75541. + * always an integral number of packets if the transfer was
  75542. + * halted before its normal completion. (Can't use the
  75543. + * hctsiz.xfersize field because that reflects the number of
  75544. + * bytes transferred via the AHB, not the USB).
  75545. + */
  75546. + length =
  75547. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  75548. + }
  75549. +
  75550. + return length;
  75551. +}
  75552. +
  75553. +/**
  75554. + * Updates the state of the URB after a Transfer Complete interrupt on the
  75555. + * host channel. Updates the actual_length field of the URB based on the
  75556. + * number of bytes transferred via the host channel. Sets the URB status
  75557. + * if the data transfer is finished.
  75558. + *
  75559. + * @return 1 if the data transfer specified by the URB is completely finished,
  75560. + * 0 otherwise.
  75561. + */
  75562. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  75563. + dwc_otg_hc_regs_t * hc_regs,
  75564. + dwc_otg_hcd_urb_t * urb,
  75565. + dwc_otg_qtd_t * qtd)
  75566. +{
  75567. + int xfer_done = 0;
  75568. + int short_read = 0;
  75569. +
  75570. + int xfer_length;
  75571. +
  75572. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  75573. + DWC_OTG_HC_XFER_COMPLETE,
  75574. + &short_read);
  75575. +
  75576. + /* non DWORD-aligned buffer case handling. */
  75577. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  75578. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  75579. + xfer_length);
  75580. + }
  75581. +
  75582. + urb->actual_length += xfer_length;
  75583. +
  75584. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  75585. + (urb->flags & URB_SEND_ZERO_PACKET)
  75586. + && (urb->actual_length == urb->length)
  75587. + && !(urb->length % hc->max_packet)) {
  75588. + xfer_done = 0;
  75589. + } else if (short_read || urb->actual_length >= urb->length) {
  75590. + xfer_done = 1;
  75591. + urb->status = 0;
  75592. + }
  75593. +
  75594. +#ifdef DEBUG
  75595. + {
  75596. + hctsiz_data_t hctsiz;
  75597. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75598. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  75599. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  75600. + hc->hc_num);
  75601. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  75602. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  75603. + hctsiz.b.xfersize);
  75604. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  75605. + urb->length);
  75606. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  75607. + urb->actual_length);
  75608. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  75609. + short_read, xfer_done);
  75610. + }
  75611. +#endif
  75612. +
  75613. + return xfer_done;
  75614. +}
  75615. +
  75616. +/*
  75617. + * Save the starting data toggle for the next transfer. The data toggle is
  75618. + * saved in the QH for non-control transfers and it's saved in the QTD for
  75619. + * control transfers.
  75620. + */
  75621. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  75622. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  75623. +{
  75624. + hctsiz_data_t hctsiz;
  75625. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75626. +
  75627. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  75628. + dwc_otg_qh_t *qh = hc->qh;
  75629. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  75630. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  75631. + } else {
  75632. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  75633. + }
  75634. + } else {
  75635. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  75636. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  75637. + } else {
  75638. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  75639. + }
  75640. + }
  75641. +}
  75642. +
  75643. +/**
  75644. + * Updates the state of an Isochronous URB when the transfer is stopped for
  75645. + * any reason. The fields of the current entry in the frame descriptor array
  75646. + * are set based on the transfer state and the input _halt_status. Completes
  75647. + * the Isochronous URB if all the URB frames have been completed.
  75648. + *
  75649. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  75650. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  75651. + */
  75652. +static dwc_otg_halt_status_e
  75653. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  75654. + dwc_hc_t * hc,
  75655. + dwc_otg_hc_regs_t * hc_regs,
  75656. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  75657. +{
  75658. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  75659. + dwc_otg_halt_status_e ret_val = halt_status;
  75660. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  75661. +
  75662. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  75663. + switch (halt_status) {
  75664. + case DWC_OTG_HC_XFER_COMPLETE:
  75665. + frame_desc->status = 0;
  75666. + frame_desc->actual_length =
  75667. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  75668. +
  75669. + /* non DWORD-aligned buffer case handling. */
  75670. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  75671. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  75672. + hc->qh->dw_align_buf, frame_desc->actual_length);
  75673. + }
  75674. +
  75675. + break;
  75676. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  75677. + urb->error_count++;
  75678. + if (hc->ep_is_in) {
  75679. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  75680. + } else {
  75681. + frame_desc->status = -DWC_E_COMMUNICATION;
  75682. + }
  75683. + frame_desc->actual_length = 0;
  75684. + break;
  75685. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  75686. + urb->error_count++;
  75687. + frame_desc->status = -DWC_E_OVERFLOW;
  75688. + /* Don't need to update actual_length in this case. */
  75689. + break;
  75690. + case DWC_OTG_HC_XFER_XACT_ERR:
  75691. + urb->error_count++;
  75692. + frame_desc->status = -DWC_E_PROTOCOL;
  75693. + frame_desc->actual_length =
  75694. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  75695. +
  75696. + /* non DWORD-aligned buffer case handling. */
  75697. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  75698. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  75699. + hc->qh->dw_align_buf, frame_desc->actual_length);
  75700. + }
  75701. + /* Skip whole frame */
  75702. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  75703. + hc->ep_is_in && hcd->core_if->dma_enable) {
  75704. + qtd->complete_split = 0;
  75705. + qtd->isoc_split_offset = 0;
  75706. + }
  75707. +
  75708. + break;
  75709. + default:
  75710. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  75711. + break;
  75712. + }
  75713. + if (++qtd->isoc_frame_index == urb->packet_count) {
  75714. + /*
  75715. + * urb->status is not used for isoc transfers.
  75716. + * The individual frame_desc statuses are used instead.
  75717. + */
  75718. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  75719. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  75720. + } else {
  75721. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  75722. + }
  75723. + return ret_val;
  75724. +}
  75725. +
  75726. +/**
  75727. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  75728. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  75729. + * still linked to the QH, the QH is added to the end of the inactive
  75730. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  75731. + * schedule if no more QTDs are linked to the QH.
  75732. + */
  75733. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  75734. +{
  75735. + int continue_split = 0;
  75736. + dwc_otg_qtd_t *qtd;
  75737. +
  75738. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  75739. +
  75740. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  75741. +
  75742. + if (qtd->complete_split) {
  75743. + continue_split = 1;
  75744. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  75745. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  75746. + continue_split = 1;
  75747. + }
  75748. +
  75749. + if (free_qtd) {
  75750. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  75751. + continue_split = 0;
  75752. + }
  75753. +
  75754. + qh->channel = NULL;
  75755. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  75756. +}
  75757. +
  75758. +/**
  75759. + * Releases a host channel for use by other transfers. Attempts to select and
  75760. + * queue more transactions since at least one host channel is available.
  75761. + *
  75762. + * @param hcd The HCD state structure.
  75763. + * @param hc The host channel to release.
  75764. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  75765. + * if the transfer is complete or an error has occurred.
  75766. + * @param halt_status Reason the channel is being released. This status
  75767. + * determines the actions taken by this function.
  75768. + */
  75769. +static void release_channel(dwc_otg_hcd_t * hcd,
  75770. + dwc_hc_t * hc,
  75771. + dwc_otg_qtd_t * qtd,
  75772. + dwc_otg_halt_status_e halt_status)
  75773. +{
  75774. + dwc_otg_transaction_type_e tr_type;
  75775. + int free_qtd;
  75776. + dwc_irqflags_t flags;
  75777. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  75778. +#ifdef FIQ_DEBUG
  75779. + int endp = qtd->urb ? qtd->urb->pipe_info.ep_num : 0;
  75780. +#endif
  75781. + int hog_port = 0;
  75782. +
  75783. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  75784. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  75785. +
  75786. + if(fiq_split_enable && hc->do_split) {
  75787. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  75788. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  75789. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  75790. + hog_port = 1;
  75791. + }
  75792. + }
  75793. + }
  75794. +
  75795. + switch (halt_status) {
  75796. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  75797. + free_qtd = 1;
  75798. + break;
  75799. + case DWC_OTG_HC_XFER_AHB_ERR:
  75800. + case DWC_OTG_HC_XFER_STALL:
  75801. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  75802. + free_qtd = 1;
  75803. + break;
  75804. + case DWC_OTG_HC_XFER_XACT_ERR:
  75805. + if (qtd->error_count >= 3) {
  75806. + DWC_DEBUGPL(DBG_HCDV,
  75807. + " Complete URB with transaction error\n");
  75808. + free_qtd = 1;
  75809. + qtd->urb->status = -DWC_E_PROTOCOL;
  75810. + hcd->fops->complete(hcd, qtd->urb->priv,
  75811. + qtd->urb, -DWC_E_PROTOCOL);
  75812. + } else {
  75813. + free_qtd = 0;
  75814. + }
  75815. + break;
  75816. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  75817. + /*
  75818. + * The QTD has already been removed and the QH has been
  75819. + * deactivated. Don't want to do anything except release the
  75820. + * host channel and try to queue more transfers.
  75821. + */
  75822. + goto cleanup;
  75823. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  75824. + free_qtd = 0;
  75825. + break;
  75826. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  75827. + DWC_DEBUGPL(DBG_HCDV,
  75828. + " Complete URB with I/O error\n");
  75829. + free_qtd = 1;
  75830. + qtd->urb->status = -DWC_E_IO;
  75831. + hcd->fops->complete(hcd, qtd->urb->priv,
  75832. + qtd->urb, -DWC_E_IO);
  75833. + break;
  75834. + default:
  75835. + free_qtd = 0;
  75836. + break;
  75837. + }
  75838. +
  75839. + deactivate_qh(hcd, hc->qh, free_qtd);
  75840. +
  75841. +cleanup:
  75842. + /*
  75843. + * Release the host channel for use by other transfers. The cleanup
  75844. + * function clears the channel interrupt enables and conditions, so
  75845. + * there's no need to clear the Channel Halted interrupt separately.
  75846. + */
  75847. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  75848. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  75849. +
  75850. + if (!microframe_schedule) {
  75851. + switch (hc->ep_type) {
  75852. + case DWC_OTG_EP_TYPE_CONTROL:
  75853. + case DWC_OTG_EP_TYPE_BULK:
  75854. + hcd->non_periodic_channels--;
  75855. + break;
  75856. +
  75857. + default:
  75858. + /*
  75859. + * Don't release reservations for periodic channels here.
  75860. + * That's done when a periodic transfer is descheduled (i.e.
  75861. + * when the QH is removed from the periodic schedule).
  75862. + */
  75863. + break;
  75864. + }
  75865. + } else {
  75866. +
  75867. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  75868. + hcd->available_host_channels++;
  75869. + fiq_print(FIQDBG_PORTHUB, "AHC = %d ", hcd->available_host_channels);
  75870. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  75871. + }
  75872. +
  75873. + if(fiq_split_enable && hc->do_split)
  75874. + {
  75875. + if(!(hcd->hub_port[hc->hub_addr] & (1 << hc->port_addr)))
  75876. + {
  75877. + fiq_print(FIQDBG_ERR, "PRTNOTAL");
  75878. + //BUG();
  75879. + }
  75880. + if(!hog_port && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC ||
  75881. + hc->ep_type == DWC_OTG_EP_TYPE_INTR)) {
  75882. + hcd->hub_port[hc->hub_addr] &= ~(1 << hc->port_addr);
  75883. +#ifdef FIQ_DEBUG
  75884. + hcd->hub_port_alloc[hc->hub_addr * 16 + hc->port_addr] = -1;
  75885. +#endif
  75886. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:RR%d", hc->hub_addr, hc->port_addr, endp);
  75887. + }
  75888. + }
  75889. +
  75890. + /* Try to queue more transfers now that there's a free channel. */
  75891. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  75892. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  75893. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  75894. + }
  75895. +}
  75896. +
  75897. +/**
  75898. + * Halts a host channel. If the channel cannot be halted immediately because
  75899. + * the request queue is full, this function ensures that the FIFO empty
  75900. + * interrupt for the appropriate queue is enabled so that the halt request can
  75901. + * be queued when there is space in the request queue.
  75902. + *
  75903. + * This function may also be called in DMA mode. In that case, the channel is
  75904. + * simply released since the core always halts the channel automatically in
  75905. + * DMA mode.
  75906. + */
  75907. +static void halt_channel(dwc_otg_hcd_t * hcd,
  75908. + dwc_hc_t * hc,
  75909. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  75910. +{
  75911. + if (hcd->core_if->dma_enable) {
  75912. + release_channel(hcd, hc, qtd, halt_status);
  75913. + return;
  75914. + }
  75915. +
  75916. + /* Slave mode processing... */
  75917. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  75918. +
  75919. + if (hc->halt_on_queue) {
  75920. + gintmsk_data_t gintmsk = {.d32 = 0 };
  75921. + dwc_otg_core_global_regs_t *global_regs;
  75922. + global_regs = hcd->core_if->core_global_regs;
  75923. +
  75924. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  75925. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  75926. + /*
  75927. + * Make sure the Non-periodic Tx FIFO empty interrupt
  75928. + * is enabled so that the non-periodic schedule will
  75929. + * be processed.
  75930. + */
  75931. + gintmsk.b.nptxfempty = 1;
  75932. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  75933. + } else {
  75934. + /*
  75935. + * Move the QH from the periodic queued schedule to
  75936. + * the periodic assigned schedule. This allows the
  75937. + * halt to be queued when the periodic schedule is
  75938. + * processed.
  75939. + */
  75940. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  75941. + &hc->qh->qh_list_entry);
  75942. +
  75943. + /*
  75944. + * Make sure the Periodic Tx FIFO Empty interrupt is
  75945. + * enabled so that the periodic schedule will be
  75946. + * processed.
  75947. + */
  75948. + gintmsk.b.ptxfempty = 1;
  75949. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  75950. + }
  75951. + }
  75952. +}
  75953. +
  75954. +/**
  75955. + * Performs common cleanup for non-periodic transfers after a Transfer
  75956. + * Complete interrupt. This function should be called after any endpoint type
  75957. + * specific handling is finished to release the host channel.
  75958. + */
  75959. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  75960. + dwc_hc_t * hc,
  75961. + dwc_otg_hc_regs_t * hc_regs,
  75962. + dwc_otg_qtd_t * qtd,
  75963. + dwc_otg_halt_status_e halt_status)
  75964. +{
  75965. + hcint_data_t hcint;
  75966. +
  75967. + qtd->error_count = 0;
  75968. +
  75969. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  75970. + if (hcint.b.nyet) {
  75971. + /*
  75972. + * Got a NYET on the last transaction of the transfer. This
  75973. + * means that the endpoint should be in the PING state at the
  75974. + * beginning of the next transfer.
  75975. + */
  75976. + hc->qh->ping_state = 1;
  75977. + clear_hc_int(hc_regs, nyet);
  75978. + }
  75979. +
  75980. + /*
  75981. + * Always halt and release the host channel to make it available for
  75982. + * more transfers. There may still be more phases for a control
  75983. + * transfer or more data packets for a bulk transfer at this point,
  75984. + * but the host channel is still halted. A channel will be reassigned
  75985. + * to the transfer when the non-periodic schedule is processed after
  75986. + * the channel is released. This allows transactions to be queued
  75987. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  75988. + * Tx FIFO Empty interrupt if necessary.
  75989. + */
  75990. + if (hc->ep_is_in) {
  75991. + /*
  75992. + * IN transfers in Slave mode require an explicit disable to
  75993. + * halt the channel. (In DMA mode, this call simply releases
  75994. + * the channel.)
  75995. + */
  75996. + halt_channel(hcd, hc, qtd, halt_status);
  75997. + } else {
  75998. + /*
  75999. + * The channel is automatically disabled by the core for OUT
  76000. + * transfers in Slave mode.
  76001. + */
  76002. + release_channel(hcd, hc, qtd, halt_status);
  76003. + }
  76004. +}
  76005. +
  76006. +/**
  76007. + * Performs common cleanup for periodic transfers after a Transfer Complete
  76008. + * interrupt. This function should be called after any endpoint type specific
  76009. + * handling is finished to release the host channel.
  76010. + */
  76011. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  76012. + dwc_hc_t * hc,
  76013. + dwc_otg_hc_regs_t * hc_regs,
  76014. + dwc_otg_qtd_t * qtd,
  76015. + dwc_otg_halt_status_e halt_status)
  76016. +{
  76017. + hctsiz_data_t hctsiz;
  76018. + qtd->error_count = 0;
  76019. +
  76020. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76021. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  76022. + /* Core halts channel in these cases. */
  76023. + release_channel(hcd, hc, qtd, halt_status);
  76024. + } else {
  76025. + /* Flush any outstanding requests from the Tx queue. */
  76026. + halt_channel(hcd, hc, qtd, halt_status);
  76027. + }
  76028. +}
  76029. +
  76030. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  76031. + dwc_hc_t * hc,
  76032. + dwc_otg_hc_regs_t * hc_regs,
  76033. + dwc_otg_qtd_t * qtd)
  76034. +{
  76035. + uint32_t len;
  76036. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  76037. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  76038. +
  76039. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  76040. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  76041. +
  76042. + if (!len) {
  76043. + qtd->complete_split = 0;
  76044. + qtd->isoc_split_offset = 0;
  76045. + return 0;
  76046. + }
  76047. + frame_desc->actual_length += len;
  76048. +
  76049. + if (hc->align_buff && len)
  76050. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  76051. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  76052. + qtd->isoc_split_offset += len;
  76053. +
  76054. + if (frame_desc->length == frame_desc->actual_length) {
  76055. + frame_desc->status = 0;
  76056. + qtd->isoc_frame_index++;
  76057. + qtd->complete_split = 0;
  76058. + qtd->isoc_split_offset = 0;
  76059. + }
  76060. +
  76061. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  76062. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  76063. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  76064. + } else {
  76065. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  76066. + }
  76067. +
  76068. + return 1; /* Indicates that channel released */
  76069. +}
  76070. +
  76071. +/**
  76072. + * Handles a host channel Transfer Complete interrupt. This handler may be
  76073. + * called in either DMA mode or Slave mode.
  76074. + */
  76075. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  76076. + dwc_hc_t * hc,
  76077. + dwc_otg_hc_regs_t * hc_regs,
  76078. + dwc_otg_qtd_t * qtd)
  76079. +{
  76080. + int urb_xfer_done;
  76081. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76082. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  76083. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  76084. +
  76085. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76086. + "Transfer Complete--\n", hc->hc_num);
  76087. +
  76088. + if (hcd->core_if->dma_desc_enable) {
  76089. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  76090. + if (pipe_type == UE_ISOCHRONOUS) {
  76091. + /* Do not disable the interrupt, just clear it */
  76092. + clear_hc_int(hc_regs, xfercomp);
  76093. + return 1;
  76094. + }
  76095. + goto handle_xfercomp_done;
  76096. + }
  76097. +
  76098. + /*
  76099. + * Handle xfer complete on CSPLIT.
  76100. + */
  76101. +
  76102. + if (hc->qh->do_split) {
  76103. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  76104. + && hcd->core_if->dma_enable) {
  76105. + if (qtd->complete_split
  76106. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  76107. + qtd))
  76108. + goto handle_xfercomp_done;
  76109. + } else {
  76110. + qtd->complete_split = 0;
  76111. + }
  76112. + }
  76113. +
  76114. + /* Update the QTD and URB states. */
  76115. + switch (pipe_type) {
  76116. + case UE_CONTROL:
  76117. + switch (qtd->control_phase) {
  76118. + case DWC_OTG_CONTROL_SETUP:
  76119. + if (urb->length > 0) {
  76120. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  76121. + } else {
  76122. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  76123. + }
  76124. + DWC_DEBUGPL(DBG_HCDV,
  76125. + " Control setup transaction done\n");
  76126. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76127. + break;
  76128. + case DWC_OTG_CONTROL_DATA:{
  76129. + urb_xfer_done =
  76130. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  76131. + qtd);
  76132. + if (urb_xfer_done) {
  76133. + qtd->control_phase =
  76134. + DWC_OTG_CONTROL_STATUS;
  76135. + DWC_DEBUGPL(DBG_HCDV,
  76136. + " Control data transfer done\n");
  76137. + } else {
  76138. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76139. + }
  76140. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76141. + break;
  76142. + }
  76143. + case DWC_OTG_CONTROL_STATUS:
  76144. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  76145. + if (urb->status == -DWC_E_IN_PROGRESS) {
  76146. + urb->status = 0;
  76147. + }
  76148. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  76149. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  76150. + break;
  76151. + }
  76152. +
  76153. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76154. + break;
  76155. + case UE_BULK:
  76156. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  76157. + urb_xfer_done =
  76158. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  76159. + if (urb_xfer_done) {
  76160. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  76161. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  76162. + } else {
  76163. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76164. + }
  76165. +
  76166. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76167. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76168. + break;
  76169. + case UE_INTERRUPT:
  76170. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  76171. + urb_xfer_done =
  76172. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  76173. +
  76174. + /*
  76175. + * Interrupt URB is done on the first transfer complete
  76176. + * interrupt.
  76177. + */
  76178. + if (urb_xfer_done) {
  76179. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  76180. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  76181. + } else {
  76182. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76183. + }
  76184. +
  76185. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76186. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76187. + break;
  76188. + case UE_ISOCHRONOUS:
  76189. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  76190. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  76191. + halt_status =
  76192. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76193. + DWC_OTG_HC_XFER_COMPLETE);
  76194. + }
  76195. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76196. + break;
  76197. + }
  76198. +
  76199. +handle_xfercomp_done:
  76200. + disable_hc_int(hc_regs, xfercompl);
  76201. +
  76202. + return 1;
  76203. +}
  76204. +
  76205. +/**
  76206. + * Handles a host channel STALL interrupt. This handler may be called in
  76207. + * either DMA mode or Slave mode.
  76208. + */
  76209. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  76210. + dwc_hc_t * hc,
  76211. + dwc_otg_hc_regs_t * hc_regs,
  76212. + dwc_otg_qtd_t * qtd)
  76213. +{
  76214. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  76215. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  76216. +
  76217. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  76218. + "STALL Received--\n", hc->hc_num);
  76219. +
  76220. + if (hcd->core_if->dma_desc_enable) {
  76221. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  76222. + goto handle_stall_done;
  76223. + }
  76224. +
  76225. + if (pipe_type == UE_CONTROL) {
  76226. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  76227. + }
  76228. +
  76229. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  76230. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  76231. + /*
  76232. + * USB protocol requires resetting the data toggle for bulk
  76233. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  76234. + * setup command is issued to the endpoint. Anticipate the
  76235. + * CLEAR_FEATURE command since a STALL has occurred and reset
  76236. + * the data toggle now.
  76237. + */
  76238. + hc->qh->data_toggle = 0;
  76239. + }
  76240. +
  76241. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  76242. +
  76243. +handle_stall_done:
  76244. + disable_hc_int(hc_regs, stall);
  76245. +
  76246. + return 1;
  76247. +}
  76248. +
  76249. +/*
  76250. + * Updates the state of the URB when a transfer has been stopped due to an
  76251. + * abnormal condition before the transfer completes. Modifies the
  76252. + * actual_length field of the URB to reflect the number of bytes that have
  76253. + * actually been transferred via the host channel.
  76254. + */
  76255. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  76256. + dwc_otg_hc_regs_t * hc_regs,
  76257. + dwc_otg_hcd_urb_t * urb,
  76258. + dwc_otg_qtd_t * qtd,
  76259. + dwc_otg_halt_status_e halt_status)
  76260. +{
  76261. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  76262. + halt_status, NULL);
  76263. + /* non DWORD-aligned buffer case handling. */
  76264. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  76265. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  76266. + bytes_transferred);
  76267. + }
  76268. +
  76269. + urb->actual_length += bytes_transferred;
  76270. +
  76271. +#ifdef DEBUG
  76272. + {
  76273. + hctsiz_data_t hctsiz;
  76274. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76275. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  76276. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  76277. + hc->hc_num);
  76278. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  76279. + hc->start_pkt_count);
  76280. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  76281. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  76282. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  76283. + bytes_transferred);
  76284. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  76285. + urb->actual_length);
  76286. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  76287. + urb->length);
  76288. + }
  76289. +#endif
  76290. +}
  76291. +
  76292. +/**
  76293. + * Handles a host channel NAK interrupt. This handler may be called in either
  76294. + * DMA mode or Slave mode.
  76295. + */
  76296. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  76297. + dwc_hc_t * hc,
  76298. + dwc_otg_hc_regs_t * hc_regs,
  76299. + dwc_otg_qtd_t * qtd)
  76300. +{
  76301. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76302. + "NAK Received--\n", hc->hc_num);
  76303. +
  76304. + /*
  76305. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  76306. + * the beginning of the next frame
  76307. + */
  76308. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76309. + case UE_BULK:
  76310. + case UE_CONTROL:
  76311. + if (nak_holdoff_enable)
  76312. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  76313. + }
  76314. +
  76315. + /*
  76316. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  76317. + * interrupt. Re-start the SSPLIT transfer.
  76318. + */
  76319. + if (hc->do_split) {
  76320. + if (hc->complete_split) {
  76321. + qtd->error_count = 0;
  76322. + }
  76323. + qtd->complete_split = 0;
  76324. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  76325. + goto handle_nak_done;
  76326. + }
  76327. +
  76328. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76329. + case UE_CONTROL:
  76330. + case UE_BULK:
  76331. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  76332. + /*
  76333. + * NAK interrupts are enabled on bulk/control IN
  76334. + * transfers in DMA mode for the sole purpose of
  76335. + * resetting the error count after a transaction error
  76336. + * occurs. The core will continue transferring data.
  76337. + * Disable other interrupts unmasked for the same
  76338. + * reason.
  76339. + */
  76340. + disable_hc_int(hc_regs, datatglerr);
  76341. + disable_hc_int(hc_regs, ack);
  76342. + qtd->error_count = 0;
  76343. + goto handle_nak_done;
  76344. + }
  76345. +
  76346. + /*
  76347. + * NAK interrupts normally occur during OUT transfers in DMA
  76348. + * or Slave mode. For IN transfers, more requests will be
  76349. + * queued as request queue space is available.
  76350. + */
  76351. + qtd->error_count = 0;
  76352. +
  76353. + if (!hc->qh->ping_state) {
  76354. + update_urb_state_xfer_intr(hc, hc_regs,
  76355. + qtd->urb, qtd,
  76356. + DWC_OTG_HC_XFER_NAK);
  76357. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76358. +
  76359. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  76360. + hc->qh->ping_state = 1;
  76361. + }
  76362. +
  76363. + /*
  76364. + * Halt the channel so the transfer can be re-started from
  76365. + * the appropriate point or the PING protocol will
  76366. + * start/continue.
  76367. + */
  76368. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  76369. + break;
  76370. + case UE_INTERRUPT:
  76371. + qtd->error_count = 0;
  76372. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  76373. + break;
  76374. + case UE_ISOCHRONOUS:
  76375. + /* Should never get called for isochronous transfers. */
  76376. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  76377. + break;
  76378. + }
  76379. +
  76380. +handle_nak_done:
  76381. + disable_hc_int(hc_regs, nak);
  76382. +
  76383. + return 1;
  76384. +}
  76385. +
  76386. +/**
  76387. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  76388. + * performing the PING protocol in Slave mode, when errors occur during
  76389. + * either Slave mode or DMA mode, and during Start Split transactions.
  76390. + */
  76391. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  76392. + dwc_hc_t * hc,
  76393. + dwc_otg_hc_regs_t * hc_regs,
  76394. + dwc_otg_qtd_t * qtd)
  76395. +{
  76396. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76397. + "ACK Received--\n", hc->hc_num);
  76398. +
  76399. + if (hc->do_split) {
  76400. + /*
  76401. + * Handle ACK on SSPLIT.
  76402. + * ACK should not occur in CSPLIT.
  76403. + */
  76404. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  76405. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  76406. + }
  76407. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  76408. + /* Don't need complete for isochronous out transfers. */
  76409. + qtd->complete_split = 1;
  76410. + }
  76411. +
  76412. + /* ISOC OUT */
  76413. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  76414. + switch (hc->xact_pos) {
  76415. + case DWC_HCSPLIT_XACTPOS_ALL:
  76416. + break;
  76417. + case DWC_HCSPLIT_XACTPOS_END:
  76418. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  76419. + qtd->isoc_split_offset = 0;
  76420. + break;
  76421. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  76422. + case DWC_HCSPLIT_XACTPOS_MID:
  76423. + /*
  76424. + * For BEGIN or MID, calculate the length for
  76425. + * the next microframe to determine the correct
  76426. + * SSPLIT token, either MID or END.
  76427. + */
  76428. + {
  76429. + struct dwc_otg_hcd_iso_packet_desc
  76430. + *frame_desc;
  76431. +
  76432. + frame_desc =
  76433. + &qtd->urb->
  76434. + iso_descs[qtd->isoc_frame_index];
  76435. + qtd->isoc_split_offset += 188;
  76436. +
  76437. + if ((frame_desc->length -
  76438. + qtd->isoc_split_offset) <= 188) {
  76439. + qtd->isoc_split_pos =
  76440. + DWC_HCSPLIT_XACTPOS_END;
  76441. + } else {
  76442. + qtd->isoc_split_pos =
  76443. + DWC_HCSPLIT_XACTPOS_MID;
  76444. + }
  76445. +
  76446. + }
  76447. + break;
  76448. + }
  76449. + } else {
  76450. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  76451. + }
  76452. + } else {
  76453. + /*
  76454. + * An unmasked ACK on a non-split DMA transaction is
  76455. + * for the sole purpose of resetting error counts. Disable other
  76456. + * interrupts unmasked for the same reason.
  76457. + */
  76458. + if(hcd->core_if->dma_enable) {
  76459. + disable_hc_int(hc_regs, datatglerr);
  76460. + disable_hc_int(hc_regs, nak);
  76461. + }
  76462. + qtd->error_count = 0;
  76463. +
  76464. + if (hc->qh->ping_state) {
  76465. + hc->qh->ping_state = 0;
  76466. + /*
  76467. + * Halt the channel so the transfer can be re-started
  76468. + * from the appropriate point. This only happens in
  76469. + * Slave mode. In DMA mode, the ping_state is cleared
  76470. + * when the transfer is started because the core
  76471. + * automatically executes the PING, then the transfer.
  76472. + */
  76473. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  76474. + }
  76475. + }
  76476. +
  76477. + /*
  76478. + * If the ACK occurred when _not_ in the PING state, let the channel
  76479. + * continue transferring data after clearing the error count.
  76480. + */
  76481. +
  76482. + disable_hc_int(hc_regs, ack);
  76483. +
  76484. + return 1;
  76485. +}
  76486. +
  76487. +/**
  76488. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  76489. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  76490. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  76491. + * handled in the xfercomp interrupt handler, not here. This handler may be
  76492. + * called in either DMA mode or Slave mode.
  76493. + */
  76494. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  76495. + dwc_hc_t * hc,
  76496. + dwc_otg_hc_regs_t * hc_regs,
  76497. + dwc_otg_qtd_t * qtd)
  76498. +{
  76499. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76500. + "NYET Received--\n", hc->hc_num);
  76501. +
  76502. + /*
  76503. + * NYET on CSPLIT
  76504. + * re-do the CSPLIT immediately on non-periodic
  76505. + */
  76506. + if (hc->do_split && hc->complete_split) {
  76507. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  76508. + && hcd->core_if->dma_enable) {
  76509. + qtd->complete_split = 0;
  76510. + qtd->isoc_split_offset = 0;
  76511. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  76512. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  76513. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  76514. + }
  76515. + else
  76516. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  76517. + goto handle_nyet_done;
  76518. + }
  76519. +
  76520. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  76521. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  76522. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  76523. +
  76524. + // With the FIQ running we only ever see the failed NYET
  76525. + if (dwc_full_frame_num(frnum) !=
  76526. + dwc_full_frame_num(hc->qh->sched_frame) ||
  76527. + fiq_split_enable) {
  76528. + /*
  76529. + * No longer in the same full speed frame.
  76530. + * Treat this as a transaction error.
  76531. + */
  76532. +#if 0
  76533. + /** @todo Fix system performance so this can
  76534. + * be treated as an error. Right now complete
  76535. + * splits cannot be scheduled precisely enough
  76536. + * due to other system activity, so this error
  76537. + * occurs regularly in Slave mode.
  76538. + */
  76539. + qtd->error_count++;
  76540. +#endif
  76541. + qtd->complete_split = 0;
  76542. + halt_channel(hcd, hc, qtd,
  76543. + DWC_OTG_HC_XFER_XACT_ERR);
  76544. + /** @todo add support for isoc release */
  76545. + goto handle_nyet_done;
  76546. + }
  76547. + }
  76548. +
  76549. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  76550. + goto handle_nyet_done;
  76551. + }
  76552. +
  76553. + hc->qh->ping_state = 1;
  76554. + qtd->error_count = 0;
  76555. +
  76556. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  76557. + DWC_OTG_HC_XFER_NYET);
  76558. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76559. +
  76560. + /*
  76561. + * Halt the channel and re-start the transfer so the PING
  76562. + * protocol will start.
  76563. + */
  76564. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  76565. +
  76566. +handle_nyet_done:
  76567. + disable_hc_int(hc_regs, nyet);
  76568. + return 1;
  76569. +}
  76570. +
  76571. +/**
  76572. + * Handles a host channel babble interrupt. This handler may be called in
  76573. + * either DMA mode or Slave mode.
  76574. + */
  76575. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  76576. + dwc_hc_t * hc,
  76577. + dwc_otg_hc_regs_t * hc_regs,
  76578. + dwc_otg_qtd_t * qtd)
  76579. +{
  76580. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76581. + "Babble Error--\n", hc->hc_num);
  76582. +
  76583. + if (hcd->core_if->dma_desc_enable) {
  76584. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76585. + DWC_OTG_HC_XFER_BABBLE_ERR);
  76586. + goto handle_babble_done;
  76587. + }
  76588. +
  76589. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  76590. + hcd->fops->complete(hcd, qtd->urb->priv,
  76591. + qtd->urb, -DWC_E_OVERFLOW);
  76592. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  76593. + } else {
  76594. + dwc_otg_halt_status_e halt_status;
  76595. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76596. + DWC_OTG_HC_XFER_BABBLE_ERR);
  76597. + halt_channel(hcd, hc, qtd, halt_status);
  76598. + }
  76599. +
  76600. +handle_babble_done:
  76601. + disable_hc_int(hc_regs, bblerr);
  76602. + return 1;
  76603. +}
  76604. +
  76605. +/**
  76606. + * Handles a host channel AHB error interrupt. This handler is only called in
  76607. + * DMA mode.
  76608. + */
  76609. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  76610. + dwc_hc_t * hc,
  76611. + dwc_otg_hc_regs_t * hc_regs,
  76612. + dwc_otg_qtd_t * qtd)
  76613. +{
  76614. + hcchar_data_t hcchar;
  76615. + hcsplt_data_t hcsplt;
  76616. + hctsiz_data_t hctsiz;
  76617. + uint32_t hcdma;
  76618. + char *pipetype, *speed;
  76619. +
  76620. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  76621. +
  76622. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76623. + "AHB Error--\n", hc->hc_num);
  76624. +
  76625. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76626. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  76627. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76628. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  76629. +
  76630. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  76631. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  76632. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  76633. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  76634. + DWC_ERROR(" Device address: %d\n",
  76635. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  76636. + DWC_ERROR(" Endpoint: %d, %s\n",
  76637. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  76638. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  76639. +
  76640. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  76641. + case UE_CONTROL:
  76642. + pipetype = "CONTROL";
  76643. + break;
  76644. + case UE_BULK:
  76645. + pipetype = "BULK";
  76646. + break;
  76647. + case UE_INTERRUPT:
  76648. + pipetype = "INTERRUPT";
  76649. + break;
  76650. + case UE_ISOCHRONOUS:
  76651. + pipetype = "ISOCHRONOUS";
  76652. + break;
  76653. + default:
  76654. + pipetype = "UNKNOWN";
  76655. + break;
  76656. + }
  76657. +
  76658. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  76659. +
  76660. + switch (hc->speed) {
  76661. + case DWC_OTG_EP_SPEED_HIGH:
  76662. + speed = "HIGH";
  76663. + break;
  76664. + case DWC_OTG_EP_SPEED_FULL:
  76665. + speed = "FULL";
  76666. + break;
  76667. + case DWC_OTG_EP_SPEED_LOW:
  76668. + speed = "LOW";
  76669. + break;
  76670. + default:
  76671. + speed = "UNKNOWN";
  76672. + break;
  76673. + };
  76674. +
  76675. + DWC_ERROR(" Speed: %s\n", speed);
  76676. +
  76677. + DWC_ERROR(" Max packet size: %d\n",
  76678. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  76679. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  76680. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  76681. + urb->buf, (void *)urb->dma);
  76682. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  76683. + urb->setup_packet, (void *)urb->setup_dma);
  76684. + DWC_ERROR(" Interval: %d\n", urb->interval);
  76685. +
  76686. + /* Core haltes the channel for Descriptor DMA mode */
  76687. + if (hcd->core_if->dma_desc_enable) {
  76688. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76689. + DWC_OTG_HC_XFER_AHB_ERR);
  76690. + goto handle_ahberr_done;
  76691. + }
  76692. +
  76693. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  76694. +
  76695. + /*
  76696. + * Force a channel halt. Don't call halt_channel because that won't
  76697. + * write to the HCCHARn register in DMA mode to force the halt.
  76698. + */
  76699. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  76700. +handle_ahberr_done:
  76701. + disable_hc_int(hc_regs, ahberr);
  76702. + return 1;
  76703. +}
  76704. +
  76705. +/**
  76706. + * Handles a host channel transaction error interrupt. This handler may be
  76707. + * called in either DMA mode or Slave mode.
  76708. + */
  76709. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  76710. + dwc_hc_t * hc,
  76711. + dwc_otg_hc_regs_t * hc_regs,
  76712. + dwc_otg_qtd_t * qtd)
  76713. +{
  76714. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76715. + "Transaction Error--\n", hc->hc_num);
  76716. +
  76717. + if (hcd->core_if->dma_desc_enable) {
  76718. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76719. + DWC_OTG_HC_XFER_XACT_ERR);
  76720. + goto handle_xacterr_done;
  76721. + }
  76722. +
  76723. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76724. + case UE_CONTROL:
  76725. + case UE_BULK:
  76726. + qtd->error_count++;
  76727. + if (!hc->qh->ping_state) {
  76728. +
  76729. + update_urb_state_xfer_intr(hc, hc_regs,
  76730. + qtd->urb, qtd,
  76731. + DWC_OTG_HC_XFER_XACT_ERR);
  76732. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76733. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  76734. + hc->qh->ping_state = 1;
  76735. + }
  76736. + }
  76737. +
  76738. + /*
  76739. + * Halt the channel so the transfer can be re-started from
  76740. + * the appropriate point or the PING protocol will start.
  76741. + */
  76742. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76743. + break;
  76744. + case UE_INTERRUPT:
  76745. + qtd->error_count++;
  76746. + if (hc->do_split && hc->complete_split) {
  76747. + qtd->complete_split = 0;
  76748. + }
  76749. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76750. + break;
  76751. + case UE_ISOCHRONOUS:
  76752. + {
  76753. + dwc_otg_halt_status_e halt_status;
  76754. + halt_status =
  76755. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76756. + DWC_OTG_HC_XFER_XACT_ERR);
  76757. +
  76758. + halt_channel(hcd, hc, qtd, halt_status);
  76759. + }
  76760. + break;
  76761. + }
  76762. +handle_xacterr_done:
  76763. + disable_hc_int(hc_regs, xacterr);
  76764. +
  76765. + return 1;
  76766. +}
  76767. +
  76768. +/**
  76769. + * Handles a host channel frame overrun interrupt. This handler may be called
  76770. + * in either DMA mode or Slave mode.
  76771. + */
  76772. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  76773. + dwc_hc_t * hc,
  76774. + dwc_otg_hc_regs_t * hc_regs,
  76775. + dwc_otg_qtd_t * qtd)
  76776. +{
  76777. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76778. + "Frame Overrun--\n", hc->hc_num);
  76779. +
  76780. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76781. + case UE_CONTROL:
  76782. + case UE_BULK:
  76783. + break;
  76784. + case UE_INTERRUPT:
  76785. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  76786. + break;
  76787. + case UE_ISOCHRONOUS:
  76788. + {
  76789. + dwc_otg_halt_status_e halt_status;
  76790. + halt_status =
  76791. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76792. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  76793. +
  76794. + halt_channel(hcd, hc, qtd, halt_status);
  76795. + }
  76796. + break;
  76797. + }
  76798. +
  76799. + disable_hc_int(hc_regs, frmovrun);
  76800. +
  76801. + return 1;
  76802. +}
  76803. +
  76804. +/**
  76805. + * Handles a host channel data toggle error interrupt. This handler may be
  76806. + * called in either DMA mode or Slave mode.
  76807. + */
  76808. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  76809. + dwc_hc_t * hc,
  76810. + dwc_otg_hc_regs_t * hc_regs,
  76811. + dwc_otg_qtd_t * qtd)
  76812. +{
  76813. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76814. + "Data Toggle Error on %s transfer--\n",
  76815. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  76816. +
  76817. + /* Data toggles on split transactions cause the hc to halt.
  76818. + * restart transfer */
  76819. + if(hc->qh->do_split)
  76820. + {
  76821. + qtd->error_count++;
  76822. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76823. + update_urb_state_xfer_intr(hc, hc_regs,
  76824. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76825. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76826. + } else if (hc->ep_is_in) {
  76827. + /* An unmasked data toggle error on a non-split DMA transaction is
  76828. + * for the sole purpose of resetting error counts. Disable other
  76829. + * interrupts unmasked for the same reason.
  76830. + */
  76831. + if(hcd->core_if->dma_enable) {
  76832. + disable_hc_int(hc_regs, ack);
  76833. + disable_hc_int(hc_regs, nak);
  76834. + }
  76835. + qtd->error_count = 0;
  76836. + }
  76837. +
  76838. + disable_hc_int(hc_regs, datatglerr);
  76839. +
  76840. + return 1;
  76841. +}
  76842. +
  76843. +#ifdef DEBUG
  76844. +/**
  76845. + * This function is for debug only. It checks that a valid halt status is set
  76846. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  76847. + * taken and a warning is issued.
  76848. + * @return 1 if halt status is ok, 0 otherwise.
  76849. + */
  76850. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  76851. + dwc_hc_t * hc,
  76852. + dwc_otg_hc_regs_t * hc_regs,
  76853. + dwc_otg_qtd_t * qtd)
  76854. +{
  76855. + hcchar_data_t hcchar;
  76856. + hctsiz_data_t hctsiz;
  76857. + hcint_data_t hcint;
  76858. + hcintmsk_data_t hcintmsk;
  76859. + hcsplt_data_t hcsplt;
  76860. +
  76861. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  76862. + /*
  76863. + * This code is here only as a check. This condition should
  76864. + * never happen. Ignore the halt if it does occur.
  76865. + */
  76866. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76867. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76868. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  76869. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  76870. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  76871. + DWC_WARN
  76872. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  76873. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  76874. + "hcint 0x%08x, hcintmsk 0x%08x, "
  76875. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  76876. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  76877. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  76878. +
  76879. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  76880. + __func__, hc->hc_num);
  76881. + DWC_WARN("\n");
  76882. + clear_hc_int(hc_regs, chhltd);
  76883. + return 0;
  76884. + }
  76885. +
  76886. + /*
  76887. + * This code is here only as a check. hcchar.chdis should
  76888. + * never be set when the halt interrupt occurs. Halt the
  76889. + * channel again if it does occur.
  76890. + */
  76891. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76892. + if (hcchar.b.chdis) {
  76893. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  76894. + "hcchar 0x%08x, trying to halt again\n",
  76895. + __func__, hcchar.d32);
  76896. + clear_hc_int(hc_regs, chhltd);
  76897. + hc->halt_pending = 0;
  76898. + halt_channel(hcd, hc, qtd, hc->halt_status);
  76899. + return 0;
  76900. + }
  76901. +
  76902. + return 1;
  76903. +}
  76904. +#endif
  76905. +
  76906. +/**
  76907. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  76908. + * determines the reason the channel halted and proceeds accordingly.
  76909. + */
  76910. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  76911. + dwc_hc_t * hc,
  76912. + dwc_otg_hc_regs_t * hc_regs,
  76913. + dwc_otg_qtd_t * qtd,
  76914. + hcint_data_t hcint,
  76915. + hcintmsk_data_t hcintmsk)
  76916. +{
  76917. + int out_nak_enh = 0;
  76918. +
  76919. + /* For core with OUT NAK enhancement, the flow for high-
  76920. + * speed CONTROL/BULK OUT is handled a little differently.
  76921. + */
  76922. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  76923. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  76924. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  76925. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  76926. + out_nak_enh = 1;
  76927. + }
  76928. + }
  76929. +
  76930. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  76931. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  76932. + && !hcd->core_if->dma_desc_enable)) {
  76933. + /*
  76934. + * Just release the channel. A dequeue can happen on a
  76935. + * transfer timeout. In the case of an AHB Error, the channel
  76936. + * was forced to halt because there's no way to gracefully
  76937. + * recover.
  76938. + */
  76939. + if (hcd->core_if->dma_desc_enable)
  76940. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76941. + hc->halt_status);
  76942. + else
  76943. + release_channel(hcd, hc, qtd, hc->halt_status);
  76944. + return;
  76945. + }
  76946. +
  76947. + /* Read the HCINTn register to determine the cause for the halt. */
  76948. + if(!fiq_split_enable)
  76949. + {
  76950. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  76951. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  76952. + }
  76953. +
  76954. + if (hcint.b.xfercomp) {
  76955. + /** @todo This is here because of a possible hardware bug. Spec
  76956. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  76957. + * interrupt w/ACK bit set should occur, but I only see the
  76958. + * XFERCOMP bit, even with it masked out. This is a workaround
  76959. + * for that behavior. Should fix this when hardware is fixed.
  76960. + */
  76961. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  76962. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  76963. + }
  76964. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  76965. + } else if (hcint.b.stall) {
  76966. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  76967. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  76968. + if (out_nak_enh) {
  76969. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  76970. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  76971. + qtd->error_count = 0;
  76972. + } else {
  76973. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  76974. + }
  76975. + }
  76976. +
  76977. + /*
  76978. + * Must handle xacterr before nak or ack. Could get a xacterr
  76979. + * at the same time as either of these on a BULK/CONTROL OUT
  76980. + * that started with a PING. The xacterr takes precedence.
  76981. + */
  76982. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  76983. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  76984. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  76985. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  76986. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  76987. + } else if (hcint.b.bblerr) {
  76988. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  76989. + } else if (hcint.b.frmovrun) {
  76990. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  76991. + } else if (hcint.b.datatglerr) {
  76992. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  76993. + } else if (!out_nak_enh) {
  76994. + if (hcint.b.nyet) {
  76995. + /*
  76996. + * Must handle nyet before nak or ack. Could get a nyet at the
  76997. + * same time as either of those on a BULK/CONTROL OUT that
  76998. + * started with a PING. The nyet takes precedence.
  76999. + */
  77000. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  77001. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  77002. + /*
  77003. + * If nak is not masked, it's because a non-split IN transfer
  77004. + * is in an error state. In that case, the nak is handled by
  77005. + * the nak interrupt handler, not here. Handle nak here for
  77006. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  77007. + * rewinding the buffer pointer.
  77008. + */
  77009. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  77010. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  77011. + /*
  77012. + * If ack is not masked, it's because a non-split IN transfer
  77013. + * is in an error state. In that case, the ack is handled by
  77014. + * the ack interrupt handler, not here. Handle ack here for
  77015. + * split transfers. Start splits halt on ACK.
  77016. + */
  77017. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  77018. + } else {
  77019. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  77020. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  77021. + /*
  77022. + * A periodic transfer halted with no other channel
  77023. + * interrupts set. Assume it was halted by the core
  77024. + * because it could not be completed in its scheduled
  77025. + * (micro)frame.
  77026. + */
  77027. +#ifdef DEBUG
  77028. + DWC_PRINTF
  77029. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  77030. + __func__, hc->hc_num);
  77031. +#endif
  77032. + halt_channel(hcd, hc, qtd,
  77033. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  77034. + } else {
  77035. + DWC_ERROR
  77036. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  77037. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  77038. + __func__, hc->hc_num, hcint.d32,
  77039. + DWC_READ_REG32(&hcd->
  77040. + core_if->core_global_regs->
  77041. + gintsts));
  77042. + /* Failthrough: use 3-strikes rule */
  77043. + qtd->error_count++;
  77044. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  77045. + update_urb_state_xfer_intr(hc, hc_regs,
  77046. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77047. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77048. + }
  77049. +
  77050. + }
  77051. + } else {
  77052. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  77053. + hcint.d32);
  77054. + /* Failthrough: use 3-strikes rule */
  77055. + qtd->error_count++;
  77056. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  77057. + update_urb_state_xfer_intr(hc, hc_regs,
  77058. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77059. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77060. + }
  77061. +}
  77062. +
  77063. +/**
  77064. + * Handles a host channel Channel Halted interrupt.
  77065. + *
  77066. + * In slave mode, this handler is called only when the driver specifically
  77067. + * requests a halt. This occurs during handling other host channel interrupts
  77068. + * (e.g. nak, xacterr, stall, nyet, etc.).
  77069. + *
  77070. + * In DMA mode, this is the interrupt that occurs when the core has finished
  77071. + * processing a transfer on a channel. Other host channel interrupts (except
  77072. + * ahberr) are disabled in DMA mode.
  77073. + */
  77074. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  77075. + dwc_hc_t * hc,
  77076. + dwc_otg_hc_regs_t * hc_regs,
  77077. + dwc_otg_qtd_t * qtd,
  77078. + hcint_data_t hcint,
  77079. + hcintmsk_data_t hcintmsk)
  77080. +{
  77081. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  77082. + "Channel Halted--\n", hc->hc_num);
  77083. +
  77084. + if (hcd->core_if->dma_enable) {
  77085. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd, hcint, hcintmsk);
  77086. + } else {
  77087. +#ifdef DEBUG
  77088. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  77089. + return 1;
  77090. + }
  77091. +#endif
  77092. + release_channel(hcd, hc, qtd, hc->halt_status);
  77093. + }
  77094. +
  77095. + return 1;
  77096. +}
  77097. +
  77098. +/** Handles interrupt for a specific Host Channel */
  77099. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  77100. +{
  77101. + int retval = 0;
  77102. + hcint_data_t hcint, hcint_orig;
  77103. + hcintmsk_data_t hcintmsk;
  77104. + dwc_hc_t *hc;
  77105. + dwc_otg_hc_regs_t *hc_regs;
  77106. + dwc_otg_qtd_t *qtd;
  77107. +
  77108. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  77109. +
  77110. + hc = dwc_otg_hcd->hc_ptr_array[num];
  77111. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  77112. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  77113. + /* We are responding to a channel disable. Driver
  77114. + * state is cleared - our qtd has gone away.
  77115. + */
  77116. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  77117. + return 1;
  77118. + }
  77119. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  77120. +
  77121. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  77122. + hcint_orig = hcint;
  77123. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  77124. + DWC_DEBUGPL(DBG_HCDV,
  77125. + " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  77126. + hcint.d32, hcintmsk.d32, (hcint.d32 & hcintmsk.d32));
  77127. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  77128. +
  77129. + if(fiq_split_enable)
  77130. + {
  77131. + // replace with the saved interrupts from the fiq handler
  77132. + local_fiq_disable();
  77133. + hcint_orig.d32 = hcint_saved[num].d32;
  77134. + hcint.d32 = hcint_orig.d32 & hcintmsk_saved[num].d32;
  77135. + hcint_saved[num].d32 = 0;
  77136. + local_fiq_enable();
  77137. + }
  77138. +
  77139. + if (!dwc_otg_hcd->core_if->dma_enable) {
  77140. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  77141. + hcint.b.chhltd = 0;
  77142. + }
  77143. + }
  77144. +
  77145. + if (hcint.b.xfercomp) {
  77146. + retval |=
  77147. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77148. + /*
  77149. + * If NYET occurred at same time as Xfer Complete, the NYET is
  77150. + * handled by the Xfer Complete interrupt handler. Don't want
  77151. + * to call the NYET interrupt handler in this case.
  77152. + */
  77153. + hcint.b.nyet = 0;
  77154. + }
  77155. + if (hcint.b.chhltd) {
  77156. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd, hcint_orig, hcintmsk_saved[num]);
  77157. + }
  77158. + if (hcint.b.ahberr) {
  77159. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77160. + }
  77161. + if (hcint.b.stall) {
  77162. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77163. + }
  77164. + if (hcint.b.nak) {
  77165. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77166. + }
  77167. + if (hcint.b.ack) {
  77168. + if(!hcint.b.chhltd)
  77169. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77170. + }
  77171. + if (hcint.b.nyet) {
  77172. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77173. + }
  77174. + if (hcint.b.xacterr) {
  77175. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77176. + }
  77177. + if (hcint.b.bblerr) {
  77178. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77179. + }
  77180. + if (hcint.b.frmovrun) {
  77181. + retval |=
  77182. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77183. + }
  77184. + if (hcint.b.datatglerr) {
  77185. + retval |=
  77186. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77187. + }
  77188. +
  77189. + return retval;
  77190. +}
  77191. +#endif /* DWC_DEVICE_ONLY */
  77192. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  77193. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  77194. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 2014-03-11 17:51:27.000000000 +0100
  77195. @@ -0,0 +1,972 @@
  77196. +
  77197. +/* ==========================================================================
  77198. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  77199. + * $Revision: #20 $
  77200. + * $Date: 2011/10/26 $
  77201. + * $Change: 1872981 $
  77202. + *
  77203. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  77204. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  77205. + * otherwise expressly agreed to in writing between Synopsys and you.
  77206. + *
  77207. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  77208. + * any End User Software License Agreement or Agreement for Licensed Product
  77209. + * with Synopsys or any supplement thereto. You are permitted to use and
  77210. + * redistribute this Software in source and binary forms, with or without
  77211. + * modification, provided that redistributions of source code must retain this
  77212. + * notice. You may not view, use, disclose, copy or distribute this file or
  77213. + * any information contained herein except pursuant to this license grant from
  77214. + * Synopsys. If you do not agree with this notice, including the disclaimer
  77215. + * below, then you are not authorized to use the Software.
  77216. + *
  77217. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  77218. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  77219. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  77220. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  77221. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  77222. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  77223. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  77224. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  77225. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  77226. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  77227. + * DAMAGE.
  77228. + * ========================================================================== */
  77229. +#ifndef DWC_DEVICE_ONLY
  77230. +
  77231. +/**
  77232. + * @file
  77233. + *
  77234. + * This file contains the implementation of the HCD. In Linux, the HCD
  77235. + * implements the hc_driver API.
  77236. + */
  77237. +#include <linux/kernel.h>
  77238. +#include <linux/module.h>
  77239. +#include <linux/moduleparam.h>
  77240. +#include <linux/init.h>
  77241. +#include <linux/device.h>
  77242. +#include <linux/errno.h>
  77243. +#include <linux/list.h>
  77244. +#include <linux/interrupt.h>
  77245. +#include <linux/string.h>
  77246. +#include <linux/dma-mapping.h>
  77247. +#include <linux/version.h>
  77248. +#include <asm/io.h>
  77249. +#include <asm/fiq.h>
  77250. +#include <linux/usb.h>
  77251. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  77252. +#include <../drivers/usb/core/hcd.h>
  77253. +#else
  77254. +#include <linux/usb/hcd.h>
  77255. +#endif
  77256. +
  77257. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  77258. +#define USB_URB_EP_LINKING 1
  77259. +#else
  77260. +#define USB_URB_EP_LINKING 0
  77261. +#endif
  77262. +
  77263. +#include "dwc_otg_hcd_if.h"
  77264. +#include "dwc_otg_dbg.h"
  77265. +#include "dwc_otg_driver.h"
  77266. +#include "dwc_otg_hcd.h"
  77267. +#include "dwc_otg_mphi_fix.h"
  77268. +
  77269. +/**
  77270. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  77271. + * qualified with its direction (possible 32 endpoints per device).
  77272. + */
  77273. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  77274. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  77275. +
  77276. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  77277. +
  77278. +extern bool fiq_fix_enable;
  77279. +
  77280. +/** @name Linux HC Driver API Functions */
  77281. +/** @{ */
  77282. +/* manage i/o requests, device state */
  77283. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  77284. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77285. + struct usb_host_endpoint *ep,
  77286. +#endif
  77287. + struct urb *urb, gfp_t mem_flags);
  77288. +
  77289. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  77290. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77291. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  77292. +#endif
  77293. +#else /* kernels at or post 2.6.30 */
  77294. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  77295. + struct urb *urb, int status);
  77296. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  77297. +
  77298. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  77299. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  77300. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  77301. +#endif
  77302. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  77303. +extern int hcd_start(struct usb_hcd *hcd);
  77304. +extern void hcd_stop(struct usb_hcd *hcd);
  77305. +static int get_frame_number(struct usb_hcd *hcd);
  77306. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  77307. +extern int hub_control(struct usb_hcd *hcd,
  77308. + u16 typeReq,
  77309. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  77310. +
  77311. +struct wrapper_priv_data {
  77312. + dwc_otg_hcd_t *dwc_otg_hcd;
  77313. +};
  77314. +
  77315. +/** @} */
  77316. +
  77317. +static struct hc_driver dwc_otg_hc_driver = {
  77318. +
  77319. + .description = dwc_otg_hcd_name,
  77320. + .product_desc = "DWC OTG Controller",
  77321. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  77322. +
  77323. + .irq = dwc_otg_hcd_irq,
  77324. +
  77325. + .flags = HCD_MEMORY | HCD_USB2,
  77326. +
  77327. + //.reset =
  77328. + .start = hcd_start,
  77329. + //.suspend =
  77330. + //.resume =
  77331. + .stop = hcd_stop,
  77332. +
  77333. + .urb_enqueue = dwc_otg_urb_enqueue,
  77334. + .urb_dequeue = dwc_otg_urb_dequeue,
  77335. + .endpoint_disable = endpoint_disable,
  77336. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  77337. + .endpoint_reset = endpoint_reset,
  77338. +#endif
  77339. + .get_frame_number = get_frame_number,
  77340. +
  77341. + .hub_status_data = hub_status_data,
  77342. + .hub_control = hub_control,
  77343. + //.bus_suspend =
  77344. + //.bus_resume =
  77345. +};
  77346. +
  77347. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  77348. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  77349. +{
  77350. + struct wrapper_priv_data *p;
  77351. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  77352. + return p->dwc_otg_hcd;
  77353. +}
  77354. +
  77355. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  77356. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  77357. +{
  77358. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  77359. +}
  77360. +
  77361. +/** Gets the usb_host_endpoint associated with an URB. */
  77362. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  77363. +{
  77364. + struct usb_device *dev = urb->dev;
  77365. + int ep_num = usb_pipeendpoint(urb->pipe);
  77366. +
  77367. + if (usb_pipein(urb->pipe))
  77368. + return dev->ep_in[ep_num];
  77369. + else
  77370. + return dev->ep_out[ep_num];
  77371. +}
  77372. +
  77373. +static int _disconnect(dwc_otg_hcd_t * hcd)
  77374. +{
  77375. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  77376. +
  77377. + usb_hcd->self.is_b_host = 0;
  77378. + return 0;
  77379. +}
  77380. +
  77381. +static int _start(dwc_otg_hcd_t * hcd)
  77382. +{
  77383. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  77384. +
  77385. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  77386. + hcd_start(usb_hcd);
  77387. +
  77388. + return 0;
  77389. +}
  77390. +
  77391. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  77392. + uint32_t * port_addr)
  77393. +{
  77394. + struct urb *urb = (struct urb *)urb_handle;
  77395. + struct usb_bus *bus;
  77396. +#if 1 //GRAYG - temporary
  77397. + if (NULL == urb_handle)
  77398. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  77399. + if (NULL == urb->dev)
  77400. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  77401. + if (NULL == port_addr)
  77402. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  77403. +#endif
  77404. + if (urb->dev->tt) {
  77405. + if (NULL == urb->dev->tt->hub) {
  77406. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  77407. + __func__); //GRAYG
  77408. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  77409. + *hub_addr = 0; //GRAYG
  77410. + // we probably shouldn't have a transaction translator if
  77411. + // there's no associated hub?
  77412. + } else {
  77413. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  77414. + if (urb->dev->tt->hub == bus->root_hub)
  77415. + *hub_addr = 0;
  77416. + else
  77417. + *hub_addr = urb->dev->tt->hub->devnum;
  77418. + }
  77419. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  77420. + } else {
  77421. + *hub_addr = 0;
  77422. + *port_addr = urb->dev->ttport;
  77423. + }
  77424. + return 0;
  77425. +}
  77426. +
  77427. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  77428. +{
  77429. + struct urb *urb = (struct urb *)urb_handle;
  77430. + return urb->dev->speed;
  77431. +}
  77432. +
  77433. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  77434. +{
  77435. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  77436. + return usb_hcd->self.b_hnp_enable;
  77437. +}
  77438. +
  77439. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  77440. + struct urb *urb)
  77441. +{
  77442. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  77443. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77444. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  77445. + } else {
  77446. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  77447. + }
  77448. +}
  77449. +
  77450. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  77451. + struct urb *urb)
  77452. +{
  77453. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  77454. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77455. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  77456. + } else {
  77457. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  77458. + }
  77459. +}
  77460. +
  77461. +/**
  77462. + * Sets the final status of an URB and returns it to the device driver. Any
  77463. + * required cleanup of the URB is performed. The HCD lock should be held on
  77464. + * entry.
  77465. + */
  77466. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  77467. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  77468. +{
  77469. + struct urb *urb = (struct urb *)urb_handle;
  77470. + urb_tq_entry_t *new_entry;
  77471. + int rc = 0;
  77472. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77473. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  77474. + __func__, urb, usb_pipedevice(urb->pipe),
  77475. + usb_pipeendpoint(urb->pipe),
  77476. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  77477. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77478. + int i;
  77479. + for (i = 0; i < urb->number_of_packets; i++) {
  77480. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  77481. + i, urb->iso_frame_desc[i].status);
  77482. + }
  77483. + }
  77484. + }
  77485. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  77486. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  77487. + /* Convert status value. */
  77488. + switch (status) {
  77489. + case -DWC_E_PROTOCOL:
  77490. + status = -EPROTO;
  77491. + break;
  77492. + case -DWC_E_IN_PROGRESS:
  77493. + status = -EINPROGRESS;
  77494. + break;
  77495. + case -DWC_E_PIPE:
  77496. + status = -EPIPE;
  77497. + break;
  77498. + case -DWC_E_IO:
  77499. + status = -EIO;
  77500. + break;
  77501. + case -DWC_E_TIMEOUT:
  77502. + status = -ETIMEDOUT;
  77503. + break;
  77504. + case -DWC_E_OVERFLOW:
  77505. + status = -EOVERFLOW;
  77506. + break;
  77507. + case -DWC_E_SHUTDOWN:
  77508. + status = -ESHUTDOWN;
  77509. + break;
  77510. + default:
  77511. + if (status) {
  77512. + DWC_PRINTF("Uknown urb status %d\n", status);
  77513. +
  77514. + }
  77515. + }
  77516. +
  77517. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77518. + int i;
  77519. +
  77520. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  77521. + for (i = 0; i < urb->number_of_packets; ++i) {
  77522. + urb->iso_frame_desc[i].actual_length =
  77523. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  77524. + (dwc_otg_urb, i);
  77525. + urb->iso_frame_desc[i].status =
  77526. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  77527. + }
  77528. + }
  77529. +
  77530. + urb->status = status;
  77531. + urb->hcpriv = NULL;
  77532. + if (!status) {
  77533. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  77534. + (urb->actual_length < urb->transfer_buffer_length)) {
  77535. + urb->status = -EREMOTEIO;
  77536. + }
  77537. + }
  77538. +
  77539. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  77540. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  77541. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  77542. + if (ep) {
  77543. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  77544. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  77545. + ep->hcpriv),
  77546. + urb);
  77547. + }
  77548. + }
  77549. +
  77550. + DWC_FREE(dwc_otg_urb);
  77551. + if (!new_entry) {
  77552. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  77553. + urb->status = -EPROTO;
  77554. + /* don't schedule the tasklet -
  77555. + * directly return the packet here with error. */
  77556. +#if USB_URB_EP_LINKING
  77557. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  77558. +#endif
  77559. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77560. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  77561. +#else
  77562. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  77563. +#endif
  77564. + } else {
  77565. + new_entry->urb = urb;
  77566. +#if USB_URB_EP_LINKING
  77567. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  77568. + if(0 == rc) {
  77569. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  77570. + }
  77571. +#endif
  77572. + if(0 == rc) {
  77573. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  77574. + urb_tq_entries);
  77575. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  77576. + }
  77577. + }
  77578. + return 0;
  77579. +}
  77580. +
  77581. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  77582. + .start = _start,
  77583. + .disconnect = _disconnect,
  77584. + .hub_info = _hub_info,
  77585. + .speed = _speed,
  77586. + .complete = _complete,
  77587. + .get_b_hnp_enable = _get_b_hnp_enable,
  77588. +};
  77589. +
  77590. +static struct fiq_handler fh = {
  77591. + .name = "usb_fiq",
  77592. +};
  77593. +struct fiq_stack_s {
  77594. + int magic1;
  77595. + uint8_t stack[2048];
  77596. + int magic2;
  77597. +} fiq_stack;
  77598. +
  77599. +extern mphi_regs_t c_mphi_regs;
  77600. +/**
  77601. + * Initializes the HCD. This function allocates memory for and initializes the
  77602. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  77603. + * USB bus with the core and calls the hc_driver->start() function. It returns
  77604. + * a negative error on failure.
  77605. + */
  77606. +int hcd_init(dwc_bus_dev_t *_dev)
  77607. +{
  77608. + struct usb_hcd *hcd = NULL;
  77609. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  77610. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  77611. + int retval = 0;
  77612. + u64 dmamask;
  77613. + struct pt_regs regs;
  77614. +
  77615. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  77616. +
  77617. + /* Set device flags indicating whether the HCD supports DMA. */
  77618. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  77619. + dmamask = DMA_BIT_MASK(32);
  77620. + else
  77621. + dmamask = 0;
  77622. +
  77623. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  77624. + dma_set_mask(&_dev->dev, dmamask);
  77625. + dma_set_coherent_mask(&_dev->dev, dmamask);
  77626. +#elif defined(PCI_INTERFACE)
  77627. + pci_set_dma_mask(_dev, dmamask);
  77628. + pci_set_consistent_dma_mask(_dev, dmamask);
  77629. +#endif
  77630. +
  77631. + if (fiq_fix_enable)
  77632. + {
  77633. + // Set up fiq
  77634. + claim_fiq(&fh);
  77635. + set_fiq_handler(__FIQ_Branch, 4);
  77636. + memset(&regs,0,sizeof(regs));
  77637. + regs.ARM_r8 = (long)dwc_otg_hcd_handle_fiq;
  77638. + regs.ARM_r9 = (long)0;
  77639. + regs.ARM_sp = (long)fiq_stack.stack + sizeof(fiq_stack.stack) - 4;
  77640. + set_fiq_regs(&regs);
  77641. + fiq_stack.magic1 = 0xdeadbeef;
  77642. + fiq_stack.magic2 = 0xaa995566;
  77643. + }
  77644. +
  77645. + /*
  77646. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  77647. + * Initialize the base HCD.
  77648. + */
  77649. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  77650. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  77651. +#else
  77652. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  77653. + hcd->has_tt = 1;
  77654. +// hcd->uses_new_polling = 1;
  77655. +// hcd->poll_rh = 0;
  77656. +#endif
  77657. + if (!hcd) {
  77658. + retval = -ENOMEM;
  77659. + goto error1;
  77660. + }
  77661. +
  77662. + hcd->regs = otg_dev->os_dep.base;
  77663. +
  77664. + if (fiq_fix_enable)
  77665. + {
  77666. + volatile extern void *dwc_regs_base;
  77667. +
  77668. + //Set the mphi periph to the required registers
  77669. + c_mphi_regs.base = otg_dev->os_dep.mphi_base;
  77670. + c_mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  77671. + c_mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  77672. + c_mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  77673. + c_mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  77674. +
  77675. + dwc_regs_base = otg_dev->os_dep.base;
  77676. +
  77677. + //Enable mphi peripheral
  77678. + writel((1<<31),c_mphi_regs.ctrl);
  77679. +#ifdef DEBUG
  77680. + if (readl(c_mphi_regs.ctrl) & 0x80000000)
  77681. + DWC_DEBUGPL(DBG_USER, "MPHI periph has been enabled\n");
  77682. + else
  77683. + DWC_DEBUGPL(DBG_USER, "MPHI periph has NOT been enabled\n");
  77684. +#endif
  77685. + // Enable FIQ interrupt from USB peripheral
  77686. + enable_fiq(INTERRUPT_VC_USB);
  77687. + }
  77688. + /* Initialize the DWC OTG HCD. */
  77689. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  77690. + if (!dwc_otg_hcd) {
  77691. + goto error2;
  77692. + }
  77693. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  77694. + dwc_otg_hcd;
  77695. + otg_dev->hcd = dwc_otg_hcd;
  77696. +
  77697. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  77698. + goto error2;
  77699. + }
  77700. +
  77701. + otg_dev->hcd->otg_dev = otg_dev;
  77702. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  77703. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  77704. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  77705. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  77706. +#endif
  77707. + /* Don't support SG list at this point */
  77708. + hcd->self.sg_tablesize = 0;
  77709. +#endif
  77710. + /*
  77711. + * Finish generic HCD initialization and start the HCD. This function
  77712. + * allocates the DMA buffer pool, registers the USB bus, requests the
  77713. + * IRQ line, and calls hcd_start method.
  77714. + */
  77715. +#ifdef PLATFORM_INTERFACE
  77716. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, 0), IRQF_SHARED | IRQF_DISABLED);
  77717. +#else
  77718. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED | IRQF_DISABLED);
  77719. +#endif
  77720. + if (retval < 0) {
  77721. + goto error2;
  77722. + }
  77723. +
  77724. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  77725. + return 0;
  77726. +
  77727. +error2:
  77728. + usb_put_hcd(hcd);
  77729. +error1:
  77730. + return retval;
  77731. +}
  77732. +
  77733. +/**
  77734. + * Removes the HCD.
  77735. + * Frees memory and resources associated with the HCD and deregisters the bus.
  77736. + */
  77737. +void hcd_remove(dwc_bus_dev_t *_dev)
  77738. +{
  77739. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  77740. + dwc_otg_hcd_t *dwc_otg_hcd;
  77741. + struct usb_hcd *hcd;
  77742. +
  77743. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  77744. +
  77745. + if (!otg_dev) {
  77746. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  77747. + return;
  77748. + }
  77749. +
  77750. + dwc_otg_hcd = otg_dev->hcd;
  77751. +
  77752. + if (!dwc_otg_hcd) {
  77753. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  77754. + return;
  77755. + }
  77756. +
  77757. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  77758. +
  77759. + if (!hcd) {
  77760. + DWC_DEBUGPL(DBG_ANY,
  77761. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  77762. + __func__);
  77763. + return;
  77764. + }
  77765. + usb_remove_hcd(hcd);
  77766. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  77767. + dwc_otg_hcd_remove(dwc_otg_hcd);
  77768. + usb_put_hcd(hcd);
  77769. +}
  77770. +
  77771. +/* =========================================================================
  77772. + * Linux HC Driver Functions
  77773. + * ========================================================================= */
  77774. +
  77775. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  77776. + * mode operation. Activates the root port. Returns 0 on success and a negative
  77777. + * error code on failure. */
  77778. +int hcd_start(struct usb_hcd *hcd)
  77779. +{
  77780. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77781. + struct usb_bus *bus;
  77782. +
  77783. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  77784. + bus = hcd_to_bus(hcd);
  77785. +
  77786. + hcd->state = HC_STATE_RUNNING;
  77787. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  77788. + return 0;
  77789. + }
  77790. +
  77791. + /* Initialize and connect root hub if one is not already attached */
  77792. + if (bus->root_hub) {
  77793. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  77794. + /* Inform the HUB driver to resume. */
  77795. + usb_hcd_resume_root_hub(hcd);
  77796. + }
  77797. +
  77798. + return 0;
  77799. +}
  77800. +
  77801. +/**
  77802. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  77803. + * stopped.
  77804. + */
  77805. +void hcd_stop(struct usb_hcd *hcd)
  77806. +{
  77807. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77808. +
  77809. + dwc_otg_hcd_stop(dwc_otg_hcd);
  77810. +}
  77811. +
  77812. +/** Returns the current frame number. */
  77813. +static int get_frame_number(struct usb_hcd *hcd)
  77814. +{
  77815. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77816. +
  77817. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  77818. +}
  77819. +
  77820. +#ifdef DEBUG
  77821. +static void dump_urb_info(struct urb *urb, char *fn_name)
  77822. +{
  77823. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  77824. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  77825. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  77826. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  77827. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  77828. + char *pipetype;
  77829. + switch (usb_pipetype(urb->pipe)) {
  77830. +case PIPE_CONTROL:
  77831. +pipetype = "CONTROL"; break; case PIPE_BULK:
  77832. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  77833. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  77834. +pipetype = "ISOCHRONOUS"; break; default:
  77835. + pipetype = "UNKNOWN"; break;};
  77836. + pipetype;}
  77837. + )) ;
  77838. + DWC_PRINTF(" Speed: %s\n", ( {
  77839. + char *speed; switch (urb->dev->speed) {
  77840. +case USB_SPEED_HIGH:
  77841. +speed = "HIGH"; break; case USB_SPEED_FULL:
  77842. +speed = "FULL"; break; case USB_SPEED_LOW:
  77843. +speed = "LOW"; break; default:
  77844. + speed = "UNKNOWN"; break;};
  77845. + speed;}
  77846. + )) ;
  77847. + DWC_PRINTF(" Max packet size: %d\n",
  77848. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  77849. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  77850. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  77851. + urb->transfer_buffer, (void *)urb->transfer_dma);
  77852. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  77853. + urb->setup_packet, (void *)urb->setup_dma);
  77854. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  77855. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77856. + int i;
  77857. + for (i = 0; i < urb->number_of_packets; i++) {
  77858. + DWC_PRINTF(" ISO Desc %d:\n", i);
  77859. + DWC_PRINTF(" offset: %d, length %d\n",
  77860. + urb->iso_frame_desc[i].offset,
  77861. + urb->iso_frame_desc[i].length);
  77862. + }
  77863. + }
  77864. +}
  77865. +#endif
  77866. +
  77867. +/** Starts processing a USB transfer request specified by a USB Request Block
  77868. + * (URB). mem_flags indicates the type of memory allocation to use while
  77869. + * processing this URB. */
  77870. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  77871. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77872. + struct usb_host_endpoint *ep,
  77873. +#endif
  77874. + struct urb *urb, gfp_t mem_flags)
  77875. +{
  77876. + int retval = 0;
  77877. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  77878. + struct usb_host_endpoint *ep = urb->ep;
  77879. +#endif
  77880. + dwc_irqflags_t irqflags;
  77881. + void **ref_ep_hcpriv = &ep->hcpriv;
  77882. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77883. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  77884. + int i;
  77885. + int alloc_bandwidth = 0;
  77886. + uint8_t ep_type = 0;
  77887. + uint32_t flags = 0;
  77888. + void *buf;
  77889. +
  77890. +#ifdef DEBUG
  77891. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77892. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  77893. + }
  77894. +#endif
  77895. +
  77896. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  77897. + return -EINVAL;
  77898. +
  77899. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  77900. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  77901. + if (!dwc_otg_hcd_is_bandwidth_allocated
  77902. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  77903. + alloc_bandwidth = 1;
  77904. + }
  77905. + }
  77906. +
  77907. + switch (usb_pipetype(urb->pipe)) {
  77908. + case PIPE_CONTROL:
  77909. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  77910. + break;
  77911. + case PIPE_ISOCHRONOUS:
  77912. + ep_type = USB_ENDPOINT_XFER_ISOC;
  77913. + break;
  77914. + case PIPE_BULK:
  77915. + ep_type = USB_ENDPOINT_XFER_BULK;
  77916. + break;
  77917. + case PIPE_INTERRUPT:
  77918. + ep_type = USB_ENDPOINT_XFER_INT;
  77919. + break;
  77920. + default:
  77921. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  77922. + }
  77923. +
  77924. + /* # of packets is often 0 - do we really need to call this then? */
  77925. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  77926. + urb->number_of_packets,
  77927. + mem_flags == GFP_ATOMIC ? 1 : 0);
  77928. +
  77929. + if(dwc_otg_urb == NULL)
  77930. + return -ENOMEM;
  77931. +
  77932. + if (!dwc_otg_urb && urb->number_of_packets)
  77933. + return -ENOMEM;
  77934. +
  77935. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  77936. + usb_pipeendpoint(urb->pipe), ep_type,
  77937. + usb_pipein(urb->pipe),
  77938. + usb_maxpacket(urb->dev, urb->pipe,
  77939. + !(usb_pipein(urb->pipe))));
  77940. +
  77941. + buf = urb->transfer_buffer;
  77942. + if (hcd->self.uses_dma) {
  77943. + /*
  77944. + * Calculate virtual address from physical address,
  77945. + * because some class driver may not fill transfer_buffer.
  77946. + * In Buffer DMA mode virual address is used,
  77947. + * when handling non DWORD aligned buffers.
  77948. + */
  77949. + //buf = phys_to_virt(urb->transfer_dma);
  77950. + // DMA addresses are bus addresses not physical addresses!
  77951. + buf = dma_to_virt(&urb->dev->dev, urb->transfer_dma);
  77952. + }
  77953. +
  77954. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  77955. + flags |= URB_GIVEBACK_ASAP;
  77956. + if (urb->transfer_flags & URB_ZERO_PACKET)
  77957. + flags |= URB_SEND_ZERO_PACKET;
  77958. +
  77959. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  77960. + urb->transfer_dma,
  77961. + urb->transfer_buffer_length,
  77962. + urb->setup_packet,
  77963. + urb->setup_dma, flags, urb->interval);
  77964. +
  77965. + for (i = 0; i < urb->number_of_packets; ++i) {
  77966. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  77967. + urb->
  77968. + iso_frame_desc[i].offset,
  77969. + urb->
  77970. + iso_frame_desc[i].length);
  77971. + }
  77972. +
  77973. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  77974. + urb->hcpriv = dwc_otg_urb;
  77975. +#if USB_URB_EP_LINKING
  77976. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  77977. + if (0 == retval)
  77978. +#endif
  77979. + {
  77980. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  77981. + /*(dwc_otg_qh_t **)*/
  77982. + ref_ep_hcpriv, 1);
  77983. + if (0 == retval) {
  77984. + if (alloc_bandwidth) {
  77985. + allocate_bus_bandwidth(hcd,
  77986. + dwc_otg_hcd_get_ep_bandwidth(
  77987. + dwc_otg_hcd, *ref_ep_hcpriv),
  77988. + urb);
  77989. + }
  77990. + } else {
  77991. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  77992. +#if USB_URB_EP_LINKING
  77993. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  77994. +#endif
  77995. + DWC_FREE(dwc_otg_urb);
  77996. + urb->hcpriv = NULL;
  77997. + if (retval == -DWC_E_NO_DEVICE)
  77998. + retval = -ENODEV;
  77999. + }
  78000. + }
  78001. +#if USB_URB_EP_LINKING
  78002. + else
  78003. + {
  78004. + DWC_FREE(dwc_otg_urb);
  78005. + urb->hcpriv = NULL;
  78006. + }
  78007. +#endif
  78008. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  78009. + return retval;
  78010. +}
  78011. +
  78012. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  78013. + * success. */
  78014. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  78015. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  78016. +#else
  78017. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  78018. +#endif
  78019. +{
  78020. + dwc_irqflags_t flags;
  78021. + dwc_otg_hcd_t *dwc_otg_hcd;
  78022. + int rc;
  78023. +
  78024. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  78025. +
  78026. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78027. +
  78028. +#ifdef DEBUG
  78029. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  78030. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  78031. + }
  78032. +#endif
  78033. +
  78034. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  78035. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  78036. + if (0 == rc) {
  78037. + if(urb->hcpriv != NULL) {
  78038. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  78039. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  78040. +
  78041. + DWC_FREE(urb->hcpriv);
  78042. + urb->hcpriv = NULL;
  78043. + }
  78044. + }
  78045. +
  78046. + if (0 == rc) {
  78047. + /* Higher layer software sets URB status. */
  78048. +#if USB_URB_EP_LINKING
  78049. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  78050. +#endif
  78051. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  78052. +
  78053. +
  78054. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  78055. + usb_hcd_giveback_urb(hcd, urb);
  78056. +#else
  78057. + usb_hcd_giveback_urb(hcd, urb, status);
  78058. +#endif
  78059. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  78060. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  78061. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  78062. + }
  78063. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  78064. + } else {
  78065. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  78066. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  78067. + rc);
  78068. + }
  78069. +
  78070. + return rc;
  78071. +}
  78072. +
  78073. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  78074. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  78075. + * must already be dequeued. */
  78076. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  78077. +{
  78078. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78079. +
  78080. + DWC_DEBUGPL(DBG_HCD,
  78081. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  78082. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  78083. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  78084. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  78085. + ep->hcpriv = NULL;
  78086. +}
  78087. +
  78088. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  78089. +/* Resets endpoint specific parameter values, in current version used to reset
  78090. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  78091. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  78092. +{
  78093. + dwc_irqflags_t flags;
  78094. + struct usb_device *udev = NULL;
  78095. + int epnum = usb_endpoint_num(&ep->desc);
  78096. + int is_out = usb_endpoint_dir_out(&ep->desc);
  78097. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  78098. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78099. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  78100. +
  78101. + if (dev)
  78102. + udev = to_usb_device(dev);
  78103. + else
  78104. + return;
  78105. +
  78106. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  78107. +
  78108. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  78109. + usb_settoggle(udev, epnum, is_out, 0);
  78110. + if (is_control)
  78111. + usb_settoggle(udev, epnum, !is_out, 0);
  78112. +
  78113. + if (ep->hcpriv) {
  78114. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  78115. + }
  78116. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  78117. +}
  78118. +#endif
  78119. +
  78120. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  78121. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  78122. + * interrupt.
  78123. + *
  78124. + * This function is called by the USB core when an interrupt occurs */
  78125. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  78126. +{
  78127. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78128. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  78129. + if (retval != 0) {
  78130. + S3C2410X_CLEAR_EINTPEND();
  78131. + }
  78132. + return IRQ_RETVAL(retval);
  78133. +}
  78134. +
  78135. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  78136. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  78137. + * is the status change indicator for the single root port. Returns 1 if either
  78138. + * change indicator is 1, otherwise returns 0. */
  78139. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  78140. +{
  78141. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78142. +
  78143. + buf[0] = 0;
  78144. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  78145. +
  78146. + return (buf[0] != 0);
  78147. +}
  78148. +
  78149. +/** Handles hub class-specific requests. */
  78150. +int hub_control(struct usb_hcd *hcd,
  78151. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  78152. +{
  78153. + int retval;
  78154. +
  78155. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  78156. + typeReq, wValue, wIndex, buf, wLength);
  78157. +
  78158. + switch (retval) {
  78159. + case -DWC_E_INVALID:
  78160. + retval = -EINVAL;
  78161. + break;
  78162. + }
  78163. +
  78164. + return retval;
  78165. +}
  78166. +
  78167. +#endif /* DWC_DEVICE_ONLY */
  78168. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  78169. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 1970-01-01 01:00:00.000000000 +0100
  78170. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 2014-03-11 17:51:27.000000000 +0100
  78171. @@ -0,0 +1,959 @@
  78172. +/* ==========================================================================
  78173. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  78174. + * $Revision: #44 $
  78175. + * $Date: 2011/10/26 $
  78176. + * $Change: 1873028 $
  78177. + *
  78178. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  78179. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  78180. + * otherwise expressly agreed to in writing between Synopsys and you.
  78181. + *
  78182. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  78183. + * any End User Software License Agreement or Agreement for Licensed Product
  78184. + * with Synopsys or any supplement thereto. You are permitted to use and
  78185. + * redistribute this Software in source and binary forms, with or without
  78186. + * modification, provided that redistributions of source code must retain this
  78187. + * notice. You may not view, use, disclose, copy or distribute this file or
  78188. + * any information contained herein except pursuant to this license grant from
  78189. + * Synopsys. If you do not agree with this notice, including the disclaimer
  78190. + * below, then you are not authorized to use the Software.
  78191. + *
  78192. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  78193. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  78194. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  78195. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  78196. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78197. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  78198. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  78199. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  78200. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  78201. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  78202. + * DAMAGE.
  78203. + * ========================================================================== */
  78204. +#ifndef DWC_DEVICE_ONLY
  78205. +
  78206. +/**
  78207. + * @file
  78208. + *
  78209. + * This file contains the functions to manage Queue Heads and Queue
  78210. + * Transfer Descriptors.
  78211. + */
  78212. +
  78213. +#include "dwc_otg_hcd.h"
  78214. +#include "dwc_otg_regs.h"
  78215. +#include "dwc_otg_mphi_fix.h"
  78216. +
  78217. +extern bool microframe_schedule;
  78218. +
  78219. +/**
  78220. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  78221. + * removed from a list. QTD list should already be empty if called from URB
  78222. + * Dequeue.
  78223. + *
  78224. + * @param hcd HCD instance.
  78225. + * @param qh The QH to free.
  78226. + */
  78227. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78228. +{
  78229. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  78230. +
  78231. + /* Free each QTD in the QTD list */
  78232. + DWC_SPINLOCK(hcd->lock);
  78233. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  78234. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  78235. + dwc_otg_hcd_qtd_free(qtd);
  78236. + }
  78237. +
  78238. + if (hcd->core_if->dma_desc_enable) {
  78239. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  78240. + } else if (qh->dw_align_buf) {
  78241. + uint32_t buf_size;
  78242. + if (qh->ep_type == UE_ISOCHRONOUS) {
  78243. + buf_size = 4096;
  78244. + } else {
  78245. + buf_size = hcd->core_if->core_params->max_transfer_size;
  78246. + }
  78247. + DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
  78248. + }
  78249. +
  78250. + DWC_FREE(qh);
  78251. + DWC_SPINUNLOCK(hcd->lock);
  78252. + return;
  78253. +}
  78254. +
  78255. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  78256. +#define HS_HOST_DELAY 5 /* nanoseconds */
  78257. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  78258. +#define HUB_LS_SETUP 333 /* nanoseconds */
  78259. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  78260. + /* convert & round nanoseconds to microseconds */
  78261. +
  78262. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  78263. +{
  78264. + unsigned long retval;
  78265. +
  78266. + switch (speed) {
  78267. + case USB_SPEED_HIGH:
  78268. + if (is_isoc) {
  78269. + retval =
  78270. + ((38 * 8 * 2083) +
  78271. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  78272. + HS_HOST_DELAY;
  78273. + } else {
  78274. + retval =
  78275. + ((55 * 8 * 2083) +
  78276. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  78277. + HS_HOST_DELAY;
  78278. + }
  78279. + break;
  78280. + case USB_SPEED_FULL:
  78281. + if (is_isoc) {
  78282. + retval =
  78283. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  78284. + if (is_in) {
  78285. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  78286. + } else {
  78287. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  78288. + }
  78289. + } else {
  78290. + retval =
  78291. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  78292. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  78293. + }
  78294. + break;
  78295. + case USB_SPEED_LOW:
  78296. + if (is_in) {
  78297. + retval =
  78298. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  78299. + 1000;
  78300. + retval =
  78301. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  78302. + retval;
  78303. + } else {
  78304. + retval =
  78305. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  78306. + 1000;
  78307. + retval =
  78308. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  78309. + retval;
  78310. + }
  78311. + break;
  78312. + default:
  78313. + DWC_WARN("Unknown device speed\n");
  78314. + retval = -1;
  78315. + }
  78316. +
  78317. + return NS_TO_US(retval);
  78318. +}
  78319. +
  78320. +/**
  78321. + * Initializes a QH structure.
  78322. + *
  78323. + * @param hcd The HCD state structure for the DWC OTG controller.
  78324. + * @param qh The QH to init.
  78325. + * @param urb Holds the information about the device/endpoint that we need
  78326. + * to initialize the QH.
  78327. + */
  78328. +#define SCHEDULE_SLOP 10
  78329. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  78330. +{
  78331. + char *speed, *type;
  78332. + int dev_speed;
  78333. + uint32_t hub_addr, hub_port;
  78334. +
  78335. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  78336. +
  78337. + /* Initialize QH */
  78338. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  78339. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  78340. +
  78341. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  78342. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  78343. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  78344. + DWC_LIST_INIT(&qh->qh_list_entry);
  78345. + qh->channel = NULL;
  78346. +
  78347. + /* FS/LS Enpoint on HS Hub
  78348. + * NOT virtual root hub */
  78349. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  78350. +
  78351. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  78352. + qh->do_split = 0;
  78353. + if (microframe_schedule)
  78354. + qh->speed = dev_speed;
  78355. +
  78356. + qh->nak_frame = 0xffff;
  78357. +
  78358. + if (((dev_speed == USB_SPEED_LOW) ||
  78359. + (dev_speed == USB_SPEED_FULL)) &&
  78360. + (hub_addr != 0 && hub_addr != 1)) {
  78361. + DWC_DEBUGPL(DBG_HCD,
  78362. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  78363. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  78364. + hub_port);
  78365. + qh->do_split = 1;
  78366. + qh->skip_count = 0;
  78367. + }
  78368. +
  78369. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  78370. + /* Compute scheduling parameters once and save them. */
  78371. + hprt0_data_t hprt;
  78372. +
  78373. + /** @todo Account for split transfers in the bus time. */
  78374. + int bytecount =
  78375. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  78376. +
  78377. + qh->usecs =
  78378. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  78379. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  78380. + bytecount);
  78381. + /* Start in a slightly future (micro)frame. */
  78382. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  78383. + SCHEDULE_SLOP);
  78384. + qh->interval = urb->interval;
  78385. +
  78386. +#if 0
  78387. + /* Increase interrupt polling rate for debugging. */
  78388. + if (qh->ep_type == UE_INTERRUPT) {
  78389. + qh->interval = 8;
  78390. + }
  78391. +#endif
  78392. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  78393. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  78394. + ((dev_speed == USB_SPEED_LOW) ||
  78395. + (dev_speed == USB_SPEED_FULL))) {
  78396. + qh->interval *= 8;
  78397. + qh->sched_frame |= 0x7;
  78398. + qh->start_split_frame = qh->sched_frame;
  78399. + }
  78400. +
  78401. + }
  78402. +
  78403. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  78404. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  78405. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  78406. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  78407. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  78408. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  78409. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  78410. + switch (dev_speed) {
  78411. + case USB_SPEED_LOW:
  78412. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  78413. + speed = "low";
  78414. + break;
  78415. + case USB_SPEED_FULL:
  78416. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  78417. + speed = "full";
  78418. + break;
  78419. + case USB_SPEED_HIGH:
  78420. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  78421. + speed = "high";
  78422. + break;
  78423. + default:
  78424. + speed = "?";
  78425. + break;
  78426. + }
  78427. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  78428. +
  78429. + switch (qh->ep_type) {
  78430. + case UE_ISOCHRONOUS:
  78431. + type = "isochronous";
  78432. + break;
  78433. + case UE_INTERRUPT:
  78434. + type = "interrupt";
  78435. + break;
  78436. + case UE_CONTROL:
  78437. + type = "control";
  78438. + break;
  78439. + case UE_BULK:
  78440. + type = "bulk";
  78441. + break;
  78442. + default:
  78443. + type = "?";
  78444. + break;
  78445. + }
  78446. +
  78447. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  78448. +
  78449. +#ifdef DEBUG
  78450. + if (qh->ep_type == UE_INTERRUPT) {
  78451. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  78452. + qh->usecs);
  78453. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  78454. + qh->interval);
  78455. + }
  78456. +#endif
  78457. +
  78458. +}
  78459. +
  78460. +/**
  78461. + * This function allocates and initializes a QH.
  78462. + *
  78463. + * @param hcd The HCD state structure for the DWC OTG controller.
  78464. + * @param urb Holds the information about the device/endpoint that we need
  78465. + * to initialize the QH.
  78466. + * @param atomic_alloc Flag to do atomic allocation if needed
  78467. + *
  78468. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  78469. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  78470. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  78471. +{
  78472. + dwc_otg_qh_t *qh;
  78473. +
  78474. + /* Allocate memory */
  78475. + /** @todo add memflags argument */
  78476. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  78477. + if (qh == NULL) {
  78478. + DWC_ERROR("qh allocation failed");
  78479. + return NULL;
  78480. + }
  78481. +
  78482. + qh_init(hcd, qh, urb);
  78483. +
  78484. + if (hcd->core_if->dma_desc_enable
  78485. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  78486. + dwc_otg_hcd_qh_free(hcd, qh);
  78487. + return NULL;
  78488. + }
  78489. +
  78490. + return qh;
  78491. +}
  78492. +
  78493. +/* microframe_schedule=0 start */
  78494. +
  78495. +/**
  78496. + * Checks that a channel is available for a periodic transfer.
  78497. + *
  78498. + * @return 0 if successful, negative error code otherise.
  78499. + */
  78500. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  78501. +{
  78502. + /*
  78503. + * Currently assuming that there is a dedicated host channnel for each
  78504. + * periodic transaction plus at least one host channel for
  78505. + * non-periodic transactions.
  78506. + */
  78507. + int status;
  78508. + int num_channels;
  78509. +
  78510. + num_channels = hcd->core_if->core_params->host_channels;
  78511. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  78512. + && (hcd->periodic_channels < num_channels - 1)) {
  78513. + status = 0;
  78514. + } else {
  78515. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  78516. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  78517. + status = -DWC_E_NO_SPACE;
  78518. + }
  78519. +
  78520. + return status;
  78521. +}
  78522. +
  78523. +/**
  78524. + * Checks that there is sufficient bandwidth for the specified QH in the
  78525. + * periodic schedule. For simplicity, this calculation assumes that all the
  78526. + * transfers in the periodic schedule may occur in the same (micro)frame.
  78527. + *
  78528. + * @param hcd The HCD state structure for the DWC OTG controller.
  78529. + * @param qh QH containing periodic bandwidth required.
  78530. + *
  78531. + * @return 0 if successful, negative error code otherwise.
  78532. + */
  78533. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78534. +{
  78535. + int status;
  78536. + int16_t max_claimed_usecs;
  78537. +
  78538. + status = 0;
  78539. +
  78540. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  78541. + /*
  78542. + * High speed mode.
  78543. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  78544. + */
  78545. +
  78546. + max_claimed_usecs = 100 - qh->usecs;
  78547. + } else {
  78548. + /*
  78549. + * Full speed mode.
  78550. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  78551. + */
  78552. + max_claimed_usecs = 900 - qh->usecs;
  78553. + }
  78554. +
  78555. + if (hcd->periodic_usecs > max_claimed_usecs) {
  78556. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  78557. + status = -DWC_E_NO_SPACE;
  78558. + }
  78559. +
  78560. + return status;
  78561. +}
  78562. +
  78563. +/* microframe_schedule=0 end */
  78564. +
  78565. +/**
  78566. + * Microframe scheduler
  78567. + * track the total use in hcd->frame_usecs
  78568. + * keep each qh use in qh->frame_usecs
  78569. + * when surrendering the qh then donate the time back
  78570. + */
  78571. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  78572. +
  78573. +/*
  78574. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  78575. + */
  78576. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  78577. +{
  78578. + int i;
  78579. + for (i=0; i<8; i++) {
  78580. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  78581. + }
  78582. + return 0;
  78583. +}
  78584. +
  78585. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  78586. +{
  78587. + int i;
  78588. + unsigned short utime;
  78589. + int t_left;
  78590. + int ret;
  78591. + int done;
  78592. +
  78593. + ret = -1;
  78594. + utime = _qh->usecs;
  78595. + t_left = utime;
  78596. + i = 0;
  78597. + done = 0;
  78598. + while (done == 0) {
  78599. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  78600. + if (utime <= _hcd->frame_usecs[i]) {
  78601. + _hcd->frame_usecs[i] -= utime;
  78602. + _qh->frame_usecs[i] += utime;
  78603. + t_left -= utime;
  78604. + ret = i;
  78605. + done = 1;
  78606. + return ret;
  78607. + } else {
  78608. + i++;
  78609. + if (i == 8) {
  78610. + done = 1;
  78611. + ret = -1;
  78612. + }
  78613. + }
  78614. + }
  78615. + return ret;
  78616. + }
  78617. +
  78618. +/*
  78619. + * use this for FS apps that can span multiple uframes
  78620. + */
  78621. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  78622. +{
  78623. + int i;
  78624. + int j;
  78625. + unsigned short utime;
  78626. + int t_left;
  78627. + int ret;
  78628. + int done;
  78629. + unsigned short xtime;
  78630. +
  78631. + ret = -1;
  78632. + utime = _qh->usecs;
  78633. + t_left = utime;
  78634. + i = 0;
  78635. + done = 0;
  78636. +loop:
  78637. + while (done == 0) {
  78638. + if(_hcd->frame_usecs[i] <= 0) {
  78639. + i++;
  78640. + if (i == 8) {
  78641. + done = 1;
  78642. + ret = -1;
  78643. + }
  78644. + goto loop;
  78645. + }
  78646. +
  78647. + /*
  78648. + * we need n consecutive slots
  78649. + * so use j as a start slot j plus j+1 must be enough time (for now)
  78650. + */
  78651. + xtime= _hcd->frame_usecs[i];
  78652. + for (j = i+1 ; j < 8 ; j++ ) {
  78653. + /*
  78654. + * if we add this frame remaining time to xtime we may
  78655. + * be OK, if not we need to test j for a complete frame
  78656. + */
  78657. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  78658. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  78659. + j = 8;
  78660. + ret = -1;
  78661. + continue;
  78662. + }
  78663. + }
  78664. + if (xtime >= utime) {
  78665. + ret = i;
  78666. + j = 8; /* stop loop with a good value ret */
  78667. + continue;
  78668. + }
  78669. + /* add the frame time to x time */
  78670. + xtime += _hcd->frame_usecs[j];
  78671. + /* we must have a fully available next frame or break */
  78672. + if ((xtime < utime)
  78673. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  78674. + ret = -1;
  78675. + j = 8; /* stop loop with a bad value ret */
  78676. + continue;
  78677. + }
  78678. + }
  78679. + if (ret >= 0) {
  78680. + t_left = utime;
  78681. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  78682. + t_left -= _hcd->frame_usecs[j];
  78683. + if ( t_left <= 0 ) {
  78684. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  78685. + _hcd->frame_usecs[j]= -t_left;
  78686. + ret = i;
  78687. + done = 1;
  78688. + } else {
  78689. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  78690. + _hcd->frame_usecs[j] = 0;
  78691. + }
  78692. + }
  78693. + } else {
  78694. + i++;
  78695. + if (i == 8) {
  78696. + done = 1;
  78697. + ret = -1;
  78698. + }
  78699. + }
  78700. + }
  78701. + return ret;
  78702. +}
  78703. +
  78704. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  78705. +{
  78706. + int ret;
  78707. + ret = -1;
  78708. +
  78709. + if (_qh->speed == USB_SPEED_HIGH) {
  78710. + /* if this is a hs transaction we need a full frame */
  78711. + ret = find_single_uframe(_hcd, _qh);
  78712. + } else {
  78713. + /* if this is a fs transaction we may need a sequence of frames */
  78714. + ret = find_multi_uframe(_hcd, _qh);
  78715. + }
  78716. + return ret;
  78717. +}
  78718. +
  78719. +/**
  78720. + * Checks that the max transfer size allowed in a host channel is large enough
  78721. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  78722. + * transfer.
  78723. + *
  78724. + * @param hcd The HCD state structure for the DWC OTG controller.
  78725. + * @param qh QH for a periodic endpoint.
  78726. + *
  78727. + * @return 0 if successful, negative error code otherwise.
  78728. + */
  78729. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78730. +{
  78731. + int status;
  78732. + uint32_t max_xfer_size;
  78733. + uint32_t max_channel_xfer_size;
  78734. +
  78735. + status = 0;
  78736. +
  78737. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  78738. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  78739. +
  78740. + if (max_xfer_size > max_channel_xfer_size) {
  78741. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  78742. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  78743. + status = -DWC_E_NO_SPACE;
  78744. + }
  78745. +
  78746. + return status;
  78747. +}
  78748. +
  78749. +
  78750. +extern int g_next_sched_frame, g_np_count, g_np_sent;
  78751. +
  78752. +/**
  78753. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  78754. + *
  78755. + * @param hcd The HCD state structure for the DWC OTG controller.
  78756. + * @param qh QH for the periodic transfer. The QH should already contain the
  78757. + * scheduling information.
  78758. + *
  78759. + * @return 0 if successful, negative error code otherwise.
  78760. + */
  78761. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78762. +{
  78763. + int status = 0;
  78764. +
  78765. + if (microframe_schedule) {
  78766. + int frame;
  78767. + status = find_uframe(hcd, qh);
  78768. + frame = -1;
  78769. + if (status == 0) {
  78770. + frame = 7;
  78771. + } else {
  78772. + if (status > 0 )
  78773. + frame = status-1;
  78774. + }
  78775. +
  78776. + /* Set the new frame up */
  78777. + if (frame > -1) {
  78778. + qh->sched_frame &= ~0x7;
  78779. + qh->sched_frame |= (frame & 7);
  78780. + }
  78781. +
  78782. + if (status != -1)
  78783. + status = 0;
  78784. + } else {
  78785. + status = periodic_channel_available(hcd);
  78786. + if (status) {
  78787. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  78788. + return status;
  78789. + }
  78790. +
  78791. + status = check_periodic_bandwidth(hcd, qh);
  78792. + }
  78793. + if (status) {
  78794. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  78795. + "periodic transfer.\n", __func__);
  78796. + return status;
  78797. + }
  78798. + status = check_max_xfer_size(hcd, qh);
  78799. + if (status) {
  78800. + DWC_INFO("%s: Channel max transfer size too small "
  78801. + "for periodic transfer.\n", __func__);
  78802. + return status;
  78803. + }
  78804. +
  78805. + if (hcd->core_if->dma_desc_enable) {
  78806. + /* Don't rely on SOF and start in ready schedule */
  78807. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  78808. + }
  78809. + else {
  78810. + if(DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, g_next_sched_frame))
  78811. + {
  78812. + g_next_sched_frame = qh->sched_frame;
  78813. +
  78814. + }
  78815. + /* Always start in the inactive schedule. */
  78816. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  78817. + }
  78818. +
  78819. + if (!microframe_schedule) {
  78820. + /* Reserve the periodic channel. */
  78821. + hcd->periodic_channels++;
  78822. + }
  78823. +
  78824. + /* Update claimed usecs per (micro)frame. */
  78825. + hcd->periodic_usecs += qh->usecs;
  78826. +
  78827. + return status;
  78828. +}
  78829. +
  78830. +
  78831. +/**
  78832. + * This function adds a QH to either the non periodic or periodic schedule if
  78833. + * it is not already in the schedule. If the QH is already in the schedule, no
  78834. + * action is taken.
  78835. + *
  78836. + * @return 0 if successful, negative error code otherwise.
  78837. + */
  78838. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78839. +{
  78840. + int status = 0;
  78841. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78842. +
  78843. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  78844. + /* QH already in a schedule. */
  78845. + return status;
  78846. + }
  78847. +
  78848. + /* Add the new QH to the appropriate schedule */
  78849. + if (dwc_qh_is_non_per(qh)) {
  78850. + /* Always start in the inactive schedule. */
  78851. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  78852. + &qh->qh_list_entry);
  78853. + g_np_count++;
  78854. + } else {
  78855. + status = schedule_periodic(hcd, qh);
  78856. + if ( !hcd->periodic_qh_count ) {
  78857. + intr_mask.b.sofintr = 1;
  78858. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  78859. + intr_mask.d32, intr_mask.d32);
  78860. + }
  78861. + hcd->periodic_qh_count++;
  78862. + }
  78863. +
  78864. + return status;
  78865. +}
  78866. +
  78867. +/**
  78868. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  78869. + *
  78870. + * @param hcd The HCD state structure for the DWC OTG controller.
  78871. + * @param qh QH for the periodic transfer.
  78872. + */
  78873. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78874. +{
  78875. + int i;
  78876. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  78877. +
  78878. + /* Update claimed usecs per (micro)frame. */
  78879. + hcd->periodic_usecs -= qh->usecs;
  78880. +
  78881. + if (!microframe_schedule) {
  78882. + /* Release the periodic channel reservation. */
  78883. + hcd->periodic_channels--;
  78884. + } else {
  78885. + for (i = 0; i < 8; i++) {
  78886. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  78887. + qh->frame_usecs[i] = 0;
  78888. + }
  78889. + }
  78890. +}
  78891. +
  78892. +/**
  78893. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  78894. + * not freed.
  78895. + *
  78896. + * @param hcd The HCD state structure.
  78897. + * @param qh QH to remove from schedule. */
  78898. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78899. +{
  78900. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78901. +
  78902. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  78903. + /* QH is not in a schedule. */
  78904. + return;
  78905. + }
  78906. +
  78907. + if (dwc_qh_is_non_per(qh)) {
  78908. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  78909. + hcd->non_periodic_qh_ptr =
  78910. + hcd->non_periodic_qh_ptr->next;
  78911. + }
  78912. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  78913. +
  78914. + // If we've removed the last non-periodic entry then there are none left!
  78915. + g_np_count = g_np_sent;
  78916. + } else {
  78917. + deschedule_periodic(hcd, qh);
  78918. + hcd->periodic_qh_count--;
  78919. + if( !hcd->periodic_qh_count ) {
  78920. + intr_mask.b.sofintr = 1;
  78921. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  78922. + intr_mask.d32, 0);
  78923. + }
  78924. + }
  78925. +}
  78926. +
  78927. +/**
  78928. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  78929. + * non-periodic schedule. The QH is added to the inactive non-periodic
  78930. + * schedule if any QTDs are still attached to the QH.
  78931. + *
  78932. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  78933. + * there are any QTDs still attached to the QH, the QH is added to either the
  78934. + * periodic inactive schedule or the periodic ready schedule and its next
  78935. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  78936. + * the scheduled frame has been reached already. Otherwise it's placed in the
  78937. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  78938. + * completely removed from the periodic schedule.
  78939. + */
  78940. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  78941. + int sched_next_periodic_split)
  78942. +{
  78943. + if (dwc_qh_is_non_per(qh)) {
  78944. +
  78945. + dwc_otg_qh_t *qh_tmp;
  78946. + dwc_list_link_t *qh_list;
  78947. + DWC_LIST_FOREACH(qh_list, &hcd->non_periodic_sched_inactive)
  78948. + {
  78949. + qh_tmp = DWC_LIST_ENTRY(qh_list, struct dwc_otg_qh, qh_list_entry);
  78950. + if(qh_tmp == qh)
  78951. + {
  78952. + /*
  78953. + * FIQ is being disabled because this one nevers gets a np_count increment
  78954. + * This is still not absolutely correct, but it should fix itself with
  78955. + * just an unnecessary extra interrupt
  78956. + */
  78957. + g_np_sent = g_np_count;
  78958. + }
  78959. + }
  78960. +
  78961. +
  78962. + dwc_otg_hcd_qh_remove(hcd, qh);
  78963. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  78964. + /* Add back to inactive non-periodic schedule. */
  78965. + dwc_otg_hcd_qh_add(hcd, qh);
  78966. + }
  78967. + } else {
  78968. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  78969. +
  78970. + if (qh->do_split) {
  78971. + /* Schedule the next continuing periodic split transfer */
  78972. + if (sched_next_periodic_split) {
  78973. +
  78974. + qh->sched_frame = frame_number;
  78975. +
  78976. + if (dwc_frame_num_le(frame_number,
  78977. + dwc_frame_num_inc
  78978. + (qh->start_split_frame,
  78979. + 1))) {
  78980. + /*
  78981. + * Allow one frame to elapse after start
  78982. + * split microframe before scheduling
  78983. + * complete split, but DONT if we are
  78984. + * doing the next start split in the
  78985. + * same frame for an ISOC out.
  78986. + */
  78987. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  78988. + (qh->ep_is_in != 0)) {
  78989. + qh->sched_frame =
  78990. + dwc_frame_num_inc(qh->sched_frame, 1);
  78991. + }
  78992. + }
  78993. + } else {
  78994. + qh->sched_frame =
  78995. + dwc_frame_num_inc(qh->start_split_frame,
  78996. + qh->interval);
  78997. + if (dwc_frame_num_le
  78998. + (qh->sched_frame, frame_number)) {
  78999. + qh->sched_frame = frame_number;
  79000. + }
  79001. + qh->sched_frame |= 0x7;
  79002. + qh->start_split_frame = qh->sched_frame;
  79003. + }
  79004. + } else {
  79005. + qh->sched_frame =
  79006. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  79007. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  79008. + qh->sched_frame = frame_number;
  79009. + }
  79010. + }
  79011. +
  79012. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  79013. + dwc_otg_hcd_qh_remove(hcd, qh);
  79014. + } else {
  79015. + /*
  79016. + * Remove from periodic_sched_queued and move to
  79017. + * appropriate queue.
  79018. + */
  79019. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  79020. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  79021. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  79022. + &qh->qh_list_entry);
  79023. + } else {
  79024. + if(!dwc_frame_num_le(g_next_sched_frame, qh->sched_frame))
  79025. + {
  79026. + g_next_sched_frame = qh->sched_frame;
  79027. + }
  79028. +
  79029. + DWC_LIST_MOVE_HEAD
  79030. + (&hcd->periodic_sched_inactive,
  79031. + &qh->qh_list_entry);
  79032. + }
  79033. + }
  79034. + }
  79035. +}
  79036. +
  79037. +/**
  79038. + * This function allocates and initializes a QTD.
  79039. + *
  79040. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  79041. + * pointing to each other so each pair should have a unique correlation.
  79042. + * @param atomic_alloc Flag to do atomic alloc if needed
  79043. + *
  79044. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  79045. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  79046. +{
  79047. + dwc_otg_qtd_t *qtd;
  79048. +
  79049. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  79050. + if (qtd == NULL) {
  79051. + return NULL;
  79052. + }
  79053. +
  79054. + dwc_otg_hcd_qtd_init(qtd, urb);
  79055. + return qtd;
  79056. +}
  79057. +
  79058. +/**
  79059. + * Initializes a QTD structure.
  79060. + *
  79061. + * @param qtd The QTD to initialize.
  79062. + * @param urb The URB to use for initialization. */
  79063. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  79064. +{
  79065. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  79066. + qtd->urb = urb;
  79067. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  79068. + /*
  79069. + * The only time the QTD data toggle is used is on the data
  79070. + * phase of control transfers. This phase always starts with
  79071. + * DATA1.
  79072. + */
  79073. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  79074. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  79075. + }
  79076. +
  79077. + /* start split */
  79078. + qtd->complete_split = 0;
  79079. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  79080. + qtd->isoc_split_offset = 0;
  79081. + qtd->in_process = 0;
  79082. +
  79083. + /* Store the qtd ptr in the urb to reference what QTD. */
  79084. + urb->qtd = qtd;
  79085. + return;
  79086. +}
  79087. +
  79088. +/**
  79089. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  79090. + * QH to place the QTD into. If it does not find a QH, then it will create a
  79091. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  79092. + * is placed into the proper schedule based on its EP type.
  79093. + * HCD lock must be held and interrupts must be disabled on entry
  79094. + *
  79095. + * @param[in] qtd The QTD to add
  79096. + * @param[in] hcd The DWC HCD structure
  79097. + * @param[out] qh out parameter to return queue head
  79098. + * @param atomic_alloc Flag to do atomic alloc if needed
  79099. + *
  79100. + * @return 0 if successful, negative error code otherwise.
  79101. + */
  79102. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  79103. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  79104. +{
  79105. + int retval = 0;
  79106. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  79107. +
  79108. + /*
  79109. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  79110. + * doesn't exist.
  79111. + */
  79112. + if (*qh == NULL) {
  79113. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  79114. + if (*qh == NULL) {
  79115. + retval = -DWC_E_NO_MEMORY;
  79116. + goto done;
  79117. + }
  79118. + }
  79119. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  79120. + if (retval == 0) {
  79121. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  79122. + qtd_list_entry);
  79123. + qtd->qh = *qh;
  79124. + }
  79125. +done:
  79126. +
  79127. + return retval;
  79128. +}
  79129. +
  79130. +#endif /* DWC_DEVICE_ONLY */
  79131. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c
  79132. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c 1970-01-01 01:00:00.000000000 +0100
  79133. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c 2014-03-11 17:51:27.000000000 +0100
  79134. @@ -0,0 +1,113 @@
  79135. +#include "dwc_otg_regs.h"
  79136. +#include "dwc_otg_dbg.h"
  79137. +
  79138. +void dwc_debug_print_core_int_reg(gintsts_data_t gintsts, const char* function_name)
  79139. +{
  79140. + DWC_DEBUGPL(DBG_USER, "*** Debugging from within the %s function: ***\n"
  79141. + "curmode: %1i Modemismatch: %1i otgintr: %1i sofintr: %1i\n"
  79142. + "rxstsqlvl: %1i nptxfempty : %1i ginnakeff: %1i goutnakeff: %1i\n"
  79143. + "ulpickint: %1i i2cintr: %1i erlysuspend:%1i usbsuspend: %1i\n"
  79144. + "usbreset: %1i enumdone: %1i isooutdrop: %1i eopframe: %1i\n"
  79145. + "restoredone: %1i epmismatch: %1i inepint: %1i outepintr: %1i\n"
  79146. + "incomplisoin:%1i incomplisoout:%1i fetsusp: %1i resetdet: %1i\n"
  79147. + "portintr: %1i hcintr: %1i ptxfempty: %1i lpmtranrcvd:%1i\n"
  79148. + "conidstschng:%1i disconnect: %1i sessreqintr:%1i wkupintr: %1i\n",
  79149. + function_name,
  79150. + gintsts.b.curmode,
  79151. + gintsts.b.modemismatch,
  79152. + gintsts.b.otgintr,
  79153. + gintsts.b.sofintr,
  79154. + gintsts.b.rxstsqlvl,
  79155. + gintsts.b.nptxfempty,
  79156. + gintsts.b.ginnakeff,
  79157. + gintsts.b.goutnakeff,
  79158. + gintsts.b.ulpickint,
  79159. + gintsts.b.i2cintr,
  79160. + gintsts.b.erlysuspend,
  79161. + gintsts.b.usbsuspend,
  79162. + gintsts.b.usbreset,
  79163. + gintsts.b.enumdone,
  79164. + gintsts.b.isooutdrop,
  79165. + gintsts.b.eopframe,
  79166. + gintsts.b.restoredone,
  79167. + gintsts.b.epmismatch,
  79168. + gintsts.b.inepint,
  79169. + gintsts.b.outepintr,
  79170. + gintsts.b.incomplisoin,
  79171. + gintsts.b.incomplisoout,
  79172. + gintsts.b.fetsusp,
  79173. + gintsts.b.resetdet,
  79174. + gintsts.b.portintr,
  79175. + gintsts.b.hcintr,
  79176. + gintsts.b.ptxfempty,
  79177. + gintsts.b.lpmtranrcvd,
  79178. + gintsts.b.conidstschng,
  79179. + gintsts.b.disconnect,
  79180. + gintsts.b.sessreqintr,
  79181. + gintsts.b.wkupintr);
  79182. + return;
  79183. +}
  79184. +
  79185. +void dwc_debug_core_int_mask(gintmsk_data_t gintmsk, const char* function_name)
  79186. +{
  79187. + DWC_DEBUGPL(DBG_USER, "Interrupt Mask status (called from %s) :\n"
  79188. + "modemismatch: %1i otgintr: %1i sofintr: %1i rxstsqlvl: %1i\n"
  79189. + "nptxfempty: %1i ginnakeff: %1i goutnakeff: %1i ulpickint: %1i\n"
  79190. + "i2cintr: %1i erlysuspend:%1i usbsuspend: %1i usbreset: %1i\n"
  79191. + "enumdone: %1i isooutdrop: %1i eopframe: %1i restoredone: %1i\n"
  79192. + "epmismatch: %1i inepintr: %1i outepintr: %1i incomplisoin:%1i\n"
  79193. + "incomplisoout:%1i fetsusp: %1i resetdet: %1i portintr: %1i\n"
  79194. + "hcintr: %1i ptxfempty: %1i lpmtranrcvd:%1i conidstschng:%1i\n"
  79195. + "disconnect: %1i sessreqintr:%1i wkupintr: %1i\n",
  79196. + function_name,
  79197. + gintmsk.b.modemismatch,
  79198. + gintmsk.b.otgintr,
  79199. + gintmsk.b.sofintr,
  79200. + gintmsk.b.rxstsqlvl,
  79201. + gintmsk.b.nptxfempty,
  79202. + gintmsk.b.ginnakeff,
  79203. + gintmsk.b.goutnakeff,
  79204. + gintmsk.b.ulpickint,
  79205. + gintmsk.b.i2cintr,
  79206. + gintmsk.b.erlysuspend,
  79207. + gintmsk.b.usbsuspend,
  79208. + gintmsk.b.usbreset,
  79209. + gintmsk.b.enumdone,
  79210. + gintmsk.b.isooutdrop,
  79211. + gintmsk.b.eopframe,
  79212. + gintmsk.b.restoredone,
  79213. + gintmsk.b.epmismatch,
  79214. + gintmsk.b.inepintr,
  79215. + gintmsk.b.outepintr,
  79216. + gintmsk.b.incomplisoin,
  79217. + gintmsk.b.incomplisoout,
  79218. + gintmsk.b.fetsusp,
  79219. + gintmsk.b.resetdet,
  79220. + gintmsk.b.portintr,
  79221. + gintmsk.b.hcintr,
  79222. + gintmsk.b.ptxfempty,
  79223. + gintmsk.b.lpmtranrcvd,
  79224. + gintmsk.b.conidstschng,
  79225. + gintmsk.b.disconnect,
  79226. + gintmsk.b.sessreqintr,
  79227. + gintmsk.b.wkupintr);
  79228. + return;
  79229. +}
  79230. +
  79231. +void dwc_debug_otg_int(gotgint_data_t gotgint, const char* function_name)
  79232. +{
  79233. + DWC_DEBUGPL(DBG_USER, "otg int register (from %s function):\n"
  79234. + "sesenddet:%1i sesreqsucstschung:%2i hstnegsucstschng:%1i\n"
  79235. + "hstnegdet:%1i adevtoutchng: %2i debdone: %1i\n"
  79236. + "mvic: %1i\n",
  79237. + function_name,
  79238. + gotgint.b.sesenddet,
  79239. + gotgint.b.sesreqsucstschng,
  79240. + gotgint.b.hstnegsucstschng,
  79241. + gotgint.b.hstnegdet,
  79242. + gotgint.b.adevtoutchng,
  79243. + gotgint.b.debdone,
  79244. + gotgint.b.mvic);
  79245. +
  79246. + return;
  79247. +}
  79248. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h
  79249. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h 1970-01-01 01:00:00.000000000 +0100
  79250. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h 2014-03-11 17:33:06.000000000 +0100
  79251. @@ -0,0 +1,48 @@
  79252. +#ifndef __DWC_OTG_MPHI_FIX_H__
  79253. +#define __DWC_OTG_MPHI_FIX_H__
  79254. +#define FIQ_WRITE(_addr_,_data_) (*(volatile uint32_t *) (_addr_) = (_data_))
  79255. +#define FIQ_READ(_addr_) (*(volatile uint32_t *) (_addr_))
  79256. +
  79257. +typedef struct {
  79258. + volatile void* base;
  79259. + volatile void* ctrl;
  79260. + volatile void* outdda;
  79261. + volatile void* outddb;
  79262. + volatile void* intstat;
  79263. +} mphi_regs_t;
  79264. +
  79265. +void dwc_debug_print_core_int_reg(gintsts_data_t gintsts, const char* function_name);
  79266. +void dwc_debug_core_int_mask(gintsts_data_t gintmsk, const char* function_name);
  79267. +void dwc_debug_otg_int(gotgint_data_t gotgint, const char* function_name);
  79268. +
  79269. +extern gintsts_data_t gintsts_saved;
  79270. +
  79271. +#ifdef DEBUG
  79272. +#define DWC_DBG_PRINT_CORE_INT(_arg_) dwc_debug_print_core_int_reg(_arg_,__func__)
  79273. +#define DWC_DBG_PRINT_CORE_INT_MASK(_arg_) dwc_debug_core_int_mask(_arg_,__func__)
  79274. +#define DWC_DBG_PRINT_OTG_INT(_arg_) dwc_debug_otg_int(_arg_,__func__)
  79275. +
  79276. +#else
  79277. +#define DWC_DBG_PRINT_CORE_INT(_arg_)
  79278. +#define DWC_DBG_PRINT_CORE_INT_MASK(_arg_)
  79279. +#define DWC_DBG_PRINT_OTG_INT(_arg_)
  79280. +
  79281. +#endif
  79282. +
  79283. +typedef enum {
  79284. + FIQDBG_SCHED = (1 << 0),
  79285. + FIQDBG_INT = (1 << 1),
  79286. + FIQDBG_ERR = (1 << 2),
  79287. + FIQDBG_PORTHUB = (1 << 3),
  79288. +} FIQDBG_T;
  79289. +
  79290. +void _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...);
  79291. +#ifdef FIQ_DEBUG
  79292. +#define fiq_print _fiq_print
  79293. +#else
  79294. +#define fiq_print(x, y, ...)
  79295. +#endif
  79296. +
  79297. +extern bool fiq_fix_enable, nak_holdoff_enable, fiq_split_enable;
  79298. +
  79299. +#endif
  79300. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  79301. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 1970-01-01 01:00:00.000000000 +0100
  79302. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 2014-03-11 17:51:27.000000000 +0100
  79303. @@ -0,0 +1,188 @@
  79304. +#ifndef _DWC_OS_DEP_H_
  79305. +#define _DWC_OS_DEP_H_
  79306. +
  79307. +/**
  79308. + * @file
  79309. + *
  79310. + * This file contains OS dependent structures.
  79311. + *
  79312. + */
  79313. +
  79314. +#include <linux/kernel.h>
  79315. +#include <linux/module.h>
  79316. +#include <linux/moduleparam.h>
  79317. +#include <linux/init.h>
  79318. +#include <linux/device.h>
  79319. +#include <linux/errno.h>
  79320. +#include <linux/types.h>
  79321. +#include <linux/slab.h>
  79322. +#include <linux/list.h>
  79323. +#include <linux/interrupt.h>
  79324. +#include <linux/ctype.h>
  79325. +#include <linux/string.h>
  79326. +#include <linux/dma-mapping.h>
  79327. +#include <linux/jiffies.h>
  79328. +#include <linux/delay.h>
  79329. +#include <linux/timer.h>
  79330. +#include <linux/workqueue.h>
  79331. +#include <linux/stat.h>
  79332. +#include <linux/pci.h>
  79333. +
  79334. +#include <linux/version.h>
  79335. +
  79336. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  79337. +# include <linux/irq.h>
  79338. +#endif
  79339. +
  79340. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  79341. +# include <linux/usb/ch9.h>
  79342. +#else
  79343. +# include <linux/usb_ch9.h>
  79344. +#endif
  79345. +
  79346. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  79347. +# include <linux/usb/gadget.h>
  79348. +#else
  79349. +# include <linux/usb_gadget.h>
  79350. +#endif
  79351. +
  79352. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  79353. +# include <asm/irq.h>
  79354. +#endif
  79355. +
  79356. +#ifdef PCI_INTERFACE
  79357. +# include <asm/io.h>
  79358. +#endif
  79359. +
  79360. +#ifdef LM_INTERFACE
  79361. +# include <asm/unaligned.h>
  79362. +# include <asm/sizes.h>
  79363. +# include <asm/param.h>
  79364. +# include <asm/io.h>
  79365. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  79366. +# include <asm/arch/hardware.h>
  79367. +# include <asm/arch/lm.h>
  79368. +# include <asm/arch/irqs.h>
  79369. +# include <asm/arch/regs-irq.h>
  79370. +# else
  79371. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  79372. + here we assume that the machine architecture provides definitions
  79373. + in its own header
  79374. +*/
  79375. +# include <mach/lm.h>
  79376. +# include <mach/hardware.h>
  79377. +# endif
  79378. +#endif
  79379. +
  79380. +#ifdef PLATFORM_INTERFACE
  79381. +#include <linux/platform_device.h>
  79382. +#include <asm/mach/map.h>
  79383. +#endif
  79384. +
  79385. +/** The OS page size */
  79386. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  79387. +
  79388. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  79389. +typedef int gfp_t;
  79390. +#endif
  79391. +
  79392. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  79393. +# define IRQF_SHARED SA_SHIRQ
  79394. +#endif
  79395. +
  79396. +typedef struct os_dependent {
  79397. + /** Base address returned from ioremap() */
  79398. + void *base;
  79399. +
  79400. + /** Register offset for Diagnostic API */
  79401. + uint32_t reg_offset;
  79402. +
  79403. + /** Base address for MPHI peripheral */
  79404. + void *mphi_base;
  79405. +
  79406. +#ifdef LM_INTERFACE
  79407. + struct lm_device *lmdev;
  79408. +#elif defined(PCI_INTERFACE)
  79409. + struct pci_dev *pcidev;
  79410. +
  79411. + /** Start address of a PCI region */
  79412. + resource_size_t rsrc_start;
  79413. +
  79414. + /** Length address of a PCI region */
  79415. + resource_size_t rsrc_len;
  79416. +#elif defined(PLATFORM_INTERFACE)
  79417. + struct platform_device *platformdev;
  79418. +#endif
  79419. +
  79420. +} os_dependent_t;
  79421. +
  79422. +#ifdef __cplusplus
  79423. +}
  79424. +#endif
  79425. +
  79426. +
  79427. +
  79428. +/* Type for the our device on the chosen bus */
  79429. +#if defined(LM_INTERFACE)
  79430. +typedef struct lm_device dwc_bus_dev_t;
  79431. +#elif defined(PCI_INTERFACE)
  79432. +typedef struct pci_dev dwc_bus_dev_t;
  79433. +#elif defined(PLATFORM_INTERFACE)
  79434. +typedef struct platform_device dwc_bus_dev_t;
  79435. +#endif
  79436. +
  79437. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  79438. +#if defined(LM_INTERFACE)
  79439. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  79440. +#elif defined(PCI_INTERFACE)
  79441. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  79442. +#elif defined(PLATFORM_INTERFACE)
  79443. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  79444. +#endif
  79445. +
  79446. +/**
  79447. + * Helper macro returning the otg_device structure of a given struct device
  79448. + *
  79449. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  79450. + */
  79451. +#ifdef LM_INTERFACE
  79452. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  79453. + struct lm_device *lm_dev = \
  79454. + container_of(_dev, struct lm_device, dev); \
  79455. + _var = lm_get_drvdata(lm_dev); \
  79456. + } while (0)
  79457. +
  79458. +#elif defined(PCI_INTERFACE)
  79459. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  79460. + _var = dev_get_drvdata(_dev); \
  79461. + } while (0)
  79462. +
  79463. +#elif defined(PLATFORM_INTERFACE)
  79464. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  79465. + struct platform_device *platform_dev = \
  79466. + container_of(_dev, struct platform_device, dev); \
  79467. + _var = platform_get_drvdata(platform_dev); \
  79468. + } while (0)
  79469. +#endif
  79470. +
  79471. +
  79472. +/**
  79473. + * Helper macro returning the struct dev of the given struct os_dependent
  79474. + *
  79475. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  79476. + */
  79477. +#ifdef LM_INTERFACE
  79478. +#define DWC_OTG_OS_GETDEV(_osdep) \
  79479. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  79480. +#elif defined(PCI_INTERFACE)
  79481. +#define DWC_OTG_OS_GETDEV(_osdep) \
  79482. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  79483. +#elif defined(PLATFORM_INTERFACE)
  79484. +#define DWC_OTG_OS_GETDEV(_osdep) \
  79485. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  79486. +#endif
  79487. +
  79488. +
  79489. +
  79490. +
  79491. +#endif /* _DWC_OS_DEP_H_ */
  79492. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  79493. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 1970-01-01 01:00:00.000000000 +0100
  79494. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 2014-03-11 17:51:27.000000000 +0100
  79495. @@ -0,0 +1,2708 @@
  79496. +/* ==========================================================================
  79497. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  79498. + * $Revision: #101 $
  79499. + * $Date: 2012/08/10 $
  79500. + * $Change: 2047372 $
  79501. + *
  79502. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  79503. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  79504. + * otherwise expressly agreed to in writing between Synopsys and you.
  79505. + *
  79506. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  79507. + * any End User Software License Agreement or Agreement for Licensed Product
  79508. + * with Synopsys or any supplement thereto. You are permitted to use and
  79509. + * redistribute this Software in source and binary forms, with or without
  79510. + * modification, provided that redistributions of source code must retain this
  79511. + * notice. You may not view, use, disclose, copy or distribute this file or
  79512. + * any information contained herein except pursuant to this license grant from
  79513. + * Synopsys. If you do not agree with this notice, including the disclaimer
  79514. + * below, then you are not authorized to use the Software.
  79515. + *
  79516. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  79517. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  79518. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  79519. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  79520. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  79521. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  79522. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  79523. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  79524. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  79525. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  79526. + * DAMAGE.
  79527. + * ========================================================================== */
  79528. +#ifndef DWC_HOST_ONLY
  79529. +
  79530. +/** @file
  79531. + * This file implements PCD Core. All code in this file is portable and doesn't
  79532. + * use any OS specific functions.
  79533. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  79534. + * header file, which can be used to implement OS specific PCD interface.
  79535. + *
  79536. + * An important function of the PCD is managing interrupts generated
  79537. + * by the DWC_otg controller. The implementation of the DWC_otg device
  79538. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  79539. + *
  79540. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  79541. + * @todo Does it work when the request size is greater than DEPTSIZ
  79542. + * transfer size
  79543. + *
  79544. + */
  79545. +
  79546. +#include "dwc_otg_pcd.h"
  79547. +
  79548. +#ifdef DWC_UTE_CFI
  79549. +#include "dwc_otg_cfi.h"
  79550. +
  79551. +extern int init_cfi(cfiobject_t * cfiobj);
  79552. +#endif
  79553. +
  79554. +/**
  79555. + * Choose endpoint from ep arrays using usb_ep structure.
  79556. + */
  79557. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  79558. +{
  79559. + int i;
  79560. + if (pcd->ep0.priv == handle) {
  79561. + return &pcd->ep0;
  79562. + }
  79563. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  79564. + if (pcd->in_ep[i].priv == handle)
  79565. + return &pcd->in_ep[i];
  79566. + if (pcd->out_ep[i].priv == handle)
  79567. + return &pcd->out_ep[i];
  79568. + }
  79569. +
  79570. + return NULL;
  79571. +}
  79572. +
  79573. +/**
  79574. + * This function completes a request. It call's the request call back.
  79575. + */
  79576. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  79577. + int32_t status)
  79578. +{
  79579. + unsigned stopped = ep->stopped;
  79580. +
  79581. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  79582. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  79583. +
  79584. + /* don't modify queue heads during completion callback */
  79585. + ep->stopped = 1;
  79586. + /* spin_unlock/spin_lock now done in fops->complete() */
  79587. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  79588. + req->actual);
  79589. +
  79590. + if (ep->pcd->request_pending > 0) {
  79591. + --ep->pcd->request_pending;
  79592. + }
  79593. +
  79594. + ep->stopped = stopped;
  79595. + DWC_FREE(req);
  79596. +}
  79597. +
  79598. +/**
  79599. + * This function terminates all the requsts in the EP request queue.
  79600. + */
  79601. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  79602. +{
  79603. + dwc_otg_pcd_request_t *req;
  79604. +
  79605. + ep->stopped = 1;
  79606. +
  79607. + /* called with irqs blocked?? */
  79608. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  79609. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  79610. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  79611. + }
  79612. +}
  79613. +
  79614. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  79615. + const struct dwc_otg_pcd_function_ops *fops)
  79616. +{
  79617. + pcd->fops = fops;
  79618. +}
  79619. +
  79620. +/**
  79621. + * PCD Callback function for initializing the PCD when switching to
  79622. + * device mode.
  79623. + *
  79624. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79625. + */
  79626. +static int32_t dwc_otg_pcd_start_cb(void *p)
  79627. +{
  79628. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79629. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  79630. +
  79631. + /*
  79632. + * Initialized the Core for Device mode.
  79633. + */
  79634. + if (dwc_otg_is_device_mode(core_if)) {
  79635. + dwc_otg_core_dev_init(core_if);
  79636. + /* Set core_if's lock pointer to the pcd->lock */
  79637. + core_if->lock = pcd->lock;
  79638. + }
  79639. + return 1;
  79640. +}
  79641. +
  79642. +/** CFI-specific buffer allocation function for EP */
  79643. +#ifdef DWC_UTE_CFI
  79644. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  79645. + size_t buflen, int flags)
  79646. +{
  79647. + dwc_otg_pcd_ep_t *ep;
  79648. + ep = get_ep_from_handle(pcd, pep);
  79649. + if (!ep) {
  79650. + DWC_WARN("bad ep\n");
  79651. + return -DWC_E_INVALID;
  79652. + }
  79653. +
  79654. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  79655. + flags);
  79656. +}
  79657. +#else
  79658. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  79659. + size_t buflen, int flags);
  79660. +#endif
  79661. +
  79662. +/**
  79663. + * PCD Callback function for notifying the PCD when resuming from
  79664. + * suspend.
  79665. + *
  79666. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79667. + */
  79668. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  79669. +{
  79670. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79671. +
  79672. + if (pcd->fops->resume) {
  79673. + pcd->fops->resume(pcd);
  79674. + }
  79675. +
  79676. + /* Stop the SRP timeout timer. */
  79677. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  79678. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  79679. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  79680. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  79681. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  79682. + }
  79683. + }
  79684. + return 1;
  79685. +}
  79686. +
  79687. +/**
  79688. + * PCD Callback function for notifying the PCD device is suspended.
  79689. + *
  79690. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79691. + */
  79692. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  79693. +{
  79694. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79695. +
  79696. + if (pcd->fops->suspend) {
  79697. + DWC_SPINUNLOCK(pcd->lock);
  79698. + pcd->fops->suspend(pcd);
  79699. + DWC_SPINLOCK(pcd->lock);
  79700. + }
  79701. +
  79702. + return 1;
  79703. +}
  79704. +
  79705. +/**
  79706. + * PCD Callback function for stopping the PCD when switching to Host
  79707. + * mode.
  79708. + *
  79709. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79710. + */
  79711. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  79712. +{
  79713. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79714. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  79715. +
  79716. + dwc_otg_pcd_stop(pcd);
  79717. + return 1;
  79718. +}
  79719. +
  79720. +/**
  79721. + * PCD Callback structure for handling mode switching.
  79722. + */
  79723. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  79724. + .start = dwc_otg_pcd_start_cb,
  79725. + .stop = dwc_otg_pcd_stop_cb,
  79726. + .suspend = dwc_otg_pcd_suspend_cb,
  79727. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  79728. + .p = 0, /* Set at registration */
  79729. +};
  79730. +
  79731. +/**
  79732. + * This function allocates a DMA Descriptor chain for the Endpoint
  79733. + * buffer to be used for a transfer to/from the specified endpoint.
  79734. + */
  79735. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(dwc_dma_t * dma_desc_addr,
  79736. + uint32_t count)
  79737. +{
  79738. + return DWC_DMA_ALLOC_ATOMIC(count * sizeof(dwc_otg_dev_dma_desc_t),
  79739. + dma_desc_addr);
  79740. +}
  79741. +
  79742. +/**
  79743. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  79744. + */
  79745. +void dwc_otg_ep_free_desc_chain(dwc_otg_dev_dma_desc_t * desc_addr,
  79746. + uint32_t dma_desc_addr, uint32_t count)
  79747. +{
  79748. + DWC_DMA_FREE(count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  79749. + dma_desc_addr);
  79750. +}
  79751. +
  79752. +#ifdef DWC_EN_ISOC
  79753. +
  79754. +/**
  79755. + * This function initializes a descriptor chain for Isochronous transfer
  79756. + *
  79757. + * @param core_if Programming view of DWC_otg controller.
  79758. + * @param dwc_ep The EP to start the transfer on.
  79759. + *
  79760. + */
  79761. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  79762. + dwc_ep_t * dwc_ep)
  79763. +{
  79764. +
  79765. + dsts_data_t dsts = {.d32 = 0 };
  79766. + depctl_data_t depctl = {.d32 = 0 };
  79767. + volatile uint32_t *addr;
  79768. + int i, j;
  79769. + uint32_t len;
  79770. +
  79771. + if (dwc_ep->is_in)
  79772. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  79773. + else
  79774. + dwc_ep->desc_cnt =
  79775. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  79776. + dwc_ep->bInterval;
  79777. +
  79778. + /** Allocate descriptors for double buffering */
  79779. + dwc_ep->iso_desc_addr =
  79780. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  79781. + dwc_ep->desc_cnt * 2);
  79782. + if (dwc_ep->desc_addr) {
  79783. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  79784. + return;
  79785. + }
  79786. +
  79787. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  79788. +
  79789. + /** ISO OUT EP */
  79790. + if (dwc_ep->is_in == 0) {
  79791. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  79792. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  79793. + dma_addr_t dma_ad;
  79794. + uint32_t data_per_desc;
  79795. + dwc_otg_dev_out_ep_regs_t *out_regs =
  79796. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  79797. + int offset;
  79798. +
  79799. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  79800. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  79801. +
  79802. + /** Buffer 0 descriptors setup */
  79803. + dma_ad = dwc_ep->dma_addr0;
  79804. +
  79805. + sts.b_iso_out.bs = BS_HOST_READY;
  79806. + sts.b_iso_out.rxsts = 0;
  79807. + sts.b_iso_out.l = 0;
  79808. + sts.b_iso_out.sp = 0;
  79809. + sts.b_iso_out.ioc = 0;
  79810. + sts.b_iso_out.pid = 0;
  79811. + sts.b_iso_out.framenum = 0;
  79812. +
  79813. + offset = 0;
  79814. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79815. + i += dwc_ep->pkt_per_frm) {
  79816. +
  79817. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  79818. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79819. + if (len > dwc_ep->data_per_frame)
  79820. + data_per_desc =
  79821. + dwc_ep->data_per_frame -
  79822. + j * dwc_ep->maxpacket;
  79823. + else
  79824. + data_per_desc = dwc_ep->maxpacket;
  79825. + len = data_per_desc % 4;
  79826. + if (len)
  79827. + data_per_desc += 4 - len;
  79828. +
  79829. + sts.b_iso_out.rxbytes = data_per_desc;
  79830. + dma_desc->buf = dma_ad;
  79831. + dma_desc->status.d32 = sts.d32;
  79832. +
  79833. + offset += data_per_desc;
  79834. + dma_desc++;
  79835. + dma_ad += data_per_desc;
  79836. + }
  79837. + }
  79838. +
  79839. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  79840. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79841. + if (len > dwc_ep->data_per_frame)
  79842. + data_per_desc =
  79843. + dwc_ep->data_per_frame -
  79844. + j * dwc_ep->maxpacket;
  79845. + else
  79846. + data_per_desc = dwc_ep->maxpacket;
  79847. + len = data_per_desc % 4;
  79848. + if (len)
  79849. + data_per_desc += 4 - len;
  79850. + sts.b_iso_out.rxbytes = data_per_desc;
  79851. + dma_desc->buf = dma_ad;
  79852. + dma_desc->status.d32 = sts.d32;
  79853. +
  79854. + offset += data_per_desc;
  79855. + dma_desc++;
  79856. + dma_ad += data_per_desc;
  79857. + }
  79858. +
  79859. + sts.b_iso_out.ioc = 1;
  79860. + len = (j + 1) * dwc_ep->maxpacket;
  79861. + if (len > dwc_ep->data_per_frame)
  79862. + data_per_desc =
  79863. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  79864. + else
  79865. + data_per_desc = dwc_ep->maxpacket;
  79866. + len = data_per_desc % 4;
  79867. + if (len)
  79868. + data_per_desc += 4 - len;
  79869. + sts.b_iso_out.rxbytes = data_per_desc;
  79870. +
  79871. + dma_desc->buf = dma_ad;
  79872. + dma_desc->status.d32 = sts.d32;
  79873. + dma_desc++;
  79874. +
  79875. + /** Buffer 1 descriptors setup */
  79876. + sts.b_iso_out.ioc = 0;
  79877. + dma_ad = dwc_ep->dma_addr1;
  79878. +
  79879. + offset = 0;
  79880. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79881. + i += dwc_ep->pkt_per_frm) {
  79882. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  79883. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79884. + if (len > dwc_ep->data_per_frame)
  79885. + data_per_desc =
  79886. + dwc_ep->data_per_frame -
  79887. + j * dwc_ep->maxpacket;
  79888. + else
  79889. + data_per_desc = dwc_ep->maxpacket;
  79890. + len = data_per_desc % 4;
  79891. + if (len)
  79892. + data_per_desc += 4 - len;
  79893. +
  79894. + data_per_desc =
  79895. + sts.b_iso_out.rxbytes = data_per_desc;
  79896. + dma_desc->buf = dma_ad;
  79897. + dma_desc->status.d32 = sts.d32;
  79898. +
  79899. + offset += data_per_desc;
  79900. + dma_desc++;
  79901. + dma_ad += data_per_desc;
  79902. + }
  79903. + }
  79904. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  79905. + data_per_desc =
  79906. + ((j + 1) * dwc_ep->maxpacket >
  79907. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  79908. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  79909. + data_per_desc +=
  79910. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  79911. + sts.b_iso_out.rxbytes = data_per_desc;
  79912. + dma_desc->buf = dma_ad;
  79913. + dma_desc->status.d32 = sts.d32;
  79914. +
  79915. + offset += data_per_desc;
  79916. + dma_desc++;
  79917. + dma_ad += data_per_desc;
  79918. + }
  79919. +
  79920. + sts.b_iso_out.ioc = 1;
  79921. + sts.b_iso_out.l = 1;
  79922. + data_per_desc =
  79923. + ((j + 1) * dwc_ep->maxpacket >
  79924. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  79925. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  79926. + data_per_desc +=
  79927. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  79928. + sts.b_iso_out.rxbytes = data_per_desc;
  79929. +
  79930. + dma_desc->buf = dma_ad;
  79931. + dma_desc->status.d32 = sts.d32;
  79932. +
  79933. + dwc_ep->next_frame = 0;
  79934. +
  79935. + /** Write dma_ad into DOEPDMA register */
  79936. + DWC_WRITE_REG32(&(out_regs->doepdma),
  79937. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  79938. +
  79939. + }
  79940. + /** ISO IN EP */
  79941. + else {
  79942. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  79943. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  79944. + dma_addr_t dma_ad;
  79945. + dwc_otg_dev_in_ep_regs_t *in_regs =
  79946. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  79947. + unsigned int frmnumber;
  79948. + fifosize_data_t txfifosize, rxfifosize;
  79949. +
  79950. + txfifosize.d32 =
  79951. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  79952. + dtxfsts);
  79953. + rxfifosize.d32 =
  79954. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  79955. +
  79956. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  79957. +
  79958. + dma_ad = dwc_ep->dma_addr0;
  79959. +
  79960. + dsts.d32 =
  79961. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  79962. +
  79963. + sts.b_iso_in.bs = BS_HOST_READY;
  79964. + sts.b_iso_in.txsts = 0;
  79965. + sts.b_iso_in.sp =
  79966. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  79967. + sts.b_iso_in.ioc = 0;
  79968. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  79969. +
  79970. + frmnumber = dwc_ep->next_frame;
  79971. +
  79972. + sts.b_iso_in.framenum = frmnumber;
  79973. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  79974. + sts.b_iso_in.l = 0;
  79975. +
  79976. + /** Buffer 0 descriptors setup */
  79977. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  79978. + dma_desc->buf = dma_ad;
  79979. + dma_desc->status.d32 = sts.d32;
  79980. + dma_desc++;
  79981. +
  79982. + dma_ad += dwc_ep->data_per_frame;
  79983. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  79984. + }
  79985. +
  79986. + sts.b_iso_in.ioc = 1;
  79987. + dma_desc->buf = dma_ad;
  79988. + dma_desc->status.d32 = sts.d32;
  79989. + ++dma_desc;
  79990. +
  79991. + /** Buffer 1 descriptors setup */
  79992. + sts.b_iso_in.ioc = 0;
  79993. + dma_ad = dwc_ep->dma_addr1;
  79994. +
  79995. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79996. + i += dwc_ep->pkt_per_frm) {
  79997. + dma_desc->buf = dma_ad;
  79998. + dma_desc->status.d32 = sts.d32;
  79999. + dma_desc++;
  80000. +
  80001. + dma_ad += dwc_ep->data_per_frame;
  80002. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  80003. +
  80004. + sts.b_iso_in.ioc = 0;
  80005. + }
  80006. + sts.b_iso_in.ioc = 1;
  80007. + sts.b_iso_in.l = 1;
  80008. +
  80009. + dma_desc->buf = dma_ad;
  80010. + dma_desc->status.d32 = sts.d32;
  80011. +
  80012. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  80013. +
  80014. + /** Write dma_ad into diepdma register */
  80015. + DWC_WRITE_REG32(&(in_regs->diepdma),
  80016. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  80017. + }
  80018. + /** Enable endpoint, clear nak */
  80019. + depctl.d32 = 0;
  80020. + depctl.b.epena = 1;
  80021. + depctl.b.usbactep = 1;
  80022. + depctl.b.cnak = 1;
  80023. +
  80024. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  80025. + depctl.d32 = DWC_READ_REG32(addr);
  80026. +}
  80027. +
  80028. +/**
  80029. + * This function initializes a descriptor chain for Isochronous transfer
  80030. + *
  80031. + * @param core_if Programming view of DWC_otg controller.
  80032. + * @param ep The EP to start the transfer on.
  80033. + *
  80034. + */
  80035. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  80036. + dwc_ep_t * ep)
  80037. +{
  80038. + depctl_data_t depctl = {.d32 = 0 };
  80039. + volatile uint32_t *addr;
  80040. +
  80041. + if (ep->is_in) {
  80042. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  80043. + } else {
  80044. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  80045. + }
  80046. +
  80047. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  80048. + return;
  80049. + } else {
  80050. + deptsiz_data_t deptsiz = {.d32 = 0 };
  80051. +
  80052. + ep->xfer_len =
  80053. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  80054. + ep->pkt_cnt =
  80055. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  80056. + ep->xfer_count = 0;
  80057. + ep->xfer_buff =
  80058. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  80059. + ep->dma_addr =
  80060. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  80061. +
  80062. + if (ep->is_in) {
  80063. + /* Program the transfer size and packet count
  80064. + * as follows: xfersize = N * maxpacket +
  80065. + * short_packet pktcnt = N + (short_packet
  80066. + * exist ? 1 : 0)
  80067. + */
  80068. + deptsiz.b.mc = ep->pkt_per_frm;
  80069. + deptsiz.b.xfersize = ep->xfer_len;
  80070. + deptsiz.b.pktcnt =
  80071. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  80072. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  80073. + dieptsiz, deptsiz.d32);
  80074. +
  80075. + /* Write the DMA register */
  80076. + DWC_WRITE_REG32(&
  80077. + (core_if->dev_if->in_ep_regs[ep->num]->
  80078. + diepdma), (uint32_t) ep->dma_addr);
  80079. +
  80080. + } else {
  80081. + deptsiz.b.pktcnt =
  80082. + (ep->xfer_len + (ep->maxpacket - 1)) /
  80083. + ep->maxpacket;
  80084. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  80085. +
  80086. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  80087. + doeptsiz, deptsiz.d32);
  80088. +
  80089. + /* Write the DMA register */
  80090. + DWC_WRITE_REG32(&
  80091. + (core_if->dev_if->out_ep_regs[ep->num]->
  80092. + doepdma), (uint32_t) ep->dma_addr);
  80093. +
  80094. + }
  80095. + /** Enable endpoint, clear nak */
  80096. + depctl.d32 = 0;
  80097. + depctl.b.epena = 1;
  80098. + depctl.b.cnak = 1;
  80099. +
  80100. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  80101. + }
  80102. +}
  80103. +
  80104. +/**
  80105. + * This function does the setup for a data transfer for an EP and
  80106. + * starts the transfer. For an IN transfer, the packets will be
  80107. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  80108. + * the packets are unloaded from the Rx FIFO in the ISR.
  80109. + *
  80110. + * @param core_if Programming view of DWC_otg controller.
  80111. + * @param ep The EP to start the transfer on.
  80112. + */
  80113. +
  80114. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  80115. + dwc_ep_t * ep)
  80116. +{
  80117. + if (core_if->dma_enable) {
  80118. + if (core_if->dma_desc_enable) {
  80119. + if (ep->is_in) {
  80120. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  80121. + } else {
  80122. + ep->desc_cnt = ep->pkt_cnt;
  80123. + }
  80124. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  80125. + } else {
  80126. + if (core_if->pti_enh_enable) {
  80127. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  80128. + } else {
  80129. + ep->cur_pkt_addr =
  80130. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  80131. + xfer_buff0;
  80132. + ep->cur_pkt_dma_addr =
  80133. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  80134. + dma_addr0;
  80135. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  80136. + }
  80137. + }
  80138. + } else {
  80139. + ep->cur_pkt_addr =
  80140. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  80141. + ep->cur_pkt_dma_addr =
  80142. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  80143. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  80144. + }
  80145. +}
  80146. +
  80147. +/**
  80148. + * This function stops transfer for an EP and
  80149. + * resets the ep's variables.
  80150. + *
  80151. + * @param core_if Programming view of DWC_otg controller.
  80152. + * @param ep The EP to start the transfer on.
  80153. + */
  80154. +
  80155. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  80156. +{
  80157. + depctl_data_t depctl = {.d32 = 0 };
  80158. + volatile uint32_t *addr;
  80159. +
  80160. + if (ep->is_in == 1) {
  80161. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  80162. + } else {
  80163. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  80164. + }
  80165. +
  80166. + /* disable the ep */
  80167. + depctl.d32 = DWC_READ_REG32(addr);
  80168. +
  80169. + depctl.b.epdis = 1;
  80170. + depctl.b.snak = 1;
  80171. +
  80172. + DWC_WRITE_REG32(addr, depctl.d32);
  80173. +
  80174. + if (core_if->dma_desc_enable &&
  80175. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  80176. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  80177. + ep->iso_dma_desc_addr,
  80178. + ep->desc_cnt * 2);
  80179. + }
  80180. +
  80181. + /* reset varibales */
  80182. + ep->dma_addr0 = 0;
  80183. + ep->dma_addr1 = 0;
  80184. + ep->xfer_buff0 = 0;
  80185. + ep->xfer_buff1 = 0;
  80186. + ep->data_per_frame = 0;
  80187. + ep->data_pattern_frame = 0;
  80188. + ep->sync_frame = 0;
  80189. + ep->buf_proc_intrvl = 0;
  80190. + ep->bInterval = 0;
  80191. + ep->proc_buf_num = 0;
  80192. + ep->pkt_per_frm = 0;
  80193. + ep->pkt_per_frm = 0;
  80194. + ep->desc_cnt = 0;
  80195. + ep->iso_desc_addr = 0;
  80196. + ep->iso_dma_desc_addr = 0;
  80197. +}
  80198. +
  80199. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  80200. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  80201. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  80202. + int data_per_frame, int start_frame,
  80203. + int buf_proc_intrvl, void *req_handle,
  80204. + int atomic_alloc)
  80205. +{
  80206. + dwc_otg_pcd_ep_t *ep;
  80207. + dwc_irqflags_t flags = 0;
  80208. + dwc_ep_t *dwc_ep;
  80209. + int32_t frm_data;
  80210. + dsts_data_t dsts;
  80211. + dwc_otg_core_if_t *core_if;
  80212. +
  80213. + ep = get_ep_from_handle(pcd, ep_handle);
  80214. +
  80215. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  80216. + DWC_WARN("bad ep\n");
  80217. + return -DWC_E_INVALID;
  80218. + }
  80219. +
  80220. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80221. + core_if = GET_CORE_IF(pcd);
  80222. + dwc_ep = &ep->dwc_ep;
  80223. +
  80224. + if (ep->iso_req_handle) {
  80225. + DWC_WARN("ISO request in progress\n");
  80226. + }
  80227. +
  80228. + dwc_ep->dma_addr0 = dma0;
  80229. + dwc_ep->dma_addr1 = dma1;
  80230. +
  80231. + dwc_ep->xfer_buff0 = buf0;
  80232. + dwc_ep->xfer_buff1 = buf1;
  80233. +
  80234. + dwc_ep->data_per_frame = data_per_frame;
  80235. +
  80236. + /** @todo - pattern data support is to be implemented in the future */
  80237. + dwc_ep->data_pattern_frame = dp_frame;
  80238. + dwc_ep->sync_frame = sync_frame;
  80239. +
  80240. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  80241. +
  80242. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  80243. +
  80244. + dwc_ep->proc_buf_num = 0;
  80245. +
  80246. + dwc_ep->pkt_per_frm = 0;
  80247. + frm_data = ep->dwc_ep.data_per_frame;
  80248. + while (frm_data > 0) {
  80249. + dwc_ep->pkt_per_frm++;
  80250. + frm_data -= ep->dwc_ep.maxpacket;
  80251. + }
  80252. +
  80253. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  80254. +
  80255. + if (start_frame == -1) {
  80256. + dwc_ep->next_frame = dsts.b.soffn + 1;
  80257. + if (dwc_ep->bInterval != 1) {
  80258. + dwc_ep->next_frame =
  80259. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  80260. + dwc_ep->next_frame %
  80261. + dwc_ep->bInterval);
  80262. + }
  80263. + } else {
  80264. + dwc_ep->next_frame = start_frame;
  80265. + }
  80266. +
  80267. + if (!core_if->pti_enh_enable) {
  80268. + dwc_ep->pkt_cnt =
  80269. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  80270. + dwc_ep->bInterval;
  80271. + } else {
  80272. + dwc_ep->pkt_cnt =
  80273. + (dwc_ep->data_per_frame *
  80274. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  80275. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  80276. + }
  80277. +
  80278. + if (core_if->dma_desc_enable) {
  80279. + dwc_ep->desc_cnt =
  80280. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  80281. + dwc_ep->bInterval;
  80282. + }
  80283. +
  80284. + if (atomic_alloc) {
  80285. + dwc_ep->pkt_info =
  80286. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  80287. + } else {
  80288. + dwc_ep->pkt_info =
  80289. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  80290. + }
  80291. + if (!dwc_ep->pkt_info) {
  80292. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80293. + return -DWC_E_NO_MEMORY;
  80294. + }
  80295. + if (core_if->pti_enh_enable) {
  80296. + dwc_memset(dwc_ep->pkt_info, 0,
  80297. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  80298. + }
  80299. +
  80300. + dwc_ep->cur_pkt = 0;
  80301. + ep->iso_req_handle = req_handle;
  80302. +
  80303. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80304. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  80305. + return 0;
  80306. +}
  80307. +
  80308. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  80309. + void *req_handle)
  80310. +{
  80311. + dwc_irqflags_t flags = 0;
  80312. + dwc_otg_pcd_ep_t *ep;
  80313. + dwc_ep_t *dwc_ep;
  80314. +
  80315. + ep = get_ep_from_handle(pcd, ep_handle);
  80316. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  80317. + DWC_WARN("bad ep\n");
  80318. + return -DWC_E_INVALID;
  80319. + }
  80320. + dwc_ep = &ep->dwc_ep;
  80321. +
  80322. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  80323. +
  80324. + DWC_FREE(dwc_ep->pkt_info);
  80325. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80326. + if (ep->iso_req_handle != req_handle) {
  80327. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80328. + return -DWC_E_INVALID;
  80329. + }
  80330. +
  80331. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80332. +
  80333. + ep->iso_req_handle = 0;
  80334. + return 0;
  80335. +}
  80336. +
  80337. +/**
  80338. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  80339. + * for Isochronous EPs
  80340. + *
  80341. + * - Every time a sync period completes this function is called to
  80342. + * perform data exchange between PCD and gadget
  80343. + */
  80344. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  80345. + void *req_handle)
  80346. +{
  80347. + int i;
  80348. + dwc_ep_t *dwc_ep;
  80349. +
  80350. + dwc_ep = &ep->dwc_ep;
  80351. +
  80352. + DWC_SPINUNLOCK(ep->pcd->lock);
  80353. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  80354. + dwc_ep->proc_buf_num ^ 0x1);
  80355. + DWC_SPINLOCK(ep->pcd->lock);
  80356. +
  80357. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  80358. + dwc_ep->pkt_info[i].status = 0;
  80359. + dwc_ep->pkt_info[i].offset = 0;
  80360. + dwc_ep->pkt_info[i].length = 0;
  80361. + }
  80362. +}
  80363. +
  80364. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  80365. + void *iso_req_handle)
  80366. +{
  80367. + dwc_otg_pcd_ep_t *ep;
  80368. + dwc_ep_t *dwc_ep;
  80369. +
  80370. + ep = get_ep_from_handle(pcd, ep_handle);
  80371. + if (!ep->desc || ep->dwc_ep.num == 0) {
  80372. + DWC_WARN("bad ep\n");
  80373. + return -DWC_E_INVALID;
  80374. + }
  80375. + dwc_ep = &ep->dwc_ep;
  80376. +
  80377. + return dwc_ep->pkt_cnt;
  80378. +}
  80379. +
  80380. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  80381. + void *iso_req_handle, int packet,
  80382. + int *status, int *actual, int *offset)
  80383. +{
  80384. + dwc_otg_pcd_ep_t *ep;
  80385. + dwc_ep_t *dwc_ep;
  80386. +
  80387. + ep = get_ep_from_handle(pcd, ep_handle);
  80388. + if (!ep)
  80389. + DWC_WARN("bad ep\n");
  80390. +
  80391. + dwc_ep = &ep->dwc_ep;
  80392. +
  80393. + *status = dwc_ep->pkt_info[packet].status;
  80394. + *actual = dwc_ep->pkt_info[packet].length;
  80395. + *offset = dwc_ep->pkt_info[packet].offset;
  80396. +}
  80397. +
  80398. +#endif /* DWC_EN_ISOC */
  80399. +
  80400. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  80401. + uint32_t is_in, uint32_t ep_num)
  80402. +{
  80403. + /* Init EP structure */
  80404. + pcd_ep->desc = 0;
  80405. + pcd_ep->pcd = pcd;
  80406. + pcd_ep->stopped = 1;
  80407. + pcd_ep->queue_sof = 0;
  80408. +
  80409. + /* Init DWC ep structure */
  80410. + pcd_ep->dwc_ep.is_in = is_in;
  80411. + pcd_ep->dwc_ep.num = ep_num;
  80412. + pcd_ep->dwc_ep.active = 0;
  80413. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  80414. + /* Control until ep is actvated */
  80415. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  80416. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  80417. + pcd_ep->dwc_ep.dma_addr = 0;
  80418. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  80419. + pcd_ep->dwc_ep.xfer_buff = 0;
  80420. + pcd_ep->dwc_ep.xfer_len = 0;
  80421. + pcd_ep->dwc_ep.xfer_count = 0;
  80422. + pcd_ep->dwc_ep.sent_zlp = 0;
  80423. + pcd_ep->dwc_ep.total_len = 0;
  80424. + pcd_ep->dwc_ep.desc_addr = 0;
  80425. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  80426. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  80427. +}
  80428. +
  80429. +/**
  80430. + * Initialize ep's
  80431. + */
  80432. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  80433. +{
  80434. + int i;
  80435. + uint32_t hwcfg1;
  80436. + dwc_otg_pcd_ep_t *ep;
  80437. + int in_ep_cntr, out_ep_cntr;
  80438. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  80439. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  80440. +
  80441. + /**
  80442. + * Initialize the EP0 structure.
  80443. + */
  80444. + ep = &pcd->ep0;
  80445. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  80446. +
  80447. + in_ep_cntr = 0;
  80448. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  80449. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  80450. + if ((hwcfg1 & 0x1) == 0) {
  80451. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  80452. + in_ep_cntr++;
  80453. + /**
  80454. + * @todo NGS: Add direction to EP, based on contents
  80455. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  80456. + * sprintf(";r
  80457. + */
  80458. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  80459. +
  80460. + DWC_CIRCLEQ_INIT(&ep->queue);
  80461. + }
  80462. + hwcfg1 >>= 2;
  80463. + }
  80464. +
  80465. + out_ep_cntr = 0;
  80466. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  80467. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  80468. + if ((hwcfg1 & 0x1) == 0) {
  80469. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  80470. + out_ep_cntr++;
  80471. + /**
  80472. + * @todo NGS: Add direction to EP, based on contents
  80473. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  80474. + * sprintf(";r
  80475. + */
  80476. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  80477. + DWC_CIRCLEQ_INIT(&ep->queue);
  80478. + }
  80479. + hwcfg1 >>= 2;
  80480. + }
  80481. +
  80482. + pcd->ep0state = EP0_DISCONNECT;
  80483. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  80484. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  80485. +}
  80486. +
  80487. +/**
  80488. + * This function is called when the SRP timer expires. The SRP should
  80489. + * complete within 6 seconds.
  80490. + */
  80491. +static void srp_timeout(void *ptr)
  80492. +{
  80493. + gotgctl_data_t gotgctl;
  80494. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  80495. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  80496. +
  80497. + gotgctl.d32 = DWC_READ_REG32(addr);
  80498. +
  80499. + core_if->srp_timer_started = 0;
  80500. +
  80501. + if (core_if->adp_enable) {
  80502. + if (gotgctl.b.bsesvld == 0) {
  80503. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  80504. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  80505. + /* Power off the core */
  80506. + if (core_if->power_down == 2) {
  80507. + gpwrdn.b.pwrdnswtch = 1;
  80508. + DWC_MODIFY_REG32(&core_if->
  80509. + core_global_regs->gpwrdn,
  80510. + gpwrdn.d32, 0);
  80511. + }
  80512. +
  80513. + gpwrdn.d32 = 0;
  80514. + gpwrdn.b.pmuintsel = 1;
  80515. + gpwrdn.b.pmuactv = 1;
  80516. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  80517. + gpwrdn.d32);
  80518. + dwc_otg_adp_probe_start(core_if);
  80519. + } else {
  80520. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  80521. + core_if->op_state = B_PERIPHERAL;
  80522. + dwc_otg_core_init(core_if);
  80523. + dwc_otg_enable_global_interrupts(core_if);
  80524. + cil_pcd_start(core_if);
  80525. + }
  80526. + }
  80527. +
  80528. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  80529. + (core_if->core_params->i2c_enable)) {
  80530. + DWC_PRINTF("SRP Timeout\n");
  80531. +
  80532. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  80533. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  80534. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  80535. + }
  80536. +
  80537. + /* Clear Session Request */
  80538. + gotgctl.d32 = 0;
  80539. + gotgctl.b.sesreq = 1;
  80540. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  80541. + gotgctl.d32, 0);
  80542. +
  80543. + core_if->srp_success = 0;
  80544. + } else {
  80545. + __DWC_ERROR("Device not connected/responding\n");
  80546. + gotgctl.b.sesreq = 0;
  80547. + DWC_WRITE_REG32(addr, gotgctl.d32);
  80548. + }
  80549. + } else if (gotgctl.b.sesreq) {
  80550. + DWC_PRINTF("SRP Timeout\n");
  80551. +
  80552. + __DWC_ERROR("Device not connected/responding\n");
  80553. + gotgctl.b.sesreq = 0;
  80554. + DWC_WRITE_REG32(addr, gotgctl.d32);
  80555. + } else {
  80556. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  80557. + }
  80558. +}
  80559. +
  80560. +/**
  80561. + * Tasklet
  80562. + *
  80563. + */
  80564. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  80565. +
  80566. +static void start_xfer_tasklet_func(void *data)
  80567. +{
  80568. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  80569. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80570. +
  80571. + int i;
  80572. + depctl_data_t diepctl;
  80573. +
  80574. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  80575. +
  80576. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  80577. +
  80578. + if (pcd->ep0.queue_sof) {
  80579. + pcd->ep0.queue_sof = 0;
  80580. + start_next_request(&pcd->ep0);
  80581. + // break;
  80582. + }
  80583. +
  80584. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  80585. + depctl_data_t diepctl;
  80586. + diepctl.d32 =
  80587. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  80588. +
  80589. + if (pcd->in_ep[i].queue_sof) {
  80590. + pcd->in_ep[i].queue_sof = 0;
  80591. + start_next_request(&pcd->in_ep[i]);
  80592. + // break;
  80593. + }
  80594. + }
  80595. +
  80596. + return;
  80597. +}
  80598. +
  80599. +/**
  80600. + * This function initialized the PCD portion of the driver.
  80601. + *
  80602. + */
  80603. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if)
  80604. +{
  80605. + dwc_otg_pcd_t *pcd = NULL;
  80606. + dwc_otg_dev_if_t *dev_if;
  80607. + int i;
  80608. +
  80609. + /*
  80610. + * Allocate PCD structure
  80611. + */
  80612. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  80613. +
  80614. + if (pcd == NULL) {
  80615. + return NULL;
  80616. + }
  80617. +
  80618. + pcd->lock = DWC_SPINLOCK_ALLOC();
  80619. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  80620. + pcd, core_if);//GRAYG
  80621. + if (!pcd->lock) {
  80622. + DWC_ERROR("Could not allocate lock for pcd");
  80623. + DWC_FREE(pcd);
  80624. + return NULL;
  80625. + }
  80626. + /* Set core_if's lock pointer to hcd->lock */
  80627. + core_if->lock = pcd->lock;
  80628. + pcd->core_if = core_if;
  80629. +
  80630. + dev_if = core_if->dev_if;
  80631. + dev_if->isoc_ep = NULL;
  80632. +
  80633. + if (core_if->hwcfg4.b.ded_fifo_en) {
  80634. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  80635. + } else {
  80636. + DWC_PRINTF("Shared Tx FIFO mode\n");
  80637. + }
  80638. +
  80639. + /*
  80640. + * Initialized the Core for Device mode here if there is nod ADP support.
  80641. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  80642. + */
  80643. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  80644. + dwc_otg_core_dev_init(core_if);
  80645. + }
  80646. +
  80647. + /*
  80648. + * Register the PCD Callbacks.
  80649. + */
  80650. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  80651. +
  80652. + /*
  80653. + * Initialize the DMA buffer for SETUP packets
  80654. + */
  80655. + if (GET_CORE_IF(pcd)->dma_enable) {
  80656. + pcd->setup_pkt =
  80657. + DWC_DMA_ALLOC(sizeof(*pcd->setup_pkt) * 5,
  80658. + &pcd->setup_pkt_dma_handle);
  80659. + if (pcd->setup_pkt == NULL) {
  80660. + DWC_FREE(pcd);
  80661. + return NULL;
  80662. + }
  80663. +
  80664. + pcd->status_buf =
  80665. + DWC_DMA_ALLOC(sizeof(uint16_t),
  80666. + &pcd->status_buf_dma_handle);
  80667. + if (pcd->status_buf == NULL) {
  80668. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  80669. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  80670. + DWC_FREE(pcd);
  80671. + return NULL;
  80672. + }
  80673. +
  80674. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80675. + dev_if->setup_desc_addr[0] =
  80676. + dwc_otg_ep_alloc_desc_chain
  80677. + (&dev_if->dma_setup_desc_addr[0], 1);
  80678. + dev_if->setup_desc_addr[1] =
  80679. + dwc_otg_ep_alloc_desc_chain
  80680. + (&dev_if->dma_setup_desc_addr[1], 1);
  80681. + dev_if->in_desc_addr =
  80682. + dwc_otg_ep_alloc_desc_chain
  80683. + (&dev_if->dma_in_desc_addr, 1);
  80684. + dev_if->out_desc_addr =
  80685. + dwc_otg_ep_alloc_desc_chain
  80686. + (&dev_if->dma_out_desc_addr, 1);
  80687. + pcd->data_terminated = 0;
  80688. +
  80689. + if (dev_if->setup_desc_addr[0] == 0
  80690. + || dev_if->setup_desc_addr[1] == 0
  80691. + || dev_if->in_desc_addr == 0
  80692. + || dev_if->out_desc_addr == 0) {
  80693. +
  80694. + if (dev_if->out_desc_addr)
  80695. + dwc_otg_ep_free_desc_chain
  80696. + (dev_if->out_desc_addr,
  80697. + dev_if->dma_out_desc_addr, 1);
  80698. + if (dev_if->in_desc_addr)
  80699. + dwc_otg_ep_free_desc_chain
  80700. + (dev_if->in_desc_addr,
  80701. + dev_if->dma_in_desc_addr, 1);
  80702. + if (dev_if->setup_desc_addr[1])
  80703. + dwc_otg_ep_free_desc_chain
  80704. + (dev_if->setup_desc_addr[1],
  80705. + dev_if->dma_setup_desc_addr[1], 1);
  80706. + if (dev_if->setup_desc_addr[0])
  80707. + dwc_otg_ep_free_desc_chain
  80708. + (dev_if->setup_desc_addr[0],
  80709. + dev_if->dma_setup_desc_addr[0], 1);
  80710. +
  80711. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  80712. + pcd->setup_pkt,
  80713. + pcd->setup_pkt_dma_handle);
  80714. + DWC_DMA_FREE(sizeof(*pcd->status_buf),
  80715. + pcd->status_buf,
  80716. + pcd->status_buf_dma_handle);
  80717. +
  80718. + DWC_FREE(pcd);
  80719. +
  80720. + return NULL;
  80721. + }
  80722. + }
  80723. + } else {
  80724. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  80725. + if (pcd->setup_pkt == NULL) {
  80726. + DWC_FREE(pcd);
  80727. + return NULL;
  80728. + }
  80729. +
  80730. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  80731. + if (pcd->status_buf == NULL) {
  80732. + DWC_FREE(pcd->setup_pkt);
  80733. + DWC_FREE(pcd);
  80734. + return NULL;
  80735. + }
  80736. + }
  80737. +
  80738. + dwc_otg_pcd_reinit(pcd);
  80739. +
  80740. + /* Allocate the cfi object for the PCD */
  80741. +#ifdef DWC_UTE_CFI
  80742. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  80743. + if (NULL == pcd->cfi)
  80744. + goto fail;
  80745. + if (init_cfi(pcd->cfi)) {
  80746. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  80747. + goto fail;
  80748. + }
  80749. +#endif
  80750. +
  80751. + /* Initialize tasklets */
  80752. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  80753. + start_xfer_tasklet_func, pcd);
  80754. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  80755. + do_test_mode, pcd);
  80756. +
  80757. + /* Initialize SRP timer */
  80758. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  80759. +
  80760. + if (core_if->core_params->dev_out_nak) {
  80761. + /**
  80762. + * Initialize xfer timeout timer. Implemented for
  80763. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  80764. + */
  80765. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  80766. + pcd->core_if->ep_xfer_timer[i] =
  80767. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  80768. + &pcd->core_if->ep_xfer_info[i]);
  80769. + }
  80770. + }
  80771. +
  80772. + return pcd;
  80773. +#ifdef DWC_UTE_CFI
  80774. +fail:
  80775. +#endif
  80776. + if (pcd->setup_pkt)
  80777. + DWC_FREE(pcd->setup_pkt);
  80778. + if (pcd->status_buf)
  80779. + DWC_FREE(pcd->status_buf);
  80780. +#ifdef DWC_UTE_CFI
  80781. + if (pcd->cfi)
  80782. + DWC_FREE(pcd->cfi);
  80783. +#endif
  80784. + if (pcd)
  80785. + DWC_FREE(pcd);
  80786. + return NULL;
  80787. +
  80788. +}
  80789. +
  80790. +/**
  80791. + * Remove PCD specific data
  80792. + */
  80793. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  80794. +{
  80795. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  80796. + int i;
  80797. + if (pcd->core_if->core_params->dev_out_nak) {
  80798. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  80799. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  80800. + pcd->core_if->ep_xfer_info[i].state = 0;
  80801. + }
  80802. + }
  80803. +
  80804. + if (GET_CORE_IF(pcd)->dma_enable) {
  80805. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  80806. + pcd->setup_pkt_dma_handle);
  80807. + DWC_DMA_FREE(sizeof(uint16_t), pcd->status_buf,
  80808. + pcd->status_buf_dma_handle);
  80809. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80810. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[0],
  80811. + dev_if->dma_setup_desc_addr
  80812. + [0], 1);
  80813. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[1],
  80814. + dev_if->dma_setup_desc_addr
  80815. + [1], 1);
  80816. + dwc_otg_ep_free_desc_chain(dev_if->in_desc_addr,
  80817. + dev_if->dma_in_desc_addr, 1);
  80818. + dwc_otg_ep_free_desc_chain(dev_if->out_desc_addr,
  80819. + dev_if->dma_out_desc_addr,
  80820. + 1);
  80821. + }
  80822. + } else {
  80823. + DWC_FREE(pcd->setup_pkt);
  80824. + DWC_FREE(pcd->status_buf);
  80825. + }
  80826. + DWC_SPINLOCK_FREE(pcd->lock);
  80827. + /* Set core_if's lock pointer to NULL */
  80828. + pcd->core_if->lock = NULL;
  80829. +
  80830. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  80831. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  80832. + if (pcd->core_if->core_params->dev_out_nak) {
  80833. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  80834. + if (pcd->core_if->ep_xfer_timer[i]) {
  80835. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  80836. + }
  80837. + }
  80838. + }
  80839. +
  80840. +/* Release the CFI object's dynamic memory */
  80841. +#ifdef DWC_UTE_CFI
  80842. + if (pcd->cfi->ops.release) {
  80843. + pcd->cfi->ops.release(pcd->cfi);
  80844. + }
  80845. +#endif
  80846. +
  80847. + DWC_FREE(pcd);
  80848. +}
  80849. +
  80850. +/**
  80851. + * Returns whether registered pcd is dual speed or not
  80852. + */
  80853. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  80854. +{
  80855. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80856. +
  80857. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  80858. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  80859. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  80860. + (core_if->core_params->ulpi_fs_ls))) {
  80861. + return 0;
  80862. + }
  80863. +
  80864. + return 1;
  80865. +}
  80866. +
  80867. +/**
  80868. + * Returns whether registered pcd is OTG capable or not
  80869. + */
  80870. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  80871. +{
  80872. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80873. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  80874. +
  80875. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  80876. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  80877. + return 0;
  80878. + }
  80879. +
  80880. + return 1;
  80881. +}
  80882. +
  80883. +/**
  80884. + * This function assigns periodic Tx FIFO to an periodic EP
  80885. + * in shared Tx FIFO mode
  80886. + */
  80887. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  80888. +{
  80889. + uint32_t TxMsk = 1;
  80890. + int i;
  80891. +
  80892. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  80893. + if ((TxMsk & core_if->tx_msk) == 0) {
  80894. + core_if->tx_msk |= TxMsk;
  80895. + return i + 1;
  80896. + }
  80897. + TxMsk <<= 1;
  80898. + }
  80899. + return 0;
  80900. +}
  80901. +
  80902. +/**
  80903. + * This function assigns periodic Tx FIFO to an periodic EP
  80904. + * in shared Tx FIFO mode
  80905. + */
  80906. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  80907. +{
  80908. + uint32_t PerTxMsk = 1;
  80909. + int i;
  80910. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  80911. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  80912. + core_if->p_tx_msk |= PerTxMsk;
  80913. + return i + 1;
  80914. + }
  80915. + PerTxMsk <<= 1;
  80916. + }
  80917. + return 0;
  80918. +}
  80919. +
  80920. +/**
  80921. + * This function releases periodic Tx FIFO
  80922. + * in shared Tx FIFO mode
  80923. + */
  80924. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  80925. + uint32_t fifo_num)
  80926. +{
  80927. + core_if->p_tx_msk =
  80928. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  80929. +}
  80930. +
  80931. +/**
  80932. + * This function releases periodic Tx FIFO
  80933. + * in shared Tx FIFO mode
  80934. + */
  80935. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  80936. +{
  80937. + core_if->tx_msk =
  80938. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  80939. +}
  80940. +
  80941. +/**
  80942. + * This function is being called from gadget
  80943. + * to enable PCD endpoint.
  80944. + */
  80945. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  80946. + const uint8_t * ep_desc, void *usb_ep)
  80947. +{
  80948. + int num, dir;
  80949. + dwc_otg_pcd_ep_t *ep = NULL;
  80950. + const usb_endpoint_descriptor_t *desc;
  80951. + dwc_irqflags_t flags;
  80952. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  80953. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  80954. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  80955. + int retval = 0;
  80956. + int i, epcount;
  80957. +
  80958. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  80959. +
  80960. + if (!desc) {
  80961. + pcd->ep0.priv = usb_ep;
  80962. + ep = &pcd->ep0;
  80963. + retval = -DWC_E_INVALID;
  80964. + goto out;
  80965. + }
  80966. +
  80967. + num = UE_GET_ADDR(desc->bEndpointAddress);
  80968. + dir = UE_GET_DIR(desc->bEndpointAddress);
  80969. +
  80970. + if (!desc->wMaxPacketSize) {
  80971. + DWC_WARN("bad maxpacketsize\n");
  80972. + retval = -DWC_E_INVALID;
  80973. + goto out;
  80974. + }
  80975. +
  80976. + if (dir == UE_DIR_IN) {
  80977. + epcount = pcd->core_if->dev_if->num_in_eps;
  80978. + for (i = 0; i < epcount; i++) {
  80979. + if (num == pcd->in_ep[i].dwc_ep.num) {
  80980. + ep = &pcd->in_ep[i];
  80981. + break;
  80982. + }
  80983. + }
  80984. + } else {
  80985. + epcount = pcd->core_if->dev_if->num_out_eps;
  80986. + for (i = 0; i < epcount; i++) {
  80987. + if (num == pcd->out_ep[i].dwc_ep.num) {
  80988. + ep = &pcd->out_ep[i];
  80989. + break;
  80990. + }
  80991. + }
  80992. + }
  80993. +
  80994. + if (!ep) {
  80995. + DWC_WARN("bad address\n");
  80996. + retval = -DWC_E_INVALID;
  80997. + goto out;
  80998. + }
  80999. +
  81000. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81001. +
  81002. + ep->desc = desc;
  81003. + ep->priv = usb_ep;
  81004. +
  81005. + /*
  81006. + * Activate the EP
  81007. + */
  81008. + ep->stopped = 0;
  81009. +
  81010. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  81011. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  81012. +
  81013. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  81014. +
  81015. + if (ep->dwc_ep.is_in) {
  81016. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81017. + ep->dwc_ep.tx_fifo_num = 0;
  81018. +
  81019. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  81020. + /*
  81021. + * if ISOC EP then assign a Periodic Tx FIFO.
  81022. + */
  81023. + ep->dwc_ep.tx_fifo_num =
  81024. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  81025. + }
  81026. + } else {
  81027. + /*
  81028. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  81029. + */
  81030. + ep->dwc_ep.tx_fifo_num =
  81031. + assign_tx_fifo(GET_CORE_IF(pcd));
  81032. + }
  81033. +
  81034. + /* Calculating EP info controller base address */
  81035. + if (ep->dwc_ep.tx_fifo_num
  81036. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81037. + gdfifocfg.d32 =
  81038. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81039. + core_global_regs->gdfifocfg);
  81040. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  81041. + dptxfsiz.d32 =
  81042. + (DWC_READ_REG32
  81043. + (&GET_CORE_IF(pcd)->core_global_regs->
  81044. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  81045. + gdfifocfg.b.epinfobase =
  81046. + gdfifocfgbase.d32 + dptxfsiz.d32;
  81047. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  81048. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  81049. + core_global_regs->gdfifocfg,
  81050. + gdfifocfg.d32);
  81051. + }
  81052. + }
  81053. + }
  81054. + /* Set initial data PID. */
  81055. + if (ep->dwc_ep.type == UE_BULK) {
  81056. + ep->dwc_ep.data_pid_start = 0;
  81057. + }
  81058. +
  81059. + /* Alloc DMA Descriptors */
  81060. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  81061. +#ifndef DWC_UTE_PER_IO
  81062. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  81063. +#endif
  81064. + ep->dwc_ep.desc_addr =
  81065. + dwc_otg_ep_alloc_desc_chain(&ep->
  81066. + dwc_ep.dma_desc_addr,
  81067. + MAX_DMA_DESC_CNT);
  81068. + if (!ep->dwc_ep.desc_addr) {
  81069. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  81070. + __func__);
  81071. + retval = -DWC_E_SHUTDOWN;
  81072. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81073. + goto out;
  81074. + }
  81075. +#ifndef DWC_UTE_PER_IO
  81076. + }
  81077. +#endif
  81078. + }
  81079. +
  81080. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  81081. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  81082. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  81083. +#ifdef DWC_UTE_PER_IO
  81084. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  81085. +#endif
  81086. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  81087. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  81088. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  81089. + }
  81090. +
  81091. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  81092. +
  81093. +#ifdef DWC_UTE_CFI
  81094. + if (pcd->cfi->ops.ep_enable) {
  81095. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  81096. + }
  81097. +#endif
  81098. +
  81099. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81100. +
  81101. +out:
  81102. + return retval;
  81103. +}
  81104. +
  81105. +/**
  81106. + * This function is being called from gadget
  81107. + * to disable PCD endpoint.
  81108. + */
  81109. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  81110. +{
  81111. + dwc_otg_pcd_ep_t *ep;
  81112. + dwc_irqflags_t flags;
  81113. + dwc_otg_dev_dma_desc_t *desc_addr;
  81114. + dwc_dma_t dma_desc_addr;
  81115. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  81116. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  81117. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  81118. +
  81119. + ep = get_ep_from_handle(pcd, ep_handle);
  81120. +
  81121. + if (!ep || !ep->desc) {
  81122. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  81123. + return -DWC_E_INVALID;
  81124. + }
  81125. +
  81126. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81127. +
  81128. + dwc_otg_request_nuke(ep);
  81129. +
  81130. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  81131. + if (pcd->core_if->core_params->dev_out_nak) {
  81132. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  81133. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  81134. + }
  81135. + ep->desc = NULL;
  81136. + ep->stopped = 1;
  81137. +
  81138. + gdfifocfg.d32 =
  81139. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  81140. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  81141. +
  81142. + if (ep->dwc_ep.is_in) {
  81143. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81144. + /* Flush the Tx FIFO */
  81145. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  81146. + ep->dwc_ep.tx_fifo_num);
  81147. + }
  81148. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  81149. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  81150. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81151. + /* Decreasing EPinfo Base Addr */
  81152. + dptxfsiz.d32 =
  81153. + (DWC_READ_REG32
  81154. + (&GET_CORE_IF(pcd)->
  81155. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  81156. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  81157. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  81158. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  81159. + gdfifocfg.d32);
  81160. + }
  81161. + }
  81162. + }
  81163. +
  81164. + /* Free DMA Descriptors */
  81165. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  81166. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  81167. + desc_addr = ep->dwc_ep.desc_addr;
  81168. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  81169. +
  81170. + /* Cannot call dma_free_coherent() with IRQs disabled */
  81171. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81172. + dwc_otg_ep_free_desc_chain(desc_addr, dma_desc_addr,
  81173. + MAX_DMA_DESC_CNT);
  81174. +
  81175. + goto out_unlocked;
  81176. + }
  81177. + }
  81178. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81179. +
  81180. +out_unlocked:
  81181. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  81182. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81183. + return 0;
  81184. +
  81185. +}
  81186. +
  81187. +/******************************************************************************/
  81188. +#ifdef DWC_UTE_PER_IO
  81189. +
  81190. +/**
  81191. + * Free the request and its extended parts
  81192. + *
  81193. + */
  81194. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  81195. +{
  81196. + DWC_FREE(req->ext_req.per_io_frame_descs);
  81197. + DWC_FREE(req);
  81198. +}
  81199. +
  81200. +/**
  81201. + * Start the next request in the endpoint's queue.
  81202. + *
  81203. + */
  81204. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  81205. + dwc_otg_pcd_ep_t * ep)
  81206. +{
  81207. + int i;
  81208. + dwc_otg_pcd_request_t *req = NULL;
  81209. + dwc_ep_t *dwcep = NULL;
  81210. + struct dwc_iso_xreq_port *ereq = NULL;
  81211. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  81212. + uint16_t nat;
  81213. + depctl_data_t diepctl;
  81214. +
  81215. + dwcep = &ep->dwc_ep;
  81216. +
  81217. + if (dwcep->xiso_active_xfers > 0) {
  81218. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  81219. + DWC_WARN("There are currently active transfers for EP%d \
  81220. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  81221. + dwcep->xiso_queued_xfers);
  81222. +#endif
  81223. + return 0;
  81224. + }
  81225. +
  81226. + nat = UGETW(ep->desc->wMaxPacketSize);
  81227. + nat = (nat >> 11) & 0x03;
  81228. +
  81229. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81230. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  81231. + ereq = &req->ext_req;
  81232. + ep->stopped = 0;
  81233. +
  81234. + /* Get the frame number */
  81235. + dwcep->xiso_frame_num =
  81236. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  81237. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  81238. +
  81239. + ddesc_iso = ereq->per_io_frame_descs;
  81240. +
  81241. + if (dwcep->is_in) {
  81242. + /* Setup DMA Descriptor chain for IN Isoc request */
  81243. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81244. + //if ((i % (nat + 1)) == 0)
  81245. + if ( i > 0 )
  81246. + dwcep->xiso_frame_num =
  81247. + (dwcep->xiso_bInterval +
  81248. + dwcep->xiso_frame_num) & 0x3FFF;
  81249. + dwcep->desc_addr[i].buf =
  81250. + req->dma + ddesc_iso[i].offset;
  81251. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  81252. + ddesc_iso[i].length;
  81253. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  81254. + dwcep->xiso_frame_num;
  81255. + dwcep->desc_addr[i].status.b_iso_in.bs =
  81256. + BS_HOST_READY;
  81257. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  81258. + dwcep->desc_addr[i].status.b_iso_in.sp =
  81259. + (ddesc_iso[i].length %
  81260. + dwcep->maxpacket) ? 1 : 0;
  81261. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  81262. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  81263. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  81264. +
  81265. + /* Process the last descriptor */
  81266. + if (i == ereq->pio_pkt_count - 1) {
  81267. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  81268. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  81269. + }
  81270. + }
  81271. +
  81272. + /* Setup and start the transfer for this endpoint */
  81273. + dwcep->xiso_active_xfers++;
  81274. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  81275. + in_ep_regs[dwcep->num]->diepdma,
  81276. + dwcep->dma_desc_addr);
  81277. + diepctl.d32 = 0;
  81278. + diepctl.b.epena = 1;
  81279. + diepctl.b.cnak = 1;
  81280. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  81281. + in_ep_regs[dwcep->num]->diepctl, 0,
  81282. + diepctl.d32);
  81283. + } else {
  81284. + /* Setup DMA Descriptor chain for OUT Isoc request */
  81285. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81286. + //if ((i % (nat + 1)) == 0)
  81287. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  81288. + dwcep->xiso_frame_num) & 0x3FFF;
  81289. + dwcep->desc_addr[i].buf =
  81290. + req->dma + ddesc_iso[i].offset;
  81291. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  81292. + ddesc_iso[i].length;
  81293. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  81294. + dwcep->xiso_frame_num;
  81295. + dwcep->desc_addr[i].status.b_iso_out.bs =
  81296. + BS_HOST_READY;
  81297. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  81298. + dwcep->desc_addr[i].status.b_iso_out.sp =
  81299. + (ddesc_iso[i].length %
  81300. + dwcep->maxpacket) ? 1 : 0;
  81301. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  81302. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  81303. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  81304. +
  81305. + /* Process the last descriptor */
  81306. + if (i == ereq->pio_pkt_count - 1) {
  81307. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  81308. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  81309. + }
  81310. + }
  81311. +
  81312. + /* Setup and start the transfer for this endpoint */
  81313. + dwcep->xiso_active_xfers++;
  81314. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  81315. + dev_if->out_ep_regs[dwcep->num]->
  81316. + doepdma, dwcep->dma_desc_addr);
  81317. + diepctl.d32 = 0;
  81318. + diepctl.b.epena = 1;
  81319. + diepctl.b.cnak = 1;
  81320. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81321. + dev_if->out_ep_regs[dwcep->num]->
  81322. + doepctl, 0, diepctl.d32);
  81323. + }
  81324. +
  81325. + } else {
  81326. + ep->stopped = 1;
  81327. + }
  81328. +
  81329. + return 0;
  81330. +}
  81331. +
  81332. +/**
  81333. + * - Remove the request from the queue
  81334. + */
  81335. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  81336. +{
  81337. + dwc_otg_pcd_request_t *req = NULL;
  81338. + struct dwc_iso_xreq_port *ereq = NULL;
  81339. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  81340. + dwc_ep_t *dwcep = NULL;
  81341. + int i;
  81342. +
  81343. + //DWC_DEBUG();
  81344. + dwcep = &ep->dwc_ep;
  81345. +
  81346. + /* Get the first pending request from the queue */
  81347. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81348. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  81349. + if (!req) {
  81350. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  81351. + return;
  81352. + }
  81353. + dwcep->xiso_active_xfers--;
  81354. + dwcep->xiso_queued_xfers--;
  81355. + /* Remove this request from the queue */
  81356. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  81357. + } else {
  81358. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  81359. + return;
  81360. + }
  81361. +
  81362. + ep->stopped = 1;
  81363. + ereq = &req->ext_req;
  81364. + ddesc_iso = ereq->per_io_frame_descs;
  81365. +
  81366. + if (dwcep->xiso_active_xfers < 0) {
  81367. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  81368. + dwcep->xiso_active_xfers);
  81369. + }
  81370. +
  81371. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  81372. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81373. + if (dwcep->is_in) { /* IN endpoints */
  81374. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  81375. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  81376. + ddesc_iso[i].status =
  81377. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  81378. + } else { /* OUT endpoints */
  81379. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  81380. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  81381. + ddesc_iso[i].status =
  81382. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  81383. + }
  81384. + }
  81385. +
  81386. + DWC_SPINUNLOCK(ep->pcd->lock);
  81387. +
  81388. + /* Call the completion function in the non-portable logic */
  81389. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  81390. + &req->ext_req);
  81391. +
  81392. + DWC_SPINLOCK(ep->pcd->lock);
  81393. +
  81394. + /* Free the request - specific freeing needed for extended request object */
  81395. + dwc_pcd_xiso_ereq_free(ep, req);
  81396. +
  81397. + /* Start the next request */
  81398. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  81399. +
  81400. + return;
  81401. +}
  81402. +
  81403. +/**
  81404. + * Create and initialize the Isoc pkt descriptors of the extended request.
  81405. + *
  81406. + */
  81407. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  81408. + void *ereq_nonport,
  81409. + int atomic_alloc)
  81410. +{
  81411. + struct dwc_iso_xreq_port *ereq = NULL;
  81412. + struct dwc_iso_xreq_port *req_mapped = NULL;
  81413. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  81414. + uint32_t pkt_count;
  81415. + int i;
  81416. +
  81417. + ereq = &req->ext_req;
  81418. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  81419. + pkt_count = req_mapped->pio_pkt_count;
  81420. +
  81421. + /* Create the isoc descs */
  81422. + if (atomic_alloc) {
  81423. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  81424. + } else {
  81425. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  81426. + }
  81427. +
  81428. + if (!ipds) {
  81429. + DWC_ERROR("Failed to allocate isoc descriptors");
  81430. + return -DWC_E_NO_MEMORY;
  81431. + }
  81432. +
  81433. + /* Initialize the extended request fields */
  81434. + ereq->per_io_frame_descs = ipds;
  81435. + ereq->error_count = 0;
  81436. + ereq->pio_alloc_pkt_count = pkt_count;
  81437. + ereq->pio_pkt_count = pkt_count;
  81438. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  81439. +
  81440. + /* Init the Isoc descriptors */
  81441. + for (i = 0; i < pkt_count; i++) {
  81442. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  81443. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  81444. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  81445. + ipds[i].actual_length =
  81446. + req_mapped->per_io_frame_descs[i].actual_length;
  81447. + }
  81448. +
  81449. + return 0;
  81450. +}
  81451. +
  81452. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  81453. +{
  81454. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  81455. + int i;
  81456. +
  81457. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  81458. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  81459. + DWC_DEBUG("error_count=%d", ereq->error_count);
  81460. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  81461. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  81462. + DWC_DEBUG("res=%d", ereq->res);
  81463. +
  81464. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81465. + xfd = &ereq->per_io_frame_descs[0];
  81466. + DWC_DEBUG("FD #%d", i);
  81467. +
  81468. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  81469. + DWC_DEBUG("xfd->length=%d", xfd->length);
  81470. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  81471. + DWC_DEBUG("xfd->status=%d", xfd->status);
  81472. + }
  81473. +}
  81474. +
  81475. +/**
  81476. + *
  81477. + */
  81478. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  81479. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  81480. + int zero, void *req_handle, int atomic_alloc,
  81481. + void *ereq_nonport)
  81482. +{
  81483. + dwc_otg_pcd_request_t *req = NULL;
  81484. + dwc_otg_pcd_ep_t *ep;
  81485. + dwc_irqflags_t flags;
  81486. + int res;
  81487. +
  81488. + ep = get_ep_from_handle(pcd, ep_handle);
  81489. + if (!ep) {
  81490. + DWC_WARN("bad ep\n");
  81491. + return -DWC_E_INVALID;
  81492. + }
  81493. +
  81494. + /* We support this extension only for DDMA mode */
  81495. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  81496. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  81497. + return -DWC_E_INVALID;
  81498. +
  81499. + /* Create a dwc_otg_pcd_request_t object */
  81500. + if (atomic_alloc) {
  81501. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  81502. + } else {
  81503. + req = DWC_ALLOC(sizeof(*req));
  81504. + }
  81505. +
  81506. + if (!req) {
  81507. + return -DWC_E_NO_MEMORY;
  81508. + }
  81509. +
  81510. + /* Create the Isoc descs for this request which shall be the exact match
  81511. + * of the structure sent to us from the non-portable logic */
  81512. + res =
  81513. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  81514. + if (res) {
  81515. + DWC_WARN("Failed to init the Isoc descriptors");
  81516. + DWC_FREE(req);
  81517. + return res;
  81518. + }
  81519. +
  81520. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81521. +
  81522. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  81523. + req->buf = buf;
  81524. + req->dma = dma_buf;
  81525. + req->length = buflen;
  81526. + req->sent_zlp = zero;
  81527. + req->priv = req_handle;
  81528. +
  81529. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81530. + ep->dwc_ep.dma_addr = dma_buf;
  81531. + ep->dwc_ep.start_xfer_buff = buf;
  81532. + ep->dwc_ep.xfer_buff = buf;
  81533. + ep->dwc_ep.xfer_len = 0;
  81534. + ep->dwc_ep.xfer_count = 0;
  81535. + ep->dwc_ep.sent_zlp = 0;
  81536. + ep->dwc_ep.total_len = buflen;
  81537. +
  81538. + /* Add this request to the tail */
  81539. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  81540. + ep->dwc_ep.xiso_queued_xfers++;
  81541. +
  81542. +//DWC_DEBUG("CP_0");
  81543. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  81544. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  81545. +//prn_ext_request(&req->ext_req);
  81546. +
  81547. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81548. +
  81549. + /* If the req->status == ASAP then check if there is any active transfer
  81550. + * for this endpoint. If no active transfers, then get the first entry
  81551. + * from the queue and start that transfer
  81552. + */
  81553. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  81554. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  81555. + if (res) {
  81556. + DWC_WARN("Failed to start the next Isoc transfer");
  81557. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81558. + DWC_FREE(req);
  81559. + return res;
  81560. + }
  81561. + }
  81562. +
  81563. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81564. + return 0;
  81565. +}
  81566. +
  81567. +#endif
  81568. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  81569. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  81570. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  81571. + int zero, void *req_handle, int atomic_alloc)
  81572. +{
  81573. + dwc_irqflags_t flags;
  81574. + dwc_otg_pcd_request_t *req;
  81575. + dwc_otg_pcd_ep_t *ep;
  81576. + uint32_t max_transfer;
  81577. +
  81578. + ep = get_ep_from_handle(pcd, ep_handle);
  81579. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  81580. + DWC_WARN("bad ep\n");
  81581. + return -DWC_E_INVALID;
  81582. + }
  81583. +
  81584. + if (atomic_alloc) {
  81585. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  81586. + } else {
  81587. + req = DWC_ALLOC(sizeof(*req));
  81588. + }
  81589. +
  81590. + if (!req) {
  81591. + return -DWC_E_NO_MEMORY;
  81592. + }
  81593. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  81594. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  81595. + if (ep->dwc_ep.num != 0) {
  81596. + DWC_ERROR("queue req %p, len %d buf %p\n",
  81597. + req_handle, buflen, buf);
  81598. + }
  81599. + }
  81600. +
  81601. + req->buf = buf;
  81602. + req->dma = dma_buf;
  81603. + req->length = buflen;
  81604. + req->sent_zlp = zero;
  81605. + req->priv = req_handle;
  81606. + req->dw_align_buf = NULL;
  81607. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  81608. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  81609. + req->dw_align_buf = DWC_DMA_ALLOC(buflen,
  81610. + &req->dw_align_buf_dma);
  81611. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81612. +
  81613. + /*
  81614. + * After adding request to the queue for IN ISOC wait for In Token Received
  81615. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  81616. + * Received when EP is disabled interrupt to obtain starting microframe
  81617. + * (odd/even) start transfer
  81618. + */
  81619. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  81620. + if (req != 0) {
  81621. + depctl_data_t depctl = {.d32 =
  81622. + DWC_READ_REG32(&pcd->core_if->dev_if->
  81623. + in_ep_regs[ep->dwc_ep.num]->
  81624. + diepctl) };
  81625. + ++pcd->request_pending;
  81626. +
  81627. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  81628. + if (ep->dwc_ep.is_in) {
  81629. + depctl.b.cnak = 1;
  81630. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  81631. + in_ep_regs[ep->dwc_ep.num]->
  81632. + diepctl, depctl.d32);
  81633. + }
  81634. +
  81635. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81636. + }
  81637. + return 0;
  81638. + }
  81639. +
  81640. + /*
  81641. + * For EP0 IN without premature status, zlp is required?
  81642. + */
  81643. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  81644. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  81645. + //_req->zero = 1;
  81646. + }
  81647. +
  81648. + /* Start the transfer */
  81649. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  81650. + /* EP0 Transfer? */
  81651. + if (ep->dwc_ep.num == 0) {
  81652. + switch (pcd->ep0state) {
  81653. + case EP0_IN_DATA_PHASE:
  81654. + DWC_DEBUGPL(DBG_PCD,
  81655. + "%s ep0: EP0_IN_DATA_PHASE\n",
  81656. + __func__);
  81657. + break;
  81658. +
  81659. + case EP0_OUT_DATA_PHASE:
  81660. + DWC_DEBUGPL(DBG_PCD,
  81661. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  81662. + __func__);
  81663. + if (pcd->request_config) {
  81664. + /* Complete STATUS PHASE */
  81665. + ep->dwc_ep.is_in = 1;
  81666. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  81667. + }
  81668. + break;
  81669. +
  81670. + case EP0_IN_STATUS_PHASE:
  81671. + DWC_DEBUGPL(DBG_PCD,
  81672. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  81673. + __func__);
  81674. + break;
  81675. +
  81676. + default:
  81677. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  81678. + pcd->ep0state);
  81679. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81680. + return -DWC_E_SHUTDOWN;
  81681. + }
  81682. +
  81683. + ep->dwc_ep.dma_addr = dma_buf;
  81684. + ep->dwc_ep.start_xfer_buff = buf;
  81685. + ep->dwc_ep.xfer_buff = buf;
  81686. + ep->dwc_ep.xfer_len = buflen;
  81687. + ep->dwc_ep.xfer_count = 0;
  81688. + ep->dwc_ep.sent_zlp = 0;
  81689. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  81690. +
  81691. + if (zero) {
  81692. + if ((ep->dwc_ep.xfer_len %
  81693. + ep->dwc_ep.maxpacket == 0)
  81694. + && (ep->dwc_ep.xfer_len != 0)) {
  81695. + ep->dwc_ep.sent_zlp = 1;
  81696. + }
  81697. +
  81698. + }
  81699. +
  81700. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  81701. + &ep->dwc_ep);
  81702. + } // non-ep0 endpoints
  81703. + else {
  81704. +#ifdef DWC_UTE_CFI
  81705. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  81706. + /* store the request length */
  81707. + ep->dwc_ep.cfi_req_len = buflen;
  81708. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  81709. + ep, req);
  81710. + } else {
  81711. +#endif
  81712. + max_transfer =
  81713. + GET_CORE_IF(ep->pcd)->core_params->
  81714. + max_transfer_size;
  81715. +
  81716. + /* Setup and start the Transfer */
  81717. + if (req->dw_align_buf){
  81718. + if (ep->dwc_ep.is_in)
  81719. + dwc_memcpy(req->dw_align_buf,
  81720. + buf, buflen);
  81721. + ep->dwc_ep.dma_addr =
  81722. + req->dw_align_buf_dma;
  81723. + ep->dwc_ep.start_xfer_buff =
  81724. + req->dw_align_buf;
  81725. + ep->dwc_ep.xfer_buff =
  81726. + req->dw_align_buf;
  81727. + } else {
  81728. + ep->dwc_ep.dma_addr = dma_buf;
  81729. + ep->dwc_ep.start_xfer_buff = buf;
  81730. + ep->dwc_ep.xfer_buff = buf;
  81731. + }
  81732. + ep->dwc_ep.xfer_len = 0;
  81733. + ep->dwc_ep.xfer_count = 0;
  81734. + ep->dwc_ep.sent_zlp = 0;
  81735. + ep->dwc_ep.total_len = buflen;
  81736. +
  81737. + ep->dwc_ep.maxxfer = max_transfer;
  81738. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  81739. + uint32_t out_max_xfer =
  81740. + DDMA_MAX_TRANSFER_SIZE -
  81741. + (DDMA_MAX_TRANSFER_SIZE % 4);
  81742. + if (ep->dwc_ep.is_in) {
  81743. + if (ep->dwc_ep.maxxfer >
  81744. + DDMA_MAX_TRANSFER_SIZE) {
  81745. + ep->dwc_ep.maxxfer =
  81746. + DDMA_MAX_TRANSFER_SIZE;
  81747. + }
  81748. + } else {
  81749. + if (ep->dwc_ep.maxxfer >
  81750. + out_max_xfer) {
  81751. + ep->dwc_ep.maxxfer =
  81752. + out_max_xfer;
  81753. + }
  81754. + }
  81755. + }
  81756. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  81757. + ep->dwc_ep.maxxfer -=
  81758. + (ep->dwc_ep.maxxfer %
  81759. + ep->dwc_ep.maxpacket);
  81760. + }
  81761. +
  81762. + if (zero) {
  81763. + if ((ep->dwc_ep.total_len %
  81764. + ep->dwc_ep.maxpacket == 0)
  81765. + && (ep->dwc_ep.total_len != 0)) {
  81766. + ep->dwc_ep.sent_zlp = 1;
  81767. + }
  81768. + }
  81769. +#ifdef DWC_UTE_CFI
  81770. + }
  81771. +#endif
  81772. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  81773. + &ep->dwc_ep);
  81774. + }
  81775. + }
  81776. +
  81777. + if (req != 0) {
  81778. + ++pcd->request_pending;
  81779. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  81780. + if (ep->dwc_ep.is_in && ep->stopped
  81781. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  81782. + /** @todo NGS Create a function for this. */
  81783. + diepmsk_data_t diepmsk = {.d32 = 0 };
  81784. + diepmsk.b.intktxfemp = 1;
  81785. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  81786. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81787. + dev_if->dev_global_regs->diepeachintmsk
  81788. + [ep->dwc_ep.num], 0,
  81789. + diepmsk.d32);
  81790. + } else {
  81791. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81792. + dev_if->dev_global_regs->
  81793. + diepmsk, 0, diepmsk.d32);
  81794. + }
  81795. +
  81796. + }
  81797. + }
  81798. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81799. +
  81800. + return 0;
  81801. +}
  81802. +
  81803. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  81804. + void *req_handle)
  81805. +{
  81806. + dwc_irqflags_t flags;
  81807. + dwc_otg_pcd_request_t *req;
  81808. + dwc_otg_pcd_ep_t *ep;
  81809. +
  81810. + ep = get_ep_from_handle(pcd, ep_handle);
  81811. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  81812. + DWC_WARN("bad argument\n");
  81813. + return -DWC_E_INVALID;
  81814. + }
  81815. +
  81816. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81817. +
  81818. + /* make sure it's actually queued on this endpoint */
  81819. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  81820. + if (req->priv == (void *)req_handle) {
  81821. + break;
  81822. + }
  81823. + }
  81824. +
  81825. + if (req->priv != (void *)req_handle) {
  81826. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81827. + return -DWC_E_INVALID;
  81828. + }
  81829. +
  81830. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  81831. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  81832. + } else {
  81833. + req = NULL;
  81834. + }
  81835. +
  81836. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81837. +
  81838. + return req ? 0 : -DWC_E_SHUTDOWN;
  81839. +
  81840. +}
  81841. +
  81842. +/**
  81843. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  81844. + *
  81845. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  81846. + * requests. If the gadget driver clears the halt status, it will
  81847. + * automatically unwedge the endpoint.
  81848. + *
  81849. + * Returns zero on success, else negative DWC error code.
  81850. + */
  81851. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  81852. +{
  81853. + dwc_otg_pcd_ep_t *ep;
  81854. + dwc_irqflags_t flags;
  81855. + int retval = 0;
  81856. +
  81857. + ep = get_ep_from_handle(pcd, ep_handle);
  81858. +
  81859. + if ((!ep->desc && ep != &pcd->ep0) ||
  81860. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  81861. + DWC_WARN("%s, bad ep\n", __func__);
  81862. + return -DWC_E_INVALID;
  81863. + }
  81864. +
  81865. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81866. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81867. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  81868. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81869. + retval = -DWC_E_AGAIN;
  81870. + } else {
  81871. + /* This code needs to be reviewed */
  81872. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  81873. + dtxfsts_data_t txstatus;
  81874. + fifosize_data_t txfifosize;
  81875. +
  81876. + txfifosize.d32 =
  81877. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81878. + core_global_regs->dtxfsiz[ep->dwc_ep.
  81879. + tx_fifo_num]);
  81880. + txstatus.d32 =
  81881. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81882. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  81883. + dtxfsts);
  81884. +
  81885. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  81886. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  81887. + retval = -DWC_E_AGAIN;
  81888. + } else {
  81889. + if (ep->dwc_ep.num == 0) {
  81890. + pcd->ep0state = EP0_STALL;
  81891. + }
  81892. +
  81893. + ep->stopped = 1;
  81894. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  81895. + &ep->dwc_ep);
  81896. + }
  81897. + } else {
  81898. + if (ep->dwc_ep.num == 0) {
  81899. + pcd->ep0state = EP0_STALL;
  81900. + }
  81901. +
  81902. + ep->stopped = 1;
  81903. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81904. + }
  81905. + }
  81906. +
  81907. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81908. +
  81909. + return retval;
  81910. +}
  81911. +
  81912. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  81913. +{
  81914. + dwc_otg_pcd_ep_t *ep;
  81915. + dwc_irqflags_t flags;
  81916. + int retval = 0;
  81917. +
  81918. + ep = get_ep_from_handle(pcd, ep_handle);
  81919. +
  81920. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  81921. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  81922. + DWC_WARN("%s, bad ep\n", __func__);
  81923. + return -DWC_E_INVALID;
  81924. + }
  81925. +
  81926. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81927. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81928. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  81929. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81930. + retval = -DWC_E_AGAIN;
  81931. + } else if (value == 0) {
  81932. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81933. + } else if (value == 1) {
  81934. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  81935. + dtxfsts_data_t txstatus;
  81936. + fifosize_data_t txfifosize;
  81937. +
  81938. + txfifosize.d32 =
  81939. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  81940. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  81941. + txstatus.d32 =
  81942. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  81943. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  81944. +
  81945. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  81946. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  81947. + retval = -DWC_E_AGAIN;
  81948. + } else {
  81949. + if (ep->dwc_ep.num == 0) {
  81950. + pcd->ep0state = EP0_STALL;
  81951. + }
  81952. +
  81953. + ep->stopped = 1;
  81954. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  81955. + &ep->dwc_ep);
  81956. + }
  81957. + } else {
  81958. + if (ep->dwc_ep.num == 0) {
  81959. + pcd->ep0state = EP0_STALL;
  81960. + }
  81961. +
  81962. + ep->stopped = 1;
  81963. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81964. + }
  81965. + } else if (value == 2) {
  81966. + ep->dwc_ep.stall_clear_flag = 0;
  81967. + } else if (value == 3) {
  81968. + ep->dwc_ep.stall_clear_flag = 1;
  81969. + }
  81970. +
  81971. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81972. +
  81973. + return retval;
  81974. +}
  81975. +
  81976. +/**
  81977. + * This function initiates remote wakeup of the host from suspend state.
  81978. + */
  81979. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  81980. +{
  81981. + dctl_data_t dctl = { 0 };
  81982. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  81983. + dsts_data_t dsts;
  81984. +
  81985. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  81986. + if (!dsts.b.suspsts) {
  81987. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  81988. + }
  81989. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  81990. + if (pcd->remote_wakeup_enable) {
  81991. + if (set) {
  81992. +
  81993. + if (core_if->adp_enable) {
  81994. + gpwrdn_data_t gpwrdn;
  81995. +
  81996. + dwc_otg_adp_probe_stop(core_if);
  81997. +
  81998. + /* Mask SRP detected interrupt from Power Down Logic */
  81999. + gpwrdn.d32 = 0;
  82000. + gpwrdn.b.srp_det_msk = 1;
  82001. + DWC_MODIFY_REG32(&core_if->
  82002. + core_global_regs->gpwrdn,
  82003. + gpwrdn.d32, 0);
  82004. +
  82005. + /* Disable Power Down Logic */
  82006. + gpwrdn.d32 = 0;
  82007. + gpwrdn.b.pmuactv = 1;
  82008. + DWC_MODIFY_REG32(&core_if->
  82009. + core_global_regs->gpwrdn,
  82010. + gpwrdn.d32, 0);
  82011. +
  82012. + /*
  82013. + * Initialize the Core for Device mode.
  82014. + */
  82015. + core_if->op_state = B_PERIPHERAL;
  82016. + dwc_otg_core_init(core_if);
  82017. + dwc_otg_enable_global_interrupts(core_if);
  82018. + cil_pcd_start(core_if);
  82019. +
  82020. + dwc_otg_initiate_srp(core_if);
  82021. + }
  82022. +
  82023. + dctl.b.rmtwkupsig = 1;
  82024. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  82025. + dctl, 0, dctl.d32);
  82026. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  82027. +
  82028. + dwc_mdelay(2);
  82029. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  82030. + dctl, dctl.d32, 0);
  82031. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  82032. + }
  82033. + } else {
  82034. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  82035. + }
  82036. +}
  82037. +
  82038. +#ifdef CONFIG_USB_DWC_OTG_LPM
  82039. +/**
  82040. + * This function initiates remote wakeup of the host from L1 sleep state.
  82041. + */
  82042. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  82043. +{
  82044. + glpmcfg_data_t lpmcfg;
  82045. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82046. +
  82047. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  82048. +
  82049. + /* Check if we are in L1 state */
  82050. + if (!lpmcfg.b.prt_sleep_sts) {
  82051. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  82052. + return;
  82053. + }
  82054. +
  82055. + /* Check if host allows remote wakeup */
  82056. + if (!lpmcfg.b.rem_wkup_en) {
  82057. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  82058. + return;
  82059. + }
  82060. +
  82061. + /* Check if Resume OK */
  82062. + if (!lpmcfg.b.sleep_state_resumeok) {
  82063. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  82064. + return;
  82065. + }
  82066. +
  82067. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  82068. + lpmcfg.b.en_utmi_sleep = 0;
  82069. + lpmcfg.b.hird_thres &= (~(1 << 4));
  82070. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  82071. +
  82072. + if (set) {
  82073. + dctl_data_t dctl = {.d32 = 0 };
  82074. + dctl.b.rmtwkupsig = 1;
  82075. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  82076. + * Hardware will automatically clear this bit.
  82077. + */
  82078. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  82079. + 0, dctl.d32);
  82080. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  82081. + }
  82082. +
  82083. +}
  82084. +#endif
  82085. +
  82086. +/**
  82087. + * Performs remote wakeup.
  82088. + */
  82089. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  82090. +{
  82091. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82092. + dwc_irqflags_t flags;
  82093. + if (dwc_otg_is_device_mode(core_if)) {
  82094. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  82095. +#ifdef CONFIG_USB_DWC_OTG_LPM
  82096. + if (core_if->lx_state == DWC_OTG_L1) {
  82097. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  82098. + } else {
  82099. +#endif
  82100. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  82101. +#ifdef CONFIG_USB_DWC_OTG_LPM
  82102. + }
  82103. +#endif
  82104. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  82105. + }
  82106. + return;
  82107. +}
  82108. +
  82109. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  82110. +{
  82111. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82112. + dctl_data_t dctl = { 0 };
  82113. +
  82114. + if (dwc_otg_is_device_mode(core_if)) {
  82115. + dctl.b.sftdiscon = 1;
  82116. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  82117. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  82118. + dwc_udelay(no_of_usecs);
  82119. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  82120. +
  82121. + } else{
  82122. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  82123. + }
  82124. + return;
  82125. +
  82126. +}
  82127. +
  82128. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  82129. +{
  82130. + dsts_data_t dsts;
  82131. + gotgctl_data_t gotgctl;
  82132. +
  82133. + /*
  82134. + * This function starts the Protocol if no session is in progress. If
  82135. + * a session is already in progress, but the device is suspended,
  82136. + * remote wakeup signaling is started.
  82137. + */
  82138. +
  82139. + /* Check if valid session */
  82140. + gotgctl.d32 =
  82141. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  82142. + if (gotgctl.b.bsesvld) {
  82143. + /* Check if suspend state */
  82144. + dsts.d32 =
  82145. + DWC_READ_REG32(&
  82146. + (GET_CORE_IF(pcd)->dev_if->
  82147. + dev_global_regs->dsts));
  82148. + if (dsts.b.suspsts) {
  82149. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  82150. + }
  82151. + } else {
  82152. + dwc_otg_pcd_initiate_srp(pcd);
  82153. + }
  82154. +
  82155. + return 0;
  82156. +
  82157. +}
  82158. +
  82159. +/**
  82160. + * Start the SRP timer to detect when the SRP does not complete within
  82161. + * 6 seconds.
  82162. + *
  82163. + * @param pcd the pcd structure.
  82164. + */
  82165. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  82166. +{
  82167. + dwc_irqflags_t flags;
  82168. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  82169. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  82170. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  82171. +}
  82172. +
  82173. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  82174. +{
  82175. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  82176. +}
  82177. +
  82178. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  82179. +{
  82180. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  82181. +}
  82182. +
  82183. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  82184. +{
  82185. + return pcd->b_hnp_enable;
  82186. +}
  82187. +
  82188. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  82189. +{
  82190. + return pcd->a_hnp_support;
  82191. +}
  82192. +
  82193. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  82194. +{
  82195. + return pcd->a_alt_hnp_support;
  82196. +}
  82197. +
  82198. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  82199. +{
  82200. + return pcd->remote_wakeup_enable;
  82201. +}
  82202. +
  82203. +#endif /* DWC_HOST_ONLY */
  82204. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  82205. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 1970-01-01 01:00:00.000000000 +0100
  82206. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 2014-03-11 17:33:06.000000000 +0100
  82207. @@ -0,0 +1,266 @@
  82208. +/* ==========================================================================
  82209. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  82210. + * $Revision: #48 $
  82211. + * $Date: 2012/08/10 $
  82212. + * $Change: 2047372 $
  82213. + *
  82214. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82215. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82216. + * otherwise expressly agreed to in writing between Synopsys and you.
  82217. + *
  82218. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82219. + * any End User Software License Agreement or Agreement for Licensed Product
  82220. + * with Synopsys or any supplement thereto. You are permitted to use and
  82221. + * redistribute this Software in source and binary forms, with or without
  82222. + * modification, provided that redistributions of source code must retain this
  82223. + * notice. You may not view, use, disclose, copy or distribute this file or
  82224. + * any information contained herein except pursuant to this license grant from
  82225. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82226. + * below, then you are not authorized to use the Software.
  82227. + *
  82228. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82229. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82230. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82231. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82232. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82233. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82234. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82235. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82236. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82237. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82238. + * DAMAGE.
  82239. + * ========================================================================== */
  82240. +#ifndef DWC_HOST_ONLY
  82241. +#if !defined(__DWC_PCD_H__)
  82242. +#define __DWC_PCD_H__
  82243. +
  82244. +#include "dwc_otg_os_dep.h"
  82245. +#include "usb.h"
  82246. +#include "dwc_otg_cil.h"
  82247. +#include "dwc_otg_pcd_if.h"
  82248. +struct cfiobject;
  82249. +
  82250. +/**
  82251. + * @file
  82252. + *
  82253. + * This file contains the structures, constants, and interfaces for
  82254. + * the Perpherial Contoller Driver (PCD).
  82255. + *
  82256. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  82257. + * Gadget API, so that the existing Gadget drivers can be used. For
  82258. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  82259. + * (FBS) driver will be used. The FBS driver supports the
  82260. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  82261. + * transports.
  82262. + *
  82263. + */
  82264. +
  82265. +/** Invalid DMA Address */
  82266. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  82267. +
  82268. +/** Max Transfer size for any EP */
  82269. +#define DDMA_MAX_TRANSFER_SIZE 65535
  82270. +
  82271. +/**
  82272. + * Get the pointer to the core_if from the pcd pointer.
  82273. + */
  82274. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  82275. +
  82276. +/**
  82277. + * States of EP0.
  82278. + */
  82279. +typedef enum ep0_state {
  82280. + EP0_DISCONNECT, /* no host */
  82281. + EP0_IDLE,
  82282. + EP0_IN_DATA_PHASE,
  82283. + EP0_OUT_DATA_PHASE,
  82284. + EP0_IN_STATUS_PHASE,
  82285. + EP0_OUT_STATUS_PHASE,
  82286. + EP0_STALL,
  82287. +} ep0state_e;
  82288. +
  82289. +/** Fordward declaration.*/
  82290. +struct dwc_otg_pcd;
  82291. +
  82292. +/** DWC_otg iso request structure.
  82293. + *
  82294. + */
  82295. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  82296. +
  82297. +#ifdef DWC_UTE_PER_IO
  82298. +
  82299. +/**
  82300. + * This shall be the exact analogy of the same type structure defined in the
  82301. + * usb_gadget.h. Each descriptor contains
  82302. + */
  82303. +struct dwc_iso_pkt_desc_port {
  82304. + uint32_t offset;
  82305. + uint32_t length; /* expected length */
  82306. + uint32_t actual_length;
  82307. + uint32_t status;
  82308. +};
  82309. +
  82310. +struct dwc_iso_xreq_port {
  82311. + /** transfer/submission flag */
  82312. + uint32_t tr_sub_flags;
  82313. + /** Start the request ASAP */
  82314. +#define DWC_EREQ_TF_ASAP 0x00000002
  82315. + /** Just enqueue the request w/o initiating a transfer */
  82316. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  82317. +
  82318. + /**
  82319. + * count of ISO packets attached to this request - shall
  82320. + * not exceed the pio_alloc_pkt_count
  82321. + */
  82322. + uint32_t pio_pkt_count;
  82323. + /** count of ISO packets allocated for this request */
  82324. + uint32_t pio_alloc_pkt_count;
  82325. + /** number of ISO packet errors */
  82326. + uint32_t error_count;
  82327. + /** reserved for future extension */
  82328. + uint32_t res;
  82329. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  82330. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  82331. +};
  82332. +#endif
  82333. +/** DWC_otg request structure.
  82334. + * This structure is a list of requests.
  82335. + */
  82336. +typedef struct dwc_otg_pcd_request {
  82337. + void *priv;
  82338. + void *buf;
  82339. + dwc_dma_t dma;
  82340. + uint32_t length;
  82341. + uint32_t actual;
  82342. + unsigned sent_zlp:1;
  82343. + /**
  82344. + * Used instead of original buffer if
  82345. + * it(physical address) is not dword-aligned.
  82346. + **/
  82347. + uint8_t *dw_align_buf;
  82348. + dwc_dma_t dw_align_buf_dma;
  82349. +
  82350. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  82351. +#ifdef DWC_UTE_PER_IO
  82352. + struct dwc_iso_xreq_port ext_req;
  82353. + //void *priv_ereq_nport; /* */
  82354. +#endif
  82355. +} dwc_otg_pcd_request_t;
  82356. +
  82357. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  82358. +
  82359. +/** PCD EP structure.
  82360. + * This structure describes an EP, there is an array of EPs in the PCD
  82361. + * structure.
  82362. + */
  82363. +typedef struct dwc_otg_pcd_ep {
  82364. + /** USB EP Descriptor */
  82365. + const usb_endpoint_descriptor_t *desc;
  82366. +
  82367. + /** queue of dwc_otg_pcd_requests. */
  82368. + struct req_list queue;
  82369. + unsigned stopped:1;
  82370. + unsigned disabling:1;
  82371. + unsigned dma:1;
  82372. + unsigned queue_sof:1;
  82373. +
  82374. +#ifdef DWC_EN_ISOC
  82375. + /** ISOC req handle passed */
  82376. + void *iso_req_handle;
  82377. +#endif //_EN_ISOC_
  82378. +
  82379. + /** DWC_otg ep data. */
  82380. + dwc_ep_t dwc_ep;
  82381. +
  82382. + /** Pointer to PCD */
  82383. + struct dwc_otg_pcd *pcd;
  82384. +
  82385. + void *priv;
  82386. +} dwc_otg_pcd_ep_t;
  82387. +
  82388. +/** DWC_otg PCD Structure.
  82389. + * This structure encapsulates the data for the dwc_otg PCD.
  82390. + */
  82391. +struct dwc_otg_pcd {
  82392. + const struct dwc_otg_pcd_function_ops *fops;
  82393. + /** The DWC otg device pointer */
  82394. + struct dwc_otg_device *otg_dev;
  82395. + /** Core Interface */
  82396. + dwc_otg_core_if_t *core_if;
  82397. + /** State of EP0 */
  82398. + ep0state_e ep0state;
  82399. + /** EP0 Request is pending */
  82400. + unsigned ep0_pending:1;
  82401. + /** Indicates when SET CONFIGURATION Request is in process */
  82402. + unsigned request_config:1;
  82403. + /** The state of the Remote Wakeup Enable. */
  82404. + unsigned remote_wakeup_enable:1;
  82405. + /** The state of the B-Device HNP Enable. */
  82406. + unsigned b_hnp_enable:1;
  82407. + /** The state of A-Device HNP Support. */
  82408. + unsigned a_hnp_support:1;
  82409. + /** The state of the A-Device Alt HNP support. */
  82410. + unsigned a_alt_hnp_support:1;
  82411. + /** Count of pending Requests */
  82412. + unsigned request_pending;
  82413. +
  82414. + /** SETUP packet for EP0
  82415. + * This structure is allocated as a DMA buffer on PCD initialization
  82416. + * with enough space for up to 3 setup packets.
  82417. + */
  82418. + union {
  82419. + usb_device_request_t req;
  82420. + uint32_t d32[2];
  82421. + } *setup_pkt;
  82422. +
  82423. + dwc_dma_t setup_pkt_dma_handle;
  82424. +
  82425. + /* Additional buffer and flag for CTRL_WR premature case */
  82426. + uint8_t *backup_buf;
  82427. + unsigned data_terminated;
  82428. +
  82429. + /** 2-byte dma buffer used to return status from GET_STATUS */
  82430. + uint16_t *status_buf;
  82431. + dwc_dma_t status_buf_dma_handle;
  82432. +
  82433. + /** EP0 */
  82434. + dwc_otg_pcd_ep_t ep0;
  82435. +
  82436. + /** Array of IN EPs. */
  82437. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  82438. + /** Array of OUT EPs. */
  82439. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  82440. + /** number of valid EPs in the above array. */
  82441. +// unsigned num_eps : 4;
  82442. + dwc_spinlock_t *lock;
  82443. +
  82444. + /** Tasklet to defer starting of TEST mode transmissions until
  82445. + * Status Phase has been completed.
  82446. + */
  82447. + dwc_tasklet_t *test_mode_tasklet;
  82448. +
  82449. + /** Tasklet to delay starting of xfer in DMA mode */
  82450. + dwc_tasklet_t *start_xfer_tasklet;
  82451. +
  82452. + /** The test mode to enter when the tasklet is executed. */
  82453. + unsigned test_mode;
  82454. + /** The cfi_api structure that implements most of the CFI API
  82455. + * and OTG specific core configuration functionality
  82456. + */
  82457. +#ifdef DWC_UTE_CFI
  82458. + struct cfiobject *cfi;
  82459. +#endif
  82460. +
  82461. +};
  82462. +
  82463. +//FIXME this functions should be static, and this prototypes should be removed
  82464. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  82465. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  82466. + dwc_otg_pcd_request_t * req, int32_t status);
  82467. +
  82468. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  82469. + void *req_handle);
  82470. +
  82471. +extern void do_test_mode(void *data);
  82472. +#endif
  82473. +#endif /* DWC_HOST_ONLY */
  82474. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  82475. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 1970-01-01 01:00:00.000000000 +0100
  82476. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 2014-03-11 17:33:06.000000000 +0100
  82477. @@ -0,0 +1,360 @@
  82478. +/* ==========================================================================
  82479. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  82480. + * $Revision: #11 $
  82481. + * $Date: 2011/10/26 $
  82482. + * $Change: 1873028 $
  82483. + *
  82484. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82485. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82486. + * otherwise expressly agreed to in writing between Synopsys and you.
  82487. + *
  82488. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82489. + * any End User Software License Agreement or Agreement for Licensed Product
  82490. + * with Synopsys or any supplement thereto. You are permitted to use and
  82491. + * redistribute this Software in source and binary forms, with or without
  82492. + * modification, provided that redistributions of source code must retain this
  82493. + * notice. You may not view, use, disclose, copy or distribute this file or
  82494. + * any information contained herein except pursuant to this license grant from
  82495. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82496. + * below, then you are not authorized to use the Software.
  82497. + *
  82498. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82499. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82500. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82501. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82502. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82503. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82504. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82505. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82506. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82507. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82508. + * DAMAGE.
  82509. + * ========================================================================== */
  82510. +#ifndef DWC_HOST_ONLY
  82511. +
  82512. +#if !defined(__DWC_PCD_IF_H__)
  82513. +#define __DWC_PCD_IF_H__
  82514. +
  82515. +//#include "dwc_os.h"
  82516. +#include "dwc_otg_core_if.h"
  82517. +
  82518. +/** @file
  82519. + * This file defines DWC_OTG PCD Core API.
  82520. + */
  82521. +
  82522. +struct dwc_otg_pcd;
  82523. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  82524. +
  82525. +/** Maxpacket size for EP0 */
  82526. +#define MAX_EP0_SIZE 64
  82527. +/** Maxpacket size for any EP */
  82528. +#define MAX_PACKET_SIZE 1024
  82529. +
  82530. +/** @name Function Driver Callbacks */
  82531. +/** @{ */
  82532. +
  82533. +/** This function will be called whenever a previously queued request has
  82534. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  82535. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  82536. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  82537. + * parameters. */
  82538. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  82539. + void *req_handle, int32_t status,
  82540. + uint32_t actual);
  82541. +/**
  82542. + * This function will be called whenever a previousle queued ISOC request has
  82543. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  82544. + * function.
  82545. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  82546. + * functions.
  82547. + */
  82548. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  82549. + void *req_handle, int proc_buf_num);
  82550. +/** This function should handle any SETUP request that cannot be handled by the
  82551. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  82552. + * class-specific requests, etc. The function must non-blocking.
  82553. + *
  82554. + * Returns 0 on success.
  82555. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  82556. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  82557. + * Returns -DWC_E_SHUTDOWN on any other error. */
  82558. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  82559. +/** This is called whenever the device has been disconnected. The function
  82560. + * driver should take appropriate action to clean up all pending requests in the
  82561. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  82562. + * state. */
  82563. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  82564. +/** This function is called when device has been connected. */
  82565. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  82566. +/** This function is called when device has been suspended */
  82567. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  82568. +/** This function is called when device has received LPM tokens, i.e.
  82569. + * device has been sent to sleep state. */
  82570. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  82571. +/** This function is called when device has been resumed
  82572. + * from suspend(L2) or L1 sleep state. */
  82573. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  82574. +/** This function is called whenever hnp params has been changed.
  82575. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  82576. + * to get hnp parameters. */
  82577. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  82578. +/** This function is called whenever USB RESET is detected. */
  82579. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  82580. +
  82581. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  82582. +
  82583. +/**
  82584. + *
  82585. + * @param ep_handle Void pointer to the usb_ep structure
  82586. + * @param ereq_port Pointer to the extended request structure created in the
  82587. + * portable part.
  82588. + */
  82589. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  82590. + void *req_handle, int32_t status,
  82591. + void *ereq_port);
  82592. +/** Function Driver Ops Data Structure */
  82593. +struct dwc_otg_pcd_function_ops {
  82594. + dwc_connect_cb_t connect;
  82595. + dwc_disconnect_cb_t disconnect;
  82596. + dwc_setup_cb_t setup;
  82597. + dwc_completion_cb_t complete;
  82598. + dwc_isoc_completion_cb_t isoc_complete;
  82599. + dwc_suspend_cb_t suspend;
  82600. + dwc_sleep_cb_t sleep;
  82601. + dwc_resume_cb_t resume;
  82602. + dwc_reset_cb_t reset;
  82603. + dwc_hnp_params_changed_cb_t hnp_changed;
  82604. + cfi_setup_cb_t cfi_setup;
  82605. +#ifdef DWC_UTE_PER_IO
  82606. + xiso_completion_cb_t xisoc_complete;
  82607. +#endif
  82608. +};
  82609. +/** @} */
  82610. +
  82611. +/** @name Function Driver Functions */
  82612. +/** @{ */
  82613. +
  82614. +/** Call this function to get pointer on dwc_otg_pcd_t,
  82615. + * this pointer will be used for all PCD API functions.
  82616. + *
  82617. + * @param core_if The DWC_OTG Core
  82618. + */
  82619. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if);
  82620. +
  82621. +/** Frees PCD allocated by dwc_otg_pcd_init
  82622. + *
  82623. + * @param pcd The PCD
  82624. + */
  82625. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  82626. +
  82627. +/** Call this to bind the function driver to the PCD Core.
  82628. + *
  82629. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  82630. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  82631. + */
  82632. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  82633. + const struct dwc_otg_pcd_function_ops *fops);
  82634. +
  82635. +/** Enables an endpoint for use. This function enables an endpoint in
  82636. + * the PCD. The endpoint is described by the ep_desc which has the
  82637. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  82638. + * to the endpoint from other API functions and in callbacks. Normally this
  82639. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  82640. + * core for that interface.
  82641. + *
  82642. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82643. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82644. + * Returns 0 on success.
  82645. + *
  82646. + * @param pcd The PCD
  82647. + * @param ep_desc Endpoint descriptor
  82648. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  82649. + */
  82650. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  82651. + const uint8_t * ep_desc, void *usb_ep);
  82652. +
  82653. +/** Disable the endpoint referenced by ep_handle.
  82654. + *
  82655. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82656. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  82657. + * Returns 0 on success. */
  82658. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  82659. +
  82660. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  82661. + * After the transfer is completes, the complete callback will be called with
  82662. + * the request status.
  82663. + *
  82664. + * @param pcd The PCD
  82665. + * @param ep_handle The handle of the endpoint
  82666. + * @param buf The buffer for the data
  82667. + * @param dma_buf The DMA buffer for the data
  82668. + * @param buflen The length of the data transfer
  82669. + * @param zero Specifies whether to send zero length last packet.
  82670. + * @param req_handle Set this handle to any value to use to reference this
  82671. + * request in the ep_dequeue function or from the complete callback
  82672. + * @param atomic_alloc If driver need to perform atomic allocations
  82673. + * for internal data structures.
  82674. + *
  82675. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82676. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82677. + * Returns 0 on success. */
  82678. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82679. + uint8_t * buf, dwc_dma_t dma_buf,
  82680. + uint32_t buflen, int zero, void *req_handle,
  82681. + int atomic_alloc);
  82682. +#ifdef DWC_UTE_PER_IO
  82683. +/**
  82684. + *
  82685. + * @param ereq_nonport Pointer to the extended request part of the
  82686. + * usb_request structure defined in usb_gadget.h file.
  82687. + */
  82688. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82689. + uint8_t * buf, dwc_dma_t dma_buf,
  82690. + uint32_t buflen, int zero,
  82691. + void *req_handle, int atomic_alloc,
  82692. + void *ereq_nonport);
  82693. +
  82694. +#endif
  82695. +
  82696. +/** De-queue the specified data transfer that has not yet completed.
  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_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82702. + void *req_handle);
  82703. +
  82704. +/** Halt (STALL) an endpoint or clear it.
  82705. + *
  82706. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82707. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82708. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  82709. + * Returns 0 on success. */
  82710. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  82711. +
  82712. +/** This function */
  82713. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  82714. +
  82715. +/** This function should be called on every hardware interrupt */
  82716. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  82717. +
  82718. +/** This function returns current frame number */
  82719. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  82720. +
  82721. +/**
  82722. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  82723. + * For isochronous transfers duble buffering is used.
  82724. + * After processing each of buffers comlete callback will be called with
  82725. + * status for each transaction.
  82726. + *
  82727. + * @param pcd The PCD
  82728. + * @param ep_handle The handle of the endpoint
  82729. + * @param buf0 The virtual address of first data buffer
  82730. + * @param buf1 The virtual address of second data buffer
  82731. + * @param dma0 The DMA address of first data buffer
  82732. + * @param dma1 The DMA address of second data buffer
  82733. + * @param sync_frame Data pattern frame number
  82734. + * @param dp_frame Data size for pattern frame
  82735. + * @param data_per_frame Data size for regular frame
  82736. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  82737. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  82738. + * @param req_handle Handle of ISOC request
  82739. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  82740. + * internal data structures.
  82741. + *
  82742. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  82743. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  82744. + * Returns -DW_E_SHUTDOWN for any other error.
  82745. + * Returns 0 on success
  82746. + */
  82747. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  82748. + uint8_t * buf0, uint8_t * buf1,
  82749. + dwc_dma_t dma0, dwc_dma_t dma1,
  82750. + int sync_frame, int dp_frame,
  82751. + int data_per_frame, int start_frame,
  82752. + int buf_proc_intrvl, void *req_handle,
  82753. + int atomic_alloc);
  82754. +
  82755. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  82756. + *
  82757. + * @param pcd The PCD
  82758. + * @param ep_handle The handle of the endpoint
  82759. + * @param req_handle Handle of ISOC request
  82760. + *
  82761. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  82762. + * Returns 0 on success
  82763. + */
  82764. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  82765. + void *req_handle);
  82766. +
  82767. +/** Get ISOC packet status.
  82768. + *
  82769. + * @param pcd The PCD
  82770. + * @param ep_handle The handle of the endpoint
  82771. + * @param iso_req_handle Isochronoush request handle
  82772. + * @param packet Number of packet
  82773. + * @param status Out parameter for returning status
  82774. + * @param actual Out parameter for returning actual length
  82775. + * @param offset Out parameter for returning offset
  82776. + *
  82777. + */
  82778. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  82779. + void *ep_handle,
  82780. + void *iso_req_handle, int packet,
  82781. + int *status, int *actual,
  82782. + int *offset);
  82783. +
  82784. +/** Get ISOC packet count.
  82785. + *
  82786. + * @param pcd The PCD
  82787. + * @param ep_handle The handle of the endpoint
  82788. + * @param iso_req_handle
  82789. + */
  82790. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  82791. + void *ep_handle,
  82792. + void *iso_req_handle);
  82793. +
  82794. +/** This function starts the SRP Protocol if no session is in progress. If
  82795. + * a session is already in progress, but the device is suspended,
  82796. + * remote wakeup signaling is started.
  82797. + */
  82798. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  82799. +
  82800. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  82801. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  82802. +
  82803. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  82804. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  82805. +
  82806. +/** Initiate SRP */
  82807. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  82808. +
  82809. +/** Starts remote wakeup signaling. */
  82810. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  82811. +
  82812. +/** Starts micorsecond soft disconnect. */
  82813. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  82814. +/** This function returns whether device is dualspeed.*/
  82815. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  82816. +
  82817. +/** This function returns whether device is otg. */
  82818. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  82819. +
  82820. +/** These functions allow to get hnp parameters */
  82821. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  82822. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  82823. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  82824. +
  82825. +/** CFI specific Interface functions */
  82826. +/** Allocate a cfi buffer */
  82827. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  82828. + dwc_dma_t * addr, size_t buflen,
  82829. + int flags);
  82830. +
  82831. +/******************************************************************************/
  82832. +
  82833. +/** @} */
  82834. +
  82835. +#endif /* __DWC_PCD_IF_H__ */
  82836. +
  82837. +#endif /* DWC_HOST_ONLY */
  82838. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  82839. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  82840. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 2014-03-11 17:51:27.000000000 +0100
  82841. @@ -0,0 +1,5147 @@
  82842. +/* ==========================================================================
  82843. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  82844. + * $Revision: #116 $
  82845. + * $Date: 2012/08/10 $
  82846. + * $Change: 2047372 $
  82847. + *
  82848. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82849. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82850. + * otherwise expressly agreed to in writing between Synopsys and you.
  82851. + *
  82852. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82853. + * any End User Software License Agreement or Agreement for Licensed Product
  82854. + * with Synopsys or any supplement thereto. You are permitted to use and
  82855. + * redistribute this Software in source and binary forms, with or without
  82856. + * modification, provided that redistributions of source code must retain this
  82857. + * notice. You may not view, use, disclose, copy or distribute this file or
  82858. + * any information contained herein except pursuant to this license grant from
  82859. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82860. + * below, then you are not authorized to use the Software.
  82861. + *
  82862. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82863. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82864. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82865. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82866. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82867. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82868. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82869. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82870. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82871. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82872. + * DAMAGE.
  82873. + * ========================================================================== */
  82874. +#ifndef DWC_HOST_ONLY
  82875. +
  82876. +#include "dwc_otg_pcd.h"
  82877. +
  82878. +#ifdef DWC_UTE_CFI
  82879. +#include "dwc_otg_cfi.h"
  82880. +#endif
  82881. +
  82882. +#ifdef DWC_UTE_PER_IO
  82883. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  82884. +#endif
  82885. +//#define PRINT_CFI_DMA_DESCS
  82886. +
  82887. +#define DEBUG_EP0
  82888. +
  82889. +/**
  82890. + * This function updates OTG.
  82891. + */
  82892. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  82893. +{
  82894. +
  82895. + if (reset) {
  82896. + pcd->b_hnp_enable = 0;
  82897. + pcd->a_hnp_support = 0;
  82898. + pcd->a_alt_hnp_support = 0;
  82899. + }
  82900. +
  82901. + if (pcd->fops->hnp_changed) {
  82902. + pcd->fops->hnp_changed(pcd);
  82903. + }
  82904. +}
  82905. +
  82906. +/** @file
  82907. + * This file contains the implementation of the PCD Interrupt handlers.
  82908. + *
  82909. + * The PCD handles the device interrupts. Many conditions can cause a
  82910. + * device interrupt. When an interrupt occurs, the device interrupt
  82911. + * service routine determines the cause of the interrupt and
  82912. + * dispatches handling to the appropriate function. These interrupt
  82913. + * handling functions are described below.
  82914. + * All interrupt registers are processed from LSB to MSB.
  82915. + */
  82916. +
  82917. +/**
  82918. + * This function prints the ep0 state for debug purposes.
  82919. + */
  82920. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  82921. +{
  82922. +#ifdef DEBUG
  82923. + char str[40];
  82924. +
  82925. + switch (pcd->ep0state) {
  82926. + case EP0_DISCONNECT:
  82927. + dwc_strcpy(str, "EP0_DISCONNECT");
  82928. + break;
  82929. + case EP0_IDLE:
  82930. + dwc_strcpy(str, "EP0_IDLE");
  82931. + break;
  82932. + case EP0_IN_DATA_PHASE:
  82933. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  82934. + break;
  82935. + case EP0_OUT_DATA_PHASE:
  82936. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  82937. + break;
  82938. + case EP0_IN_STATUS_PHASE:
  82939. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  82940. + break;
  82941. + case EP0_OUT_STATUS_PHASE:
  82942. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  82943. + break;
  82944. + case EP0_STALL:
  82945. + dwc_strcpy(str, "EP0_STALL");
  82946. + break;
  82947. + default:
  82948. + dwc_strcpy(str, "EP0_INVALID");
  82949. + }
  82950. +
  82951. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  82952. +#endif
  82953. +}
  82954. +
  82955. +/**
  82956. + * This function calculate the size of the payload in the memory
  82957. + * for out endpoints and prints size for debug purposes(used in
  82958. + * 2.93a DevOutNak feature).
  82959. + */
  82960. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  82961. +{
  82962. +#ifdef DEBUG
  82963. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  82964. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  82965. + int pack_num;
  82966. + unsigned payload;
  82967. +
  82968. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  82969. + deptsiz_updt.d32 =
  82970. + DWC_READ_REG32(&pcd->core_if->dev_if->
  82971. + out_ep_regs[ep->num]->doeptsiz);
  82972. + /* Payload will be */
  82973. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  82974. + /* Packet count is decremented every time a packet
  82975. + * is written to the RxFIFO not in to the external memory
  82976. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  82977. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  82978. + DWC_DEBUGPL(DBG_PCDV,
  82979. + "Payload for EP%d-%s\n",
  82980. + ep->num, (ep->is_in ? "IN" : "OUT"));
  82981. + DWC_DEBUGPL(DBG_PCDV,
  82982. + "Number of transfered bytes = 0x%08x\n", payload);
  82983. + DWC_DEBUGPL(DBG_PCDV,
  82984. + "Number of transfered packets = %d\n", pack_num);
  82985. +#endif
  82986. +}
  82987. +
  82988. +
  82989. +#ifdef DWC_UTE_CFI
  82990. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  82991. + const uint8_t * epname, int descnum)
  82992. +{
  82993. + CFI_INFO
  82994. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  82995. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  82996. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  82997. + ddesc->status.b.bs);
  82998. +}
  82999. +#endif
  83000. +
  83001. +/**
  83002. + * This function returns pointer to in ep struct with number ep_num
  83003. + */
  83004. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  83005. +{
  83006. + int i;
  83007. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  83008. + if (ep_num == 0) {
  83009. + return &pcd->ep0;
  83010. + } else {
  83011. + for (i = 0; i < num_in_eps; ++i) {
  83012. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  83013. + return &pcd->in_ep[i];
  83014. + }
  83015. + return 0;
  83016. + }
  83017. +}
  83018. +
  83019. +/**
  83020. + * This function returns pointer to out ep struct with number ep_num
  83021. + */
  83022. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  83023. +{
  83024. + int i;
  83025. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  83026. + if (ep_num == 0) {
  83027. + return &pcd->ep0;
  83028. + } else {
  83029. + for (i = 0; i < num_out_eps; ++i) {
  83030. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  83031. + return &pcd->out_ep[i];
  83032. + }
  83033. + return 0;
  83034. + }
  83035. +}
  83036. +
  83037. +/**
  83038. + * This functions gets a pointer to an EP from the wIndex address
  83039. + * value of the control request.
  83040. + */
  83041. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  83042. +{
  83043. + dwc_otg_pcd_ep_t *ep;
  83044. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  83045. +
  83046. + if (ep_num == 0) {
  83047. + ep = &pcd->ep0;
  83048. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  83049. + ep = &pcd->in_ep[ep_num - 1];
  83050. + } else {
  83051. + ep = &pcd->out_ep[ep_num - 1];
  83052. + }
  83053. +
  83054. + return ep;
  83055. +}
  83056. +
  83057. +/**
  83058. + * This function checks the EP request queue, if the queue is not
  83059. + * empty the next request is started.
  83060. + */
  83061. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  83062. +{
  83063. + dwc_otg_pcd_request_t *req = 0;
  83064. + uint32_t max_transfer =
  83065. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  83066. +
  83067. +#ifdef DWC_UTE_CFI
  83068. + struct dwc_otg_pcd *pcd;
  83069. + pcd = ep->pcd;
  83070. +#endif
  83071. +
  83072. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  83073. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  83074. +
  83075. +#ifdef DWC_UTE_CFI
  83076. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  83077. + ep->dwc_ep.cfi_req_len = req->length;
  83078. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  83079. + } else {
  83080. +#endif
  83081. + /* Setup and start the Transfer */
  83082. + if (req->dw_align_buf) {
  83083. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  83084. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  83085. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  83086. + } else {
  83087. + ep->dwc_ep.dma_addr = req->dma;
  83088. + ep->dwc_ep.start_xfer_buff = req->buf;
  83089. + ep->dwc_ep.xfer_buff = req->buf;
  83090. + }
  83091. + ep->dwc_ep.sent_zlp = 0;
  83092. + ep->dwc_ep.total_len = req->length;
  83093. + ep->dwc_ep.xfer_len = 0;
  83094. + ep->dwc_ep.xfer_count = 0;
  83095. +
  83096. + ep->dwc_ep.maxxfer = max_transfer;
  83097. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  83098. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  83099. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  83100. + if (ep->dwc_ep.is_in) {
  83101. + if (ep->dwc_ep.maxxfer >
  83102. + DDMA_MAX_TRANSFER_SIZE) {
  83103. + ep->dwc_ep.maxxfer =
  83104. + DDMA_MAX_TRANSFER_SIZE;
  83105. + }
  83106. + } else {
  83107. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  83108. + ep->dwc_ep.maxxfer =
  83109. + out_max_xfer;
  83110. + }
  83111. + }
  83112. + }
  83113. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  83114. + ep->dwc_ep.maxxfer -=
  83115. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  83116. + }
  83117. + if (req->sent_zlp) {
  83118. + if ((ep->dwc_ep.total_len %
  83119. + ep->dwc_ep.maxpacket == 0)
  83120. + && (ep->dwc_ep.total_len != 0)) {
  83121. + ep->dwc_ep.sent_zlp = 1;
  83122. + }
  83123. +
  83124. + }
  83125. +#ifdef DWC_UTE_CFI
  83126. + }
  83127. +#endif
  83128. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  83129. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  83130. + DWC_PRINTF("There are no more ISOC requests \n");
  83131. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  83132. + }
  83133. +}
  83134. +
  83135. +/**
  83136. + * This function handles the SOF Interrupts. At this time the SOF
  83137. + * Interrupt is disabled.
  83138. + */
  83139. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  83140. +{
  83141. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83142. +
  83143. + gintsts_data_t gintsts;
  83144. +
  83145. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  83146. +
  83147. + /* Clear interrupt */
  83148. + gintsts.d32 = 0;
  83149. + gintsts.b.sofintr = 1;
  83150. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  83151. +
  83152. + return 1;
  83153. +}
  83154. +
  83155. +/**
  83156. + * This function handles the Rx Status Queue Level Interrupt, which
  83157. + * indicates that there is a least one packet in the Rx FIFO. The
  83158. + * packets are moved from the FIFO to memory, where they will be
  83159. + * processed when the Endpoint Interrupt Register indicates Transfer
  83160. + * Complete or SETUP Phase Done.
  83161. + *
  83162. + * Repeat the following until the Rx Status Queue is empty:
  83163. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  83164. + * info
  83165. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  83166. + * and exit
  83167. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  83168. + * SETUP data to the buffer
  83169. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  83170. + * to the destination buffer
  83171. + */
  83172. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  83173. +{
  83174. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83175. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  83176. + gintmsk_data_t gintmask = {.d32 = 0 };
  83177. + device_grxsts_data_t status;
  83178. + dwc_otg_pcd_ep_t *ep;
  83179. + gintsts_data_t gintsts;
  83180. +#ifdef DEBUG
  83181. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  83182. +#endif
  83183. +
  83184. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  83185. + /* Disable the Rx Status Queue Level interrupt */
  83186. + gintmask.b.rxstsqlvl = 1;
  83187. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  83188. +
  83189. + /* Get the Status from the top of the FIFO */
  83190. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  83191. +
  83192. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  83193. + "pktsts:%x Frame:%d(0x%0x)\n",
  83194. + status.b.epnum, status.b.bcnt,
  83195. + dpid_str[status.b.dpid],
  83196. + status.b.pktsts, status.b.fn, status.b.fn);
  83197. + /* Get pointer to EP structure */
  83198. + ep = get_out_ep(pcd, status.b.epnum);
  83199. +
  83200. + switch (status.b.pktsts) {
  83201. + case DWC_DSTS_GOUT_NAK:
  83202. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  83203. + break;
  83204. + case DWC_STS_DATA_UPDT:
  83205. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  83206. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  83207. + /** @todo NGS Check for buffer overflow? */
  83208. + dwc_otg_read_packet(core_if,
  83209. + ep->dwc_ep.xfer_buff,
  83210. + status.b.bcnt);
  83211. + ep->dwc_ep.xfer_count += status.b.bcnt;
  83212. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  83213. + }
  83214. + break;
  83215. + case DWC_STS_XFER_COMP:
  83216. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  83217. + break;
  83218. + case DWC_DSTS_SETUP_COMP:
  83219. +#ifdef DEBUG_EP0
  83220. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  83221. +#endif
  83222. + break;
  83223. + case DWC_DSTS_SETUP_UPDT:
  83224. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  83225. +#ifdef DEBUG_EP0
  83226. + DWC_DEBUGPL(DBG_PCD,
  83227. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  83228. + pcd->setup_pkt->req.bmRequestType,
  83229. + pcd->setup_pkt->req.bRequest,
  83230. + UGETW(pcd->setup_pkt->req.wValue),
  83231. + UGETW(pcd->setup_pkt->req.wIndex),
  83232. + UGETW(pcd->setup_pkt->req.wLength));
  83233. +#endif
  83234. + ep->dwc_ep.xfer_count += status.b.bcnt;
  83235. + break;
  83236. + default:
  83237. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  83238. + status.b.pktsts);
  83239. + break;
  83240. + }
  83241. +
  83242. + /* Enable the Rx Status Queue Level interrupt */
  83243. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  83244. + /* Clear interrupt */
  83245. + gintsts.d32 = 0;
  83246. + gintsts.b.rxstsqlvl = 1;
  83247. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  83248. +
  83249. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  83250. + return 1;
  83251. +}
  83252. +
  83253. +/**
  83254. + * This function examines the Device IN Token Learning Queue to
  83255. + * determine the EP number of the last IN token received. This
  83256. + * implementation is for the Mass Storage device where there are only
  83257. + * 2 IN EPs (Control-IN and BULK-IN).
  83258. + *
  83259. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  83260. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  83261. + *
  83262. + * @param core_if Programming view of DWC_otg controller.
  83263. + *
  83264. + */
  83265. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  83266. +{
  83267. + dwc_otg_device_global_regs_t *dev_global_regs =
  83268. + core_if->dev_if->dev_global_regs;
  83269. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  83270. + /* Number of Token Queue Registers */
  83271. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  83272. + dtknq1_data_t dtknqr1;
  83273. + uint32_t in_tkn_epnums[4];
  83274. + int ndx = 0;
  83275. + int i = 0;
  83276. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  83277. + int epnum = 0;
  83278. +
  83279. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  83280. +
  83281. + /* Read the DTKNQ Registers */
  83282. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  83283. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  83284. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  83285. + in_tkn_epnums[i]);
  83286. + if (addr == &dev_global_regs->dvbusdis) {
  83287. + addr = &dev_global_regs->dtknqr3_dthrctl;
  83288. + } else {
  83289. + ++addr;
  83290. + }
  83291. +
  83292. + }
  83293. +
  83294. + /* Copy the DTKNQR1 data to the bit field. */
  83295. + dtknqr1.d32 = in_tkn_epnums[0];
  83296. + /* Get the EP numbers */
  83297. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  83298. + ndx = dtknqr1.b.intknwptr - 1;
  83299. +
  83300. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  83301. + if (ndx == -1) {
  83302. + /** @todo Find a simpler way to calculate the max
  83303. + * queue position.*/
  83304. + int cnt = TOKEN_Q_DEPTH;
  83305. + if (TOKEN_Q_DEPTH <= 6) {
  83306. + cnt = TOKEN_Q_DEPTH - 1;
  83307. + } else if (TOKEN_Q_DEPTH <= 14) {
  83308. + cnt = TOKEN_Q_DEPTH - 7;
  83309. + } else if (TOKEN_Q_DEPTH <= 22) {
  83310. + cnt = TOKEN_Q_DEPTH - 15;
  83311. + } else {
  83312. + cnt = TOKEN_Q_DEPTH - 23;
  83313. + }
  83314. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  83315. + } else {
  83316. + if (ndx <= 5) {
  83317. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  83318. + } else if (ndx <= 13) {
  83319. + ndx -= 6;
  83320. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  83321. + } else if (ndx <= 21) {
  83322. + ndx -= 14;
  83323. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  83324. + } else if (ndx <= 29) {
  83325. + ndx -= 22;
  83326. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  83327. + }
  83328. + }
  83329. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  83330. + return epnum;
  83331. +}
  83332. +
  83333. +/**
  83334. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  83335. + * The active request is checked for the next packet to be loaded into
  83336. + * the non-periodic Tx FIFO.
  83337. + */
  83338. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  83339. +{
  83340. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83341. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  83342. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  83343. + gnptxsts_data_t txstatus = {.d32 = 0 };
  83344. + gintsts_data_t gintsts;
  83345. +
  83346. + int epnum = 0;
  83347. + dwc_otg_pcd_ep_t *ep = 0;
  83348. + uint32_t len = 0;
  83349. + int dwords;
  83350. +
  83351. + /* Get the epnum from the IN Token Learning Queue. */
  83352. + epnum = get_ep_of_last_in_token(core_if);
  83353. + ep = get_in_ep(pcd, epnum);
  83354. +
  83355. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  83356. +
  83357. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  83358. +
  83359. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83360. + if (len > ep->dwc_ep.maxpacket) {
  83361. + len = ep->dwc_ep.maxpacket;
  83362. + }
  83363. + dwords = (len + 3) / 4;
  83364. +
  83365. + /* While there is space in the queue and space in the FIFO and
  83366. + * More data to tranfer, Write packets to the Tx FIFO */
  83367. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  83368. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  83369. +
  83370. + while (txstatus.b.nptxqspcavail > 0 &&
  83371. + txstatus.b.nptxfspcavail > dwords &&
  83372. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  83373. + /* Write the FIFO */
  83374. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  83375. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83376. +
  83377. + if (len > ep->dwc_ep.maxpacket) {
  83378. + len = ep->dwc_ep.maxpacket;
  83379. + }
  83380. +
  83381. + dwords = (len + 3) / 4;
  83382. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  83383. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  83384. + }
  83385. +
  83386. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  83387. + DWC_READ_REG32(&global_regs->gnptxsts));
  83388. +
  83389. + /* Clear interrupt */
  83390. + gintsts.d32 = 0;
  83391. + gintsts.b.nptxfempty = 1;
  83392. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  83393. +
  83394. + return 1;
  83395. +}
  83396. +
  83397. +/**
  83398. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  83399. + * The active request is checked for the next packet to be loaded into
  83400. + * apropriate Tx FIFO.
  83401. + */
  83402. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  83403. +{
  83404. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83405. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83406. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  83407. + dtxfsts_data_t txstatus = {.d32 = 0 };
  83408. + dwc_otg_pcd_ep_t *ep = 0;
  83409. + uint32_t len = 0;
  83410. + int dwords;
  83411. +
  83412. + ep = get_in_ep(pcd, epnum);
  83413. +
  83414. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  83415. +
  83416. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  83417. +
  83418. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83419. +
  83420. + if (len > ep->dwc_ep.maxpacket) {
  83421. + len = ep->dwc_ep.maxpacket;
  83422. + }
  83423. +
  83424. + dwords = (len + 3) / 4;
  83425. +
  83426. + /* While there is space in the queue and space in the FIFO and
  83427. + * More data to tranfer, Write packets to the Tx FIFO */
  83428. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  83429. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  83430. +
  83431. + while (txstatus.b.txfspcavail > dwords &&
  83432. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  83433. + ep->dwc_ep.xfer_len != 0) {
  83434. + /* Write the FIFO */
  83435. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  83436. +
  83437. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83438. + if (len > ep->dwc_ep.maxpacket) {
  83439. + len = ep->dwc_ep.maxpacket;
  83440. + }
  83441. +
  83442. + dwords = (len + 3) / 4;
  83443. + txstatus.d32 =
  83444. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  83445. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  83446. + txstatus.d32);
  83447. + }
  83448. +
  83449. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  83450. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  83451. +
  83452. + return 1;
  83453. +}
  83454. +
  83455. +/**
  83456. + * This function is called when the Device is disconnected. It stops
  83457. + * any active requests and informs the Gadget driver of the
  83458. + * disconnect.
  83459. + */
  83460. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  83461. +{
  83462. + int i, num_in_eps, num_out_eps;
  83463. + dwc_otg_pcd_ep_t *ep;
  83464. +
  83465. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83466. +
  83467. + DWC_SPINLOCK(pcd->lock);
  83468. +
  83469. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  83470. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  83471. +
  83472. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  83473. + /* don't disconnect drivers more than once */
  83474. + if (pcd->ep0state == EP0_DISCONNECT) {
  83475. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  83476. + DWC_SPINUNLOCK(pcd->lock);
  83477. + return;
  83478. + }
  83479. + pcd->ep0state = EP0_DISCONNECT;
  83480. +
  83481. + /* Reset the OTG state. */
  83482. + dwc_otg_pcd_update_otg(pcd, 1);
  83483. +
  83484. + /* Disable the NP Tx Fifo Empty Interrupt. */
  83485. + intr_mask.b.nptxfempty = 1;
  83486. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  83487. + intr_mask.d32, 0);
  83488. +
  83489. + /* Flush the FIFOs */
  83490. + /**@todo NGS Flush Periodic FIFOs */
  83491. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  83492. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  83493. +
  83494. + /* prevent new request submissions, kill any outstanding requests */
  83495. + ep = &pcd->ep0;
  83496. + dwc_otg_request_nuke(ep);
  83497. + /* prevent new request submissions, kill any outstanding requests */
  83498. + for (i = 0; i < num_in_eps; i++) {
  83499. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  83500. + dwc_otg_request_nuke(ep);
  83501. + }
  83502. + /* prevent new request submissions, kill any outstanding requests */
  83503. + for (i = 0; i < num_out_eps; i++) {
  83504. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  83505. + dwc_otg_request_nuke(ep);
  83506. + }
  83507. +
  83508. + /* report disconnect; the driver is already quiesced */
  83509. + if (pcd->fops->disconnect) {
  83510. + DWC_SPINUNLOCK(pcd->lock);
  83511. + pcd->fops->disconnect(pcd);
  83512. + DWC_SPINLOCK(pcd->lock);
  83513. + }
  83514. + DWC_SPINUNLOCK(pcd->lock);
  83515. +}
  83516. +
  83517. +/**
  83518. + * This interrupt indicates that ...
  83519. + */
  83520. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  83521. +{
  83522. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83523. + gintsts_data_t gintsts;
  83524. +
  83525. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  83526. + intr_mask.b.i2cintr = 1;
  83527. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  83528. + intr_mask.d32, 0);
  83529. +
  83530. + /* Clear interrupt */
  83531. + gintsts.d32 = 0;
  83532. + gintsts.b.i2cintr = 1;
  83533. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83534. + gintsts.d32);
  83535. + return 1;
  83536. +}
  83537. +
  83538. +/**
  83539. + * This interrupt indicates that ...
  83540. + */
  83541. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  83542. +{
  83543. + gintsts_data_t gintsts;
  83544. +#if defined(VERBOSE)
  83545. + DWC_PRINTF("Early Suspend Detected\n");
  83546. +#endif
  83547. +
  83548. + /* Clear interrupt */
  83549. + gintsts.d32 = 0;
  83550. + gintsts.b.erlysuspend = 1;
  83551. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83552. + gintsts.d32);
  83553. + return 1;
  83554. +}
  83555. +
  83556. +/**
  83557. + * This function configures EPO to receive SETUP packets.
  83558. + *
  83559. + * @todo NGS: Update the comments from the HW FS.
  83560. + *
  83561. + * -# Program the following fields in the endpoint specific registers
  83562. + * for Control OUT EP 0, in order to receive a setup packet
  83563. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  83564. + * setup packets)
  83565. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  83566. + * to back setup packets)
  83567. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  83568. + * store any setup packets received
  83569. + *
  83570. + * @param core_if Programming view of DWC_otg controller.
  83571. + * @param pcd Programming view of the PCD.
  83572. + */
  83573. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  83574. + dwc_otg_pcd_t * pcd)
  83575. +{
  83576. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83577. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  83578. + dwc_otg_dev_dma_desc_t *dma_desc;
  83579. + depctl_data_t doepctl = {.d32 = 0 };
  83580. +
  83581. +#ifdef VERBOSE
  83582. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  83583. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  83584. +#endif
  83585. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  83586. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  83587. + if (doepctl.b.epena) {
  83588. + return;
  83589. + }
  83590. + }
  83591. +
  83592. + doeptsize0.b.supcnt = 3;
  83593. + doeptsize0.b.pktcnt = 1;
  83594. + doeptsize0.b.xfersize = 8 * 3;
  83595. +
  83596. + if (core_if->dma_enable) {
  83597. + if (!core_if->dma_desc_enable) {
  83598. + /** put here as for Hermes mode deptisz register should not be written */
  83599. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  83600. + doeptsize0.d32);
  83601. +
  83602. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  83603. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  83604. + pcd->setup_pkt_dma_handle);
  83605. + } else {
  83606. + dev_if->setup_desc_index =
  83607. + (dev_if->setup_desc_index + 1) & 1;
  83608. + dma_desc =
  83609. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  83610. +
  83611. + /** DMA Descriptor Setup */
  83612. + dma_desc->status.b.bs = BS_HOST_BUSY;
  83613. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  83614. + dma_desc->status.b.sr = 0;
  83615. + dma_desc->status.b.mtrf = 0;
  83616. + }
  83617. + dma_desc->status.b.l = 1;
  83618. + dma_desc->status.b.ioc = 1;
  83619. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  83620. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  83621. + dma_desc->status.b.sts = 0;
  83622. + dma_desc->status.b.bs = BS_HOST_READY;
  83623. +
  83624. + /** DOEPDMA0 Register write */
  83625. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  83626. + dev_if->dma_setup_desc_addr
  83627. + [dev_if->setup_desc_index]);
  83628. + }
  83629. +
  83630. + } else {
  83631. + /** put here as for Hermes mode deptisz register should not be written */
  83632. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  83633. + doeptsize0.d32);
  83634. + }
  83635. +
  83636. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  83637. + doepctl.d32 = 0;
  83638. + doepctl.b.epena = 1;
  83639. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  83640. + doepctl.b.cnak = 1;
  83641. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  83642. + } else {
  83643. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  83644. + }
  83645. +
  83646. +#ifdef VERBOSE
  83647. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  83648. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  83649. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  83650. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  83651. +#endif
  83652. +}
  83653. +
  83654. +/**
  83655. + * This interrupt occurs when a USB Reset is detected. When the USB
  83656. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  83657. + * EP0 state is set to IDLE.
  83658. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  83659. + * -# Unmask the following interrupt bits
  83660. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  83661. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  83662. + * - DOEPMSK.SETUP = 1
  83663. + * - DOEPMSK.XferCompl = 1
  83664. + * - DIEPMSK.XferCompl = 1
  83665. + * - DIEPMSK.TimeOut = 1
  83666. + * -# Program the following fields in the endpoint specific registers
  83667. + * for Control OUT EP 0, in order to receive a setup packet
  83668. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  83669. + * setup packets)
  83670. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  83671. + * to back setup packets)
  83672. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  83673. + * store any setup packets received
  83674. + * At this point, all the required initialization, except for enabling
  83675. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  83676. + */
  83677. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  83678. +{
  83679. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83680. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83681. + depctl_data_t doepctl = {.d32 = 0 };
  83682. + depctl_data_t diepctl = {.d32 = 0 };
  83683. + daint_data_t daintmsk = {.d32 = 0 };
  83684. + doepmsk_data_t doepmsk = {.d32 = 0 };
  83685. + diepmsk_data_t diepmsk = {.d32 = 0 };
  83686. + dcfg_data_t dcfg = {.d32 = 0 };
  83687. + grstctl_t resetctl = {.d32 = 0 };
  83688. + dctl_data_t dctl = {.d32 = 0 };
  83689. + int i = 0;
  83690. + gintsts_data_t gintsts;
  83691. + pcgcctl_data_t power = {.d32 = 0 };
  83692. +
  83693. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  83694. + if (power.b.stoppclk) {
  83695. + power.d32 = 0;
  83696. + power.b.stoppclk = 1;
  83697. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83698. +
  83699. + power.b.pwrclmp = 1;
  83700. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83701. +
  83702. + power.b.rstpdwnmodule = 1;
  83703. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83704. + }
  83705. +
  83706. + core_if->lx_state = DWC_OTG_L0;
  83707. +
  83708. + DWC_PRINTF("USB RESET\n");
  83709. +#ifdef DWC_EN_ISOC
  83710. + for (i = 1; i < 16; ++i) {
  83711. + dwc_otg_pcd_ep_t *ep;
  83712. + dwc_ep_t *dwc_ep;
  83713. + ep = get_in_ep(pcd, i);
  83714. + if (ep != 0) {
  83715. + dwc_ep = &ep->dwc_ep;
  83716. + dwc_ep->next_frame = 0xffffffff;
  83717. + }
  83718. + }
  83719. +#endif /* DWC_EN_ISOC */
  83720. +
  83721. + /* reset the HNP settings */
  83722. + dwc_otg_pcd_update_otg(pcd, 1);
  83723. +
  83724. + /* Clear the Remote Wakeup Signalling */
  83725. + dctl.b.rmtwkupsig = 1;
  83726. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  83727. +
  83728. + /* Set NAK for all OUT EPs */
  83729. + doepctl.b.snak = 1;
  83730. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  83731. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  83732. + }
  83733. +
  83734. + /* Flush the NP Tx FIFO */
  83735. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  83736. + /* Flush the Learning Queue */
  83737. + resetctl.b.intknqflsh = 1;
  83738. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  83739. +
  83740. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  83741. + core_if->start_predict = 0;
  83742. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  83743. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  83744. + }
  83745. + core_if->nextep_seq[0] = 0;
  83746. + core_if->first_in_nextep_seq = 0;
  83747. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  83748. + diepctl.b.nextep = 0;
  83749. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  83750. +
  83751. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  83752. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  83753. + dcfg.b.epmscnt = 2;
  83754. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  83755. +
  83756. + DWC_DEBUGPL(DBG_PCDV,
  83757. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  83758. + __func__, core_if->first_in_nextep_seq);
  83759. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  83760. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  83761. + }
  83762. + }
  83763. +
  83764. + if (core_if->multiproc_int_enable) {
  83765. + daintmsk.b.inep0 = 1;
  83766. + daintmsk.b.outep0 = 1;
  83767. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  83768. + daintmsk.d32);
  83769. +
  83770. + doepmsk.b.setup = 1;
  83771. + doepmsk.b.xfercompl = 1;
  83772. + doepmsk.b.ahberr = 1;
  83773. + doepmsk.b.epdisabled = 1;
  83774. +
  83775. + if ((core_if->dma_desc_enable) ||
  83776. + (core_if->dma_enable
  83777. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  83778. + doepmsk.b.stsphsercvd = 1;
  83779. + }
  83780. + if (core_if->dma_desc_enable)
  83781. + doepmsk.b.bna = 1;
  83782. +/*
  83783. + doepmsk.b.babble = 1;
  83784. + doepmsk.b.nyet = 1;
  83785. +
  83786. + if (core_if->dma_enable) {
  83787. + doepmsk.b.nak = 1;
  83788. + }
  83789. +*/
  83790. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  83791. + doepmsk.d32);
  83792. +
  83793. + diepmsk.b.xfercompl = 1;
  83794. + diepmsk.b.timeout = 1;
  83795. + diepmsk.b.epdisabled = 1;
  83796. + diepmsk.b.ahberr = 1;
  83797. + diepmsk.b.intknepmis = 1;
  83798. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  83799. + diepmsk.b.intknepmis = 0;
  83800. +
  83801. +/* if (core_if->dma_desc_enable) {
  83802. + diepmsk.b.bna = 1;
  83803. + }
  83804. +*/
  83805. +/*
  83806. + if (core_if->dma_enable) {
  83807. + diepmsk.b.nak = 1;
  83808. + }
  83809. +*/
  83810. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  83811. + diepmsk.d32);
  83812. + } else {
  83813. + daintmsk.b.inep0 = 1;
  83814. + daintmsk.b.outep0 = 1;
  83815. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  83816. + daintmsk.d32);
  83817. +
  83818. + doepmsk.b.setup = 1;
  83819. + doepmsk.b.xfercompl = 1;
  83820. + doepmsk.b.ahberr = 1;
  83821. + doepmsk.b.epdisabled = 1;
  83822. +
  83823. + if ((core_if->dma_desc_enable) ||
  83824. + (core_if->dma_enable
  83825. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  83826. + doepmsk.b.stsphsercvd = 1;
  83827. + }
  83828. + if (core_if->dma_desc_enable)
  83829. + doepmsk.b.bna = 1;
  83830. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  83831. +
  83832. + diepmsk.b.xfercompl = 1;
  83833. + diepmsk.b.timeout = 1;
  83834. + diepmsk.b.epdisabled = 1;
  83835. + diepmsk.b.ahberr = 1;
  83836. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  83837. + diepmsk.b.intknepmis = 0;
  83838. +/*
  83839. + if (core_if->dma_desc_enable) {
  83840. + diepmsk.b.bna = 1;
  83841. + }
  83842. +*/
  83843. +
  83844. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  83845. + }
  83846. +
  83847. + /* Reset Device Address */
  83848. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  83849. + dcfg.b.devaddr = 0;
  83850. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  83851. +
  83852. + /* setup EP0 to receive SETUP packets */
  83853. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  83854. + ep0_out_start(core_if, pcd);
  83855. +
  83856. + /* Clear interrupt */
  83857. + gintsts.d32 = 0;
  83858. + gintsts.b.usbreset = 1;
  83859. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  83860. +
  83861. + return 1;
  83862. +}
  83863. +
  83864. +/**
  83865. + * Get the device speed from the device status register and convert it
  83866. + * to USB speed constant.
  83867. + *
  83868. + * @param core_if Programming view of DWC_otg controller.
  83869. + */
  83870. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  83871. +{
  83872. + dsts_data_t dsts;
  83873. + int speed = 0;
  83874. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  83875. +
  83876. + switch (dsts.b.enumspd) {
  83877. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  83878. + speed = USB_SPEED_HIGH;
  83879. + break;
  83880. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  83881. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  83882. + speed = USB_SPEED_FULL;
  83883. + break;
  83884. +
  83885. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  83886. + speed = USB_SPEED_LOW;
  83887. + break;
  83888. + }
  83889. +
  83890. + return speed;
  83891. +}
  83892. +
  83893. +/**
  83894. + * Read the device status register and set the device speed in the
  83895. + * data structure.
  83896. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  83897. + */
  83898. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  83899. +{
  83900. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  83901. + gintsts_data_t gintsts;
  83902. + gusbcfg_data_t gusbcfg;
  83903. + dwc_otg_core_global_regs_t *global_regs =
  83904. + GET_CORE_IF(pcd)->core_global_regs;
  83905. + uint8_t utmi16b, utmi8b;
  83906. + int speed;
  83907. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  83908. +
  83909. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  83910. + utmi16b = 6; //vahrama old value was 6;
  83911. + utmi8b = 9;
  83912. + } else {
  83913. + utmi16b = 4;
  83914. + utmi8b = 8;
  83915. + }
  83916. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  83917. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  83918. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  83919. + }
  83920. +
  83921. +#ifdef DEBUG_EP0
  83922. + print_ep0_state(pcd);
  83923. +#endif
  83924. +
  83925. + if (pcd->ep0state == EP0_DISCONNECT) {
  83926. + pcd->ep0state = EP0_IDLE;
  83927. + } else if (pcd->ep0state == EP0_STALL) {
  83928. + pcd->ep0state = EP0_IDLE;
  83929. + }
  83930. +
  83931. + pcd->ep0state = EP0_IDLE;
  83932. +
  83933. + ep0->stopped = 0;
  83934. +
  83935. + speed = get_device_speed(GET_CORE_IF(pcd));
  83936. + pcd->fops->connect(pcd, speed);
  83937. +
  83938. + /* Set USB turnaround time based on device speed and PHY interface. */
  83939. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  83940. + if (speed == USB_SPEED_HIGH) {
  83941. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83942. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  83943. + /* ULPI interface */
  83944. + gusbcfg.b.usbtrdtim = 9;
  83945. + }
  83946. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83947. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  83948. + /* UTMI+ interface */
  83949. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  83950. + gusbcfg.b.usbtrdtim = utmi8b;
  83951. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  83952. + b.utmi_phy_data_width == 1) {
  83953. + gusbcfg.b.usbtrdtim = utmi16b;
  83954. + } else if (GET_CORE_IF(pcd)->
  83955. + core_params->phy_utmi_width == 8) {
  83956. + gusbcfg.b.usbtrdtim = utmi8b;
  83957. + } else {
  83958. + gusbcfg.b.usbtrdtim = utmi16b;
  83959. + }
  83960. + }
  83961. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83962. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  83963. + /* UTMI+ OR ULPI interface */
  83964. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  83965. + /* ULPI interface */
  83966. + gusbcfg.b.usbtrdtim = 9;
  83967. + } else {
  83968. + /* UTMI+ interface */
  83969. + if (GET_CORE_IF(pcd)->
  83970. + core_params->phy_utmi_width == 16) {
  83971. + gusbcfg.b.usbtrdtim = utmi16b;
  83972. + } else {
  83973. + gusbcfg.b.usbtrdtim = utmi8b;
  83974. + }
  83975. + }
  83976. + }
  83977. + } else {
  83978. + /* Full or low speed */
  83979. + gusbcfg.b.usbtrdtim = 9;
  83980. + }
  83981. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  83982. +
  83983. + /* Clear interrupt */
  83984. + gintsts.d32 = 0;
  83985. + gintsts.b.enumdone = 1;
  83986. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83987. + gintsts.d32);
  83988. + return 1;
  83989. +}
  83990. +
  83991. +/**
  83992. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  83993. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  83994. + * read all the data from the Rx FIFO.
  83995. + */
  83996. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  83997. +{
  83998. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83999. + gintsts_data_t gintsts;
  84000. +
  84001. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  84002. + "ISOC Out Dropped");
  84003. +
  84004. + intr_mask.b.isooutdrop = 1;
  84005. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  84006. + intr_mask.d32, 0);
  84007. +
  84008. + /* Clear interrupt */
  84009. + gintsts.d32 = 0;
  84010. + gintsts.b.isooutdrop = 1;
  84011. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  84012. + gintsts.d32);
  84013. +
  84014. + return 1;
  84015. +}
  84016. +
  84017. +/**
  84018. + * This interrupt indicates the end of the portion of the micro-frame
  84019. + * for periodic transactions. If there is a periodic transaction for
  84020. + * the next frame, load the packets into the EP periodic Tx FIFO.
  84021. + */
  84022. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  84023. +{
  84024. + gintmsk_data_t intr_mask = {.d32 = 0 };
  84025. + gintsts_data_t gintsts;
  84026. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  84027. +
  84028. + intr_mask.b.eopframe = 1;
  84029. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  84030. + intr_mask.d32, 0);
  84031. +
  84032. + /* Clear interrupt */
  84033. + gintsts.d32 = 0;
  84034. + gintsts.b.eopframe = 1;
  84035. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  84036. + gintsts.d32);
  84037. +
  84038. + return 1;
  84039. +}
  84040. +
  84041. +/**
  84042. + * This interrupt indicates that EP of the packet on the top of the
  84043. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  84044. + *
  84045. + * The "Device IN Token Queue" Registers are read to determine the
  84046. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  84047. + * is flushed, so it can be reloaded in the order seen in the IN Token
  84048. + * Queue.
  84049. + */
  84050. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  84051. +{
  84052. + gintsts_data_t gintsts;
  84053. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84054. + dctl_data_t dctl;
  84055. + gintmsk_data_t intr_mask = {.d32 = 0 };
  84056. +
  84057. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  84058. + core_if->start_predict = 1;
  84059. +
  84060. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  84061. +
  84062. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  84063. + if (!gintsts.b.ginnakeff) {
  84064. + /* Disable EP Mismatch interrupt */
  84065. + intr_mask.d32 = 0;
  84066. + intr_mask.b.epmismatch = 1;
  84067. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  84068. + /* Enable the Global IN NAK Effective Interrupt */
  84069. + intr_mask.d32 = 0;
  84070. + intr_mask.b.ginnakeff = 1;
  84071. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  84072. + /* Set the global non-periodic IN NAK handshake */
  84073. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  84074. + dctl.b.sgnpinnak = 1;
  84075. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  84076. + } else {
  84077. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  84078. + }
  84079. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  84080. + * handler after Global IN NAK Effective interrupt will be asserted */
  84081. + }
  84082. + /* Clear interrupt */
  84083. + gintsts.d32 = 0;
  84084. + gintsts.b.epmismatch = 1;
  84085. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  84086. +
  84087. + return 1;
  84088. +}
  84089. +
  84090. +/**
  84091. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  84092. + * core has stopped fetching data for IN endpoints due to the unavailability of
  84093. + * TxFIFO space or Request Queue space. This interrupt is used by the
  84094. + * application for an endpoint mismatch algorithm.
  84095. + *
  84096. + * @param pcd The PCD
  84097. + */
  84098. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  84099. +{
  84100. + gintsts_data_t gintsts;
  84101. + gintmsk_data_t gintmsk_data;
  84102. + dctl_data_t dctl;
  84103. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84104. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  84105. +
  84106. + /* Clear the global non-periodic IN NAK handshake */
  84107. + dctl.d32 = 0;
  84108. + dctl.b.cgnpinnak = 1;
  84109. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  84110. +
  84111. + /* Mask GINTSTS.FETSUSP interrupt */
  84112. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  84113. + gintmsk_data.b.fetsusp = 0;
  84114. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  84115. +
  84116. + /* Clear interrupt */
  84117. + gintsts.d32 = 0;
  84118. + gintsts.b.fetsusp = 1;
  84119. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  84120. +
  84121. + return 1;
  84122. +}
  84123. +/**
  84124. + * This funcion stalls EP0.
  84125. + */
  84126. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  84127. +{
  84128. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84129. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  84130. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  84131. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  84132. +
  84133. + ep0->dwc_ep.is_in = 1;
  84134. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84135. + pcd->ep0.stopped = 1;
  84136. + pcd->ep0state = EP0_IDLE;
  84137. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  84138. +}
  84139. +
  84140. +/**
  84141. + * This functions delegates the setup command to the gadget driver.
  84142. + */
  84143. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  84144. + usb_device_request_t * ctrl)
  84145. +{
  84146. + int ret = 0;
  84147. + DWC_SPINUNLOCK(pcd->lock);
  84148. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  84149. + DWC_SPINLOCK(pcd->lock);
  84150. + if (ret < 0) {
  84151. + ep0_do_stall(pcd, ret);
  84152. + }
  84153. +
  84154. + /** @todo This is a g_file_storage gadget driver specific
  84155. + * workaround: a DELAYED_STATUS result from the fsg_setup
  84156. + * routine will result in the gadget queueing a EP0 IN status
  84157. + * phase for a two-stage control transfer. Exactly the same as
  84158. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  84159. + * specific request. Need a generic way to know when the gadget
  84160. + * driver will queue the status phase. Can we assume when we
  84161. + * call the gadget driver setup() function that it will always
  84162. + * queue and require the following flag? Need to look into
  84163. + * this.
  84164. + */
  84165. +
  84166. + if (ret == 256 + 999) {
  84167. + pcd->request_config = 1;
  84168. + }
  84169. +}
  84170. +
  84171. +#ifdef DWC_UTE_CFI
  84172. +/**
  84173. + * This functions delegates the CFI setup commands to the gadget driver.
  84174. + * This function will return a negative value to indicate a failure.
  84175. + */
  84176. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  84177. + struct cfi_usb_ctrlrequest *ctrl_req)
  84178. +{
  84179. + int ret = 0;
  84180. +
  84181. + if (pcd->fops && pcd->fops->cfi_setup) {
  84182. + DWC_SPINUNLOCK(pcd->lock);
  84183. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  84184. + DWC_SPINLOCK(pcd->lock);
  84185. + if (ret < 0) {
  84186. + ep0_do_stall(pcd, ret);
  84187. + return ret;
  84188. + }
  84189. + }
  84190. +
  84191. + return ret;
  84192. +}
  84193. +#endif
  84194. +
  84195. +/**
  84196. + * This function starts the Zero-Length Packet for the IN status phase
  84197. + * of a 2 stage control transfer.
  84198. + */
  84199. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  84200. +{
  84201. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84202. + if (pcd->ep0state == EP0_STALL) {
  84203. + return;
  84204. + }
  84205. +
  84206. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  84207. +
  84208. + /* Prepare for more SETUP Packets */
  84209. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  84210. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  84211. + && (pcd->core_if->dma_desc_enable)
  84212. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  84213. + DWC_DEBUGPL(DBG_PCDV,
  84214. + "Data terminated wait next packet in out_desc_addr\n");
  84215. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  84216. + pcd->data_terminated = 1;
  84217. + }
  84218. + ep0->dwc_ep.xfer_len = 0;
  84219. + ep0->dwc_ep.xfer_count = 0;
  84220. + ep0->dwc_ep.is_in = 1;
  84221. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  84222. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84223. +
  84224. + /* Prepare for more SETUP Packets */
  84225. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  84226. +}
  84227. +
  84228. +/**
  84229. + * This function starts the Zero-Length Packet for the OUT status phase
  84230. + * of a 2 stage control transfer.
  84231. + */
  84232. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  84233. +{
  84234. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84235. + if (pcd->ep0state == EP0_STALL) {
  84236. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  84237. + return;
  84238. + }
  84239. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  84240. +
  84241. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  84242. + ep0->dwc_ep.xfer_len = 0;
  84243. + ep0->dwc_ep.xfer_count = 0;
  84244. + ep0->dwc_ep.is_in = 0;
  84245. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  84246. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84247. +
  84248. + /* Prepare for more SETUP Packets */
  84249. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  84250. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  84251. + }
  84252. +}
  84253. +
  84254. +/**
  84255. + * Clear the EP halt (STALL) and if pending requests start the
  84256. + * transfer.
  84257. + */
  84258. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  84259. +{
  84260. + if (ep->dwc_ep.stall_clear_flag == 0)
  84261. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  84262. +
  84263. + /* Reactive the EP */
  84264. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  84265. + if (ep->stopped) {
  84266. + ep->stopped = 0;
  84267. + /* If there is a request in the EP queue start it */
  84268. +
  84269. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  84270. + * epmismatch not yet implemented. */
  84271. +
  84272. + /*
  84273. + * Above fixme is solved by implmenting a tasklet to call the
  84274. + * start_next_request(), outside of interrupt context at some
  84275. + * time after the current time, after a clear-halt setup packet.
  84276. + * Still need to implement ep mismatch in the future if a gadget
  84277. + * ever uses more than one endpoint at once
  84278. + */
  84279. + ep->queue_sof = 1;
  84280. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  84281. + }
  84282. + /* Start Control Status Phase */
  84283. + do_setup_in_status_phase(pcd);
  84284. +}
  84285. +
  84286. +/**
  84287. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  84288. + * is sent from the host. The Device Control register is written with
  84289. + * the Test Mode bits set to the specified Test Mode. This is done as
  84290. + * a tasklet so that the "Status" phase of the control transfer
  84291. + * completes before transmitting the TEST packets.
  84292. + *
  84293. + * @todo This has not been tested since the tasklet struct was put
  84294. + * into the PCD struct!
  84295. + *
  84296. + */
  84297. +void do_test_mode(void *data)
  84298. +{
  84299. + dctl_data_t dctl;
  84300. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  84301. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84302. + int test_mode = pcd->test_mode;
  84303. +
  84304. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  84305. +
  84306. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  84307. + switch (test_mode) {
  84308. + case 1: // TEST_J
  84309. + dctl.b.tstctl = 1;
  84310. + break;
  84311. +
  84312. + case 2: // TEST_K
  84313. + dctl.b.tstctl = 2;
  84314. + break;
  84315. +
  84316. + case 3: // TEST_SE0_NAK
  84317. + dctl.b.tstctl = 3;
  84318. + break;
  84319. +
  84320. + case 4: // TEST_PACKET
  84321. + dctl.b.tstctl = 4;
  84322. + break;
  84323. +
  84324. + case 5: // TEST_FORCE_ENABLE
  84325. + dctl.b.tstctl = 5;
  84326. + break;
  84327. + }
  84328. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  84329. +}
  84330. +
  84331. +/**
  84332. + * This function process the GET_STATUS Setup Commands.
  84333. + */
  84334. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  84335. +{
  84336. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84337. + dwc_otg_pcd_ep_t *ep;
  84338. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84339. + uint16_t *status = pcd->status_buf;
  84340. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84341. +
  84342. +#ifdef DEBUG_EP0
  84343. + DWC_DEBUGPL(DBG_PCD,
  84344. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  84345. + ctrl.bmRequestType, ctrl.bRequest,
  84346. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84347. + UGETW(ctrl.wLength));
  84348. +#endif
  84349. +
  84350. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  84351. + case UT_DEVICE:
  84352. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  84353. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  84354. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  84355. + DWC_PRINTF("OTG CAP - %d, %d\n",
  84356. + core_if->core_params->otg_cap,
  84357. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  84358. + if (core_if->otg_ver == 1
  84359. + && core_if->core_params->otg_cap ==
  84360. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84361. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  84362. + *otgsts = (core_if->otg_sts & 0x1);
  84363. + pcd->ep0_pending = 1;
  84364. + ep0->dwc_ep.start_xfer_buff =
  84365. + (uint8_t *) otgsts;
  84366. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  84367. + ep0->dwc_ep.dma_addr =
  84368. + pcd->status_buf_dma_handle;
  84369. + ep0->dwc_ep.xfer_len = 1;
  84370. + ep0->dwc_ep.xfer_count = 0;
  84371. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  84372. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  84373. + &ep0->dwc_ep);
  84374. + return;
  84375. + } else {
  84376. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84377. + return;
  84378. + }
  84379. + break;
  84380. + } else {
  84381. + *status = 0x1; /* Self powered */
  84382. + *status |= pcd->remote_wakeup_enable << 1;
  84383. + break;
  84384. + }
  84385. + case UT_INTERFACE:
  84386. + *status = 0;
  84387. + break;
  84388. +
  84389. + case UT_ENDPOINT:
  84390. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  84391. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  84392. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84393. + return;
  84394. + }
  84395. + /** @todo check for EP stall */
  84396. + *status = ep->stopped;
  84397. + break;
  84398. + }
  84399. + pcd->ep0_pending = 1;
  84400. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  84401. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  84402. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  84403. + ep0->dwc_ep.xfer_len = 2;
  84404. + ep0->dwc_ep.xfer_count = 0;
  84405. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  84406. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84407. +}
  84408. +
  84409. +/**
  84410. + * This function process the SET_FEATURE Setup Commands.
  84411. + */
  84412. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  84413. +{
  84414. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84415. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  84416. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84417. + dwc_otg_pcd_ep_t *ep = 0;
  84418. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  84419. + gotgctl_data_t gotgctl = {.d32 = 0 };
  84420. +
  84421. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  84422. + ctrl.bmRequestType, ctrl.bRequest,
  84423. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84424. + UGETW(ctrl.wLength));
  84425. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  84426. +
  84427. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  84428. + case UT_DEVICE:
  84429. + switch (UGETW(ctrl.wValue)) {
  84430. + case UF_DEVICE_REMOTE_WAKEUP:
  84431. + pcd->remote_wakeup_enable = 1;
  84432. + break;
  84433. +
  84434. + case UF_TEST_MODE:
  84435. + /* Setup the Test Mode tasklet to do the Test
  84436. + * Packet generation after the SETUP Status
  84437. + * phase has completed. */
  84438. +
  84439. + /** @todo This has not been tested since the
  84440. + * tasklet struct was put into the PCD
  84441. + * struct! */
  84442. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  84443. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  84444. + break;
  84445. +
  84446. + case UF_DEVICE_B_HNP_ENABLE:
  84447. + DWC_DEBUGPL(DBG_PCDV,
  84448. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  84449. +
  84450. + /* dev may initiate HNP */
  84451. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84452. + pcd->b_hnp_enable = 1;
  84453. + dwc_otg_pcd_update_otg(pcd, 0);
  84454. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  84455. + /**@todo Is the gotgctl.devhnpen cleared
  84456. + * by a USB Reset? */
  84457. + gotgctl.b.devhnpen = 1;
  84458. + gotgctl.b.hnpreq = 1;
  84459. + DWC_WRITE_REG32(&global_regs->gotgctl,
  84460. + gotgctl.d32);
  84461. + } else {
  84462. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84463. + return;
  84464. + }
  84465. + break;
  84466. +
  84467. + case UF_DEVICE_A_HNP_SUPPORT:
  84468. + /* RH port supports HNP */
  84469. + DWC_DEBUGPL(DBG_PCDV,
  84470. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  84471. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84472. + pcd->a_hnp_support = 1;
  84473. + dwc_otg_pcd_update_otg(pcd, 0);
  84474. + } else {
  84475. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84476. + return;
  84477. + }
  84478. + break;
  84479. +
  84480. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  84481. + /* other RH port does */
  84482. + DWC_DEBUGPL(DBG_PCDV,
  84483. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  84484. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84485. + pcd->a_alt_hnp_support = 1;
  84486. + dwc_otg_pcd_update_otg(pcd, 0);
  84487. + } else {
  84488. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84489. + return;
  84490. + }
  84491. + break;
  84492. +
  84493. + default:
  84494. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84495. + return;
  84496. +
  84497. + }
  84498. + do_setup_in_status_phase(pcd);
  84499. + break;
  84500. +
  84501. + case UT_INTERFACE:
  84502. + do_gadget_setup(pcd, &ctrl);
  84503. + break;
  84504. +
  84505. + case UT_ENDPOINT:
  84506. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  84507. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  84508. + if (ep == 0) {
  84509. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84510. + return;
  84511. + }
  84512. + ep->stopped = 1;
  84513. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  84514. + }
  84515. + do_setup_in_status_phase(pcd);
  84516. + break;
  84517. + }
  84518. +}
  84519. +
  84520. +/**
  84521. + * This function process the CLEAR_FEATURE Setup Commands.
  84522. + */
  84523. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  84524. +{
  84525. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84526. + dwc_otg_pcd_ep_t *ep = 0;
  84527. +
  84528. + DWC_DEBUGPL(DBG_PCD,
  84529. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  84530. + ctrl.bmRequestType, ctrl.bRequest,
  84531. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84532. + UGETW(ctrl.wLength));
  84533. +
  84534. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  84535. + case UT_DEVICE:
  84536. + switch (UGETW(ctrl.wValue)) {
  84537. + case UF_DEVICE_REMOTE_WAKEUP:
  84538. + pcd->remote_wakeup_enable = 0;
  84539. + break;
  84540. +
  84541. + case UF_TEST_MODE:
  84542. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  84543. + break;
  84544. +
  84545. + default:
  84546. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84547. + return;
  84548. + }
  84549. + do_setup_in_status_phase(pcd);
  84550. + break;
  84551. +
  84552. + case UT_ENDPOINT:
  84553. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  84554. + if (ep == 0) {
  84555. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84556. + return;
  84557. + }
  84558. +
  84559. + pcd_clear_halt(pcd, ep);
  84560. +
  84561. + break;
  84562. + }
  84563. +}
  84564. +
  84565. +/**
  84566. + * This function process the SET_ADDRESS Setup Commands.
  84567. + */
  84568. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  84569. +{
  84570. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  84571. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84572. +
  84573. + if (ctrl.bmRequestType == UT_DEVICE) {
  84574. + dcfg_data_t dcfg = {.d32 = 0 };
  84575. +
  84576. +#ifdef DEBUG_EP0
  84577. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  84578. +#endif
  84579. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  84580. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  84581. + do_setup_in_status_phase(pcd);
  84582. + }
  84583. +}
  84584. +
  84585. +/**
  84586. + * This function processes SETUP commands. In Linux, the USB Command
  84587. + * processing is done in two places - the first being the PCD and the
  84588. + * second in the Gadget Driver (for example, the File-Backed Storage
  84589. + * Gadget Driver).
  84590. + *
  84591. + * <table>
  84592. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  84593. + *
  84594. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  84595. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  84596. + * </td></tr>
  84597. + *
  84598. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  84599. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  84600. + * interface requests are ignored.</td></tr>
  84601. + *
  84602. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  84603. + * requests are processed by the PCD. Interface requests are passed
  84604. + * to the Gadget Driver.</td></tr>
  84605. + *
  84606. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  84607. + * with device address received </td></tr>
  84608. + *
  84609. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  84610. + * requested descriptor</td></tr>
  84611. + *
  84612. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  84613. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  84614. + *
  84615. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  84616. + * all EPs and enable EPs for new configuration.</td></tr>
  84617. + *
  84618. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  84619. + * the current configuration</td></tr>
  84620. + *
  84621. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  84622. + * EPs and enable EPs for new configuration.</td></tr>
  84623. + *
  84624. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  84625. + * current interface.</td></tr>
  84626. + *
  84627. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  84628. + * message.</td></tr>
  84629. + * </table>
  84630. + *
  84631. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  84632. + * processed by pcd_setup. Calling the Function Driver's setup function from
  84633. + * pcd_setup processes the gadget SETUP commands.
  84634. + */
  84635. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  84636. +{
  84637. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84638. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  84639. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84640. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84641. +
  84642. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  84643. +
  84644. +#ifdef DWC_UTE_CFI
  84645. + int retval = 0;
  84646. + struct cfi_usb_ctrlrequest cfi_req;
  84647. +#endif
  84648. +
  84649. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  84650. +
  84651. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  84652. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  84653. + && (doeptsize0.b.supcnt < 2)
  84654. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  84655. + DWC_ERROR
  84656. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  84657. + }
  84658. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  84659. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  84660. + ctrl =
  84661. + (pcd->setup_pkt +
  84662. + (3 - doeptsize0.b.supcnt - 1 +
  84663. + ep0->dwc_ep.stp_rollover))->req;
  84664. + }
  84665. +#ifdef DEBUG_EP0
  84666. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  84667. + ctrl.bmRequestType, ctrl.bRequest,
  84668. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84669. + UGETW(ctrl.wLength));
  84670. +#endif
  84671. +
  84672. + /* Clean up the request queue */
  84673. + dwc_otg_request_nuke(ep0);
  84674. + ep0->stopped = 0;
  84675. +
  84676. + if (ctrl.bmRequestType & UE_DIR_IN) {
  84677. + ep0->dwc_ep.is_in = 1;
  84678. + pcd->ep0state = EP0_IN_DATA_PHASE;
  84679. + } else {
  84680. + ep0->dwc_ep.is_in = 0;
  84681. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  84682. + }
  84683. +
  84684. + if (UGETW(ctrl.wLength) == 0) {
  84685. + ep0->dwc_ep.is_in = 1;
  84686. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  84687. + }
  84688. +
  84689. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  84690. +
  84691. +#ifdef DWC_UTE_CFI
  84692. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  84693. +
  84694. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  84695. + ctrl.bRequestType, ctrl.bRequest);
  84696. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  84697. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  84698. + retval = cfi_setup(pcd, &cfi_req);
  84699. + if (retval < 0) {
  84700. + ep0_do_stall(pcd, retval);
  84701. + pcd->ep0_pending = 0;
  84702. + return;
  84703. + }
  84704. +
  84705. + /* if need gadget setup then call it and check the retval */
  84706. + if (pcd->cfi->need_gadget_att) {
  84707. + retval =
  84708. + cfi_gadget_setup(pcd,
  84709. + &pcd->
  84710. + cfi->ctrl_req);
  84711. + if (retval < 0) {
  84712. + pcd->ep0_pending = 0;
  84713. + return;
  84714. + }
  84715. + }
  84716. +
  84717. + if (pcd->cfi->need_status_in_complete) {
  84718. + do_setup_in_status_phase(pcd);
  84719. + }
  84720. + return;
  84721. + }
  84722. + }
  84723. +#endif
  84724. +
  84725. + /* handle non-standard (class/vendor) requests in the gadget driver */
  84726. + do_gadget_setup(pcd, &ctrl);
  84727. + return;
  84728. + }
  84729. +
  84730. + /** @todo NGS: Handle bad setup packet? */
  84731. +
  84732. +///////////////////////////////////////////
  84733. +//// --- Standard Request handling --- ////
  84734. +
  84735. + switch (ctrl.bRequest) {
  84736. + case UR_GET_STATUS:
  84737. + do_get_status(pcd);
  84738. + break;
  84739. +
  84740. + case UR_CLEAR_FEATURE:
  84741. + do_clear_feature(pcd);
  84742. + break;
  84743. +
  84744. + case UR_SET_FEATURE:
  84745. + do_set_feature(pcd);
  84746. + break;
  84747. +
  84748. + case UR_SET_ADDRESS:
  84749. + do_set_address(pcd);
  84750. + break;
  84751. +
  84752. + case UR_SET_INTERFACE:
  84753. + case UR_SET_CONFIG:
  84754. +// _pcd->request_config = 1; /* Configuration changed */
  84755. + do_gadget_setup(pcd, &ctrl);
  84756. + break;
  84757. +
  84758. + case UR_SYNCH_FRAME:
  84759. + do_gadget_setup(pcd, &ctrl);
  84760. + break;
  84761. +
  84762. + default:
  84763. + /* Call the Gadget Driver's setup functions */
  84764. + do_gadget_setup(pcd, &ctrl);
  84765. + break;
  84766. + }
  84767. +}
  84768. +
  84769. +/**
  84770. + * This function completes the ep0 control transfer.
  84771. + */
  84772. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  84773. +{
  84774. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  84775. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  84776. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  84777. + dev_if->in_ep_regs[ep->dwc_ep.num];
  84778. +#ifdef DEBUG_EP0
  84779. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  84780. + dev_if->out_ep_regs[ep->dwc_ep.num];
  84781. +#endif
  84782. + deptsiz0_data_t deptsiz;
  84783. + dev_dma_desc_sts_t desc_sts;
  84784. + dwc_otg_pcd_request_t *req;
  84785. + int is_last = 0;
  84786. + dwc_otg_pcd_t *pcd = ep->pcd;
  84787. +
  84788. +#ifdef DWC_UTE_CFI
  84789. + struct cfi_usb_ctrlrequest *ctrlreq;
  84790. + int retval = -DWC_E_NOT_SUPPORTED;
  84791. +#endif
  84792. +
  84793. + desc_sts.b.bytes = 0;
  84794. +
  84795. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  84796. + if (ep->dwc_ep.is_in) {
  84797. +#ifdef DEBUG_EP0
  84798. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  84799. +#endif
  84800. + do_setup_out_status_phase(pcd);
  84801. + } else {
  84802. +#ifdef DEBUG_EP0
  84803. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  84804. +#endif
  84805. +
  84806. +#ifdef DWC_UTE_CFI
  84807. + ctrlreq = &pcd->cfi->ctrl_req;
  84808. +
  84809. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  84810. + if (ctrlreq->bRequest > 0xB0
  84811. + && ctrlreq->bRequest < 0xBF) {
  84812. +
  84813. + /* Return if the PCD failed to handle the request */
  84814. + if ((retval =
  84815. + pcd->cfi->ops.
  84816. + ctrl_write_complete(pcd->cfi,
  84817. + pcd)) < 0) {
  84818. + CFI_INFO
  84819. + ("ERROR setting a new value in the PCD(%d)\n",
  84820. + retval);
  84821. + ep0_do_stall(pcd, retval);
  84822. + pcd->ep0_pending = 0;
  84823. + return 0;
  84824. + }
  84825. +
  84826. + /* If the gadget needs to be notified on the request */
  84827. + if (pcd->cfi->need_gadget_att == 1) {
  84828. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  84829. + retval =
  84830. + cfi_gadget_setup(pcd,
  84831. + &pcd->cfi->
  84832. + ctrl_req);
  84833. +
  84834. + /* Return from the function if the gadget failed to process
  84835. + * the request properly - this should never happen !!!
  84836. + */
  84837. + if (retval < 0) {
  84838. + CFI_INFO
  84839. + ("ERROR setting a new value in the gadget(%d)\n",
  84840. + retval);
  84841. + pcd->ep0_pending = 0;
  84842. + return 0;
  84843. + }
  84844. + }
  84845. +
  84846. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  84847. + retval);
  84848. + /* If we hit here then the PCD and the gadget has properly
  84849. + * handled the request - so send the ZLP IN to the host.
  84850. + */
  84851. + /* @todo: MAS - decide whether we need to start the setup
  84852. + * stage based on the need_setup value of the cfi object
  84853. + */
  84854. + do_setup_in_status_phase(pcd);
  84855. + pcd->ep0_pending = 0;
  84856. + return 1;
  84857. + }
  84858. + }
  84859. +#endif
  84860. +
  84861. + do_setup_in_status_phase(pcd);
  84862. + }
  84863. + pcd->ep0_pending = 0;
  84864. + return 1;
  84865. + }
  84866. +
  84867. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  84868. + return 0;
  84869. + }
  84870. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  84871. +
  84872. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  84873. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  84874. + is_last = 1;
  84875. + } else if (ep->dwc_ep.is_in) {
  84876. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  84877. + if (core_if->dma_desc_enable != 0)
  84878. + desc_sts = dev_if->in_desc_addr->status;
  84879. +#ifdef DEBUG_EP0
  84880. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  84881. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  84882. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84883. +#endif
  84884. +
  84885. + if (((core_if->dma_desc_enable == 0)
  84886. + && (deptsiz.b.xfersize == 0))
  84887. + || ((core_if->dma_desc_enable != 0)
  84888. + && (desc_sts.b.bytes == 0))) {
  84889. + req->actual = ep->dwc_ep.xfer_count;
  84890. + /* Is a Zero Len Packet needed? */
  84891. + if (req->sent_zlp) {
  84892. +#ifdef DEBUG_EP0
  84893. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  84894. +#endif
  84895. + req->sent_zlp = 0;
  84896. + }
  84897. + do_setup_out_status_phase(pcd);
  84898. + }
  84899. + } else {
  84900. + /* ep0-OUT */
  84901. +#ifdef DEBUG_EP0
  84902. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  84903. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  84904. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  84905. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84906. +#endif
  84907. + req->actual = ep->dwc_ep.xfer_count;
  84908. +
  84909. + /* Is a Zero Len Packet needed? */
  84910. + if (req->sent_zlp) {
  84911. +#ifdef DEBUG_EP0
  84912. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  84913. +#endif
  84914. + req->sent_zlp = 0;
  84915. + }
  84916. + /* For older cores do setup in status phase in Slave/BDMA modes,
  84917. + * starting from 3.00 do that only in slave, and for DMA modes
  84918. + * just re-enable ep 0 OUT here*/
  84919. + if (core_if->dma_enable == 0
  84920. + || (core_if->dma_desc_enable == 0
  84921. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  84922. + do_setup_in_status_phase(pcd);
  84923. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  84924. + DWC_DEBUGPL(DBG_PCDV,
  84925. + "Enable out ep before in status phase\n");
  84926. + ep0_out_start(core_if, pcd);
  84927. + }
  84928. + }
  84929. +
  84930. + /* Complete the request */
  84931. + if (is_last) {
  84932. + dwc_otg_request_done(ep, req, 0);
  84933. + ep->dwc_ep.start_xfer_buff = 0;
  84934. + ep->dwc_ep.xfer_buff = 0;
  84935. + ep->dwc_ep.xfer_len = 0;
  84936. + return 1;
  84937. + }
  84938. + return 0;
  84939. +}
  84940. +
  84941. +#ifdef DWC_UTE_CFI
  84942. +/**
  84943. + * This function calculates traverses all the CFI DMA descriptors and
  84944. + * and accumulates the bytes that are left to be transfered.
  84945. + *
  84946. + * @return The total bytes left to transfered, or a negative value as failure
  84947. + */
  84948. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  84949. +{
  84950. + int32_t ret = 0;
  84951. + int i;
  84952. + struct dwc_otg_dma_desc *ddesc = NULL;
  84953. + struct cfi_ep *cfiep;
  84954. +
  84955. + /* See if the pcd_ep has its respective cfi_ep mapped */
  84956. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  84957. + if (!cfiep) {
  84958. + CFI_INFO("%s: Failed to find ep\n", __func__);
  84959. + return -1;
  84960. + }
  84961. +
  84962. + ddesc = ep->dwc_ep.descs;
  84963. +
  84964. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  84965. +
  84966. +#if defined(PRINT_CFI_DMA_DESCS)
  84967. + print_desc(ddesc, ep->ep.name, i);
  84968. +#endif
  84969. + ret += ddesc->status.b.bytes;
  84970. + ddesc++;
  84971. + }
  84972. +
  84973. + if (ret)
  84974. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  84975. + ret);
  84976. +
  84977. + return ret;
  84978. +}
  84979. +#endif
  84980. +
  84981. +/**
  84982. + * This function completes the request for the EP. If there are
  84983. + * additional requests for the EP in the queue they will be started.
  84984. + */
  84985. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  84986. +{
  84987. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  84988. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  84989. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  84990. + dev_if->in_ep_regs[ep->dwc_ep.num];
  84991. + deptsiz_data_t deptsiz;
  84992. + dev_dma_desc_sts_t desc_sts;
  84993. + dwc_otg_pcd_request_t *req = 0;
  84994. + dwc_otg_dev_dma_desc_t *dma_desc;
  84995. + uint32_t byte_count = 0;
  84996. + int is_last = 0;
  84997. + int i;
  84998. +
  84999. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  85000. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  85001. +
  85002. + /* Get any pending requests */
  85003. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  85004. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  85005. + if (!req) {
  85006. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  85007. + return;
  85008. + }
  85009. + } else {
  85010. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  85011. + return;
  85012. + }
  85013. +
  85014. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  85015. +
  85016. + if (ep->dwc_ep.is_in) {
  85017. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  85018. +
  85019. + if (core_if->dma_enable) {
  85020. + if (core_if->dma_desc_enable == 0) {
  85021. + if (deptsiz.b.xfersize == 0
  85022. + && deptsiz.b.pktcnt == 0) {
  85023. + byte_count =
  85024. + ep->dwc_ep.xfer_len -
  85025. + ep->dwc_ep.xfer_count;
  85026. +
  85027. + ep->dwc_ep.xfer_buff += byte_count;
  85028. + ep->dwc_ep.dma_addr += byte_count;
  85029. + ep->dwc_ep.xfer_count += byte_count;
  85030. +
  85031. + DWC_DEBUGPL(DBG_PCDV,
  85032. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  85033. + ep->dwc_ep.num,
  85034. + (ep->dwc_ep.
  85035. + is_in ? "IN" : "OUT"),
  85036. + ep->dwc_ep.xfer_len,
  85037. + deptsiz.b.xfersize,
  85038. + deptsiz.b.pktcnt);
  85039. +
  85040. + if (ep->dwc_ep.xfer_len <
  85041. + ep->dwc_ep.total_len) {
  85042. + dwc_otg_ep_start_transfer
  85043. + (core_if, &ep->dwc_ep);
  85044. + } else if (ep->dwc_ep.sent_zlp) {
  85045. + /*
  85046. + * This fragment of code should initiate 0
  85047. + * length transfer in case if it is queued
  85048. + * a transfer with size divisible to EPs max
  85049. + * packet size and with usb_request zero field
  85050. + * is set, which means that after data is transfered,
  85051. + * it is also should be transfered
  85052. + * a 0 length packet at the end. For Slave and
  85053. + * Buffer DMA modes in this case SW has
  85054. + * to initiate 2 transfers one with transfer size,
  85055. + * and the second with 0 size. For Descriptor
  85056. + * DMA mode SW is able to initiate a transfer,
  85057. + * which will handle all the packets including
  85058. + * the last 0 length.
  85059. + */
  85060. + ep->dwc_ep.sent_zlp = 0;
  85061. + dwc_otg_ep_start_zl_transfer
  85062. + (core_if, &ep->dwc_ep);
  85063. + } else {
  85064. + is_last = 1;
  85065. + }
  85066. + } else {
  85067. + if (ep->dwc_ep.type ==
  85068. + DWC_OTG_EP_TYPE_ISOC) {
  85069. + req->actual = 0;
  85070. + dwc_otg_request_done(ep, req, 0);
  85071. +
  85072. + ep->dwc_ep.start_xfer_buff = 0;
  85073. + ep->dwc_ep.xfer_buff = 0;
  85074. + ep->dwc_ep.xfer_len = 0;
  85075. +
  85076. + /* If there is a request in the queue start it. */
  85077. + start_next_request(ep);
  85078. + } else
  85079. + DWC_WARN
  85080. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  85081. + ep->dwc_ep.num,
  85082. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  85083. + deptsiz.b.xfersize,
  85084. + deptsiz.b.pktcnt);
  85085. + }
  85086. + } else {
  85087. + dma_desc = ep->dwc_ep.desc_addr;
  85088. + byte_count = 0;
  85089. + ep->dwc_ep.sent_zlp = 0;
  85090. +
  85091. +#ifdef DWC_UTE_CFI
  85092. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  85093. + ep->dwc_ep.buff_mode);
  85094. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  85095. + int residue;
  85096. +
  85097. + residue = cfi_calc_desc_residue(ep);
  85098. + if (residue < 0)
  85099. + return;
  85100. +
  85101. + byte_count = residue;
  85102. + } else {
  85103. +#endif
  85104. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  85105. + ++i) {
  85106. + desc_sts = dma_desc->status;
  85107. + byte_count += desc_sts.b.bytes;
  85108. + dma_desc++;
  85109. + }
  85110. +#ifdef DWC_UTE_CFI
  85111. + }
  85112. +#endif
  85113. + if (byte_count == 0) {
  85114. + ep->dwc_ep.xfer_count =
  85115. + ep->dwc_ep.total_len;
  85116. + is_last = 1;
  85117. + } else {
  85118. + DWC_WARN("Incomplete transfer\n");
  85119. + }
  85120. + }
  85121. + } else {
  85122. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  85123. + DWC_DEBUGPL(DBG_PCDV,
  85124. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  85125. + ep->dwc_ep.num,
  85126. + ep->dwc_ep.is_in ? "IN" : "OUT",
  85127. + ep->dwc_ep.xfer_len,
  85128. + deptsiz.b.xfersize,
  85129. + deptsiz.b.pktcnt);
  85130. +
  85131. + /* Check if the whole transfer was completed,
  85132. + * if no, setup transfer for next portion of data
  85133. + */
  85134. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  85135. + dwc_otg_ep_start_transfer(core_if,
  85136. + &ep->dwc_ep);
  85137. + } else if (ep->dwc_ep.sent_zlp) {
  85138. + /*
  85139. + * This fragment of code should initiate 0
  85140. + * length trasfer in case if it is queued
  85141. + * a trasfer with size divisible to EPs max
  85142. + * packet size and with usb_request zero field
  85143. + * is set, which means that after data is transfered,
  85144. + * it is also should be transfered
  85145. + * a 0 length packet at the end. For Slave and
  85146. + * Buffer DMA modes in this case SW has
  85147. + * to initiate 2 transfers one with transfer size,
  85148. + * and the second with 0 size. For Desriptor
  85149. + * DMA mode SW is able to initiate a transfer,
  85150. + * which will handle all the packets including
  85151. + * the last 0 legth.
  85152. + */
  85153. + ep->dwc_ep.sent_zlp = 0;
  85154. + dwc_otg_ep_start_zl_transfer(core_if,
  85155. + &ep->dwc_ep);
  85156. + } else {
  85157. + is_last = 1;
  85158. + }
  85159. + } else {
  85160. + DWC_WARN
  85161. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  85162. + ep->dwc_ep.num,
  85163. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  85164. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  85165. + }
  85166. + }
  85167. + } else {
  85168. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  85169. + dev_if->out_ep_regs[ep->dwc_ep.num];
  85170. + desc_sts.d32 = 0;
  85171. + if (core_if->dma_enable) {
  85172. + if (core_if->dma_desc_enable) {
  85173. + dma_desc = ep->dwc_ep.desc_addr;
  85174. + byte_count = 0;
  85175. + ep->dwc_ep.sent_zlp = 0;
  85176. +
  85177. +#ifdef DWC_UTE_CFI
  85178. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  85179. + ep->dwc_ep.buff_mode);
  85180. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  85181. + int residue;
  85182. + residue = cfi_calc_desc_residue(ep);
  85183. + if (residue < 0)
  85184. + return;
  85185. + byte_count = residue;
  85186. + } else {
  85187. +#endif
  85188. +
  85189. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  85190. + ++i) {
  85191. + desc_sts = dma_desc->status;
  85192. + byte_count += desc_sts.b.bytes;
  85193. + dma_desc++;
  85194. + }
  85195. +
  85196. +#ifdef DWC_UTE_CFI
  85197. + }
  85198. +#endif
  85199. + /* Checking for interrupt Out transfers with not
  85200. + * dword aligned mps sizes
  85201. + */
  85202. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  85203. + (ep->dwc_ep.maxpacket%4)) {
  85204. + ep->dwc_ep.xfer_count =
  85205. + ep->dwc_ep.total_len - byte_count;
  85206. + if ((ep->dwc_ep.xfer_len %
  85207. + ep->dwc_ep.maxpacket)
  85208. + && (ep->dwc_ep.xfer_len /
  85209. + ep->dwc_ep.maxpacket <
  85210. + MAX_DMA_DESC_CNT))
  85211. + ep->dwc_ep.xfer_len -=
  85212. + (ep->dwc_ep.desc_cnt -
  85213. + 1) * ep->dwc_ep.maxpacket +
  85214. + ep->dwc_ep.xfer_len %
  85215. + ep->dwc_ep.maxpacket;
  85216. + else
  85217. + ep->dwc_ep.xfer_len -=
  85218. + ep->dwc_ep.desc_cnt *
  85219. + ep->dwc_ep.maxpacket;
  85220. + if (ep->dwc_ep.xfer_len > 0) {
  85221. + dwc_otg_ep_start_transfer
  85222. + (core_if, &ep->dwc_ep);
  85223. + } else {
  85224. + is_last = 1;
  85225. + }
  85226. + } else {
  85227. + ep->dwc_ep.xfer_count =
  85228. + ep->dwc_ep.total_len - byte_count +
  85229. + ((4 -
  85230. + (ep->dwc_ep.
  85231. + total_len & 0x3)) & 0x3);
  85232. + is_last = 1;
  85233. + }
  85234. + } else {
  85235. + deptsiz.d32 = 0;
  85236. + deptsiz.d32 =
  85237. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  85238. +
  85239. + byte_count = (ep->dwc_ep.xfer_len -
  85240. + ep->dwc_ep.xfer_count -
  85241. + deptsiz.b.xfersize);
  85242. + ep->dwc_ep.xfer_buff += byte_count;
  85243. + ep->dwc_ep.dma_addr += byte_count;
  85244. + ep->dwc_ep.xfer_count += byte_count;
  85245. +
  85246. + /* Check if the whole transfer was completed,
  85247. + * if no, setup transfer for next portion of data
  85248. + */
  85249. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  85250. + dwc_otg_ep_start_transfer(core_if,
  85251. + &ep->dwc_ep);
  85252. + } else if (ep->dwc_ep.sent_zlp) {
  85253. + /*
  85254. + * This fragment of code should initiate 0
  85255. + * length trasfer in case if it is queued
  85256. + * a trasfer with size divisible to EPs max
  85257. + * packet size and with usb_request zero field
  85258. + * is set, which means that after data is transfered,
  85259. + * it is also should be transfered
  85260. + * a 0 length packet at the end. For Slave and
  85261. + * Buffer DMA modes in this case SW has
  85262. + * to initiate 2 transfers one with transfer size,
  85263. + * and the second with 0 size. For Desriptor
  85264. + * DMA mode SW is able to initiate a transfer,
  85265. + * which will handle all the packets including
  85266. + * the last 0 legth.
  85267. + */
  85268. + ep->dwc_ep.sent_zlp = 0;
  85269. + dwc_otg_ep_start_zl_transfer(core_if,
  85270. + &ep->dwc_ep);
  85271. + } else {
  85272. + is_last = 1;
  85273. + }
  85274. + }
  85275. + } else {
  85276. + /* Check if the whole transfer was completed,
  85277. + * if no, setup transfer for next portion of data
  85278. + */
  85279. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  85280. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  85281. + } else if (ep->dwc_ep.sent_zlp) {
  85282. + /*
  85283. + * This fragment of code should initiate 0
  85284. + * length transfer in case if it is queued
  85285. + * a transfer with size divisible to EPs max
  85286. + * packet size and with usb_request zero field
  85287. + * is set, which means that after data is transfered,
  85288. + * it is also should be transfered
  85289. + * a 0 length packet at the end. For Slave and
  85290. + * Buffer DMA modes in this case SW has
  85291. + * to initiate 2 transfers one with transfer size,
  85292. + * and the second with 0 size. For Descriptor
  85293. + * DMA mode SW is able to initiate a transfer,
  85294. + * which will handle all the packets including
  85295. + * the last 0 length.
  85296. + */
  85297. + ep->dwc_ep.sent_zlp = 0;
  85298. + dwc_otg_ep_start_zl_transfer(core_if,
  85299. + &ep->dwc_ep);
  85300. + } else {
  85301. + is_last = 1;
  85302. + }
  85303. + }
  85304. +
  85305. + DWC_DEBUGPL(DBG_PCDV,
  85306. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  85307. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  85308. + ep->dwc_ep.is_in ? "IN" : "OUT",
  85309. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  85310. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  85311. + }
  85312. +
  85313. + /* Complete the request */
  85314. + if (is_last) {
  85315. +#ifdef DWC_UTE_CFI
  85316. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  85317. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  85318. + } else {
  85319. +#endif
  85320. + req->actual = ep->dwc_ep.xfer_count;
  85321. +#ifdef DWC_UTE_CFI
  85322. + }
  85323. +#endif
  85324. + if (req->dw_align_buf) {
  85325. + if (!ep->dwc_ep.is_in) {
  85326. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  85327. + }
  85328. + DWC_DMA_FREE(req->length, req->dw_align_buf,
  85329. + req->dw_align_buf_dma);
  85330. + }
  85331. +
  85332. + dwc_otg_request_done(ep, req, 0);
  85333. +
  85334. + ep->dwc_ep.start_xfer_buff = 0;
  85335. + ep->dwc_ep.xfer_buff = 0;
  85336. + ep->dwc_ep.xfer_len = 0;
  85337. +
  85338. + /* If there is a request in the queue start it. */
  85339. + start_next_request(ep);
  85340. + }
  85341. +}
  85342. +
  85343. +#ifdef DWC_EN_ISOC
  85344. +
  85345. +/**
  85346. + * This function BNA interrupt for Isochronous EPs
  85347. + *
  85348. + */
  85349. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  85350. +{
  85351. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85352. + volatile uint32_t *addr;
  85353. + depctl_data_t depctl = {.d32 = 0 };
  85354. + dwc_otg_pcd_t *pcd = ep->pcd;
  85355. + dwc_otg_dev_dma_desc_t *dma_desc;
  85356. + int i;
  85357. +
  85358. + dma_desc =
  85359. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  85360. +
  85361. + if (dwc_ep->is_in) {
  85362. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85363. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  85364. + sts.d32 = dma_desc->status.d32;
  85365. + sts.b_iso_in.bs = BS_HOST_READY;
  85366. + dma_desc->status.d32 = sts.d32;
  85367. + }
  85368. + } else {
  85369. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85370. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  85371. + sts.d32 = dma_desc->status.d32;
  85372. + sts.b_iso_out.bs = BS_HOST_READY;
  85373. + dma_desc->status.d32 = sts.d32;
  85374. + }
  85375. + }
  85376. +
  85377. + if (dwc_ep->is_in == 0) {
  85378. + addr =
  85379. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  85380. + num]->doepctl;
  85381. + } else {
  85382. + addr =
  85383. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  85384. + }
  85385. + depctl.b.epena = 1;
  85386. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  85387. +}
  85388. +
  85389. +/**
  85390. + * This function sets latest iso packet information(non-PTI mode)
  85391. + *
  85392. + * @param core_if Programming view of DWC_otg controller.
  85393. + * @param ep The EP to start the transfer on.
  85394. + *
  85395. + */
  85396. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  85397. +{
  85398. + deptsiz_data_t deptsiz = {.d32 = 0 };
  85399. + dma_addr_t dma_addr;
  85400. + uint32_t offset;
  85401. +
  85402. + if (ep->proc_buf_num)
  85403. + dma_addr = ep->dma_addr1;
  85404. + else
  85405. + dma_addr = ep->dma_addr0;
  85406. +
  85407. + if (ep->is_in) {
  85408. + deptsiz.d32 =
  85409. + DWC_READ_REG32(&core_if->dev_if->
  85410. + in_ep_regs[ep->num]->dieptsiz);
  85411. + offset = ep->data_per_frame;
  85412. + } else {
  85413. + deptsiz.d32 =
  85414. + DWC_READ_REG32(&core_if->dev_if->
  85415. + out_ep_regs[ep->num]->doeptsiz);
  85416. + offset =
  85417. + ep->data_per_frame +
  85418. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  85419. + }
  85420. +
  85421. + if (!deptsiz.b.xfersize) {
  85422. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  85423. + ep->pkt_info[ep->cur_pkt].offset =
  85424. + ep->cur_pkt_dma_addr - dma_addr;
  85425. + ep->pkt_info[ep->cur_pkt].status = 0;
  85426. + } else {
  85427. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  85428. + ep->pkt_info[ep->cur_pkt].offset =
  85429. + ep->cur_pkt_dma_addr - dma_addr;
  85430. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  85431. + }
  85432. + ep->cur_pkt_addr += offset;
  85433. + ep->cur_pkt_dma_addr += offset;
  85434. + ep->cur_pkt++;
  85435. +}
  85436. +
  85437. +/**
  85438. + * This function sets latest iso packet information(DDMA mode)
  85439. + *
  85440. + * @param core_if Programming view of DWC_otg controller.
  85441. + * @param dwc_ep The EP to start the transfer on.
  85442. + *
  85443. + */
  85444. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  85445. + dwc_ep_t * dwc_ep)
  85446. +{
  85447. + dwc_otg_dev_dma_desc_t *dma_desc;
  85448. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85449. + iso_pkt_info_t *iso_packet;
  85450. + uint32_t data_per_desc;
  85451. + uint32_t offset;
  85452. + int i, j;
  85453. +
  85454. + iso_packet = dwc_ep->pkt_info;
  85455. +
  85456. + /** Reinit closed DMA Descriptors*/
  85457. + /** ISO OUT EP */
  85458. + if (dwc_ep->is_in == 0) {
  85459. + dma_desc =
  85460. + dwc_ep->iso_desc_addr +
  85461. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85462. + offset = 0;
  85463. +
  85464. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  85465. + i += dwc_ep->pkt_per_frm) {
  85466. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  85467. + data_per_desc =
  85468. + ((j + 1) * dwc_ep->maxpacket >
  85469. + dwc_ep->
  85470. + data_per_frame) ? dwc_ep->data_per_frame -
  85471. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85472. + data_per_desc +=
  85473. + (data_per_desc % 4) ? (4 -
  85474. + data_per_desc %
  85475. + 4) : 0;
  85476. +
  85477. + sts.d32 = dma_desc->status.d32;
  85478. +
  85479. + /* Write status in iso_packet_decsriptor */
  85480. + iso_packet->status =
  85481. + sts.b_iso_out.rxsts +
  85482. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  85483. + if (iso_packet->status) {
  85484. + iso_packet->status = -DWC_E_NO_DATA;
  85485. + }
  85486. +
  85487. + /* Received data length */
  85488. + if (!sts.b_iso_out.rxbytes) {
  85489. + iso_packet->length =
  85490. + data_per_desc -
  85491. + sts.b_iso_out.rxbytes;
  85492. + } else {
  85493. + iso_packet->length =
  85494. + data_per_desc -
  85495. + sts.b_iso_out.rxbytes + (4 -
  85496. + dwc_ep->data_per_frame
  85497. + % 4);
  85498. + }
  85499. +
  85500. + iso_packet->offset = offset;
  85501. +
  85502. + offset += data_per_desc;
  85503. + dma_desc++;
  85504. + iso_packet++;
  85505. + }
  85506. + }
  85507. +
  85508. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  85509. + data_per_desc =
  85510. + ((j + 1) * dwc_ep->maxpacket >
  85511. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85512. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85513. + data_per_desc +=
  85514. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85515. +
  85516. + sts.d32 = dma_desc->status.d32;
  85517. +
  85518. + /* Write status in iso_packet_decsriptor */
  85519. + iso_packet->status =
  85520. + sts.b_iso_out.rxsts +
  85521. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  85522. + if (iso_packet->status) {
  85523. + iso_packet->status = -DWC_E_NO_DATA;
  85524. + }
  85525. +
  85526. + /* Received data length */
  85527. + iso_packet->length =
  85528. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  85529. +
  85530. + iso_packet->offset = offset;
  85531. +
  85532. + offset += data_per_desc;
  85533. + iso_packet++;
  85534. + dma_desc++;
  85535. + }
  85536. +
  85537. + sts.d32 = dma_desc->status.d32;
  85538. +
  85539. + /* Write status in iso_packet_decsriptor */
  85540. + iso_packet->status =
  85541. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  85542. + if (iso_packet->status) {
  85543. + iso_packet->status = -DWC_E_NO_DATA;
  85544. + }
  85545. + /* Received data length */
  85546. + if (!sts.b_iso_out.rxbytes) {
  85547. + iso_packet->length =
  85548. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  85549. + } else {
  85550. + iso_packet->length =
  85551. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  85552. + (4 - dwc_ep->data_per_frame % 4);
  85553. + }
  85554. +
  85555. + iso_packet->offset = offset;
  85556. + } else {
  85557. +/** ISO IN EP */
  85558. +
  85559. + dma_desc =
  85560. + dwc_ep->iso_desc_addr +
  85561. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85562. +
  85563. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  85564. + sts.d32 = dma_desc->status.d32;
  85565. +
  85566. + /* Write status in iso packet descriptor */
  85567. + iso_packet->status =
  85568. + sts.b_iso_in.txsts +
  85569. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  85570. + if (iso_packet->status != 0) {
  85571. + iso_packet->status = -DWC_E_NO_DATA;
  85572. +
  85573. + }
  85574. + /* Bytes has been transfered */
  85575. + iso_packet->length =
  85576. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  85577. +
  85578. + dma_desc++;
  85579. + iso_packet++;
  85580. + }
  85581. +
  85582. + sts.d32 = dma_desc->status.d32;
  85583. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  85584. + sts.d32 = dma_desc->status.d32;
  85585. + }
  85586. +
  85587. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  85588. + iso_packet->status =
  85589. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  85590. + if (iso_packet->status != 0) {
  85591. + iso_packet->status = -DWC_E_NO_DATA;
  85592. + }
  85593. +
  85594. + /* Bytes has been transfered */
  85595. + iso_packet->length =
  85596. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  85597. + }
  85598. +}
  85599. +
  85600. +/**
  85601. + * This function reinitialize DMA Descriptors for Isochronous transfer
  85602. + *
  85603. + * @param core_if Programming view of DWC_otg controller.
  85604. + * @param dwc_ep The EP to start the transfer on.
  85605. + *
  85606. + */
  85607. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  85608. +{
  85609. + int i, j;
  85610. + dwc_otg_dev_dma_desc_t *dma_desc;
  85611. + dma_addr_t dma_ad;
  85612. + volatile uint32_t *addr;
  85613. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85614. + uint32_t data_per_desc;
  85615. +
  85616. + if (dwc_ep->is_in == 0) {
  85617. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  85618. + } else {
  85619. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  85620. + }
  85621. +
  85622. + if (dwc_ep->proc_buf_num == 0) {
  85623. + /** Buffer 0 descriptors setup */
  85624. + dma_ad = dwc_ep->dma_addr0;
  85625. + } else {
  85626. + /** Buffer 1 descriptors setup */
  85627. + dma_ad = dwc_ep->dma_addr1;
  85628. + }
  85629. +
  85630. + /** Reinit closed DMA Descriptors*/
  85631. + /** ISO OUT EP */
  85632. + if (dwc_ep->is_in == 0) {
  85633. + dma_desc =
  85634. + dwc_ep->iso_desc_addr +
  85635. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85636. +
  85637. + sts.b_iso_out.bs = BS_HOST_READY;
  85638. + sts.b_iso_out.rxsts = 0;
  85639. + sts.b_iso_out.l = 0;
  85640. + sts.b_iso_out.sp = 0;
  85641. + sts.b_iso_out.ioc = 0;
  85642. + sts.b_iso_out.pid = 0;
  85643. + sts.b_iso_out.framenum = 0;
  85644. +
  85645. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  85646. + i += dwc_ep->pkt_per_frm) {
  85647. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  85648. + data_per_desc =
  85649. + ((j + 1) * dwc_ep->maxpacket >
  85650. + dwc_ep->
  85651. + data_per_frame) ? dwc_ep->data_per_frame -
  85652. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85653. + data_per_desc +=
  85654. + (data_per_desc % 4) ? (4 -
  85655. + data_per_desc %
  85656. + 4) : 0;
  85657. + sts.b_iso_out.rxbytes = data_per_desc;
  85658. + dma_desc->buf = dma_ad;
  85659. + dma_desc->status.d32 = sts.d32;
  85660. +
  85661. + dma_ad += data_per_desc;
  85662. + dma_desc++;
  85663. + }
  85664. + }
  85665. +
  85666. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  85667. +
  85668. + data_per_desc =
  85669. + ((j + 1) * dwc_ep->maxpacket >
  85670. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85671. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85672. + data_per_desc +=
  85673. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85674. + sts.b_iso_out.rxbytes = data_per_desc;
  85675. +
  85676. + dma_desc->buf = dma_ad;
  85677. + dma_desc->status.d32 = sts.d32;
  85678. +
  85679. + dma_desc++;
  85680. + dma_ad += data_per_desc;
  85681. + }
  85682. +
  85683. + sts.b_iso_out.ioc = 1;
  85684. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  85685. +
  85686. + data_per_desc =
  85687. + ((j + 1) * dwc_ep->maxpacket >
  85688. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85689. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85690. + data_per_desc +=
  85691. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85692. + sts.b_iso_out.rxbytes = data_per_desc;
  85693. +
  85694. + dma_desc->buf = dma_ad;
  85695. + dma_desc->status.d32 = sts.d32;
  85696. + } else {
  85697. +/** ISO IN EP */
  85698. +
  85699. + dma_desc =
  85700. + dwc_ep->iso_desc_addr +
  85701. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85702. +
  85703. + sts.b_iso_in.bs = BS_HOST_READY;
  85704. + sts.b_iso_in.txsts = 0;
  85705. + sts.b_iso_in.sp = 0;
  85706. + sts.b_iso_in.ioc = 0;
  85707. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  85708. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  85709. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  85710. + sts.b_iso_in.l = 0;
  85711. +
  85712. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  85713. + dma_desc->buf = dma_ad;
  85714. + dma_desc->status.d32 = sts.d32;
  85715. +
  85716. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  85717. + dma_ad += dwc_ep->data_per_frame;
  85718. + dma_desc++;
  85719. + }
  85720. +
  85721. + sts.b_iso_in.ioc = 1;
  85722. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  85723. +
  85724. + dma_desc->buf = dma_ad;
  85725. + dma_desc->status.d32 = sts.d32;
  85726. +
  85727. + dwc_ep->next_frame =
  85728. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  85729. + }
  85730. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85731. +}
  85732. +
  85733. +/**
  85734. + * This function is to handle Iso EP transfer complete interrupt
  85735. + * in case Iso out packet was dropped
  85736. + *
  85737. + * @param core_if Programming view of DWC_otg controller.
  85738. + * @param dwc_ep The EP for wihich transfer complete was asserted
  85739. + *
  85740. + */
  85741. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  85742. + dwc_ep_t * dwc_ep)
  85743. +{
  85744. + uint32_t dma_addr;
  85745. + uint32_t drp_pkt;
  85746. + uint32_t drp_pkt_cnt;
  85747. + deptsiz_data_t deptsiz = {.d32 = 0 };
  85748. + depctl_data_t depctl = {.d32 = 0 };
  85749. + int i;
  85750. +
  85751. + deptsiz.d32 =
  85752. + DWC_READ_REG32(&core_if->dev_if->
  85753. + out_ep_regs[dwc_ep->num]->doeptsiz);
  85754. +
  85755. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  85756. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  85757. +
  85758. + /* Setting dropped packets status */
  85759. + for (i = 0; i < drp_pkt_cnt; ++i) {
  85760. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  85761. + drp_pkt++;
  85762. + deptsiz.b.pktcnt--;
  85763. + }
  85764. +
  85765. + if (deptsiz.b.pktcnt > 0) {
  85766. + deptsiz.b.xfersize =
  85767. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  85768. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  85769. + } else {
  85770. + deptsiz.b.xfersize = 0;
  85771. + deptsiz.b.pktcnt = 0;
  85772. + }
  85773. +
  85774. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  85775. + deptsiz.d32);
  85776. +
  85777. + if (deptsiz.b.pktcnt > 0) {
  85778. + if (dwc_ep->proc_buf_num) {
  85779. + dma_addr =
  85780. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  85781. + deptsiz.b.xfersize;
  85782. + } else {
  85783. + dma_addr =
  85784. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  85785. + deptsiz.b.xfersize;;
  85786. + }
  85787. +
  85788. + DWC_WRITE_REG32(&core_if->dev_if->
  85789. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  85790. +
  85791. + /** Re-enable endpoint, clear nak */
  85792. + depctl.d32 = 0;
  85793. + depctl.b.epena = 1;
  85794. + depctl.b.cnak = 1;
  85795. +
  85796. + DWC_MODIFY_REG32(&core_if->dev_if->
  85797. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  85798. + depctl.d32);
  85799. + return 0;
  85800. + } else {
  85801. + return 1;
  85802. + }
  85803. +}
  85804. +
  85805. +/**
  85806. + * This function sets iso packets information(PTI mode)
  85807. + *
  85808. + * @param core_if Programming view of DWC_otg controller.
  85809. + * @param ep The EP to start the transfer on.
  85810. + *
  85811. + */
  85812. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  85813. +{
  85814. + int i, j;
  85815. + dma_addr_t dma_ad;
  85816. + iso_pkt_info_t *packet_info = ep->pkt_info;
  85817. + uint32_t offset;
  85818. + uint32_t frame_data;
  85819. + deptsiz_data_t deptsiz;
  85820. +
  85821. + if (ep->proc_buf_num == 0) {
  85822. + /** Buffer 0 descriptors setup */
  85823. + dma_ad = ep->dma_addr0;
  85824. + } else {
  85825. + /** Buffer 1 descriptors setup */
  85826. + dma_ad = ep->dma_addr1;
  85827. + }
  85828. +
  85829. + if (ep->is_in) {
  85830. + deptsiz.d32 =
  85831. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  85832. + dieptsiz);
  85833. + } else {
  85834. + deptsiz.d32 =
  85835. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  85836. + doeptsiz);
  85837. + }
  85838. +
  85839. + if (!deptsiz.b.xfersize) {
  85840. + offset = 0;
  85841. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  85842. + frame_data = ep->data_per_frame;
  85843. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  85844. +
  85845. + /* Packet status - is not set as initially
  85846. + * it is set to 0 and if packet was sent
  85847. + successfully, status field will remain 0*/
  85848. +
  85849. + /* Bytes has been transfered */
  85850. + packet_info->length =
  85851. + (ep->maxpacket <
  85852. + frame_data) ? ep->maxpacket : frame_data;
  85853. +
  85854. + /* Received packet offset */
  85855. + packet_info->offset = offset;
  85856. + offset += packet_info->length;
  85857. + frame_data -= packet_info->length;
  85858. +
  85859. + packet_info++;
  85860. + }
  85861. + }
  85862. + return 1;
  85863. + } else {
  85864. + /* This is a workaround for in case of Transfer Complete with
  85865. + * PktDrpSts interrupts merging - in this case Transfer complete
  85866. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  85867. + * set and with DOEPTSIZ register non zero. Investigations showed,
  85868. + * that this happens when Out packet is dropped, but because of
  85869. + * interrupts merging during first interrupt handling PktDrpSts
  85870. + * bit is cleared and for next merged interrupts it is not reset.
  85871. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  85872. + */
  85873. + if (ep->is_in) {
  85874. + return 1;
  85875. + } else {
  85876. + return handle_iso_out_pkt_dropped(core_if, ep);
  85877. + }
  85878. + }
  85879. +}
  85880. +
  85881. +/**
  85882. + * This function is to handle Iso EP transfer complete interrupt
  85883. + *
  85884. + * @param pcd The PCD
  85885. + * @param ep The EP for which transfer complete was asserted
  85886. + *
  85887. + */
  85888. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  85889. +{
  85890. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  85891. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85892. + uint8_t is_last = 0;
  85893. +
  85894. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  85895. + DWC_WARN("Next frame is not set!\n");
  85896. + return;
  85897. + }
  85898. +
  85899. + if (core_if->dma_enable) {
  85900. + if (core_if->dma_desc_enable) {
  85901. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  85902. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  85903. + is_last = 1;
  85904. + } else {
  85905. + if (core_if->pti_enh_enable) {
  85906. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  85907. + dwc_ep->proc_buf_num =
  85908. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85909. + dwc_otg_iso_ep_start_buf_transfer
  85910. + (core_if, dwc_ep);
  85911. + is_last = 1;
  85912. + }
  85913. + } else {
  85914. + set_current_pkt_info(core_if, dwc_ep);
  85915. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  85916. + is_last = 1;
  85917. + dwc_ep->cur_pkt = 0;
  85918. + dwc_ep->proc_buf_num =
  85919. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85920. + if (dwc_ep->proc_buf_num) {
  85921. + dwc_ep->cur_pkt_addr =
  85922. + dwc_ep->xfer_buff1;
  85923. + dwc_ep->cur_pkt_dma_addr =
  85924. + dwc_ep->dma_addr1;
  85925. + } else {
  85926. + dwc_ep->cur_pkt_addr =
  85927. + dwc_ep->xfer_buff0;
  85928. + dwc_ep->cur_pkt_dma_addr =
  85929. + dwc_ep->dma_addr0;
  85930. + }
  85931. +
  85932. + }
  85933. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  85934. + dwc_ep);
  85935. + }
  85936. + }
  85937. + } else {
  85938. + set_current_pkt_info(core_if, dwc_ep);
  85939. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  85940. + is_last = 1;
  85941. + dwc_ep->cur_pkt = 0;
  85942. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85943. + if (dwc_ep->proc_buf_num) {
  85944. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  85945. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  85946. + } else {
  85947. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  85948. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  85949. + }
  85950. +
  85951. + }
  85952. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  85953. + }
  85954. + if (is_last)
  85955. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  85956. +}
  85957. +#endif /* DWC_EN_ISOC */
  85958. +
  85959. +/**
  85960. + * This function handle BNA interrupt for Non Isochronous EPs
  85961. + *
  85962. + */
  85963. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  85964. +{
  85965. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85966. + volatile uint32_t *addr;
  85967. + depctl_data_t depctl = {.d32 = 0 };
  85968. + dwc_otg_pcd_t *pcd = ep->pcd;
  85969. + dwc_otg_dev_dma_desc_t *dma_desc;
  85970. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85971. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  85972. + int i, start;
  85973. +
  85974. + if (!dwc_ep->desc_cnt)
  85975. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  85976. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  85977. +
  85978. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  85979. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  85980. + uint32_t doepdma;
  85981. + dwc_otg_dev_out_ep_regs_t *out_regs =
  85982. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  85983. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  85984. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  85985. + dma_desc = &(dwc_ep->desc_addr[start]);
  85986. + } else {
  85987. + start = 0;
  85988. + dma_desc = dwc_ep->desc_addr;
  85989. + }
  85990. +
  85991. +
  85992. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  85993. + sts.d32 = dma_desc->status.d32;
  85994. + sts.b.bs = BS_HOST_READY;
  85995. + dma_desc->status.d32 = sts.d32;
  85996. + }
  85997. +
  85998. + if (dwc_ep->is_in == 0) {
  85999. + addr =
  86000. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  86001. + doepctl;
  86002. + } else {
  86003. + addr =
  86004. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  86005. + }
  86006. + depctl.b.epena = 1;
  86007. + depctl.b.cnak = 1;
  86008. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  86009. +}
  86010. +
  86011. +/**
  86012. + * This function handles EP0 Control transfers.
  86013. + *
  86014. + * The state of the control transfers are tracked in
  86015. + * <code>ep0state</code>.
  86016. + */
  86017. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  86018. +{
  86019. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86020. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  86021. + dev_dma_desc_sts_t desc_sts;
  86022. + deptsiz0_data_t deptsiz;
  86023. + uint32_t byte_count;
  86024. +
  86025. +#ifdef DEBUG_EP0
  86026. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  86027. + print_ep0_state(pcd);
  86028. +#endif
  86029. +
  86030. +// DWC_PRINTF("HANDLE EP0\n");
  86031. +
  86032. + switch (pcd->ep0state) {
  86033. + case EP0_DISCONNECT:
  86034. + break;
  86035. +
  86036. + case EP0_IDLE:
  86037. + pcd->request_config = 0;
  86038. +
  86039. + pcd_setup(pcd);
  86040. + break;
  86041. +
  86042. + case EP0_IN_DATA_PHASE:
  86043. +#ifdef DEBUG_EP0
  86044. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  86045. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  86046. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  86047. +#endif
  86048. +
  86049. + if (core_if->dma_enable != 0) {
  86050. + /*
  86051. + * For EP0 we can only program 1 packet at a time so we
  86052. + * need to do the make calculations after each complete.
  86053. + * Call write_packet to make the calculations, as in
  86054. + * slave mode, and use those values to determine if we
  86055. + * can complete.
  86056. + */
  86057. + if (core_if->dma_desc_enable == 0) {
  86058. + deptsiz.d32 =
  86059. + DWC_READ_REG32(&core_if->
  86060. + dev_if->in_ep_regs[0]->
  86061. + dieptsiz);
  86062. + byte_count =
  86063. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  86064. + } else {
  86065. + desc_sts =
  86066. + core_if->dev_if->in_desc_addr->status;
  86067. + byte_count =
  86068. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  86069. + }
  86070. + ep0->dwc_ep.xfer_count += byte_count;
  86071. + ep0->dwc_ep.xfer_buff += byte_count;
  86072. + ep0->dwc_ep.dma_addr += byte_count;
  86073. + }
  86074. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  86075. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86076. + &ep0->dwc_ep);
  86077. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  86078. + } else if (ep0->dwc_ep.sent_zlp) {
  86079. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86080. + &ep0->dwc_ep);
  86081. + ep0->dwc_ep.sent_zlp = 0;
  86082. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  86083. + } else {
  86084. + ep0_complete_request(ep0);
  86085. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  86086. + }
  86087. + break;
  86088. + case EP0_OUT_DATA_PHASE:
  86089. +#ifdef DEBUG_EP0
  86090. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  86091. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  86092. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  86093. +#endif
  86094. + if (core_if->dma_enable != 0) {
  86095. + if (core_if->dma_desc_enable == 0) {
  86096. + deptsiz.d32 =
  86097. + DWC_READ_REG32(&core_if->
  86098. + dev_if->out_ep_regs[0]->
  86099. + doeptsiz);
  86100. + byte_count =
  86101. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  86102. + } else {
  86103. + desc_sts =
  86104. + core_if->dev_if->out_desc_addr->status;
  86105. + byte_count =
  86106. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  86107. + }
  86108. + ep0->dwc_ep.xfer_count += byte_count;
  86109. + ep0->dwc_ep.xfer_buff += byte_count;
  86110. + ep0->dwc_ep.dma_addr += byte_count;
  86111. + }
  86112. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  86113. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86114. + &ep0->dwc_ep);
  86115. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  86116. + } else if (ep0->dwc_ep.sent_zlp) {
  86117. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86118. + &ep0->dwc_ep);
  86119. + ep0->dwc_ep.sent_zlp = 0;
  86120. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  86121. + } else {
  86122. + ep0_complete_request(ep0);
  86123. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  86124. + }
  86125. + break;
  86126. +
  86127. + case EP0_IN_STATUS_PHASE:
  86128. + case EP0_OUT_STATUS_PHASE:
  86129. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  86130. + ep0_complete_request(ep0);
  86131. + pcd->ep0state = EP0_IDLE;
  86132. + ep0->stopped = 1;
  86133. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  86134. +
  86135. + /* Prepare for more SETUP Packets */
  86136. + if (core_if->dma_enable) {
  86137. + ep0_out_start(core_if, pcd);
  86138. + }
  86139. + break;
  86140. +
  86141. + case EP0_STALL:
  86142. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  86143. + break;
  86144. + }
  86145. +#ifdef DEBUG_EP0
  86146. + print_ep0_state(pcd);
  86147. +#endif
  86148. +}
  86149. +
  86150. +/**
  86151. + * Restart transfer
  86152. + */
  86153. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  86154. +{
  86155. + dwc_otg_core_if_t *core_if;
  86156. + dwc_otg_dev_if_t *dev_if;
  86157. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  86158. + dwc_otg_pcd_ep_t *ep;
  86159. +
  86160. + ep = get_in_ep(pcd, epnum);
  86161. +
  86162. +#ifdef DWC_EN_ISOC
  86163. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  86164. + return;
  86165. + }
  86166. +#endif /* DWC_EN_ISOC */
  86167. +
  86168. + core_if = GET_CORE_IF(pcd);
  86169. + dev_if = core_if->dev_if;
  86170. +
  86171. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  86172. +
  86173. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  86174. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  86175. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  86176. + /*
  86177. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  86178. + */
  86179. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  86180. + ep->dwc_ep.start_xfer_buff != 0) {
  86181. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  86182. + ep->dwc_ep.xfer_count = 0;
  86183. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  86184. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  86185. + } else {
  86186. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  86187. + /* convert packet size to dwords. */
  86188. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  86189. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  86190. + }
  86191. + ep->stopped = 0;
  86192. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  86193. + "xfer_len=%0x stopped=%d\n",
  86194. + ep->dwc_ep.xfer_buff,
  86195. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  86196. + ep->stopped);
  86197. + if (epnum == 0) {
  86198. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  86199. + } else {
  86200. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  86201. + }
  86202. + }
  86203. +}
  86204. +
  86205. +/*
  86206. + * This function create new nextep sequnce based on Learn Queue.
  86207. + *
  86208. + * @param core_if Programming view of DWC_otg controller
  86209. + */
  86210. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  86211. +{
  86212. + dwc_otg_device_global_regs_t *dev_global_regs =
  86213. + core_if->dev_if->dev_global_regs;
  86214. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  86215. + /* Number of Token Queue Registers */
  86216. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  86217. + dtknq1_data_t dtknqr1;
  86218. + uint32_t in_tkn_epnums[4];
  86219. + uint8_t seqnum[MAX_EPS_CHANNELS];
  86220. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  86221. + grstctl_t resetctl = {.d32 = 0 };
  86222. + uint8_t temp;
  86223. + int ndx = 0;
  86224. + int start = 0;
  86225. + int end = 0;
  86226. + int sort_done = 0;
  86227. + int i = 0;
  86228. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  86229. +
  86230. +
  86231. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  86232. +
  86233. + /* Read the DTKNQ Registers */
  86234. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  86235. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  86236. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  86237. + in_tkn_epnums[i]);
  86238. + if (addr == &dev_global_regs->dvbusdis) {
  86239. + addr = &dev_global_regs->dtknqr3_dthrctl;
  86240. + } else {
  86241. + ++addr;
  86242. + }
  86243. +
  86244. + }
  86245. +
  86246. + /* Copy the DTKNQR1 data to the bit field. */
  86247. + dtknqr1.d32 = in_tkn_epnums[0];
  86248. + if (dtknqr1.b.wrap_bit) {
  86249. + ndx = dtknqr1.b.intknwptr;
  86250. + end = ndx -1;
  86251. + if (end < 0)
  86252. + end = TOKEN_Q_DEPTH -1;
  86253. + } else {
  86254. + ndx = 0;
  86255. + end = dtknqr1.b.intknwptr -1;
  86256. + if (end < 0)
  86257. + end = 0;
  86258. + }
  86259. + start = ndx;
  86260. +
  86261. + /* Fill seqnum[] by initial values: EP number + 31 */
  86262. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  86263. + seqnum[i] = i +31;
  86264. + }
  86265. +
  86266. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  86267. + for (i=0; i < 6; i++)
  86268. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  86269. +
  86270. + if (TOKEN_Q_DEPTH > 6) {
  86271. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  86272. + for (i=6; i < 14; i++)
  86273. + intkn_seq[i] =
  86274. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  86275. + }
  86276. +
  86277. + if (TOKEN_Q_DEPTH > 14) {
  86278. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  86279. + for (i=14; i < 22; i++)
  86280. + intkn_seq[i] =
  86281. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  86282. + }
  86283. +
  86284. + if (TOKEN_Q_DEPTH > 22) {
  86285. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  86286. + for (i=22; i < 30; i++)
  86287. + intkn_seq[i] =
  86288. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  86289. + }
  86290. +
  86291. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  86292. + start, end);
  86293. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  86294. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  86295. +
  86296. + /* Update seqnum based on intkn_seq[] */
  86297. + i = 0;
  86298. + do {
  86299. + seqnum[intkn_seq[ndx]] = i;
  86300. + ndx++;
  86301. + i++;
  86302. + if (ndx == TOKEN_Q_DEPTH)
  86303. + ndx = 0;
  86304. + } while ( i < TOKEN_Q_DEPTH );
  86305. +
  86306. + /* Mark non active EP's in seqnum[] by 0xff */
  86307. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  86308. + if (core_if->nextep_seq[i] == 0xff )
  86309. + seqnum[i] = 0xff;
  86310. + }
  86311. +
  86312. + /* Sort seqnum[] */
  86313. + sort_done = 0;
  86314. + while (!sort_done) {
  86315. + sort_done = 1;
  86316. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  86317. + if (seqnum[i] > seqnum[i+1]) {
  86318. + temp = seqnum[i];
  86319. + seqnum[i] = seqnum[i+1];
  86320. + seqnum[i+1] = temp;
  86321. + sort_done = 0;
  86322. + }
  86323. + }
  86324. + }
  86325. +
  86326. + ndx = start + seqnum[0];
  86327. + if (ndx >= TOKEN_Q_DEPTH)
  86328. + ndx = ndx % TOKEN_Q_DEPTH;
  86329. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  86330. +
  86331. + /* Update seqnum[] by EP numbers */
  86332. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  86333. + ndx = start + i;
  86334. + if (seqnum[i] < 31) {
  86335. + ndx = start + seqnum[i];
  86336. + if (ndx >= TOKEN_Q_DEPTH)
  86337. + ndx = ndx % TOKEN_Q_DEPTH;
  86338. + seqnum[i] = intkn_seq[ndx];
  86339. + } else {
  86340. + if (seqnum[i] < 0xff) {
  86341. + seqnum[i] = seqnum[i] - 31;
  86342. + } else {
  86343. + break;
  86344. + }
  86345. + }
  86346. + }
  86347. +
  86348. + /* Update nextep_seq[] based on seqnum[] */
  86349. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  86350. + if (seqnum[i] != 0xff) {
  86351. + if (seqnum[i+1] != 0xff) {
  86352. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  86353. + } else {
  86354. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  86355. + break;
  86356. + }
  86357. + } else {
  86358. + break;
  86359. + }
  86360. + }
  86361. +
  86362. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  86363. + __func__, core_if->first_in_nextep_seq);
  86364. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  86365. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  86366. + }
  86367. +
  86368. + /* Flush the Learning Queue */
  86369. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  86370. + resetctl.b.intknqflsh = 1;
  86371. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  86372. +
  86373. +
  86374. +}
  86375. +
  86376. +/**
  86377. + * handle the IN EP disable interrupt.
  86378. + */
  86379. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  86380. + const uint32_t epnum)
  86381. +{
  86382. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86383. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  86384. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  86385. + dctl_data_t dctl = {.d32 = 0 };
  86386. + dwc_otg_pcd_ep_t *ep;
  86387. + dwc_ep_t *dwc_ep;
  86388. + gintmsk_data_t gintmsk_data;
  86389. + depctl_data_t depctl;
  86390. + uint32_t diepdma;
  86391. + uint32_t remain_to_transfer = 0;
  86392. + uint8_t i;
  86393. + uint32_t xfer_size;
  86394. +
  86395. + ep = get_in_ep(pcd, epnum);
  86396. + dwc_ep = &ep->dwc_ep;
  86397. +
  86398. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86399. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  86400. + complete_ep(ep);
  86401. + return;
  86402. + }
  86403. +
  86404. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  86405. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  86406. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  86407. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86408. +
  86409. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  86410. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  86411. +
  86412. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  86413. + if (ep->stopped) {
  86414. + if (core_if->en_multiple_tx_fifo)
  86415. + /* Flush the Tx FIFO */
  86416. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  86417. + /* Clear the Global IN NP NAK */
  86418. + dctl.d32 = 0;
  86419. + dctl.b.cgnpinnak = 1;
  86420. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  86421. + /* Restart the transaction */
  86422. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  86423. + restart_transfer(pcd, epnum);
  86424. + }
  86425. + } else {
  86426. + /* Restart the transaction */
  86427. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  86428. + restart_transfer(pcd, epnum);
  86429. + }
  86430. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  86431. + }
  86432. + return;
  86433. + }
  86434. +
  86435. + if (core_if->start_predict > 2) { // NP IN EP
  86436. + core_if->start_predict--;
  86437. + return;
  86438. + }
  86439. +
  86440. + core_if->start_predict--;
  86441. +
  86442. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  86443. +
  86444. + predict_nextep_seq(core_if);
  86445. +
  86446. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  86447. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  86448. + depctl.d32 =
  86449. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86450. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  86451. + depctl.b.nextep = core_if->nextep_seq[i];
  86452. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  86453. + }
  86454. + }
  86455. + /* Flush Shared NP TxFIFO */
  86456. + dwc_otg_flush_tx_fifo(core_if, 0);
  86457. + /* Rewind buffers */
  86458. + if (!core_if->dma_desc_enable) {
  86459. + i = core_if->first_in_nextep_seq;
  86460. + do {
  86461. + ep = get_in_ep(pcd, i);
  86462. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  86463. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  86464. + if (xfer_size > ep->dwc_ep.maxxfer)
  86465. + xfer_size = ep->dwc_ep.maxxfer;
  86466. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86467. + if (dieptsiz.b.pktcnt != 0) {
  86468. + if (xfer_size == 0) {
  86469. + remain_to_transfer = 0;
  86470. + } else {
  86471. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  86472. + remain_to_transfer =
  86473. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  86474. + } else {
  86475. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  86476. + + (xfer_size % ep->dwc_ep.maxpacket);
  86477. + }
  86478. + }
  86479. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  86480. + dieptsiz.b.xfersize = remain_to_transfer;
  86481. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  86482. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  86483. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  86484. + }
  86485. + i = core_if->nextep_seq[i];
  86486. + } while (i != core_if->first_in_nextep_seq);
  86487. + } else { // dma_desc_enable
  86488. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  86489. + }
  86490. +
  86491. + /* Restart transfers in predicted sequences */
  86492. + i = core_if->first_in_nextep_seq;
  86493. + do {
  86494. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  86495. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86496. + if (dieptsiz.b.pktcnt != 0) {
  86497. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86498. + depctl.b.epena = 1;
  86499. + depctl.b.cnak = 1;
  86500. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  86501. + }
  86502. + i = core_if->nextep_seq[i];
  86503. + } while (i != core_if->first_in_nextep_seq);
  86504. +
  86505. + /* Clear the global non-periodic IN NAK handshake */
  86506. + dctl.d32 = 0;
  86507. + dctl.b.cgnpinnak = 1;
  86508. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  86509. +
  86510. + /* Unmask EP Mismatch interrupt */
  86511. + gintmsk_data.d32 = 0;
  86512. + gintmsk_data.b.epmismatch = 1;
  86513. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  86514. +
  86515. + core_if->start_predict = 0;
  86516. +
  86517. + }
  86518. +}
  86519. +
  86520. +/**
  86521. + * Handler for the IN EP timeout handshake interrupt.
  86522. + */
  86523. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  86524. + const uint32_t epnum)
  86525. +{
  86526. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86527. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  86528. +
  86529. +#ifdef DEBUG
  86530. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  86531. + uint32_t num = 0;
  86532. +#endif
  86533. + dctl_data_t dctl = {.d32 = 0 };
  86534. + dwc_otg_pcd_ep_t *ep;
  86535. +
  86536. + gintmsk_data_t intr_mask = {.d32 = 0 };
  86537. +
  86538. + ep = get_in_ep(pcd, epnum);
  86539. +
  86540. + /* Disable the NP Tx Fifo Empty Interrrupt */
  86541. + if (!core_if->dma_enable) {
  86542. + intr_mask.b.nptxfempty = 1;
  86543. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  86544. + intr_mask.d32, 0);
  86545. + }
  86546. + /** @todo NGS Check EP type.
  86547. + * Implement for Periodic EPs */
  86548. + /*
  86549. + * Non-periodic EP
  86550. + */
  86551. + /* Enable the Global IN NAK Effective Interrupt */
  86552. + intr_mask.b.ginnakeff = 1;
  86553. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  86554. +
  86555. + /* Set Global IN NAK */
  86556. + dctl.b.sgnpinnak = 1;
  86557. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  86558. +
  86559. + ep->stopped = 1;
  86560. +
  86561. +#ifdef DEBUG
  86562. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  86563. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  86564. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  86565. +#endif
  86566. +
  86567. +#ifdef DISABLE_PERIODIC_EP
  86568. + /*
  86569. + * Set the NAK bit for this EP to
  86570. + * start the disable process.
  86571. + */
  86572. + diepctl.d32 = 0;
  86573. + diepctl.b.snak = 1;
  86574. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  86575. + diepctl.d32);
  86576. + ep->disabling = 1;
  86577. + ep->stopped = 1;
  86578. +#endif
  86579. +}
  86580. +
  86581. +/**
  86582. + * Handler for the IN EP NAK interrupt.
  86583. + */
  86584. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  86585. + const uint32_t epnum)
  86586. +{
  86587. + /** @todo implement ISR */
  86588. + dwc_otg_core_if_t *core_if;
  86589. + diepmsk_data_t intr_mask = {.d32 = 0 };
  86590. +
  86591. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  86592. + core_if = GET_CORE_IF(pcd);
  86593. + intr_mask.b.nak = 1;
  86594. +
  86595. + if (core_if->multiproc_int_enable) {
  86596. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86597. + diepeachintmsk[epnum], intr_mask.d32, 0);
  86598. + } else {
  86599. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  86600. + intr_mask.d32, 0);
  86601. + }
  86602. +
  86603. + return 1;
  86604. +}
  86605. +
  86606. +/**
  86607. + * Handler for the OUT EP Babble interrupt.
  86608. + */
  86609. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  86610. + const uint32_t epnum)
  86611. +{
  86612. + /** @todo implement ISR */
  86613. + dwc_otg_core_if_t *core_if;
  86614. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86615. +
  86616. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  86617. + "OUT EP Babble");
  86618. + core_if = GET_CORE_IF(pcd);
  86619. + intr_mask.b.babble = 1;
  86620. +
  86621. + if (core_if->multiproc_int_enable) {
  86622. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86623. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86624. + } else {
  86625. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86626. + intr_mask.d32, 0);
  86627. + }
  86628. +
  86629. + return 1;
  86630. +}
  86631. +
  86632. +/**
  86633. + * Handler for the OUT EP NAK interrupt.
  86634. + */
  86635. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  86636. + const uint32_t epnum)
  86637. +{
  86638. + /** @todo implement ISR */
  86639. + dwc_otg_core_if_t *core_if;
  86640. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86641. +
  86642. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  86643. + core_if = GET_CORE_IF(pcd);
  86644. + intr_mask.b.nak = 1;
  86645. +
  86646. + if (core_if->multiproc_int_enable) {
  86647. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86648. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86649. + } else {
  86650. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86651. + intr_mask.d32, 0);
  86652. + }
  86653. +
  86654. + return 1;
  86655. +}
  86656. +
  86657. +/**
  86658. + * Handler for the OUT EP NYET interrupt.
  86659. + */
  86660. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  86661. + const uint32_t epnum)
  86662. +{
  86663. + /** @todo implement ISR */
  86664. + dwc_otg_core_if_t *core_if;
  86665. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86666. +
  86667. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  86668. + core_if = GET_CORE_IF(pcd);
  86669. + intr_mask.b.nyet = 1;
  86670. +
  86671. + if (core_if->multiproc_int_enable) {
  86672. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86673. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86674. + } else {
  86675. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86676. + intr_mask.d32, 0);
  86677. + }
  86678. +
  86679. + return 1;
  86680. +}
  86681. +
  86682. +/**
  86683. + * This interrupt indicates that an IN EP has a pending Interrupt.
  86684. + * The sequence for handling the IN EP interrupt is shown below:
  86685. + * -# Read the Device All Endpoint Interrupt register
  86686. + * -# Repeat the following for each IN EP interrupt bit set (from
  86687. + * LSB to MSB).
  86688. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  86689. + * -# If "Transfer Complete" call the request complete function
  86690. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  86691. + * -# If "AHB Error Interrupt" log error
  86692. + * -# If "Time-out Handshake" log error
  86693. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  86694. + * FIFO.
  86695. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  86696. + * Mismatch Interrupt)
  86697. + */
  86698. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  86699. +{
  86700. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  86701. +do { \
  86702. + diepint_data_t diepint = {.d32=0}; \
  86703. + diepint.b.__intr = 1; \
  86704. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  86705. + diepint.d32); \
  86706. +} while (0)
  86707. +
  86708. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86709. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  86710. + diepint_data_t diepint = {.d32 = 0 };
  86711. + depctl_data_t depctl = {.d32 = 0 };
  86712. + uint32_t ep_intr;
  86713. + uint32_t epnum = 0;
  86714. + dwc_otg_pcd_ep_t *ep;
  86715. + dwc_ep_t *dwc_ep;
  86716. + gintmsk_data_t intr_mask = {.d32 = 0 };
  86717. +
  86718. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  86719. +
  86720. + /* Read in the device interrupt bits */
  86721. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  86722. +
  86723. + /* Service the Device IN interrupts for each endpoint */
  86724. + while (ep_intr) {
  86725. + if (ep_intr & 0x1) {
  86726. + uint32_t empty_msk;
  86727. + /* Get EP pointer */
  86728. + ep = get_in_ep(pcd, epnum);
  86729. + dwc_ep = &ep->dwc_ep;
  86730. +
  86731. + depctl.d32 =
  86732. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86733. + empty_msk =
  86734. + DWC_READ_REG32(&dev_if->
  86735. + dev_global_regs->dtknqr4_fifoemptymsk);
  86736. +
  86737. + DWC_DEBUGPL(DBG_PCDV,
  86738. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  86739. + epnum, empty_msk, depctl.d32);
  86740. +
  86741. + DWC_DEBUGPL(DBG_PCD,
  86742. + "EP%d-%s: type=%d, mps=%d\n",
  86743. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  86744. + dwc_ep->type, dwc_ep->maxpacket);
  86745. +
  86746. + diepint.d32 =
  86747. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  86748. +
  86749. + DWC_DEBUGPL(DBG_PCDV,
  86750. + "EP %d Interrupt Register - 0x%x\n", epnum,
  86751. + diepint.d32);
  86752. + /* Transfer complete */
  86753. + if (diepint.b.xfercompl) {
  86754. + /* Disable the NP Tx FIFO Empty
  86755. + * Interrupt */
  86756. + if (core_if->en_multiple_tx_fifo == 0) {
  86757. + intr_mask.b.nptxfempty = 1;
  86758. + DWC_MODIFY_REG32
  86759. + (&core_if->core_global_regs->gintmsk,
  86760. + intr_mask.d32, 0);
  86761. + } else {
  86762. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  86763. + uint32_t fifoemptymsk =
  86764. + 0x1 << dwc_ep->num;
  86765. + DWC_MODIFY_REG32(&core_if->
  86766. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  86767. + fifoemptymsk, 0);
  86768. + }
  86769. + /* Clear the bit in DIEPINTn for this interrupt */
  86770. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  86771. +
  86772. + /* Complete the transfer */
  86773. + if (epnum == 0) {
  86774. + handle_ep0(pcd);
  86775. + }
  86776. +#ifdef DWC_EN_ISOC
  86777. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86778. + if (!ep->stopped)
  86779. + complete_iso_ep(pcd, ep);
  86780. + }
  86781. +#endif /* DWC_EN_ISOC */
  86782. +#ifdef DWC_UTE_PER_IO
  86783. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86784. + if (!ep->stopped)
  86785. + complete_xiso_ep(ep);
  86786. + }
  86787. +#endif /* DWC_UTE_PER_IO */
  86788. + else {
  86789. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  86790. + dwc_ep->bInterval > 1) {
  86791. + dwc_ep->frame_num += dwc_ep->bInterval;
  86792. + if (dwc_ep->frame_num > 0x3FFF)
  86793. + {
  86794. + dwc_ep->frm_overrun = 1;
  86795. + dwc_ep->frame_num &= 0x3FFF;
  86796. + } else
  86797. + dwc_ep->frm_overrun = 0;
  86798. + }
  86799. + complete_ep(ep);
  86800. + if(diepint.b.nak)
  86801. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  86802. + }
  86803. + }
  86804. + /* Endpoint disable */
  86805. + if (diepint.b.epdisabled) {
  86806. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  86807. + epnum);
  86808. + handle_in_ep_disable_intr(pcd, epnum);
  86809. +
  86810. + /* Clear the bit in DIEPINTn for this interrupt */
  86811. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  86812. + }
  86813. + /* AHB Error */
  86814. + if (diepint.b.ahberr) {
  86815. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  86816. + /* Clear the bit in DIEPINTn for this interrupt */
  86817. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  86818. + }
  86819. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  86820. + if (diepint.b.timeout) {
  86821. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  86822. + handle_in_ep_timeout_intr(pcd, epnum);
  86823. +
  86824. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  86825. + }
  86826. + /** IN Token received with TxF Empty */
  86827. + if (diepint.b.intktxfemp) {
  86828. + DWC_DEBUGPL(DBG_ANY,
  86829. + "EP%d IN TKN TxFifo Empty\n",
  86830. + epnum);
  86831. + if (!ep->stopped && epnum != 0) {
  86832. +
  86833. + diepmsk_data_t diepmsk = {.d32 = 0 };
  86834. + diepmsk.b.intktxfemp = 1;
  86835. +
  86836. + if (core_if->multiproc_int_enable) {
  86837. + DWC_MODIFY_REG32
  86838. + (&dev_if->dev_global_regs->diepeachintmsk
  86839. + [epnum], diepmsk.d32, 0);
  86840. + } else {
  86841. + DWC_MODIFY_REG32
  86842. + (&dev_if->dev_global_regs->diepmsk,
  86843. + diepmsk.d32, 0);
  86844. + }
  86845. + } else if (core_if->dma_desc_enable
  86846. + && epnum == 0
  86847. + && pcd->ep0state ==
  86848. + EP0_OUT_STATUS_PHASE) {
  86849. + // EP0 IN set STALL
  86850. + depctl.d32 =
  86851. + DWC_READ_REG32(&dev_if->in_ep_regs
  86852. + [epnum]->diepctl);
  86853. +
  86854. + /* set the disable and stall bits */
  86855. + if (depctl.b.epena) {
  86856. + depctl.b.epdis = 1;
  86857. + }
  86858. + depctl.b.stall = 1;
  86859. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  86860. + [epnum]->diepctl,
  86861. + depctl.d32);
  86862. + }
  86863. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  86864. + }
  86865. + /** IN Token Received with EP mismatch */
  86866. + if (diepint.b.intknepmis) {
  86867. + DWC_DEBUGPL(DBG_ANY,
  86868. + "EP%d IN TKN EP Mismatch\n", epnum);
  86869. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  86870. + }
  86871. + /** IN Endpoint NAK Effective */
  86872. + if (diepint.b.inepnakeff) {
  86873. + DWC_DEBUGPL(DBG_ANY,
  86874. + "EP%d IN EP NAK Effective\n",
  86875. + epnum);
  86876. + /* Periodic EP */
  86877. + if (ep->disabling) {
  86878. + depctl.d32 = 0;
  86879. + depctl.b.snak = 1;
  86880. + depctl.b.epdis = 1;
  86881. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  86882. + [epnum]->diepctl,
  86883. + depctl.d32,
  86884. + depctl.d32);
  86885. + }
  86886. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  86887. +
  86888. + }
  86889. +
  86890. + /** IN EP Tx FIFO Empty Intr */
  86891. + if (diepint.b.emptyintr) {
  86892. + DWC_DEBUGPL(DBG_ANY,
  86893. + "EP%d Tx FIFO Empty Intr \n",
  86894. + epnum);
  86895. + write_empty_tx_fifo(pcd, epnum);
  86896. +
  86897. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  86898. +
  86899. + }
  86900. +
  86901. + /** IN EP BNA Intr */
  86902. + if (diepint.b.bna) {
  86903. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  86904. + if (core_if->dma_desc_enable) {
  86905. +#ifdef DWC_EN_ISOC
  86906. + if (dwc_ep->type ==
  86907. + DWC_OTG_EP_TYPE_ISOC) {
  86908. + /*
  86909. + * This checking is performed to prevent first "false" BNA
  86910. + * handling occuring right after reconnect
  86911. + */
  86912. + if (dwc_ep->next_frame !=
  86913. + 0xffffffff)
  86914. + dwc_otg_pcd_handle_iso_bna(ep);
  86915. + } else
  86916. +#endif /* DWC_EN_ISOC */
  86917. + {
  86918. + dwc_otg_pcd_handle_noniso_bna(ep);
  86919. + }
  86920. + }
  86921. + }
  86922. + /* NAK Interrutp */
  86923. + if (diepint.b.nak) {
  86924. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  86925. + epnum);
  86926. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  86927. + depctl_data_t depctl;
  86928. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  86929. + ep->dwc_ep.frame_num = core_if->frame_num;
  86930. + if (ep->dwc_ep.bInterval > 1) {
  86931. + depctl.d32 = 0;
  86932. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86933. + if (ep->dwc_ep.frame_num & 0x1) {
  86934. + depctl.b.setd1pid = 1;
  86935. + depctl.b.setd0pid = 0;
  86936. + } else {
  86937. + depctl.b.setd0pid = 1;
  86938. + depctl.b.setd1pid = 0;
  86939. + }
  86940. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  86941. + }
  86942. + start_next_request(ep);
  86943. + }
  86944. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  86945. + if (dwc_ep->frame_num > 0x3FFF) {
  86946. + dwc_ep->frm_overrun = 1;
  86947. + dwc_ep->frame_num &= 0x3FFF;
  86948. + } else
  86949. + dwc_ep->frm_overrun = 0;
  86950. + }
  86951. +
  86952. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  86953. + }
  86954. + }
  86955. + epnum++;
  86956. + ep_intr >>= 1;
  86957. + }
  86958. +
  86959. + return 1;
  86960. +#undef CLEAR_IN_EP_INTR
  86961. +}
  86962. +
  86963. +/**
  86964. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  86965. + * The sequence for handling the OUT EP interrupt is shown below:
  86966. + * -# Read the Device All Endpoint Interrupt register
  86967. + * -# Repeat the following for each OUT EP interrupt bit set (from
  86968. + * LSB to MSB).
  86969. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  86970. + * -# If "Transfer Complete" call the request complete function
  86971. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  86972. + * -# If "AHB Error Interrupt" log error
  86973. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  86974. + * Command Processing)
  86975. + */
  86976. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  86977. +{
  86978. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  86979. +do { \
  86980. + doepint_data_t doepint = {.d32=0}; \
  86981. + doepint.b.__intr = 1; \
  86982. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  86983. + doepint.d32); \
  86984. +} while (0)
  86985. +
  86986. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86987. + uint32_t ep_intr;
  86988. + doepint_data_t doepint = {.d32 = 0 };
  86989. + uint32_t epnum = 0;
  86990. + dwc_otg_pcd_ep_t *ep;
  86991. + dwc_ep_t *dwc_ep;
  86992. + dctl_data_t dctl = {.d32 = 0 };
  86993. + gintmsk_data_t gintmsk = {.d32 = 0 };
  86994. +
  86995. +
  86996. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  86997. +
  86998. + /* Read in the device interrupt bits */
  86999. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  87000. +
  87001. + while (ep_intr) {
  87002. + if (ep_intr & 0x1) {
  87003. + /* Get EP pointer */
  87004. + ep = get_out_ep(pcd, epnum);
  87005. + dwc_ep = &ep->dwc_ep;
  87006. +
  87007. +#ifdef VERBOSE
  87008. + DWC_DEBUGPL(DBG_PCDV,
  87009. + "EP%d-%s: type=%d, mps=%d\n",
  87010. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  87011. + dwc_ep->type, dwc_ep->maxpacket);
  87012. +#endif
  87013. + doepint.d32 =
  87014. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  87015. + /* Moved this interrupt upper due to core deffect of asserting
  87016. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  87017. + if (doepint.b.stsphsercvd) {
  87018. + deptsiz0_data_t deptsiz;
  87019. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  87020. + deptsiz.d32 =
  87021. + DWC_READ_REG32(&core_if->dev_if->
  87022. + out_ep_regs[0]->doeptsiz);
  87023. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  87024. + && core_if->dma_enable
  87025. + && core_if->dma_desc_enable == 0
  87026. + && doepint.b.xfercompl
  87027. + && deptsiz.b.xfersize == 24) {
  87028. + CLEAR_OUT_EP_INTR(core_if, epnum,
  87029. + xfercompl);
  87030. + doepint.b.xfercompl = 0;
  87031. + ep0_out_start(core_if, pcd);
  87032. + }
  87033. + if ((core_if->dma_desc_enable) ||
  87034. + (core_if->dma_enable
  87035. + && core_if->snpsid >=
  87036. + OTG_CORE_REV_3_00a)) {
  87037. + do_setup_in_status_phase(pcd);
  87038. + }
  87039. + }
  87040. + /* Transfer complete */
  87041. + if (doepint.b.xfercompl) {
  87042. +
  87043. + if (epnum == 0) {
  87044. + /* Clear the bit in DOEPINTn for this interrupt */
  87045. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  87046. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  87047. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  87048. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  87049. + doepint.d32);
  87050. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  87051. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  87052. +
  87053. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  87054. + && core_if->dma_enable == 0) {
  87055. + doepint_data_t doepint;
  87056. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87057. + out_ep_regs[0]->doepint);
  87058. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  87059. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  87060. + goto exit_xfercompl;
  87061. + }
  87062. + }
  87063. + /* In case of DDMA look at SR bit to go to the Data Stage */
  87064. + if (core_if->dma_desc_enable) {
  87065. + dev_dma_desc_sts_t status = {.d32 = 0};
  87066. + if (pcd->ep0state == EP0_IDLE) {
  87067. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  87068. + dev_if->setup_desc_index]->status.d32;
  87069. + if(pcd->data_terminated) {
  87070. + pcd->data_terminated = 0;
  87071. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  87072. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  87073. + }
  87074. + if (status.b.sr) {
  87075. + if (doepint.b.setup) {
  87076. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  87077. + /* Already started data stage, clear setup */
  87078. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87079. + doepint.b.setup = 0;
  87080. + handle_ep0(pcd);
  87081. + /* Prepare for more setup packets */
  87082. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  87083. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  87084. + ep0_out_start(core_if, pcd);
  87085. + }
  87086. +
  87087. + goto exit_xfercompl;
  87088. + } else {
  87089. + /* Prepare for more setup packets */
  87090. + DWC_DEBUGPL(DBG_PCDV,
  87091. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  87092. + ep0_out_start(core_if, pcd);
  87093. + }
  87094. + }
  87095. + } else {
  87096. + dwc_otg_pcd_request_t *req;
  87097. + dev_dma_desc_sts_t status = {.d32 = 0};
  87098. + diepint_data_t diepint0;
  87099. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87100. + in_ep_regs[0]->diepint);
  87101. +
  87102. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  87103. + DWC_ERROR("EP0 is stalled/disconnected\n");
  87104. + }
  87105. +
  87106. + /* Clear IN xfercompl if set */
  87107. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  87108. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  87109. + DWC_WRITE_REG32(&core_if->dev_if->
  87110. + in_ep_regs[0]->diepint, diepint0.d32);
  87111. + }
  87112. +
  87113. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  87114. + dev_if->setup_desc_index]->status.d32;
  87115. +
  87116. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  87117. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  87118. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  87119. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  87120. + status.d32 = core_if->dev_if->
  87121. + out_desc_addr->status.d32;
  87122. +
  87123. + if (status.b.sr) {
  87124. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  87125. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  87126. + } else {
  87127. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  87128. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  87129. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  87130. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  87131. + /* Read arrived setup packet from req->buf */
  87132. + dwc_memcpy(&pcd->setup_pkt->req,
  87133. + req->buf + ep->dwc_ep.xfer_count, 8);
  87134. + }
  87135. + req->actual = ep->dwc_ep.xfer_count;
  87136. + dwc_otg_request_done(ep, req, -ECONNRESET);
  87137. + ep->dwc_ep.start_xfer_buff = 0;
  87138. + ep->dwc_ep.xfer_buff = 0;
  87139. + ep->dwc_ep.xfer_len = 0;
  87140. + }
  87141. + pcd->ep0state = EP0_IDLE;
  87142. + if (doepint.b.setup) {
  87143. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  87144. + /* Data stage started, clear setup */
  87145. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87146. + doepint.b.setup = 0;
  87147. + handle_ep0(pcd);
  87148. + /* Prepare for setup packets if ep0in was enabled*/
  87149. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  87150. + ep0_out_start(core_if, pcd);
  87151. + }
  87152. +
  87153. + goto exit_xfercompl;
  87154. + } else {
  87155. + /* Prepare for more setup packets */
  87156. + DWC_DEBUGPL(DBG_PCDV,
  87157. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  87158. + ep0_out_start(core_if, pcd);
  87159. + }
  87160. + }
  87161. + }
  87162. + }
  87163. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  87164. + && core_if->dma_desc_enable == 0) {
  87165. + doepint_data_t doepint_temp = {.d32 = 0};
  87166. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  87167. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  87168. + out_ep_regs[ep->dwc_ep.num]->doepint);
  87169. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87170. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  87171. + if (pcd->ep0state == EP0_IDLE) {
  87172. + if (doepint_temp.b.sr) {
  87173. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  87174. + }
  87175. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87176. + out_ep_regs[0]->doepint);
  87177. + if (doeptsize0.b.supcnt == 3) {
  87178. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  87179. + ep->dwc_ep.stp_rollover = 1;
  87180. + }
  87181. + if (doepint.b.setup) {
  87182. +retry:
  87183. + /* Already started data stage, clear setup */
  87184. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87185. + doepint.b.setup = 0;
  87186. + handle_ep0(pcd);
  87187. + ep->dwc_ep.stp_rollover = 0;
  87188. + /* Prepare for more setup packets */
  87189. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  87190. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  87191. + ep0_out_start(core_if, pcd);
  87192. + }
  87193. + goto exit_xfercompl;
  87194. + } else {
  87195. + /* Prepare for more setup packets */
  87196. + DWC_DEBUGPL(DBG_ANY,
  87197. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  87198. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87199. + out_ep_regs[0]->doepint);
  87200. + if(doepint.b.setup)
  87201. + goto retry;
  87202. + ep0_out_start(core_if, pcd);
  87203. + }
  87204. + } else {
  87205. + dwc_otg_pcd_request_t *req;
  87206. + diepint_data_t diepint0 = {.d32 = 0};
  87207. + doepint_data_t doepint_temp = {.d32 = 0};
  87208. + depctl_data_t diepctl0;
  87209. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87210. + in_ep_regs[0]->diepint);
  87211. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87212. + in_ep_regs[0]->diepctl);
  87213. +
  87214. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  87215. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  87216. + if (diepint0.b.xfercompl) {
  87217. + DWC_WRITE_REG32(&core_if->dev_if->
  87218. + in_ep_regs[0]->diepint, diepint0.d32);
  87219. + }
  87220. + if (diepctl0.b.epena) {
  87221. + diepint_data_t diepint = {.d32 = 0};
  87222. + diepctl0.b.snak = 1;
  87223. + DWC_WRITE_REG32(&core_if->dev_if->
  87224. + in_ep_regs[0]->diepctl, diepctl0.d32);
  87225. + do {
  87226. + dwc_udelay(10);
  87227. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87228. + in_ep_regs[0]->diepint);
  87229. + } while (!diepint.b.inepnakeff);
  87230. + diepint.b.inepnakeff = 1;
  87231. + DWC_WRITE_REG32(&core_if->dev_if->
  87232. + in_ep_regs[0]->diepint, diepint.d32);
  87233. + diepctl0.d32 = 0;
  87234. + diepctl0.b.epdis = 1;
  87235. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  87236. + diepctl0.d32);
  87237. + do {
  87238. + dwc_udelay(10);
  87239. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87240. + in_ep_regs[0]->diepint);
  87241. + } while (!diepint.b.epdisabled);
  87242. + diepint.b.epdisabled = 1;
  87243. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  87244. + diepint.d32);
  87245. + }
  87246. + }
  87247. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  87248. + out_ep_regs[ep->dwc_ep.num]->doepint);
  87249. + if (doepint_temp.b.sr) {
  87250. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  87251. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  87252. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  87253. + } else {
  87254. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  87255. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  87256. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  87257. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  87258. + /* Read arrived setup packet from req->buf */
  87259. + dwc_memcpy(&pcd->setup_pkt->req,
  87260. + req->buf + ep->dwc_ep.xfer_count, 8);
  87261. + }
  87262. + req->actual = ep->dwc_ep.xfer_count;
  87263. + dwc_otg_request_done(ep, req, -ECONNRESET);
  87264. + ep->dwc_ep.start_xfer_buff = 0;
  87265. + ep->dwc_ep.xfer_buff = 0;
  87266. + ep->dwc_ep.xfer_len = 0;
  87267. + }
  87268. + pcd->ep0state = EP0_IDLE;
  87269. + if (doepint.b.setup) {
  87270. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  87271. + /* Data stage started, clear setup */
  87272. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87273. + doepint.b.setup = 0;
  87274. + handle_ep0(pcd);
  87275. + /* Prepare for setup packets if ep0in was enabled*/
  87276. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  87277. + ep0_out_start(core_if, pcd);
  87278. + }
  87279. + goto exit_xfercompl;
  87280. + } else {
  87281. + /* Prepare for more setup packets */
  87282. + DWC_DEBUGPL(DBG_PCDV,
  87283. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  87284. + ep0_out_start(core_if, pcd);
  87285. + }
  87286. + }
  87287. + }
  87288. + }
  87289. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  87290. + handle_ep0(pcd);
  87291. +exit_xfercompl:
  87292. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  87293. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  87294. + } else {
  87295. + if (core_if->dma_desc_enable == 0
  87296. + || pcd->ep0state != EP0_IDLE)
  87297. + handle_ep0(pcd);
  87298. + }
  87299. +#ifdef DWC_EN_ISOC
  87300. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87301. + if (doepint.b.pktdrpsts == 0) {
  87302. + /* Clear the bit in DOEPINTn for this interrupt */
  87303. + CLEAR_OUT_EP_INTR(core_if,
  87304. + epnum,
  87305. + xfercompl);
  87306. + complete_iso_ep(pcd, ep);
  87307. + } else {
  87308. +
  87309. + doepint_data_t doepint = {.d32 = 0 };
  87310. + doepint.b.xfercompl = 1;
  87311. + doepint.b.pktdrpsts = 1;
  87312. + DWC_WRITE_REG32
  87313. + (&core_if->dev_if->out_ep_regs
  87314. + [epnum]->doepint,
  87315. + doepint.d32);
  87316. + if (handle_iso_out_pkt_dropped
  87317. + (core_if, dwc_ep)) {
  87318. + complete_iso_ep(pcd,
  87319. + ep);
  87320. + }
  87321. + }
  87322. +#endif /* DWC_EN_ISOC */
  87323. +#ifdef DWC_UTE_PER_IO
  87324. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87325. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  87326. + if (!ep->stopped)
  87327. + complete_xiso_ep(ep);
  87328. +#endif /* DWC_UTE_PER_IO */
  87329. + } else {
  87330. + /* Clear the bit in DOEPINTn for this interrupt */
  87331. + CLEAR_OUT_EP_INTR(core_if, epnum,
  87332. + xfercompl);
  87333. +
  87334. + if (core_if->core_params->dev_out_nak) {
  87335. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  87336. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  87337. +#ifdef DEBUG
  87338. + print_memory_payload(pcd, dwc_ep);
  87339. +#endif
  87340. + }
  87341. + complete_ep(ep);
  87342. + }
  87343. +
  87344. + }
  87345. +
  87346. + /* Endpoint disable */
  87347. + if (doepint.b.epdisabled) {
  87348. +
  87349. + /* Clear the bit in DOEPINTn for this interrupt */
  87350. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  87351. + if (core_if->core_params->dev_out_nak) {
  87352. +#ifdef DEBUG
  87353. + print_memory_payload(pcd, dwc_ep);
  87354. +#endif
  87355. + /* In case of timeout condition */
  87356. + if (core_if->ep_xfer_info[epnum].state == 2) {
  87357. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  87358. + dev_global_regs->dctl);
  87359. + dctl.b.cgoutnak = 1;
  87360. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  87361. + dctl.d32);
  87362. + /* Unmask goutnakeff interrupt which was masked
  87363. + * during handle nak out interrupt */
  87364. + gintmsk.b.goutnakeff = 1;
  87365. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  87366. + 0, gintmsk.d32);
  87367. +
  87368. + complete_ep(ep);
  87369. + }
  87370. + }
  87371. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  87372. + {
  87373. + dctl_data_t dctl;
  87374. + gintmsk_data_t intr_mask = {.d32 = 0};
  87375. + dwc_otg_pcd_request_t *req = 0;
  87376. +
  87377. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  87378. + dev_global_regs->dctl);
  87379. + dctl.b.cgoutnak = 1;
  87380. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  87381. + dctl.d32);
  87382. +
  87383. + intr_mask.d32 = 0;
  87384. + intr_mask.b.incomplisoout = 1;
  87385. +
  87386. + /* Get any pending requests */
  87387. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  87388. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  87389. + if (!req) {
  87390. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  87391. + } else {
  87392. + dwc_otg_request_done(ep, req, 0);
  87393. + start_next_request(ep);
  87394. + }
  87395. + } else {
  87396. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  87397. + }
  87398. + }
  87399. + }
  87400. + /* AHB Error */
  87401. + if (doepint.b.ahberr) {
  87402. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  87403. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  87404. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  87405. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  87406. + }
  87407. + /* Setup Phase Done (contorl EPs) */
  87408. + if (doepint.b.setup) {
  87409. +#ifdef DEBUG_EP0
  87410. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  87411. +#endif
  87412. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87413. +
  87414. + handle_ep0(pcd);
  87415. + }
  87416. +
  87417. + /** OUT EP BNA Intr */
  87418. + if (doepint.b.bna) {
  87419. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  87420. + if (core_if->dma_desc_enable) {
  87421. +#ifdef DWC_EN_ISOC
  87422. + if (dwc_ep->type ==
  87423. + DWC_OTG_EP_TYPE_ISOC) {
  87424. + /*
  87425. + * This checking is performed to prevent first "false" BNA
  87426. + * handling occuring right after reconnect
  87427. + */
  87428. + if (dwc_ep->next_frame !=
  87429. + 0xffffffff)
  87430. + dwc_otg_pcd_handle_iso_bna(ep);
  87431. + } else
  87432. +#endif /* DWC_EN_ISOC */
  87433. + {
  87434. + dwc_otg_pcd_handle_noniso_bna(ep);
  87435. + }
  87436. + }
  87437. + }
  87438. + /* Babble Interrupt */
  87439. + if (doepint.b.babble) {
  87440. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  87441. + epnum);
  87442. + handle_out_ep_babble_intr(pcd, epnum);
  87443. +
  87444. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  87445. + }
  87446. + if (doepint.b.outtknepdis) {
  87447. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  87448. + disabled\n",epnum);
  87449. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  87450. + doepmsk_data_t doepmsk = {.d32 = 0};
  87451. + ep->dwc_ep.frame_num = core_if->frame_num;
  87452. + if (ep->dwc_ep.bInterval > 1) {
  87453. + depctl_data_t depctl;
  87454. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  87455. + out_ep_regs[epnum]->doepctl);
  87456. + if (ep->dwc_ep.frame_num & 0x1) {
  87457. + depctl.b.setd1pid = 1;
  87458. + depctl.b.setd0pid = 0;
  87459. + } else {
  87460. + depctl.b.setd0pid = 1;
  87461. + depctl.b.setd1pid = 0;
  87462. + }
  87463. + DWC_WRITE_REG32(&core_if->dev_if->
  87464. + out_ep_regs[epnum]->doepctl, depctl.d32);
  87465. + }
  87466. + start_next_request(ep);
  87467. + doepmsk.b.outtknepdis = 1;
  87468. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  87469. + doepmsk.d32, 0);
  87470. + }
  87471. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  87472. + }
  87473. +
  87474. + /* NAK Interrutp */
  87475. + if (doepint.b.nak) {
  87476. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  87477. + handle_out_ep_nak_intr(pcd, epnum);
  87478. +
  87479. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  87480. + }
  87481. + /* NYET Interrutp */
  87482. + if (doepint.b.nyet) {
  87483. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  87484. + handle_out_ep_nyet_intr(pcd, epnum);
  87485. +
  87486. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  87487. + }
  87488. + }
  87489. +
  87490. + epnum++;
  87491. + ep_intr >>= 1;
  87492. + }
  87493. +
  87494. + return 1;
  87495. +
  87496. +#undef CLEAR_OUT_EP_INTR
  87497. +}
  87498. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  87499. +{
  87500. + int retval = 0;
  87501. + if(!frm_overrun && curr_fr >= trgt_fr)
  87502. + retval = 1;
  87503. + else if (frm_overrun
  87504. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  87505. + retval = 1;
  87506. + return retval;
  87507. +}
  87508. +/**
  87509. + * Incomplete ISO IN Transfer Interrupt.
  87510. + * This interrupt indicates one of the following conditions occurred
  87511. + * while transmitting an ISOC transaction.
  87512. + * - Corrupted IN Token for ISOC EP.
  87513. + * - Packet not complete in FIFO.
  87514. + * The follow actions will be taken:
  87515. + * -# Determine the EP
  87516. + * -# Set incomplete flag in dwc_ep structure
  87517. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  87518. + * Flush FIFO
  87519. + */
  87520. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  87521. +{
  87522. + gintsts_data_t gintsts;
  87523. +
  87524. +#ifdef DWC_EN_ISOC
  87525. + dwc_otg_dev_if_t *dev_if;
  87526. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87527. + depctl_data_t depctl = {.d32 = 0 };
  87528. + dsts_data_t dsts = {.d32 = 0 };
  87529. + dwc_ep_t *dwc_ep;
  87530. + int i;
  87531. +
  87532. + dev_if = GET_CORE_IF(pcd)->dev_if;
  87533. +
  87534. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  87535. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  87536. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87537. + deptsiz.d32 =
  87538. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  87539. + depctl.d32 =
  87540. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87541. +
  87542. + if (depctl.b.epdis && deptsiz.d32) {
  87543. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  87544. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  87545. + dwc_ep->cur_pkt = 0;
  87546. + dwc_ep->proc_buf_num =
  87547. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  87548. +
  87549. + if (dwc_ep->proc_buf_num) {
  87550. + dwc_ep->cur_pkt_addr =
  87551. + dwc_ep->xfer_buff1;
  87552. + dwc_ep->cur_pkt_dma_addr =
  87553. + dwc_ep->dma_addr1;
  87554. + } else {
  87555. + dwc_ep->cur_pkt_addr =
  87556. + dwc_ep->xfer_buff0;
  87557. + dwc_ep->cur_pkt_dma_addr =
  87558. + dwc_ep->dma_addr0;
  87559. + }
  87560. +
  87561. + }
  87562. +
  87563. + dsts.d32 =
  87564. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  87565. + dev_global_regs->dsts);
  87566. + dwc_ep->next_frame = dsts.b.soffn;
  87567. +
  87568. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  87569. + (pcd),
  87570. + dwc_ep);
  87571. + }
  87572. + }
  87573. + }
  87574. +
  87575. +#else
  87576. + depctl_data_t depctl = {.d32 = 0 };
  87577. + dwc_ep_t *dwc_ep;
  87578. + dwc_otg_dev_if_t *dev_if;
  87579. + int i;
  87580. + dev_if = GET_CORE_IF(pcd)->dev_if;
  87581. +
  87582. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  87583. +
  87584. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  87585. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  87586. + depctl.d32 =
  87587. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87588. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87589. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  87590. + dwc_ep->frm_overrun))
  87591. + {
  87592. + depctl.d32 =
  87593. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87594. + depctl.b.snak = 1;
  87595. + depctl.b.epdis = 1;
  87596. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  87597. + }
  87598. + }
  87599. + }
  87600. +
  87601. + /*intr_mask.b.incomplisoin = 1;
  87602. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87603. + intr_mask.d32, 0); */
  87604. +#endif //DWC_EN_ISOC
  87605. +
  87606. + /* Clear interrupt */
  87607. + gintsts.d32 = 0;
  87608. + gintsts.b.incomplisoin = 1;
  87609. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87610. + gintsts.d32);
  87611. +
  87612. + return 1;
  87613. +}
  87614. +
  87615. +/**
  87616. + * Incomplete ISO OUT Transfer Interrupt.
  87617. + *
  87618. + * This interrupt indicates that the core has dropped an ISO OUT
  87619. + * packet. The following conditions can be the cause:
  87620. + * - FIFO Full, the entire packet would not fit in the FIFO.
  87621. + * - CRC Error
  87622. + * - Corrupted Token
  87623. + * The follow actions will be taken:
  87624. + * -# Determine the EP
  87625. + * -# Set incomplete flag in dwc_ep structure
  87626. + * -# Read any data from the FIFO
  87627. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  87628. + * re-enable EP.
  87629. + */
  87630. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  87631. +{
  87632. +
  87633. + gintsts_data_t gintsts;
  87634. +
  87635. +#ifdef DWC_EN_ISOC
  87636. + dwc_otg_dev_if_t *dev_if;
  87637. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87638. + depctl_data_t depctl = {.d32 = 0 };
  87639. + dsts_data_t dsts = {.d32 = 0 };
  87640. + dwc_ep_t *dwc_ep;
  87641. + int i;
  87642. +
  87643. + dev_if = GET_CORE_IF(pcd)->dev_if;
  87644. +
  87645. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  87646. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  87647. + if (pcd->out_ep[i].dwc_ep.active &&
  87648. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  87649. + deptsiz.d32 =
  87650. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  87651. + depctl.d32 =
  87652. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  87653. +
  87654. + if (depctl.b.epdis && deptsiz.d32) {
  87655. + set_current_pkt_info(GET_CORE_IF(pcd),
  87656. + &pcd->out_ep[i].dwc_ep);
  87657. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  87658. + dwc_ep->cur_pkt = 0;
  87659. + dwc_ep->proc_buf_num =
  87660. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  87661. +
  87662. + if (dwc_ep->proc_buf_num) {
  87663. + dwc_ep->cur_pkt_addr =
  87664. + dwc_ep->xfer_buff1;
  87665. + dwc_ep->cur_pkt_dma_addr =
  87666. + dwc_ep->dma_addr1;
  87667. + } else {
  87668. + dwc_ep->cur_pkt_addr =
  87669. + dwc_ep->xfer_buff0;
  87670. + dwc_ep->cur_pkt_dma_addr =
  87671. + dwc_ep->dma_addr0;
  87672. + }
  87673. +
  87674. + }
  87675. +
  87676. + dsts.d32 =
  87677. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  87678. + dev_global_regs->dsts);
  87679. + dwc_ep->next_frame = dsts.b.soffn;
  87680. +
  87681. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  87682. + (pcd),
  87683. + dwc_ep);
  87684. + }
  87685. + }
  87686. + }
  87687. +#else
  87688. + /** @todo implement ISR */
  87689. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87690. + dwc_otg_core_if_t *core_if;
  87691. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87692. + depctl_data_t depctl = {.d32 = 0 };
  87693. + dctl_data_t dctl = {.d32 = 0 };
  87694. + dwc_ep_t *dwc_ep = NULL;
  87695. + int i;
  87696. + core_if = GET_CORE_IF(pcd);
  87697. +
  87698. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  87699. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  87700. + depctl.d32 =
  87701. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  87702. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  87703. + core_if->dev_if->isoc_ep = dwc_ep;
  87704. + deptsiz.d32 =
  87705. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  87706. + break;
  87707. + }
  87708. + }
  87709. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  87710. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  87711. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  87712. +
  87713. + if (!intr_mask.b.goutnakeff) {
  87714. + /* Unmask it */
  87715. + intr_mask.b.goutnakeff = 1;
  87716. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  87717. + }
  87718. + if (!gintsts.b.goutnakeff) {
  87719. + dctl.b.sgoutnak = 1;
  87720. + }
  87721. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  87722. +
  87723. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  87724. + if (depctl.b.epena) {
  87725. + depctl.b.epdis = 1;
  87726. + depctl.b.snak = 1;
  87727. + }
  87728. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  87729. +
  87730. + intr_mask.d32 = 0;
  87731. + intr_mask.b.incomplisoout = 1;
  87732. +
  87733. +#endif /* DWC_EN_ISOC */
  87734. +
  87735. + /* Clear interrupt */
  87736. + gintsts.d32 = 0;
  87737. + gintsts.b.incomplisoout = 1;
  87738. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87739. + gintsts.d32);
  87740. +
  87741. + return 1;
  87742. +}
  87743. +
  87744. +/**
  87745. + * This function handles the Global IN NAK Effective interrupt.
  87746. + *
  87747. + */
  87748. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  87749. +{
  87750. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  87751. + depctl_data_t diepctl = {.d32 = 0 };
  87752. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87753. + gintsts_data_t gintsts;
  87754. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87755. + int i;
  87756. +
  87757. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  87758. +
  87759. + /* Disable all active IN EPs */
  87760. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  87761. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87762. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  87763. + if (core_if->start_predict > 0)
  87764. + core_if->start_predict++;
  87765. + diepctl.b.epdis = 1;
  87766. + diepctl.b.snak = 1;
  87767. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  87768. + }
  87769. + }
  87770. +
  87771. +
  87772. + /* Disable the Global IN NAK Effective Interrupt */
  87773. + intr_mask.b.ginnakeff = 1;
  87774. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87775. + intr_mask.d32, 0);
  87776. +
  87777. + /* Clear interrupt */
  87778. + gintsts.d32 = 0;
  87779. + gintsts.b.ginnakeff = 1;
  87780. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87781. + gintsts.d32);
  87782. +
  87783. + return 1;
  87784. +}
  87785. +
  87786. +/**
  87787. + * OUT NAK Effective.
  87788. + *
  87789. + */
  87790. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  87791. +{
  87792. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  87793. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87794. + gintsts_data_t gintsts;
  87795. + depctl_data_t doepctl;
  87796. + int i;
  87797. +
  87798. + /* Disable the Global OUT NAK Effective Interrupt */
  87799. + intr_mask.b.goutnakeff = 1;
  87800. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87801. + intr_mask.d32, 0);
  87802. +
  87803. + /* If DEV OUT NAK enabled*/
  87804. + if (pcd->core_if->core_params->dev_out_nak) {
  87805. + /* Run over all out endpoints to determine the ep number on
  87806. + * which the timeout has happened
  87807. + */
  87808. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  87809. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  87810. + break;
  87811. + }
  87812. + if (i > dev_if->num_out_eps) {
  87813. + dctl_data_t dctl;
  87814. + dctl.d32 =
  87815. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  87816. + dctl.b.cgoutnak = 1;
  87817. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  87818. + dctl.d32);
  87819. + goto out;
  87820. + }
  87821. +
  87822. + /* Disable the endpoint */
  87823. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  87824. + if (doepctl.b.epena) {
  87825. + doepctl.b.epdis = 1;
  87826. + doepctl.b.snak = 1;
  87827. + }
  87828. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  87829. + return 1;
  87830. + }
  87831. + /* We come here from Incomplete ISO OUT handler */
  87832. + if (dev_if->isoc_ep) {
  87833. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  87834. + uint32_t epnum = dwc_ep->num;
  87835. + doepint_data_t doepint;
  87836. + doepint.d32 =
  87837. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  87838. + dev_if->isoc_ep = NULL;
  87839. + doepctl.d32 =
  87840. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  87841. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  87842. + if (doepctl.b.epena) {
  87843. + doepctl.b.epdis = 1;
  87844. + doepctl.b.snak = 1;
  87845. + }
  87846. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  87847. + doepctl.d32);
  87848. + return 1;
  87849. + } else
  87850. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  87851. + "Global OUT NAK Effective\n");
  87852. +
  87853. +out:
  87854. + /* Clear interrupt */
  87855. + gintsts.d32 = 0;
  87856. + gintsts.b.goutnakeff = 1;
  87857. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87858. + gintsts.d32);
  87859. +
  87860. + return 1;
  87861. +}
  87862. +
  87863. +/**
  87864. + * PCD interrupt handler.
  87865. + *
  87866. + * The PCD handles the device interrupts. Many conditions can cause a
  87867. + * device interrupt. When an interrupt occurs, the device interrupt
  87868. + * service routine determines the cause of the interrupt and
  87869. + * dispatches handling to the appropriate function. These interrupt
  87870. + * handling functions are described below.
  87871. + *
  87872. + * All interrupt registers are processed from LSB to MSB.
  87873. + *
  87874. + */
  87875. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  87876. +{
  87877. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87878. +#ifdef VERBOSE
  87879. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  87880. +#endif
  87881. + gintsts_data_t gintr_status;
  87882. + int32_t retval = 0;
  87883. +
  87884. + /* Exit from ISR if core is hibernated */
  87885. + if (core_if->hibernation_suspend == 1) {
  87886. + return retval;
  87887. + }
  87888. +#ifdef VERBOSE
  87889. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  87890. + __func__,
  87891. + DWC_READ_REG32(&global_regs->gintsts),
  87892. + DWC_READ_REG32(&global_regs->gintmsk));
  87893. +#endif
  87894. +
  87895. + if (dwc_otg_is_device_mode(core_if)) {
  87896. + DWC_SPINLOCK(pcd->lock);
  87897. +#ifdef VERBOSE
  87898. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  87899. + __func__,
  87900. + DWC_READ_REG32(&global_regs->gintsts),
  87901. + DWC_READ_REG32(&global_regs->gintmsk));
  87902. +#endif
  87903. +
  87904. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  87905. +
  87906. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  87907. + __func__, gintr_status.d32);
  87908. +
  87909. + if (gintr_status.b.sofintr) {
  87910. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  87911. + }
  87912. + if (gintr_status.b.rxstsqlvl) {
  87913. + retval |=
  87914. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  87915. + }
  87916. + if (gintr_status.b.nptxfempty) {
  87917. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  87918. + }
  87919. + if (gintr_status.b.goutnakeff) {
  87920. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  87921. + }
  87922. + if (gintr_status.b.i2cintr) {
  87923. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  87924. + }
  87925. + if (gintr_status.b.erlysuspend) {
  87926. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  87927. + }
  87928. + if (gintr_status.b.usbreset) {
  87929. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  87930. + }
  87931. + if (gintr_status.b.enumdone) {
  87932. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  87933. + }
  87934. + if (gintr_status.b.isooutdrop) {
  87935. + retval |=
  87936. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  87937. + (pcd);
  87938. + }
  87939. + if (gintr_status.b.eopframe) {
  87940. + retval |=
  87941. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  87942. + }
  87943. + if (gintr_status.b.inepint) {
  87944. + if (!core_if->multiproc_int_enable) {
  87945. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  87946. + }
  87947. + }
  87948. + if (gintr_status.b.outepintr) {
  87949. + if (!core_if->multiproc_int_enable) {
  87950. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  87951. + }
  87952. + }
  87953. + if (gintr_status.b.epmismatch) {
  87954. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  87955. + }
  87956. + if (gintr_status.b.fetsusp) {
  87957. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  87958. + }
  87959. + if (gintr_status.b.ginnakeff) {
  87960. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  87961. + }
  87962. + if (gintr_status.b.incomplisoin) {
  87963. + retval |=
  87964. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  87965. + }
  87966. + if (gintr_status.b.incomplisoout) {
  87967. + retval |=
  87968. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  87969. + }
  87970. +
  87971. + /* In MPI mode Device Endpoints interrupts are asserted
  87972. + * without setting outepintr and inepint bits set, so these
  87973. + * Interrupt handlers are called without checking these bit-fields
  87974. + */
  87975. + if (core_if->multiproc_int_enable) {
  87976. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  87977. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  87978. + }
  87979. +#ifdef VERBOSE
  87980. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  87981. + DWC_READ_REG32(&global_regs->gintsts));
  87982. +#endif
  87983. + DWC_SPINUNLOCK(pcd->lock);
  87984. + }
  87985. + return retval;
  87986. +}
  87987. +
  87988. +#endif /* DWC_HOST_ONLY */
  87989. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  87990. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  87991. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 2014-03-11 17:51:27.000000000 +0100
  87992. @@ -0,0 +1,1358 @@
  87993. + /* ==========================================================================
  87994. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  87995. + * $Revision: #21 $
  87996. + * $Date: 2012/08/10 $
  87997. + * $Change: 2047372 $
  87998. + *
  87999. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  88000. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  88001. + * otherwise expressly agreed to in writing between Synopsys and you.
  88002. + *
  88003. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  88004. + * any End User Software License Agreement or Agreement for Licensed Product
  88005. + * with Synopsys or any supplement thereto. You are permitted to use and
  88006. + * redistribute this Software in source and binary forms, with or without
  88007. + * modification, provided that redistributions of source code must retain this
  88008. + * notice. You may not view, use, disclose, copy or distribute this file or
  88009. + * any information contained herein except pursuant to this license grant from
  88010. + * Synopsys. If you do not agree with this notice, including the disclaimer
  88011. + * below, then you are not authorized to use the Software.
  88012. + *
  88013. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  88014. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  88015. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  88016. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  88017. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  88018. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  88019. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  88020. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  88021. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  88022. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  88023. + * DAMAGE.
  88024. + * ========================================================================== */
  88025. +#ifndef DWC_HOST_ONLY
  88026. +
  88027. +/** @file
  88028. + * This file implements the Peripheral Controller Driver.
  88029. + *
  88030. + * The Peripheral Controller Driver (PCD) is responsible for
  88031. + * translating requests from the Function Driver into the appropriate
  88032. + * actions on the DWC_otg controller. It isolates the Function Driver
  88033. + * from the specifics of the controller by providing an API to the
  88034. + * Function Driver.
  88035. + *
  88036. + * The Peripheral Controller Driver for Linux will implement the
  88037. + * Gadget API, so that the existing Gadget drivers can be used.
  88038. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  88039. + *
  88040. + * The Linux Gadget API is defined in the header file
  88041. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  88042. + * defined in the structure <code>usb_ep_ops</code> and the USB
  88043. + * Controller API is defined in the structure
  88044. + * <code>usb_gadget_ops</code>.
  88045. + *
  88046. + */
  88047. +
  88048. +#include "dwc_otg_os_dep.h"
  88049. +#include "dwc_otg_pcd_if.h"
  88050. +#include "dwc_otg_pcd.h"
  88051. +#include "dwc_otg_driver.h"
  88052. +#include "dwc_otg_dbg.h"
  88053. +
  88054. +static struct gadget_wrapper {
  88055. + dwc_otg_pcd_t *pcd;
  88056. +
  88057. + struct usb_gadget gadget;
  88058. + struct usb_gadget_driver *driver;
  88059. +
  88060. + struct usb_ep ep0;
  88061. + struct usb_ep in_ep[16];
  88062. + struct usb_ep out_ep[16];
  88063. +
  88064. +} *gadget_wrapper;
  88065. +
  88066. +/* Display the contents of the buffer */
  88067. +extern void dump_msg(const u8 * buf, unsigned int length);
  88068. +/**
  88069. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  88070. + * if the endpoint is not found
  88071. + */
  88072. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  88073. +{
  88074. + int i;
  88075. + if (pcd->ep0.priv == handle) {
  88076. + return &pcd->ep0;
  88077. + }
  88078. +
  88079. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  88080. + if (pcd->in_ep[i].priv == handle)
  88081. + return &pcd->in_ep[i];
  88082. + if (pcd->out_ep[i].priv == handle)
  88083. + return &pcd->out_ep[i];
  88084. + }
  88085. +
  88086. + return NULL;
  88087. +}
  88088. +
  88089. +/* USB Endpoint Operations */
  88090. +/*
  88091. + * The following sections briefly describe the behavior of the Gadget
  88092. + * API endpoint operations implemented in the DWC_otg driver
  88093. + * software. Detailed descriptions of the generic behavior of each of
  88094. + * these functions can be found in the Linux header file
  88095. + * include/linux/usb_gadget.h.
  88096. + *
  88097. + * The Gadget API provides wrapper functions for each of the function
  88098. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  88099. + * function, which then calls the underlying PCD function. The
  88100. + * following sections are named according to the wrapper
  88101. + * functions. Within each section, the corresponding DWC_otg PCD
  88102. + * function name is specified.
  88103. + *
  88104. + */
  88105. +
  88106. +/**
  88107. + * This function is called by the Gadget Driver for each EP to be
  88108. + * configured for the current configuration (SET_CONFIGURATION).
  88109. + *
  88110. + * This function initializes the dwc_otg_ep_t data structure, and then
  88111. + * calls dwc_otg_ep_activate.
  88112. + */
  88113. +static int ep_enable(struct usb_ep *usb_ep,
  88114. + const struct usb_endpoint_descriptor *ep_desc)
  88115. +{
  88116. + int retval;
  88117. +
  88118. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  88119. +
  88120. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  88121. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  88122. + return -EINVAL;
  88123. + }
  88124. + if (usb_ep == &gadget_wrapper->ep0) {
  88125. + DWC_WARN("%s, bad ep(0)\n", __func__);
  88126. + return -EINVAL;
  88127. + }
  88128. +
  88129. + /* Check FIFO size? */
  88130. + if (!ep_desc->wMaxPacketSize) {
  88131. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  88132. + return -ERANGE;
  88133. + }
  88134. +
  88135. + if (!gadget_wrapper->driver ||
  88136. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88137. + DWC_WARN("%s, bogus device state\n", __func__);
  88138. + return -ESHUTDOWN;
  88139. + }
  88140. +
  88141. + /* Delete after check - MAS */
  88142. +#if 0
  88143. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  88144. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  88145. + nat = (nat >> 11) & 0x03;
  88146. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  88147. +#endif
  88148. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  88149. + (const uint8_t *)ep_desc,
  88150. + (void *)usb_ep);
  88151. + if (retval) {
  88152. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  88153. + return -EINVAL;
  88154. + }
  88155. +
  88156. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  88157. +
  88158. + return 0;
  88159. +}
  88160. +
  88161. +/**
  88162. + * This function is called when an EP is disabled due to disconnect or
  88163. + * change in configuration. Any pending requests will terminate with a
  88164. + * status of -ESHUTDOWN.
  88165. + *
  88166. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  88167. + * and then calls dwc_otg_ep_deactivate.
  88168. + */
  88169. +static int ep_disable(struct usb_ep *usb_ep)
  88170. +{
  88171. + int retval;
  88172. +
  88173. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  88174. + if (!usb_ep) {
  88175. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  88176. + usb_ep ? usb_ep->name : NULL);
  88177. + return -EINVAL;
  88178. + }
  88179. +
  88180. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  88181. + if (retval) {
  88182. + retval = -EINVAL;
  88183. + }
  88184. +
  88185. + return retval;
  88186. +}
  88187. +
  88188. +/**
  88189. + * This function allocates a request object to use with the specified
  88190. + * endpoint.
  88191. + *
  88192. + * @param ep The endpoint to be used with with the request
  88193. + * @param gfp_flags the GFP_* flags to use.
  88194. + */
  88195. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  88196. + gfp_t gfp_flags)
  88197. +{
  88198. + struct usb_request *usb_req;
  88199. +
  88200. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  88201. + if (0 == ep) {
  88202. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  88203. + return 0;
  88204. + }
  88205. + usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  88206. + if (0 == usb_req) {
  88207. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  88208. + return 0;
  88209. + }
  88210. + memset(usb_req, 0, sizeof(*usb_req));
  88211. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  88212. +
  88213. + return usb_req;
  88214. +}
  88215. +
  88216. +/**
  88217. + * This function frees a request object.
  88218. + *
  88219. + * @param ep The endpoint associated with the request
  88220. + * @param req The request being freed
  88221. + */
  88222. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  88223. +{
  88224. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  88225. +
  88226. + if (0 == ep || 0 == req) {
  88227. + DWC_WARN("%s() %s\n", __func__,
  88228. + "Invalid ep or req argument!\n");
  88229. + return;
  88230. + }
  88231. +
  88232. + kfree(req);
  88233. +}
  88234. +
  88235. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88236. +/**
  88237. + * This function allocates an I/O buffer to be used for a transfer
  88238. + * to/from the specified endpoint.
  88239. + *
  88240. + * @param usb_ep The endpoint to be used with with the request
  88241. + * @param bytes The desired number of bytes for the buffer
  88242. + * @param dma Pointer to the buffer's DMA address; must be valid
  88243. + * @param gfp_flags the GFP_* flags to use.
  88244. + * @return address of a new buffer or null is buffer could not be allocated.
  88245. + */
  88246. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  88247. + dma_addr_t * dma, gfp_t gfp_flags)
  88248. +{
  88249. + void *buf;
  88250. + dwc_otg_pcd_t *pcd = 0;
  88251. +
  88252. + pcd = gadget_wrapper->pcd;
  88253. +
  88254. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  88255. + dma, gfp_flags);
  88256. +
  88257. + /* Check dword alignment */
  88258. + if ((bytes & 0x3UL) != 0) {
  88259. + DWC_WARN("%s() Buffer size is not a multiple of"
  88260. + "DWORD size (%d)", __func__, bytes);
  88261. + }
  88262. +
  88263. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  88264. +
  88265. + /* Check dword alignment */
  88266. + if (((int)buf & 0x3UL) != 0) {
  88267. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  88268. + __func__, buf);
  88269. + }
  88270. +
  88271. + return buf;
  88272. +}
  88273. +
  88274. +/**
  88275. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  88276. + *
  88277. + * @param usb_ep the endpoint associated with the buffer
  88278. + * @param buf address of the buffer
  88279. + * @param dma The buffer's DMA address
  88280. + * @param bytes The number of bytes of the buffer
  88281. + */
  88282. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  88283. + dma_addr_t dma, unsigned bytes)
  88284. +{
  88285. + dwc_otg_pcd_t *pcd = 0;
  88286. +
  88287. + pcd = gadget_wrapper->pcd;
  88288. +
  88289. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  88290. +
  88291. + dma_free_coherent(NULL, bytes, buf, dma);
  88292. +}
  88293. +#endif
  88294. +
  88295. +/**
  88296. + * This function is used to submit an I/O Request to an EP.
  88297. + *
  88298. + * - When the request completes the request's completion callback
  88299. + * is called to return the request to the driver.
  88300. + * - An EP, except control EPs, may have multiple requests
  88301. + * pending.
  88302. + * - Once submitted the request cannot be examined or modified.
  88303. + * - Each request is turned into one or more packets.
  88304. + * - A BULK EP can queue any amount of data; the transfer is
  88305. + * packetized.
  88306. + * - Zero length Packets are specified with the request 'zero'
  88307. + * flag.
  88308. + */
  88309. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  88310. + gfp_t gfp_flags)
  88311. +{
  88312. + dwc_otg_pcd_t *pcd;
  88313. + struct dwc_otg_pcd_ep *ep = NULL;
  88314. + int retval = 0, is_isoc_ep = 0;
  88315. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  88316. +
  88317. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  88318. + __func__, usb_ep, usb_req, gfp_flags);
  88319. +
  88320. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  88321. + DWC_WARN("bad params\n");
  88322. + return -EINVAL;
  88323. + }
  88324. +
  88325. + if (!usb_ep) {
  88326. + DWC_WARN("bad ep\n");
  88327. + return -EINVAL;
  88328. + }
  88329. +
  88330. + pcd = gadget_wrapper->pcd;
  88331. + if (!gadget_wrapper->driver ||
  88332. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88333. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  88334. + gadget_wrapper->gadget.speed);
  88335. + DWC_WARN("bogus device state\n");
  88336. + return -ESHUTDOWN;
  88337. + }
  88338. +
  88339. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  88340. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  88341. +
  88342. + usb_req->status = -EINPROGRESS;
  88343. + usb_req->actual = 0;
  88344. +
  88345. + ep = ep_from_handle(pcd, usb_ep);
  88346. + if (ep == NULL)
  88347. + is_isoc_ep = 0;
  88348. + else
  88349. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  88350. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88351. + dma_addr = usb_req->dma;
  88352. +#else
  88353. + if (GET_CORE_IF(pcd)->dma_enable) {
  88354. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  88355. + struct device *dev = NULL;
  88356. +
  88357. + if (otg_dev != NULL)
  88358. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  88359. +
  88360. + if (usb_req->length != 0 &&
  88361. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  88362. + dma_addr = dma_map_single(dev, usb_req->buf,
  88363. + usb_req->length,
  88364. + ep->dwc_ep.is_in ?
  88365. + DMA_TO_DEVICE:
  88366. + DMA_FROM_DEVICE);
  88367. + }
  88368. + }
  88369. +#endif
  88370. +
  88371. +#ifdef DWC_UTE_PER_IO
  88372. + if (is_isoc_ep == 1) {
  88373. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  88374. + usb_req->length, usb_req->zero, usb_req,
  88375. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  88376. + if (retval)
  88377. + return -EINVAL;
  88378. +
  88379. + return 0;
  88380. + }
  88381. +#endif
  88382. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  88383. + usb_req->length, usb_req->zero, usb_req,
  88384. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  88385. + if (retval) {
  88386. + return -EINVAL;
  88387. + }
  88388. +
  88389. + return 0;
  88390. +}
  88391. +
  88392. +/**
  88393. + * This function cancels an I/O request from an EP.
  88394. + */
  88395. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  88396. +{
  88397. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  88398. +
  88399. + if (!usb_ep || !usb_req) {
  88400. + DWC_WARN("bad argument\n");
  88401. + return -EINVAL;
  88402. + }
  88403. + if (!gadget_wrapper->driver ||
  88404. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88405. + DWC_WARN("bogus device state\n");
  88406. + return -ESHUTDOWN;
  88407. + }
  88408. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  88409. + return -EINVAL;
  88410. + }
  88411. +
  88412. + return 0;
  88413. +}
  88414. +
  88415. +/**
  88416. + * usb_ep_set_halt stalls an endpoint.
  88417. + *
  88418. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  88419. + * toggle.
  88420. + *
  88421. + * Both of these functions are implemented with the same underlying
  88422. + * function. The behavior depends on the value argument.
  88423. + *
  88424. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  88425. + * @param[in] value
  88426. + * - 0 means clear_halt.
  88427. + * - 1 means set_halt,
  88428. + * - 2 means clear stall lock flag.
  88429. + * - 3 means set stall lock flag.
  88430. + */
  88431. +static int ep_halt(struct usb_ep *usb_ep, int value)
  88432. +{
  88433. + int retval = 0;
  88434. +
  88435. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  88436. +
  88437. + if (!usb_ep) {
  88438. + DWC_WARN("bad ep\n");
  88439. + return -EINVAL;
  88440. + }
  88441. +
  88442. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  88443. + if (retval == -DWC_E_AGAIN) {
  88444. + return -EAGAIN;
  88445. + } else if (retval) {
  88446. + retval = -EINVAL;
  88447. + }
  88448. +
  88449. + return retval;
  88450. +}
  88451. +
  88452. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  88453. +#if 0
  88454. +/**
  88455. + * ep_wedge: sets the halt feature and ignores clear requests
  88456. + *
  88457. + * @usb_ep: the endpoint being wedged
  88458. + *
  88459. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  88460. + * requests. If the gadget driver clears the halt status, it will
  88461. + * automatically unwedge the endpoint.
  88462. + *
  88463. + * Returns zero on success, else negative errno. *
  88464. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  88465. + */
  88466. +static int ep_wedge(struct usb_ep *usb_ep)
  88467. +{
  88468. + int retval = 0;
  88469. +
  88470. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  88471. +
  88472. + if (!usb_ep) {
  88473. + DWC_WARN("bad ep\n");
  88474. + return -EINVAL;
  88475. + }
  88476. +
  88477. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  88478. + if (retval == -DWC_E_AGAIN) {
  88479. + retval = -EAGAIN;
  88480. + } else if (retval) {
  88481. + retval = -EINVAL;
  88482. + }
  88483. +
  88484. + return retval;
  88485. +}
  88486. +#endif
  88487. +
  88488. +#ifdef DWC_EN_ISOC
  88489. +/**
  88490. + * This function is used to submit an ISOC Transfer Request to an EP.
  88491. + *
  88492. + * - Every time a sync period completes the request's completion callback
  88493. + * is called to provide data to the gadget driver.
  88494. + * - Once submitted the request cannot be modified.
  88495. + * - Each request is turned into periodic data packets untill ISO
  88496. + * Transfer is stopped..
  88497. + */
  88498. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  88499. + gfp_t gfp_flags)
  88500. +{
  88501. + int retval = 0;
  88502. +
  88503. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  88504. + DWC_WARN("bad params\n");
  88505. + return -EINVAL;
  88506. + }
  88507. +
  88508. + if (!usb_ep) {
  88509. + DWC_PRINTF("bad params\n");
  88510. + return -EINVAL;
  88511. + }
  88512. +
  88513. + req->status = -EINPROGRESS;
  88514. +
  88515. + retval =
  88516. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  88517. + req->buf1, req->dma0, req->dma1,
  88518. + req->sync_frame, req->data_pattern_frame,
  88519. + req->data_per_frame,
  88520. + req->
  88521. + flags & USB_REQ_ISO_ASAP ? -1 :
  88522. + req->start_frame, req->buf_proc_intrvl,
  88523. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  88524. +
  88525. + if (retval) {
  88526. + return -EINVAL;
  88527. + }
  88528. +
  88529. + return retval;
  88530. +}
  88531. +
  88532. +/**
  88533. + * This function stops ISO EP Periodic Data Transfer.
  88534. + */
  88535. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  88536. +{
  88537. + int retval = 0;
  88538. + if (!usb_ep) {
  88539. + DWC_WARN("bad ep\n");
  88540. + }
  88541. +
  88542. + if (!gadget_wrapper->driver ||
  88543. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88544. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  88545. + gadget_wrapper->gadget.speed);
  88546. + DWC_WARN("bogus device state\n");
  88547. + }
  88548. +
  88549. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  88550. + if (retval) {
  88551. + retval = -EINVAL;
  88552. + }
  88553. +
  88554. + return retval;
  88555. +}
  88556. +
  88557. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  88558. + int packets, gfp_t gfp_flags)
  88559. +{
  88560. + struct usb_iso_request *pReq = NULL;
  88561. + uint32_t req_size;
  88562. +
  88563. + req_size = sizeof(struct usb_iso_request);
  88564. + req_size +=
  88565. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  88566. +
  88567. + pReq = kmalloc(req_size, gfp_flags);
  88568. + if (!pReq) {
  88569. + DWC_WARN("Can't allocate Iso Request\n");
  88570. + return 0;
  88571. + }
  88572. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  88573. +
  88574. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  88575. +
  88576. + return pReq;
  88577. +}
  88578. +
  88579. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  88580. +{
  88581. + kfree(req);
  88582. +}
  88583. +
  88584. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  88585. + .ep_ops = {
  88586. + .enable = ep_enable,
  88587. + .disable = ep_disable,
  88588. +
  88589. + .alloc_request = dwc_otg_pcd_alloc_request,
  88590. + .free_request = dwc_otg_pcd_free_request,
  88591. +
  88592. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88593. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  88594. + .free_buffer = dwc_otg_pcd_free_buffer,
  88595. +#endif
  88596. +
  88597. + .queue = ep_queue,
  88598. + .dequeue = ep_dequeue,
  88599. +
  88600. + .set_halt = ep_halt,
  88601. + .fifo_status = 0,
  88602. + .fifo_flush = 0,
  88603. + },
  88604. + .iso_ep_start = iso_ep_start,
  88605. + .iso_ep_stop = iso_ep_stop,
  88606. + .alloc_iso_request = alloc_iso_request,
  88607. + .free_iso_request = free_iso_request,
  88608. +};
  88609. +
  88610. +#else
  88611. +
  88612. + int (*enable) (struct usb_ep *ep,
  88613. + const struct usb_endpoint_descriptor *desc);
  88614. + int (*disable) (struct usb_ep *ep);
  88615. +
  88616. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  88617. + gfp_t gfp_flags);
  88618. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  88619. +
  88620. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  88621. + gfp_t gfp_flags);
  88622. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  88623. +
  88624. + int (*set_halt) (struct usb_ep *ep, int value);
  88625. + int (*set_wedge) (struct usb_ep *ep);
  88626. +
  88627. + int (*fifo_status) (struct usb_ep *ep);
  88628. + void (*fifo_flush) (struct usb_ep *ep);
  88629. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  88630. + .enable = ep_enable,
  88631. + .disable = ep_disable,
  88632. +
  88633. + .alloc_request = dwc_otg_pcd_alloc_request,
  88634. + .free_request = dwc_otg_pcd_free_request,
  88635. +
  88636. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88637. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  88638. + .free_buffer = dwc_otg_pcd_free_buffer,
  88639. +#else
  88640. + /* .set_wedge = ep_wedge, */
  88641. + .set_wedge = NULL, /* uses set_halt instead */
  88642. +#endif
  88643. +
  88644. + .queue = ep_queue,
  88645. + .dequeue = ep_dequeue,
  88646. +
  88647. + .set_halt = ep_halt,
  88648. + .fifo_status = 0,
  88649. + .fifo_flush = 0,
  88650. +
  88651. +};
  88652. +
  88653. +#endif /* _EN_ISOC_ */
  88654. +/* Gadget Operations */
  88655. +/**
  88656. + * The following gadget operations will be implemented in the DWC_otg
  88657. + * PCD. Functions in the API that are not described below are not
  88658. + * implemented.
  88659. + *
  88660. + * The Gadget API provides wrapper functions for each of the function
  88661. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  88662. + * wrapper function, which then calls the underlying PCD function. The
  88663. + * following sections are named according to the wrapper functions
  88664. + * (except for ioctl, which doesn't have a wrapper function). Within
  88665. + * each section, the corresponding DWC_otg PCD function name is
  88666. + * specified.
  88667. + *
  88668. + */
  88669. +
  88670. +/**
  88671. + *Gets the USB Frame number of the last SOF.
  88672. + */
  88673. +static int get_frame_number(struct usb_gadget *gadget)
  88674. +{
  88675. + struct gadget_wrapper *d;
  88676. +
  88677. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  88678. +
  88679. + if (gadget == 0) {
  88680. + return -ENODEV;
  88681. + }
  88682. +
  88683. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88684. + return dwc_otg_pcd_get_frame_number(d->pcd);
  88685. +}
  88686. +
  88687. +#ifdef CONFIG_USB_DWC_OTG_LPM
  88688. +static int test_lpm_enabled(struct usb_gadget *gadget)
  88689. +{
  88690. + struct gadget_wrapper *d;
  88691. +
  88692. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88693. +
  88694. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  88695. +}
  88696. +#endif
  88697. +
  88698. +/**
  88699. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  88700. + * session is in progress. If a session is already in progress, but
  88701. + * the device is suspended, remote wakeup signaling is started.
  88702. + *
  88703. + */
  88704. +static int wakeup(struct usb_gadget *gadget)
  88705. +{
  88706. + struct gadget_wrapper *d;
  88707. +
  88708. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  88709. +
  88710. + if (gadget == 0) {
  88711. + return -ENODEV;
  88712. + } else {
  88713. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88714. + }
  88715. + dwc_otg_pcd_wakeup(d->pcd);
  88716. + return 0;
  88717. +}
  88718. +
  88719. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  88720. + .get_frame = get_frame_number,
  88721. + .wakeup = wakeup,
  88722. +#ifdef CONFIG_USB_DWC_OTG_LPM
  88723. + .lpm_support = test_lpm_enabled,
  88724. +#endif
  88725. + // current versions must always be self-powered
  88726. +};
  88727. +
  88728. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  88729. +{
  88730. + int retval = -DWC_E_NOT_SUPPORTED;
  88731. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  88732. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  88733. + (struct usb_ctrlrequest
  88734. + *)bytes);
  88735. + }
  88736. +
  88737. + if (retval == -ENOTSUPP) {
  88738. + retval = -DWC_E_NOT_SUPPORTED;
  88739. + } else if (retval < 0) {
  88740. + retval = -DWC_E_INVALID;
  88741. + }
  88742. +
  88743. + return retval;
  88744. +}
  88745. +
  88746. +#ifdef DWC_EN_ISOC
  88747. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88748. + void *req_handle, int proc_buf_num)
  88749. +{
  88750. + int i, packet_count;
  88751. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  88752. + struct usb_iso_request *iso_req = req_handle;
  88753. +
  88754. + if (proc_buf_num) {
  88755. + iso_packet = iso_req->iso_packet_desc1;
  88756. + } else {
  88757. + iso_packet = iso_req->iso_packet_desc0;
  88758. + }
  88759. + packet_count =
  88760. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  88761. + for (i = 0; i < packet_count; ++i) {
  88762. + int status;
  88763. + int actual;
  88764. + int offset;
  88765. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  88766. + i, &status, &actual, &offset);
  88767. + switch (status) {
  88768. + case -DWC_E_NO_DATA:
  88769. + status = -ENODATA;
  88770. + break;
  88771. + default:
  88772. + if (status) {
  88773. + DWC_PRINTF("unknown status in isoc packet\n");
  88774. + }
  88775. +
  88776. + }
  88777. + iso_packet[i].status = status;
  88778. + iso_packet[i].offset = offset;
  88779. + iso_packet[i].actual_length = actual;
  88780. + }
  88781. +
  88782. + iso_req->status = 0;
  88783. + iso_req->process_buffer(ep_handle, iso_req);
  88784. +
  88785. + return 0;
  88786. +}
  88787. +#endif /* DWC_EN_ISOC */
  88788. +
  88789. +#ifdef DWC_UTE_PER_IO
  88790. +/**
  88791. + * Copy the contents of the extended request to the Linux usb_request's
  88792. + * extended part and call the gadget's completion.
  88793. + *
  88794. + * @param pcd Pointer to the pcd structure
  88795. + * @param ep_handle Void pointer to the usb_ep structure
  88796. + * @param req_handle Void pointer to the usb_request structure
  88797. + * @param status Request status returned from the portable logic
  88798. + * @param ereq_port Void pointer to the extended request structure
  88799. + * created in the the portable part that contains the
  88800. + * results of the processed iso packets.
  88801. + */
  88802. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88803. + void *req_handle, int32_t status, void *ereq_port)
  88804. +{
  88805. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  88806. + struct dwc_iso_xreq_port *ereqport = NULL;
  88807. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  88808. + int i;
  88809. + struct usb_request *req;
  88810. + //struct dwc_ute_iso_packet_descriptor *
  88811. + //int status = 0;
  88812. +
  88813. + req = (struct usb_request *)req_handle;
  88814. + ereqorg = &req->ext_req;
  88815. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  88816. + desc_org = ereqorg->per_io_frame_descs;
  88817. +
  88818. + if (req && req->complete) {
  88819. + /* Copy the request data from the portable logic to our request */
  88820. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  88821. + desc_org[i].actual_length =
  88822. + ereqport->per_io_frame_descs[i].actual_length;
  88823. + desc_org[i].status =
  88824. + ereqport->per_io_frame_descs[i].status;
  88825. + }
  88826. +
  88827. + switch (status) {
  88828. + case -DWC_E_SHUTDOWN:
  88829. + req->status = -ESHUTDOWN;
  88830. + break;
  88831. + case -DWC_E_RESTART:
  88832. + req->status = -ECONNRESET;
  88833. + break;
  88834. + case -DWC_E_INVALID:
  88835. + req->status = -EINVAL;
  88836. + break;
  88837. + case -DWC_E_TIMEOUT:
  88838. + req->status = -ETIMEDOUT;
  88839. + break;
  88840. + default:
  88841. + req->status = status;
  88842. + }
  88843. +
  88844. + /* And call the gadget's completion */
  88845. + req->complete(ep_handle, req);
  88846. + }
  88847. +
  88848. + return 0;
  88849. +}
  88850. +#endif /* DWC_UTE_PER_IO */
  88851. +
  88852. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88853. + void *req_handle, int32_t status, uint32_t actual)
  88854. +{
  88855. + struct usb_request *req = (struct usb_request *)req_handle;
  88856. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  88857. + struct dwc_otg_pcd_ep *ep = NULL;
  88858. +#endif
  88859. +
  88860. + if (req && req->complete) {
  88861. + switch (status) {
  88862. + case -DWC_E_SHUTDOWN:
  88863. + req->status = -ESHUTDOWN;
  88864. + break;
  88865. + case -DWC_E_RESTART:
  88866. + req->status = -ECONNRESET;
  88867. + break;
  88868. + case -DWC_E_INVALID:
  88869. + req->status = -EINVAL;
  88870. + break;
  88871. + case -DWC_E_TIMEOUT:
  88872. + req->status = -ETIMEDOUT;
  88873. + break;
  88874. + default:
  88875. + req->status = status;
  88876. +
  88877. + }
  88878. +
  88879. + req->actual = actual;
  88880. + DWC_SPINUNLOCK(pcd->lock);
  88881. + req->complete(ep_handle, req);
  88882. + DWC_SPINLOCK(pcd->lock);
  88883. + }
  88884. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  88885. + ep = ep_from_handle(pcd, ep_handle);
  88886. + if (GET_CORE_IF(pcd)->dma_enable) {
  88887. + if (req->length != 0) {
  88888. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  88889. + struct device *dev = NULL;
  88890. +
  88891. + if (otg_dev != NULL)
  88892. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  88893. +
  88894. + dma_unmap_single(dev, req->dma, req->length,
  88895. + ep->dwc_ep.is_in ?
  88896. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  88897. + }
  88898. + }
  88899. +#endif
  88900. +
  88901. + return 0;
  88902. +}
  88903. +
  88904. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  88905. +{
  88906. + gadget_wrapper->gadget.speed = speed;
  88907. + return 0;
  88908. +}
  88909. +
  88910. +static int _disconnect(dwc_otg_pcd_t * pcd)
  88911. +{
  88912. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  88913. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  88914. + }
  88915. + return 0;
  88916. +}
  88917. +
  88918. +static int _resume(dwc_otg_pcd_t * pcd)
  88919. +{
  88920. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  88921. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  88922. + }
  88923. +
  88924. + return 0;
  88925. +}
  88926. +
  88927. +static int _suspend(dwc_otg_pcd_t * pcd)
  88928. +{
  88929. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  88930. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  88931. + }
  88932. + return 0;
  88933. +}
  88934. +
  88935. +/**
  88936. + * This function updates the otg values in the gadget structure.
  88937. + */
  88938. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  88939. +{
  88940. +
  88941. + if (!gadget_wrapper->gadget.is_otg)
  88942. + return 0;
  88943. +
  88944. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  88945. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  88946. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  88947. + return 0;
  88948. +}
  88949. +
  88950. +static int _reset(dwc_otg_pcd_t * pcd)
  88951. +{
  88952. + return 0;
  88953. +}
  88954. +
  88955. +#ifdef DWC_UTE_CFI
  88956. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  88957. +{
  88958. + int retval = -DWC_E_INVALID;
  88959. + if (gadget_wrapper->driver->cfi_feature_setup) {
  88960. + retval =
  88961. + gadget_wrapper->driver->
  88962. + cfi_feature_setup(&gadget_wrapper->gadget,
  88963. + (struct cfi_usb_ctrlrequest *)cfi_req);
  88964. + }
  88965. +
  88966. + return retval;
  88967. +}
  88968. +#endif
  88969. +
  88970. +static const struct dwc_otg_pcd_function_ops fops = {
  88971. + .complete = _complete,
  88972. +#ifdef DWC_EN_ISOC
  88973. + .isoc_complete = _isoc_complete,
  88974. +#endif
  88975. + .setup = _setup,
  88976. + .disconnect = _disconnect,
  88977. + .connect = _connect,
  88978. + .resume = _resume,
  88979. + .suspend = _suspend,
  88980. + .hnp_changed = _hnp_changed,
  88981. + .reset = _reset,
  88982. +#ifdef DWC_UTE_CFI
  88983. + .cfi_setup = _cfi_setup,
  88984. +#endif
  88985. +#ifdef DWC_UTE_PER_IO
  88986. + .xisoc_complete = _xisoc_complete,
  88987. +#endif
  88988. +};
  88989. +
  88990. +/**
  88991. + * This function is the top level PCD interrupt handler.
  88992. + */
  88993. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  88994. +{
  88995. + dwc_otg_pcd_t *pcd = dev;
  88996. + int32_t retval = IRQ_NONE;
  88997. +
  88998. + retval = dwc_otg_pcd_handle_intr(pcd);
  88999. + if (retval != 0) {
  89000. + S3C2410X_CLEAR_EINTPEND();
  89001. + }
  89002. + return IRQ_RETVAL(retval);
  89003. +}
  89004. +
  89005. +/**
  89006. + * This function initialized the usb_ep structures to there default
  89007. + * state.
  89008. + *
  89009. + * @param d Pointer on gadget_wrapper.
  89010. + */
  89011. +void gadget_add_eps(struct gadget_wrapper *d)
  89012. +{
  89013. + static const char *names[] = {
  89014. +
  89015. + "ep0",
  89016. + "ep1in",
  89017. + "ep2in",
  89018. + "ep3in",
  89019. + "ep4in",
  89020. + "ep5in",
  89021. + "ep6in",
  89022. + "ep7in",
  89023. + "ep8in",
  89024. + "ep9in",
  89025. + "ep10in",
  89026. + "ep11in",
  89027. + "ep12in",
  89028. + "ep13in",
  89029. + "ep14in",
  89030. + "ep15in",
  89031. + "ep1out",
  89032. + "ep2out",
  89033. + "ep3out",
  89034. + "ep4out",
  89035. + "ep5out",
  89036. + "ep6out",
  89037. + "ep7out",
  89038. + "ep8out",
  89039. + "ep9out",
  89040. + "ep10out",
  89041. + "ep11out",
  89042. + "ep12out",
  89043. + "ep13out",
  89044. + "ep14out",
  89045. + "ep15out"
  89046. + };
  89047. +
  89048. + int i;
  89049. + struct usb_ep *ep;
  89050. + int8_t dev_endpoints;
  89051. +
  89052. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  89053. +
  89054. + INIT_LIST_HEAD(&d->gadget.ep_list);
  89055. + d->gadget.ep0 = &d->ep0;
  89056. + d->gadget.speed = USB_SPEED_UNKNOWN;
  89057. +
  89058. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  89059. +
  89060. + /**
  89061. + * Initialize the EP0 structure.
  89062. + */
  89063. + ep = &d->ep0;
  89064. +
  89065. + /* Init the usb_ep structure. */
  89066. + ep->name = names[0];
  89067. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  89068. +
  89069. + /**
  89070. + * @todo NGS: What should the max packet size be set to
  89071. + * here? Before EP type is set?
  89072. + */
  89073. + ep->maxpacket = MAX_PACKET_SIZE;
  89074. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  89075. +
  89076. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  89077. +
  89078. + /**
  89079. + * Initialize the EP structures.
  89080. + */
  89081. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  89082. +
  89083. + for (i = 0; i < dev_endpoints; i++) {
  89084. + ep = &d->in_ep[i];
  89085. +
  89086. + /* Init the usb_ep structure. */
  89087. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  89088. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  89089. +
  89090. + /**
  89091. + * @todo NGS: What should the max packet size be set to
  89092. + * here? Before EP type is set?
  89093. + */
  89094. + ep->maxpacket = MAX_PACKET_SIZE;
  89095. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  89096. + }
  89097. +
  89098. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  89099. +
  89100. + for (i = 0; i < dev_endpoints; i++) {
  89101. + ep = &d->out_ep[i];
  89102. +
  89103. + /* Init the usb_ep structure. */
  89104. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  89105. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  89106. +
  89107. + /**
  89108. + * @todo NGS: What should the max packet size be set to
  89109. + * here? Before EP type is set?
  89110. + */
  89111. + ep->maxpacket = MAX_PACKET_SIZE;
  89112. +
  89113. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  89114. + }
  89115. +
  89116. + /* remove ep0 from the list. There is a ep0 pointer. */
  89117. + list_del_init(&d->ep0.ep_list);
  89118. +
  89119. + d->ep0.maxpacket = MAX_EP0_SIZE;
  89120. +}
  89121. +
  89122. +/**
  89123. + * This function releases the Gadget device.
  89124. + * required by device_unregister().
  89125. + *
  89126. + * @todo Should this do something? Should it free the PCD?
  89127. + */
  89128. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  89129. +{
  89130. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  89131. +}
  89132. +
  89133. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  89134. +{
  89135. + static char pcd_name[] = "dwc_otg_pcd";
  89136. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  89137. + struct gadget_wrapper *d;
  89138. + int retval;
  89139. +
  89140. + d = DWC_ALLOC(sizeof(*d));
  89141. + if (d == NULL) {
  89142. + return NULL;
  89143. + }
  89144. +
  89145. + memset(d, 0, sizeof(*d));
  89146. +
  89147. + d->gadget.name = pcd_name;
  89148. + d->pcd = otg_dev->pcd;
  89149. +
  89150. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  89151. + strcpy(d->gadget.dev.bus_id, "gadget");
  89152. +#else
  89153. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  89154. +#endif
  89155. +
  89156. + d->gadget.dev.parent = &_dev->dev;
  89157. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  89158. + d->gadget.ops = &dwc_otg_pcd_ops;
  89159. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  89160. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  89161. +
  89162. + d->driver = 0;
  89163. + /* Register the gadget device */
  89164. + retval = device_register(&d->gadget.dev);
  89165. + if (retval != 0) {
  89166. + DWC_ERROR("device_register failed\n");
  89167. + DWC_FREE(d);
  89168. + return NULL;
  89169. + }
  89170. +
  89171. + return d;
  89172. +}
  89173. +
  89174. +static void free_wrapper(struct gadget_wrapper *d)
  89175. +{
  89176. + if (d->driver) {
  89177. + /* should have been done already by driver model core */
  89178. + DWC_WARN("driver '%s' is still registered\n",
  89179. + d->driver->driver.name);
  89180. + usb_gadget_unregister_driver(d->driver);
  89181. + }
  89182. +
  89183. + device_unregister(&d->gadget.dev);
  89184. + DWC_FREE(d);
  89185. +}
  89186. +
  89187. +/**
  89188. + * This function initialized the PCD portion of the driver.
  89189. + *
  89190. + */
  89191. +int pcd_init(dwc_bus_dev_t *_dev)
  89192. +{
  89193. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  89194. + int retval = 0;
  89195. +
  89196. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  89197. +
  89198. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  89199. +
  89200. + if (!otg_dev->pcd) {
  89201. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  89202. + return -ENOMEM;
  89203. + }
  89204. +
  89205. + otg_dev->pcd->otg_dev = otg_dev;
  89206. + gadget_wrapper = alloc_wrapper(_dev);
  89207. +
  89208. + /*
  89209. + * Initialize EP structures
  89210. + */
  89211. + gadget_add_eps(gadget_wrapper);
  89212. + /*
  89213. + * Setup interupt handler
  89214. + */
  89215. +#ifdef PLATFORM_INTERFACE
  89216. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  89217. + platform_get_irq(_dev, 0));
  89218. + retval = request_irq(platform_get_irq(_dev, 0), dwc_otg_pcd_irq,
  89219. + IRQF_SHARED, gadget_wrapper->gadget.name,
  89220. + otg_dev->pcd);
  89221. + if (retval != 0) {
  89222. + DWC_ERROR("request of irq%d failed\n",
  89223. + platform_get_irq(_dev, 0));
  89224. + free_wrapper(gadget_wrapper);
  89225. + return -EBUSY;
  89226. + }
  89227. +#else
  89228. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  89229. + _dev->irq);
  89230. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  89231. + IRQF_SHARED | IRQF_DISABLED,
  89232. + gadget_wrapper->gadget.name, otg_dev->pcd);
  89233. + if (retval != 0) {
  89234. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  89235. + free_wrapper(gadget_wrapper);
  89236. + return -EBUSY;
  89237. + }
  89238. +#endif
  89239. +
  89240. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  89241. +
  89242. + return retval;
  89243. +}
  89244. +
  89245. +/**
  89246. + * Cleanup the PCD.
  89247. + */
  89248. +void pcd_remove(dwc_bus_dev_t *_dev)
  89249. +{
  89250. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  89251. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  89252. +
  89253. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  89254. +
  89255. + /*
  89256. + * Free the IRQ
  89257. + */
  89258. +#ifdef PLATFORM_INTERFACE
  89259. + free_irq(platform_get_irq(_dev, 0), pcd);
  89260. +#else
  89261. + free_irq(_dev->irq, pcd);
  89262. +#endif
  89263. + dwc_otg_pcd_remove(otg_dev->pcd);
  89264. + free_wrapper(gadget_wrapper);
  89265. + otg_dev->pcd = 0;
  89266. +}
  89267. +
  89268. +/**
  89269. + * This function registers a gadget driver with the PCD.
  89270. + *
  89271. + * When a driver is successfully registered, it will receive control
  89272. + * requests including set_configuration(), which enables non-control
  89273. + * requests. then usb traffic follows until a disconnect is reported.
  89274. + * then a host may connect again, or the driver might get unbound.
  89275. + *
  89276. + * @param driver The driver being registered
  89277. + * @param bind The bind function of gadget driver
  89278. + */
  89279. +
  89280. +int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
  89281. +{
  89282. + int retval;
  89283. +
  89284. + DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n",
  89285. + driver->driver.name);
  89286. +
  89287. + if (!driver || driver->max_speed == USB_SPEED_UNKNOWN ||
  89288. + !driver->bind ||
  89289. + !driver->unbind || !driver->disconnect || !driver->setup) {
  89290. + DWC_DEBUGPL(DBG_PCDV, "EINVAL\n");
  89291. + return -EINVAL;
  89292. + }
  89293. + if (gadget_wrapper == 0) {
  89294. + DWC_DEBUGPL(DBG_PCDV, "ENODEV\n");
  89295. + return -ENODEV;
  89296. + }
  89297. + if (gadget_wrapper->driver != 0) {
  89298. + DWC_DEBUGPL(DBG_PCDV, "EBUSY (%p)\n", gadget_wrapper->driver);
  89299. + return -EBUSY;
  89300. + }
  89301. +
  89302. + /* hook up the driver */
  89303. + gadget_wrapper->driver = driver;
  89304. + gadget_wrapper->gadget.dev.driver = &driver->driver;
  89305. +
  89306. + DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", driver->driver.name);
  89307. + retval = driver->bind(&gadget_wrapper->gadget, gadget_wrapper->driver);
  89308. + if (retval) {
  89309. + DWC_ERROR("bind to driver %s --> error %d\n",
  89310. + driver->driver.name, retval);
  89311. + gadget_wrapper->driver = 0;
  89312. + gadget_wrapper->gadget.dev.driver = 0;
  89313. + return retval;
  89314. + }
  89315. + DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n",
  89316. + driver->driver.name);
  89317. + return 0;
  89318. +}
  89319. +EXPORT_SYMBOL(usb_gadget_probe_driver);
  89320. +
  89321. +/**
  89322. + * This function unregisters a gadget driver
  89323. + *
  89324. + * @param driver The driver being unregistered
  89325. + */
  89326. +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  89327. +{
  89328. + //DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
  89329. +
  89330. + if (gadget_wrapper == 0) {
  89331. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__,
  89332. + -ENODEV);
  89333. + return -ENODEV;
  89334. + }
  89335. + if (driver == 0 || driver != gadget_wrapper->driver) {
  89336. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): driver?\n", __func__,
  89337. + -EINVAL);
  89338. + return -EINVAL;
  89339. + }
  89340. +
  89341. + driver->unbind(&gadget_wrapper->gadget);
  89342. + gadget_wrapper->driver = 0;
  89343. +
  89344. + DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", driver->driver.name);
  89345. + return 0;
  89346. +}
  89347. +
  89348. +EXPORT_SYMBOL(usb_gadget_unregister_driver);
  89349. +
  89350. +#endif /* DWC_HOST_ONLY */
  89351. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_regs.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  89352. --- linux-3.12.13/drivers/usb/host/dwc_otg/dwc_otg_regs.h 1970-01-01 01:00:00.000000000 +0100
  89353. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_regs.h 2014-03-11 17:51:27.000000000 +0100
  89354. @@ -0,0 +1,2550 @@
  89355. +/* ==========================================================================
  89356. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  89357. + * $Revision: #98 $
  89358. + * $Date: 2012/08/10 $
  89359. + * $Change: 2047372 $
  89360. + *
  89361. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  89362. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  89363. + * otherwise expressly agreed to in writing between Synopsys and you.
  89364. + *
  89365. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  89366. + * any End User Software License Agreement or Agreement for Licensed Product
  89367. + * with Synopsys or any supplement thereto. You are permitted to use and
  89368. + * redistribute this Software in source and binary forms, with or without
  89369. + * modification, provided that redistributions of source code must retain this
  89370. + * notice. You may not view, use, disclose, copy or distribute this file or
  89371. + * any information contained herein except pursuant to this license grant from
  89372. + * Synopsys. If you do not agree with this notice, including the disclaimer
  89373. + * below, then you are not authorized to use the Software.
  89374. + *
  89375. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  89376. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  89377. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  89378. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  89379. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  89380. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89381. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  89382. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  89383. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  89384. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  89385. + * DAMAGE.
  89386. + * ========================================================================== */
  89387. +
  89388. +#ifndef __DWC_OTG_REGS_H__
  89389. +#define __DWC_OTG_REGS_H__
  89390. +
  89391. +#include "dwc_otg_core_if.h"
  89392. +
  89393. +/**
  89394. + * @file
  89395. + *
  89396. + * This file contains the data structures for accessing the DWC_otg core registers.
  89397. + *
  89398. + * The application interfaces with the HS OTG core by reading from and
  89399. + * writing to the Control and Status Register (CSR) space through the
  89400. + * AHB Slave interface. These registers are 32 bits wide, and the
  89401. + * addresses are 32-bit-block aligned.
  89402. + * CSRs are classified as follows:
  89403. + * - Core Global Registers
  89404. + * - Device Mode Registers
  89405. + * - Device Global Registers
  89406. + * - Device Endpoint Specific Registers
  89407. + * - Host Mode Registers
  89408. + * - Host Global Registers
  89409. + * - Host Port CSRs
  89410. + * - Host Channel Specific Registers
  89411. + *
  89412. + * Only the Core Global registers can be accessed in both Device and
  89413. + * Host modes. When the HS OTG core is operating in one mode, either
  89414. + * Device or Host, the application must not access registers from the
  89415. + * other mode. When the core switches from one mode to another, the
  89416. + * registers in the new mode of operation must be reprogrammed as they
  89417. + * would be after a power-on reset.
  89418. + */
  89419. +
  89420. +/****************************************************************************/
  89421. +/** DWC_otg Core registers .
  89422. + * The dwc_otg_core_global_regs structure defines the size
  89423. + * and relative field offsets for the Core Global registers.
  89424. + */
  89425. +typedef struct dwc_otg_core_global_regs {
  89426. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  89427. + volatile uint32_t gotgctl;
  89428. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  89429. + volatile uint32_t gotgint;
  89430. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  89431. + volatile uint32_t gahbcfg;
  89432. +
  89433. +#define DWC_GLBINTRMASK 0x0001
  89434. +#define DWC_DMAENABLE 0x0020
  89435. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  89436. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  89437. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  89438. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  89439. +
  89440. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  89441. + volatile uint32_t gusbcfg;
  89442. + /**Core Reset Register. <i>Offset: 010h</i> */
  89443. + volatile uint32_t grstctl;
  89444. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  89445. + volatile uint32_t gintsts;
  89446. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  89447. + volatile uint32_t gintmsk;
  89448. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  89449. + volatile uint32_t grxstsr;
  89450. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  89451. + volatile uint32_t grxstsp;
  89452. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  89453. + volatile uint32_t grxfsiz;
  89454. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  89455. + volatile uint32_t gnptxfsiz;
  89456. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  89457. + * Only). <i>Offset: 02Ch</i> */
  89458. + volatile uint32_t gnptxsts;
  89459. + /**I2C Access Register. <i>Offset: 030h</i> */
  89460. + volatile uint32_t gi2cctl;
  89461. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  89462. + volatile uint32_t gpvndctl;
  89463. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  89464. + volatile uint32_t ggpio;
  89465. + /**User ID Register. <i>Offset: 03Ch</i> */
  89466. + volatile uint32_t guid;
  89467. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  89468. + volatile uint32_t gsnpsid;
  89469. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  89470. + volatile uint32_t ghwcfg1;
  89471. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  89472. + volatile uint32_t ghwcfg2;
  89473. +#define DWC_SLAVE_ONLY_ARCH 0
  89474. +#define DWC_EXT_DMA_ARCH 1
  89475. +#define DWC_INT_DMA_ARCH 2
  89476. +
  89477. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  89478. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  89479. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  89480. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  89481. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  89482. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  89483. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  89484. +
  89485. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  89486. + volatile uint32_t ghwcfg3;
  89487. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  89488. + volatile uint32_t ghwcfg4;
  89489. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  89490. + volatile uint32_t glpmcfg;
  89491. + /** Global PowerDn Register <i>Offset: 058h</i> */
  89492. + volatile uint32_t gpwrdn;
  89493. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  89494. + volatile uint32_t gdfifocfg;
  89495. + /** ADP Control Register <i>Offset: 060h</i> */
  89496. + volatile uint32_t adpctl;
  89497. + /** Reserved <i>Offset: 064h-0FFh</i> */
  89498. + volatile uint32_t reserved39[39];
  89499. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  89500. + volatile uint32_t hptxfsiz;
  89501. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  89502. + otherwise Device Transmit FIFO#n Register.
  89503. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  89504. + volatile uint32_t dtxfsiz[15];
  89505. +} dwc_otg_core_global_regs_t;
  89506. +
  89507. +/**
  89508. + * This union represents the bit fields of the Core OTG Control
  89509. + * and Status Register (GOTGCTL). Set the bits using the bit
  89510. + * fields then write the <i>d32</i> value to the register.
  89511. + */
  89512. +typedef union gotgctl_data {
  89513. + /** raw register data */
  89514. + uint32_t d32;
  89515. + /** register bits */
  89516. + struct {
  89517. + unsigned sesreqscs:1;
  89518. + unsigned sesreq:1;
  89519. + unsigned vbvalidoven:1;
  89520. + unsigned vbvalidovval:1;
  89521. + unsigned avalidoven:1;
  89522. + unsigned avalidovval:1;
  89523. + unsigned bvalidoven:1;
  89524. + unsigned bvalidovval:1;
  89525. + unsigned hstnegscs:1;
  89526. + unsigned hnpreq:1;
  89527. + unsigned hstsethnpen:1;
  89528. + unsigned devhnpen:1;
  89529. + unsigned reserved12_15:4;
  89530. + unsigned conidsts:1;
  89531. + unsigned dbnctime:1;
  89532. + unsigned asesvld:1;
  89533. + unsigned bsesvld:1;
  89534. + unsigned otgver:1;
  89535. + unsigned reserved1:1;
  89536. + unsigned multvalidbc:5;
  89537. + unsigned chirpen:1;
  89538. + unsigned reserved28_31:4;
  89539. + } b;
  89540. +} gotgctl_data_t;
  89541. +
  89542. +/**
  89543. + * This union represents the bit fields of the Core OTG Interrupt Register
  89544. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  89545. + * value to the register.
  89546. + */
  89547. +typedef union gotgint_data {
  89548. + /** raw register data */
  89549. + uint32_t d32;
  89550. + /** register bits */
  89551. + struct {
  89552. + /** Current Mode */
  89553. + unsigned reserved0_1:2;
  89554. +
  89555. + /** Session End Detected */
  89556. + unsigned sesenddet:1;
  89557. +
  89558. + unsigned reserved3_7:5;
  89559. +
  89560. + /** Session Request Success Status Change */
  89561. + unsigned sesreqsucstschng:1;
  89562. + /** Host Negotiation Success Status Change */
  89563. + unsigned hstnegsucstschng:1;
  89564. +
  89565. + unsigned reserved10_16:7;
  89566. +
  89567. + /** Host Negotiation Detected */
  89568. + unsigned hstnegdet:1;
  89569. + /** A-Device Timeout Change */
  89570. + unsigned adevtoutchng:1;
  89571. + /** Debounce Done */
  89572. + unsigned debdone:1;
  89573. + /** Multi-Valued input changed */
  89574. + unsigned mvic:1;
  89575. +
  89576. + unsigned reserved31_21:11;
  89577. +
  89578. + } b;
  89579. +} gotgint_data_t;
  89580. +
  89581. +/**
  89582. + * This union represents the bit fields of the Core AHB Configuration
  89583. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  89584. + * write the <i>d32</i> value to the register.
  89585. + */
  89586. +typedef union gahbcfg_data {
  89587. + /** raw register data */
  89588. + uint32_t d32;
  89589. + /** register bits */
  89590. + struct {
  89591. + unsigned glblintrmsk:1;
  89592. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  89593. +
  89594. + unsigned hburstlen:4;
  89595. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  89596. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  89597. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  89598. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  89599. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  89600. +
  89601. + unsigned dmaenable:1;
  89602. +#define DWC_GAHBCFG_DMAENABLE 1
  89603. + unsigned reserved:1;
  89604. + unsigned nptxfemplvl_txfemplvl:1;
  89605. + unsigned ptxfemplvl:1;
  89606. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  89607. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  89608. + unsigned reserved9_20:12;
  89609. + unsigned remmemsupp:1;
  89610. + unsigned notialldmawrit:1;
  89611. + unsigned ahbsingle:1;
  89612. + unsigned reserved24_31:8;
  89613. + } b;
  89614. +} gahbcfg_data_t;
  89615. +
  89616. +/**
  89617. + * This union represents the bit fields of the Core USB Configuration
  89618. + * Register (GUSBCFG). Set the bits using the bit fields then write
  89619. + * the <i>d32</i> value to the register.
  89620. + */
  89621. +typedef union gusbcfg_data {
  89622. + /** raw register data */
  89623. + uint32_t d32;
  89624. + /** register bits */
  89625. + struct {
  89626. + unsigned toutcal:3;
  89627. + unsigned phyif:1;
  89628. + unsigned ulpi_utmi_sel:1;
  89629. + unsigned fsintf:1;
  89630. + unsigned physel:1;
  89631. + unsigned ddrsel:1;
  89632. + unsigned srpcap:1;
  89633. + unsigned hnpcap:1;
  89634. + unsigned usbtrdtim:4;
  89635. + unsigned reserved1:1;
  89636. + unsigned phylpwrclksel:1;
  89637. + unsigned otgutmifssel:1;
  89638. + unsigned ulpi_fsls:1;
  89639. + unsigned ulpi_auto_res:1;
  89640. + unsigned ulpi_clk_sus_m:1;
  89641. + unsigned ulpi_ext_vbus_drv:1;
  89642. + unsigned ulpi_int_vbus_indicator:1;
  89643. + unsigned term_sel_dl_pulse:1;
  89644. + unsigned indicator_complement:1;
  89645. + unsigned indicator_pass_through:1;
  89646. + unsigned ulpi_int_prot_dis:1;
  89647. + unsigned ic_usb_cap:1;
  89648. + unsigned ic_traffic_pull_remove:1;
  89649. + unsigned tx_end_delay:1;
  89650. + unsigned force_host_mode:1;
  89651. + unsigned force_dev_mode:1;
  89652. + unsigned reserved31:1;
  89653. + } b;
  89654. +} gusbcfg_data_t;
  89655. +
  89656. +/**
  89657. + * This union represents the bit fields of the Core Reset Register
  89658. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  89659. + * <i>d32</i> value to the register.
  89660. + */
  89661. +typedef union grstctl_data {
  89662. + /** raw register data */
  89663. + uint32_t d32;
  89664. + /** register bits */
  89665. + struct {
  89666. + /** Core Soft Reset (CSftRst) (Device and Host)
  89667. + *
  89668. + * The application can flush the control logic in the
  89669. + * entire core using this bit. This bit resets the
  89670. + * pipelines in the AHB Clock domain as well as the
  89671. + * PHY Clock domain.
  89672. + *
  89673. + * The state machines are reset to an IDLE state, the
  89674. + * control bits in the CSRs are cleared, all the
  89675. + * transmit FIFOs and the receive FIFO are flushed.
  89676. + *
  89677. + * The status mask bits that control the generation of
  89678. + * the interrupt, are cleared, to clear the
  89679. + * interrupt. The interrupt status bits are not
  89680. + * cleared, so the application can get the status of
  89681. + * any events that occurred in the core after it has
  89682. + * set this bit.
  89683. + *
  89684. + * Any transactions on the AHB are terminated as soon
  89685. + * as possible following the protocol. Any
  89686. + * transactions on the USB are terminated immediately.
  89687. + *
  89688. + * The configuration settings in the CSRs are
  89689. + * unchanged, so the software doesn't have to
  89690. + * reprogram these registers (Device
  89691. + * Configuration/Host Configuration/Core System
  89692. + * Configuration/Core PHY Configuration).
  89693. + *
  89694. + * The application can write to this bit, any time it
  89695. + * wants to reset the core. This is a self clearing
  89696. + * bit and the core clears this bit after all the
  89697. + * necessary logic is reset in the core, which may
  89698. + * take several clocks, depending on the current state
  89699. + * of the core.
  89700. + */
  89701. + unsigned csftrst:1;
  89702. + /** Hclk Soft Reset
  89703. + *
  89704. + * The application uses this bit to reset the control logic in
  89705. + * the AHB clock domain. Only AHB clock domain pipelines are
  89706. + * reset.
  89707. + */
  89708. + unsigned hsftrst:1;
  89709. + /** Host Frame Counter Reset (Host Only)<br>
  89710. + *
  89711. + * The application can reset the (micro)frame number
  89712. + * counter inside the core, using this bit. When the
  89713. + * (micro)frame counter is reset, the subsequent SOF
  89714. + * sent out by the core, will have a (micro)frame
  89715. + * number of 0.
  89716. + */
  89717. + unsigned hstfrm:1;
  89718. + /** In Token Sequence Learning Queue Flush
  89719. + * (INTknQFlsh) (Device Only)
  89720. + */
  89721. + unsigned intknqflsh:1;
  89722. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  89723. + *
  89724. + * The application can flush the entire Receive FIFO
  89725. + * using this bit. The application must first
  89726. + * ensure that the core is not in the middle of a
  89727. + * transaction. The application should write into
  89728. + * this bit, only after making sure that neither the
  89729. + * DMA engine is reading from the RxFIFO nor the MAC
  89730. + * is writing the data in to the FIFO. The
  89731. + * application should wait until the bit is cleared
  89732. + * before performing any other operations. This bit
  89733. + * will takes 8 clocks (slowest of PHY or AHB clock)
  89734. + * to clear.
  89735. + */
  89736. + unsigned rxfflsh:1;
  89737. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  89738. + *
  89739. + * This bit is used to selectively flush a single or
  89740. + * all transmit FIFOs. The application must first
  89741. + * ensure that the core is not in the middle of a
  89742. + * transaction. The application should write into
  89743. + * this bit, only after making sure that neither the
  89744. + * DMA engine is writing into the TxFIFO nor the MAC
  89745. + * is reading the data out of the FIFO. The
  89746. + * application should wait until the core clears this
  89747. + * bit, before performing any operations. This bit
  89748. + * will takes 8 clocks (slowest of PHY or AHB clock)
  89749. + * to clear.
  89750. + */
  89751. + unsigned txfflsh:1;
  89752. +
  89753. + /** TxFIFO Number (TxFNum) (Device and Host).
  89754. + *
  89755. + * This is the FIFO number which needs to be flushed,
  89756. + * using the TxFIFO Flush bit. This field should not
  89757. + * be changed until the TxFIFO Flush bit is cleared by
  89758. + * the core.
  89759. + * - 0x0 : Non Periodic TxFIFO Flush
  89760. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  89761. + * or Periodic TxFIFO in host mode
  89762. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  89763. + * - ...
  89764. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  89765. + * - 0x10: Flush all the Transmit NonPeriodic and
  89766. + * Transmit Periodic FIFOs in the core
  89767. + */
  89768. + unsigned txfnum:5;
  89769. + /** Reserved */
  89770. + unsigned reserved11_29:19;
  89771. + /** DMA Request Signal. Indicated DMA request is in
  89772. + * probress. Used for debug purpose. */
  89773. + unsigned dmareq:1;
  89774. + /** AHB Master Idle. Indicates the AHB Master State
  89775. + * Machine is in IDLE condition. */
  89776. + unsigned ahbidle:1;
  89777. + } b;
  89778. +} grstctl_t;
  89779. +
  89780. +/**
  89781. + * This union represents the bit fields of the Core Interrupt Mask
  89782. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  89783. + * write the <i>d32</i> value to the register.
  89784. + */
  89785. +typedef union gintmsk_data {
  89786. + /** raw register data */
  89787. + uint32_t d32;
  89788. + /** register bits */
  89789. + struct {
  89790. + unsigned reserved0:1;
  89791. + unsigned modemismatch:1;
  89792. + unsigned otgintr:1;
  89793. + unsigned sofintr:1;
  89794. + unsigned rxstsqlvl:1;
  89795. + unsigned nptxfempty:1;
  89796. + unsigned ginnakeff:1;
  89797. + unsigned goutnakeff:1;
  89798. + unsigned ulpickint:1;
  89799. + unsigned i2cintr:1;
  89800. + unsigned erlysuspend:1;
  89801. + unsigned usbsuspend:1;
  89802. + unsigned usbreset:1;
  89803. + unsigned enumdone:1;
  89804. + unsigned isooutdrop:1;
  89805. + unsigned eopframe:1;
  89806. + unsigned restoredone:1;
  89807. + unsigned epmismatch:1;
  89808. + unsigned inepintr:1;
  89809. + unsigned outepintr:1;
  89810. + unsigned incomplisoin:1;
  89811. + unsigned incomplisoout:1;
  89812. + unsigned fetsusp:1;
  89813. + unsigned resetdet:1;
  89814. + unsigned portintr:1;
  89815. + unsigned hcintr:1;
  89816. + unsigned ptxfempty:1;
  89817. + unsigned lpmtranrcvd:1;
  89818. + unsigned conidstschng:1;
  89819. + unsigned disconnect:1;
  89820. + unsigned sessreqintr:1;
  89821. + unsigned wkupintr:1;
  89822. + } b;
  89823. +} gintmsk_data_t;
  89824. +/**
  89825. + * This union represents the bit fields of the Core Interrupt Register
  89826. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  89827. + * <i>d32</i> value to the register.
  89828. + */
  89829. +typedef union gintsts_data {
  89830. + /** raw register data */
  89831. + uint32_t d32;
  89832. +#define DWC_SOF_INTR_MASK 0x0008
  89833. + /** register bits */
  89834. + struct {
  89835. +#define DWC_HOST_MODE 1
  89836. + unsigned curmode:1;
  89837. + unsigned modemismatch:1;
  89838. + unsigned otgintr:1;
  89839. + unsigned sofintr:1;
  89840. + unsigned rxstsqlvl:1;
  89841. + unsigned nptxfempty:1;
  89842. + unsigned ginnakeff:1;
  89843. + unsigned goutnakeff:1;
  89844. + unsigned ulpickint:1;
  89845. + unsigned i2cintr:1;
  89846. + unsigned erlysuspend:1;
  89847. + unsigned usbsuspend:1;
  89848. + unsigned usbreset:1;
  89849. + unsigned enumdone:1;
  89850. + unsigned isooutdrop:1;
  89851. + unsigned eopframe:1;
  89852. + unsigned restoredone:1;
  89853. + unsigned epmismatch:1;
  89854. + unsigned inepint:1;
  89855. + unsigned outepintr:1;
  89856. + unsigned incomplisoin:1;
  89857. + unsigned incomplisoout:1;
  89858. + unsigned fetsusp:1;
  89859. + unsigned resetdet:1;
  89860. + unsigned portintr:1;
  89861. + unsigned hcintr:1;
  89862. + unsigned ptxfempty:1;
  89863. + unsigned lpmtranrcvd:1;
  89864. + unsigned conidstschng:1;
  89865. + unsigned disconnect:1;
  89866. + unsigned sessreqintr:1;
  89867. + unsigned wkupintr:1;
  89868. + } b;
  89869. +} gintsts_data_t;
  89870. +
  89871. +/**
  89872. + * This union represents the bit fields in the Device Receive Status Read and
  89873. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  89874. + * element then read out the bits using the <i>b</i>it elements.
  89875. + */
  89876. +typedef union device_grxsts_data {
  89877. + /** raw register data */
  89878. + uint32_t d32;
  89879. + /** register bits */
  89880. + struct {
  89881. + unsigned epnum:4;
  89882. + unsigned bcnt:11;
  89883. + unsigned dpid:2;
  89884. +
  89885. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  89886. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  89887. +
  89888. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  89889. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  89890. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  89891. + unsigned pktsts:4;
  89892. + unsigned fn:4;
  89893. + unsigned reserved25_31:7;
  89894. + } b;
  89895. +} device_grxsts_data_t;
  89896. +
  89897. +/**
  89898. + * This union represents the bit fields in the Host Receive Status Read and
  89899. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  89900. + * element then read out the bits using the <i>b</i>it elements.
  89901. + */
  89902. +typedef union host_grxsts_data {
  89903. + /** raw register data */
  89904. + uint32_t d32;
  89905. + /** register bits */
  89906. + struct {
  89907. + unsigned chnum:4;
  89908. + unsigned bcnt:11;
  89909. + unsigned dpid:2;
  89910. +
  89911. + unsigned pktsts:4;
  89912. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  89913. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  89914. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  89915. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  89916. +
  89917. + unsigned reserved21_31:11;
  89918. + } b;
  89919. +} host_grxsts_data_t;
  89920. +
  89921. +/**
  89922. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  89923. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  89924. + * then read out the bits using the <i>b</i>it elements.
  89925. + */
  89926. +typedef union fifosize_data {
  89927. + /** raw register data */
  89928. + uint32_t d32;
  89929. + /** register bits */
  89930. + struct {
  89931. + unsigned startaddr:16;
  89932. + unsigned depth:16;
  89933. + } b;
  89934. +} fifosize_data_t;
  89935. +
  89936. +/**
  89937. + * This union represents the bit fields in the Non-Periodic Transmit
  89938. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  89939. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  89940. + * elements.
  89941. + */
  89942. +typedef union gnptxsts_data {
  89943. + /** raw register data */
  89944. + uint32_t d32;
  89945. + /** register bits */
  89946. + struct {
  89947. + unsigned nptxfspcavail:16;
  89948. + unsigned nptxqspcavail:8;
  89949. + /** Top of the Non-Periodic Transmit Request Queue
  89950. + * - bit 24 - Terminate (Last entry for the selected
  89951. + * channel/EP)
  89952. + * - bits 26:25 - Token Type
  89953. + * - 2'b00 - IN/OUT
  89954. + * - 2'b01 - Zero Length OUT
  89955. + * - 2'b10 - PING/Complete Split
  89956. + * - 2'b11 - Channel Halt
  89957. + * - bits 30:27 - Channel/EP Number
  89958. + */
  89959. + unsigned nptxqtop_terminate:1;
  89960. + unsigned nptxqtop_token:2;
  89961. + unsigned nptxqtop_chnep:4;
  89962. + unsigned reserved:1;
  89963. + } b;
  89964. +} gnptxsts_data_t;
  89965. +
  89966. +/**
  89967. + * This union represents the bit fields in the Transmit
  89968. + * FIFO Status Register (DTXFSTS). Read the register into the
  89969. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  89970. + * elements.
  89971. + */
  89972. +typedef union dtxfsts_data {
  89973. + /** raw register data */
  89974. + uint32_t d32;
  89975. + /** register bits */
  89976. + struct {
  89977. + unsigned txfspcavail:16;
  89978. + unsigned reserved:16;
  89979. + } b;
  89980. +} dtxfsts_data_t;
  89981. +
  89982. +/**
  89983. + * This union represents the bit fields in the I2C Control Register
  89984. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  89985. + * bits using the <i>b</i>it elements.
  89986. + */
  89987. +typedef union gi2cctl_data {
  89988. + /** raw register data */
  89989. + uint32_t d32;
  89990. + /** register bits */
  89991. + struct {
  89992. + unsigned rwdata:8;
  89993. + unsigned regaddr:8;
  89994. + unsigned addr:7;
  89995. + unsigned i2cen:1;
  89996. + unsigned ack:1;
  89997. + unsigned i2csuspctl:1;
  89998. + unsigned i2cdevaddr:2;
  89999. + unsigned i2cdatse0:1;
  90000. + unsigned reserved:1;
  90001. + unsigned rw:1;
  90002. + unsigned bsydne:1;
  90003. + } b;
  90004. +} gi2cctl_data_t;
  90005. +
  90006. +/**
  90007. + * This union represents the bit fields in the PHY Vendor Control Register
  90008. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  90009. + * bits using the <i>b</i>it elements.
  90010. + */
  90011. +typedef union gpvndctl_data {
  90012. + /** raw register data */
  90013. + uint32_t d32;
  90014. + /** register bits */
  90015. + struct {
  90016. + unsigned regdata:8;
  90017. + unsigned vctrl:8;
  90018. + unsigned regaddr16_21:6;
  90019. + unsigned regwr:1;
  90020. + unsigned reserved23_24:2;
  90021. + unsigned newregreq:1;
  90022. + unsigned vstsbsy:1;
  90023. + unsigned vstsdone:1;
  90024. + unsigned reserved28_30:3;
  90025. + unsigned disulpidrvr:1;
  90026. + } b;
  90027. +} gpvndctl_data_t;
  90028. +
  90029. +/**
  90030. + * This union represents the bit fields in the General Purpose
  90031. + * Input/Output Register (GGPIO).
  90032. + * Read the register into the <i>d32</i> element then read out the
  90033. + * bits using the <i>b</i>it elements.
  90034. + */
  90035. +typedef union ggpio_data {
  90036. + /** raw register data */
  90037. + uint32_t d32;
  90038. + /** register bits */
  90039. + struct {
  90040. + unsigned gpi:16;
  90041. + unsigned gpo:16;
  90042. + } b;
  90043. +} ggpio_data_t;
  90044. +
  90045. +/**
  90046. + * This union represents the bit fields in the User ID Register
  90047. + * (GUID). Read the register into the <i>d32</i> element then read out the
  90048. + * bits using the <i>b</i>it elements.
  90049. + */
  90050. +typedef union guid_data {
  90051. + /** raw register data */
  90052. + uint32_t d32;
  90053. + /** register bits */
  90054. + struct {
  90055. + unsigned rwdata:32;
  90056. + } b;
  90057. +} guid_data_t;
  90058. +
  90059. +/**
  90060. + * This union represents the bit fields in the Synopsys ID Register
  90061. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  90062. + * bits using the <i>b</i>it elements.
  90063. + */
  90064. +typedef union gsnpsid_data {
  90065. + /** raw register data */
  90066. + uint32_t d32;
  90067. + /** register bits */
  90068. + struct {
  90069. + unsigned rwdata:32;
  90070. + } b;
  90071. +} gsnpsid_data_t;
  90072. +
  90073. +/**
  90074. + * This union represents the bit fields in the User HW Config1
  90075. + * Register. Read the register into the <i>d32</i> element then read
  90076. + * out the bits using the <i>b</i>it elements.
  90077. + */
  90078. +typedef union hwcfg1_data {
  90079. + /** raw register data */
  90080. + uint32_t d32;
  90081. + /** register bits */
  90082. + struct {
  90083. + unsigned ep_dir0:2;
  90084. + unsigned ep_dir1:2;
  90085. + unsigned ep_dir2:2;
  90086. + unsigned ep_dir3:2;
  90087. + unsigned ep_dir4:2;
  90088. + unsigned ep_dir5:2;
  90089. + unsigned ep_dir6:2;
  90090. + unsigned ep_dir7:2;
  90091. + unsigned ep_dir8:2;
  90092. + unsigned ep_dir9:2;
  90093. + unsigned ep_dir10:2;
  90094. + unsigned ep_dir11:2;
  90095. + unsigned ep_dir12:2;
  90096. + unsigned ep_dir13:2;
  90097. + unsigned ep_dir14:2;
  90098. + unsigned ep_dir15:2;
  90099. + } b;
  90100. +} hwcfg1_data_t;
  90101. +
  90102. +/**
  90103. + * This union represents the bit fields in the User HW Config2
  90104. + * Register. Read the register into the <i>d32</i> element then read
  90105. + * out the bits using the <i>b</i>it elements.
  90106. + */
  90107. +typedef union hwcfg2_data {
  90108. + /** raw register data */
  90109. + uint32_t d32;
  90110. + /** register bits */
  90111. + struct {
  90112. + /* GHWCFG2 */
  90113. + unsigned op_mode:3;
  90114. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  90115. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  90116. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  90117. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  90118. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  90119. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  90120. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  90121. +
  90122. + unsigned architecture:2;
  90123. + unsigned point2point:1;
  90124. + unsigned hs_phy_type:2;
  90125. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  90126. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  90127. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  90128. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  90129. +
  90130. + unsigned fs_phy_type:2;
  90131. + unsigned num_dev_ep:4;
  90132. + unsigned num_host_chan:4;
  90133. + unsigned perio_ep_supported:1;
  90134. + unsigned dynamic_fifo:1;
  90135. + unsigned multi_proc_int:1;
  90136. + unsigned reserved21:1;
  90137. + unsigned nonperio_tx_q_depth:2;
  90138. + unsigned host_perio_tx_q_depth:2;
  90139. + unsigned dev_token_q_depth:5;
  90140. + unsigned otg_enable_ic_usb:1;
  90141. + } b;
  90142. +} hwcfg2_data_t;
  90143. +
  90144. +/**
  90145. + * This union represents the bit fields in the User HW Config3
  90146. + * Register. Read the register into the <i>d32</i> element then read
  90147. + * out the bits using the <i>b</i>it elements.
  90148. + */
  90149. +typedef union hwcfg3_data {
  90150. + /** raw register data */
  90151. + uint32_t d32;
  90152. + /** register bits */
  90153. + struct {
  90154. + /* GHWCFG3 */
  90155. + unsigned xfer_size_cntr_width:4;
  90156. + unsigned packet_size_cntr_width:3;
  90157. + unsigned otg_func:1;
  90158. + unsigned i2c:1;
  90159. + unsigned vendor_ctrl_if:1;
  90160. + unsigned optional_features:1;
  90161. + unsigned synch_reset_type:1;
  90162. + unsigned adp_supp:1;
  90163. + unsigned otg_enable_hsic:1;
  90164. + unsigned bc_support:1;
  90165. + unsigned otg_lpm_en:1;
  90166. + unsigned dfifo_depth:16;
  90167. + } b;
  90168. +} hwcfg3_data_t;
  90169. +
  90170. +/**
  90171. + * This union represents the bit fields in the User HW Config4
  90172. + * Register. Read the register into the <i>d32</i> element then read
  90173. + * out the bits using the <i>b</i>it elements.
  90174. + */
  90175. +typedef union hwcfg4_data {
  90176. + /** raw register data */
  90177. + uint32_t d32;
  90178. + /** register bits */
  90179. + struct {
  90180. + unsigned num_dev_perio_in_ep:4;
  90181. + unsigned power_optimiz:1;
  90182. + unsigned min_ahb_freq:1;
  90183. + unsigned hiber:1;
  90184. + unsigned xhiber:1;
  90185. + unsigned reserved:6;
  90186. + unsigned utmi_phy_data_width:2;
  90187. + unsigned num_dev_mode_ctrl_ep:4;
  90188. + unsigned iddig_filt_en:1;
  90189. + unsigned vbus_valid_filt_en:1;
  90190. + unsigned a_valid_filt_en:1;
  90191. + unsigned b_valid_filt_en:1;
  90192. + unsigned session_end_filt_en:1;
  90193. + unsigned ded_fifo_en:1;
  90194. + unsigned num_in_eps:4;
  90195. + unsigned desc_dma:1;
  90196. + unsigned desc_dma_dyn:1;
  90197. + } b;
  90198. +} hwcfg4_data_t;
  90199. +
  90200. +/**
  90201. + * This union represents the bit fields of the Core LPM Configuration
  90202. + * Register (GLPMCFG). Set the bits using bit fields then write
  90203. + * the <i>d32</i> value to the register.
  90204. + */
  90205. +typedef union glpmctl_data {
  90206. + /** raw register data */
  90207. + uint32_t d32;
  90208. + /** register bits */
  90209. + struct {
  90210. + /** LPM-Capable (LPMCap) (Device and Host)
  90211. + * The application uses this bit to control
  90212. + * the DWC_otg core LPM capabilities.
  90213. + */
  90214. + unsigned lpm_cap_en:1;
  90215. + /** LPM response programmed by application (AppL1Res) (Device)
  90216. + * Handshake response to LPM token pre-programmed
  90217. + * by device application software.
  90218. + */
  90219. + unsigned appl_resp:1;
  90220. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  90221. + * In Host mode this field indicates the value of HIRD
  90222. + * to be sent in an LPM transaction.
  90223. + * In Device mode this field is updated with the
  90224. + * Received LPM Token HIRD bmAttribute
  90225. + * when an ACK/NYET/STALL response is sent
  90226. + * to an LPM transaction.
  90227. + */
  90228. + unsigned hird:4;
  90229. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  90230. + * In Host mode this bit indicates the value of remote
  90231. + * wake up to be sent in wIndex field of LPM transaction.
  90232. + * In Device mode this field is updated with the
  90233. + * Received LPM Token bRemoteWake bmAttribute
  90234. + * when an ACK/NYET/STALL response is sent
  90235. + * to an LPM transaction.
  90236. + */
  90237. + unsigned rem_wkup_en:1;
  90238. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  90239. + * The application uses this bit to control
  90240. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  90241. + */
  90242. + unsigned en_utmi_sleep:1;
  90243. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  90244. + */
  90245. + unsigned hird_thres:5;
  90246. + /** LPM Response (CoreL1Res) (Device and Host)
  90247. + * In Host mode this bit contains handsake response to
  90248. + * LPM transaction.
  90249. + * In Device mode the response of the core to
  90250. + * LPM transaction received is reflected in these two bits.
  90251. + - 0x0 : ERROR (No handshake response)
  90252. + - 0x1 : STALL
  90253. + - 0x2 : NYET
  90254. + - 0x3 : ACK
  90255. + */
  90256. + unsigned lpm_resp:2;
  90257. + /** Port Sleep Status (SlpSts) (Device and Host)
  90258. + * This bit is set as long as a Sleep condition
  90259. + * is present on the USB bus.
  90260. + */
  90261. + unsigned prt_sleep_sts:1;
  90262. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  90263. + * Indicates that the application or host
  90264. + * can start resume from Sleep state.
  90265. + */
  90266. + unsigned sleep_state_resumeok:1;
  90267. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  90268. + * The channel number on which the LPM transaction
  90269. + * has to be applied while sending
  90270. + * an LPM transaction to the local device.
  90271. + */
  90272. + unsigned lpm_chan_index:4;
  90273. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  90274. + * Number host retries that would be performed
  90275. + * if the device response was not valid response.
  90276. + */
  90277. + unsigned retry_count:3;
  90278. + /** Send LPM Transaction (SndLPM) (Host)
  90279. + * When set by application software,
  90280. + * an LPM transaction containing two tokens
  90281. + * is sent.
  90282. + */
  90283. + unsigned send_lpm:1;
  90284. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  90285. + * Number of LPM Host Retries still remaining
  90286. + * to be transmitted for the current LPM sequence
  90287. + */
  90288. + unsigned retry_count_sts:3;
  90289. + unsigned reserved28_29:2;
  90290. + /** In host mode once this bit is set, the host
  90291. + * configures to drive the HSIC Idle state on the bus.
  90292. + * It then waits for the device to initiate the Connect sequence.
  90293. + * In device mode once this bit is set, the device waits for
  90294. + * the HSIC Idle line state on the bus. Upon receving the Idle
  90295. + * line state, it initiates the HSIC Connect sequence.
  90296. + */
  90297. + unsigned hsic_connect:1;
  90298. + /** This bit overrides and functionally inverts
  90299. + * the if_select_hsic input port signal.
  90300. + */
  90301. + unsigned inv_sel_hsic:1;
  90302. + } b;
  90303. +} glpmcfg_data_t;
  90304. +
  90305. +/**
  90306. + * This union represents the bit fields of the Core ADP Timer, Control and
  90307. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  90308. + * the <i>d32</i> value to the register.
  90309. + */
  90310. +typedef union adpctl_data {
  90311. + /** raw register data */
  90312. + uint32_t d32;
  90313. + /** register bits */
  90314. + struct {
  90315. + /** Probe Discharge (PRB_DSCHG)
  90316. + * These bits set the times for TADP_DSCHG.
  90317. + * These bits are defined as follows:
  90318. + * 2'b00 - 4 msec
  90319. + * 2'b01 - 8 msec
  90320. + * 2'b10 - 16 msec
  90321. + * 2'b11 - 32 msec
  90322. + */
  90323. + unsigned prb_dschg:2;
  90324. + /** Probe Delta (PRB_DELTA)
  90325. + * These bits set the resolution for RTIM value.
  90326. + * The bits are defined in units of 32 kHz clock cycles as follows:
  90327. + * 2'b00 - 1 cycles
  90328. + * 2'b01 - 2 cycles
  90329. + * 2'b10 - 3 cycles
  90330. + * 2'b11 - 4 cycles
  90331. + * For example if this value is chosen to 2'b01, it means that RTIM
  90332. + * increments for every 3(three) 32Khz clock cycles.
  90333. + */
  90334. + unsigned prb_delta:2;
  90335. + /** Probe Period (PRB_PER)
  90336. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  90337. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  90338. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  90339. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  90340. + * 2'b11 - Reserved
  90341. + */
  90342. + unsigned prb_per:2;
  90343. + /** These bits capture the latest time it took for VBUS to ramp from
  90344. + * VADP_SINK to VADP_PRB.
  90345. + * 0x000 - 1 cycles
  90346. + * 0x001 - 2 cycles
  90347. + * 0x002 - 3 cycles
  90348. + * etc
  90349. + * 0x7FF - 2048 cycles
  90350. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  90351. + */
  90352. + unsigned rtim:11;
  90353. + /** Enable Probe (EnaPrb)
  90354. + * When programmed to 1'b1, the core performs a probe operation.
  90355. + * This bit is valid only if OTG_Ver = 1'b1.
  90356. + */
  90357. + unsigned enaprb:1;
  90358. + /** Enable Sense (EnaSns)
  90359. + * When programmed to 1'b1, the core performs a Sense operation.
  90360. + * This bit is valid only if OTG_Ver = 1'b1.
  90361. + */
  90362. + unsigned enasns:1;
  90363. + /** ADP Reset (ADPRes)
  90364. + * When set, ADP controller is reset.
  90365. + * This bit is valid only if OTG_Ver = 1'b1.
  90366. + */
  90367. + unsigned adpres:1;
  90368. + /** ADP Enable (ADPEn)
  90369. + * When set, the core performs either ADP probing or sensing
  90370. + * based on EnaPrb or EnaSns.
  90371. + * This bit is valid only if OTG_Ver = 1'b1.
  90372. + */
  90373. + unsigned adpen:1;
  90374. + /** ADP Probe Interrupt (ADP_PRB_INT)
  90375. + * When this bit is set, it means that the VBUS
  90376. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  90377. + * This bit is valid only if OTG_Ver = 1'b1.
  90378. + */
  90379. + unsigned adp_prb_int:1;
  90380. + /**
  90381. + * ADP Sense Interrupt (ADP_SNS_INT)
  90382. + * When this bit is set, it means that the VBUS voltage is greater than
  90383. + * VADP_SNS value or VADP_SNS is reached.
  90384. + * This bit is valid only if OTG_Ver = 1'b1.
  90385. + */
  90386. + unsigned adp_sns_int:1;
  90387. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  90388. + * This bit is relevant only for an ADP probe.
  90389. + * When this bit is set, it means that the ramp time has
  90390. + * completed ie ADPCTL.RTIM has reached its terminal value
  90391. + * of 0x7FF. This is a debug feature that allows software
  90392. + * to read the ramp time after each cycle.
  90393. + * This bit is valid only if OTG_Ver = 1'b1.
  90394. + */
  90395. + unsigned adp_tmout_int:1;
  90396. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  90397. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  90398. + * This bit is valid only if OTG_Ver = 1'b1.
  90399. + */
  90400. + unsigned adp_prb_int_msk:1;
  90401. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  90402. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  90403. + * This bit is valid only if OTG_Ver = 1'b1.
  90404. + */
  90405. + unsigned adp_sns_int_msk:1;
  90406. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  90407. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  90408. + * This bit is valid only if OTG_Ver = 1'b1.
  90409. + */
  90410. + unsigned adp_tmout_int_msk:1;
  90411. + /** Access Request
  90412. + * 2'b00 - Read/Write Valid (updated by the core)
  90413. + * 2'b01 - Read
  90414. + * 2'b00 - Write
  90415. + * 2'b00 - Reserved
  90416. + */
  90417. + unsigned ar:2;
  90418. + /** Reserved */
  90419. + unsigned reserved29_31:3;
  90420. + } b;
  90421. +} adpctl_data_t;
  90422. +
  90423. +////////////////////////////////////////////
  90424. +// Device Registers
  90425. +/**
  90426. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  90427. + *
  90428. + * The following structures define the size and relative field offsets
  90429. + * for the Device Mode Registers.
  90430. + *
  90431. + * <i>These registers are visible only in Device mode and must not be
  90432. + * accessed in Host mode, as the results are unknown.</i>
  90433. + */
  90434. +typedef struct dwc_otg_dev_global_regs {
  90435. + /** Device Configuration Register. <i>Offset 800h</i> */
  90436. + volatile uint32_t dcfg;
  90437. + /** Device Control Register. <i>Offset: 804h</i> */
  90438. + volatile uint32_t dctl;
  90439. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  90440. + volatile uint32_t dsts;
  90441. + /** Reserved. <i>Offset: 80Ch</i> */
  90442. + uint32_t unused;
  90443. + /** Device IN Endpoint Common Interrupt Mask
  90444. + * Register. <i>Offset: 810h</i> */
  90445. + volatile uint32_t diepmsk;
  90446. + /** Device OUT Endpoint Common Interrupt Mask
  90447. + * Register. <i>Offset: 814h</i> */
  90448. + volatile uint32_t doepmsk;
  90449. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  90450. + volatile uint32_t daint;
  90451. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  90452. + * 81Ch</i> */
  90453. + volatile uint32_t daintmsk;
  90454. + /** Device IN Token Queue Read Register-1 (Read Only).
  90455. + * <i>Offset: 820h</i> */
  90456. + volatile uint32_t dtknqr1;
  90457. + /** Device IN Token Queue Read Register-2 (Read Only).
  90458. + * <i>Offset: 824h</i> */
  90459. + volatile uint32_t dtknqr2;
  90460. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  90461. + volatile uint32_t dvbusdis;
  90462. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  90463. + volatile uint32_t dvbuspulse;
  90464. + /** Device IN Token Queue Read Register-3 (Read Only). /
  90465. + * Device Thresholding control register (Read/Write)
  90466. + * <i>Offset: 830h</i> */
  90467. + volatile uint32_t dtknqr3_dthrctl;
  90468. + /** Device IN Token Queue Read Register-4 (Read Only). /
  90469. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  90470. + * <i>Offset: 834h</i> */
  90471. + volatile uint32_t dtknqr4_fifoemptymsk;
  90472. + /** Device Each Endpoint Interrupt Register (Read Only). /
  90473. + * <i>Offset: 838h</i> */
  90474. + volatile uint32_t deachint;
  90475. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  90476. + * <i>Offset: 83Ch</i> */
  90477. + volatile uint32_t deachintmsk;
  90478. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  90479. + * <i>Offset: 840h</i> */
  90480. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  90481. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  90482. + * <i>Offset: 880h</i> */
  90483. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  90484. +} dwc_otg_device_global_regs_t;
  90485. +
  90486. +/**
  90487. + * This union represents the bit fields in the Device Configuration
  90488. + * Register. Read the register into the <i>d32</i> member then
  90489. + * set/clear the bits using the <i>b</i>it elements. Write the
  90490. + * <i>d32</i> member to the dcfg register.
  90491. + */
  90492. +typedef union dcfg_data {
  90493. + /** raw register data */
  90494. + uint32_t d32;
  90495. + /** register bits */
  90496. + struct {
  90497. + /** Device Speed */
  90498. + unsigned devspd:2;
  90499. + /** Non Zero Length Status OUT Handshake */
  90500. + unsigned nzstsouthshk:1;
  90501. +#define DWC_DCFG_SEND_STALL 1
  90502. +
  90503. + unsigned ena32khzs:1;
  90504. + /** Device Addresses */
  90505. + unsigned devaddr:7;
  90506. + /** Periodic Frame Interval */
  90507. + unsigned perfrint:2;
  90508. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  90509. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  90510. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  90511. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  90512. +
  90513. + /** Enable Device OUT NAK for bulk in DDMA mode */
  90514. + unsigned endevoutnak:1;
  90515. +
  90516. + unsigned reserved14_17:4;
  90517. + /** In Endpoint Mis-match count */
  90518. + unsigned epmscnt:5;
  90519. + /** Enable Descriptor DMA in Device mode */
  90520. + unsigned descdma:1;
  90521. + unsigned perschintvl:2;
  90522. + unsigned resvalid:6;
  90523. + } b;
  90524. +} dcfg_data_t;
  90525. +
  90526. +/**
  90527. + * This union represents the bit fields in the Device Control
  90528. + * Register. Read the register into the <i>d32</i> member then
  90529. + * set/clear the bits using the <i>b</i>it elements.
  90530. + */
  90531. +typedef union dctl_data {
  90532. + /** raw register data */
  90533. + uint32_t d32;
  90534. + /** register bits */
  90535. + struct {
  90536. + /** Remote Wakeup */
  90537. + unsigned rmtwkupsig:1;
  90538. + /** Soft Disconnect */
  90539. + unsigned sftdiscon:1;
  90540. + /** Global Non-Periodic IN NAK Status */
  90541. + unsigned gnpinnaksts:1;
  90542. + /** Global OUT NAK Status */
  90543. + unsigned goutnaksts:1;
  90544. + /** Test Control */
  90545. + unsigned tstctl:3;
  90546. + /** Set Global Non-Periodic IN NAK */
  90547. + unsigned sgnpinnak:1;
  90548. + /** Clear Global Non-Periodic IN NAK */
  90549. + unsigned cgnpinnak:1;
  90550. + /** Set Global OUT NAK */
  90551. + unsigned sgoutnak:1;
  90552. + /** Clear Global OUT NAK */
  90553. + unsigned cgoutnak:1;
  90554. + /** Power-On Programming Done */
  90555. + unsigned pwronprgdone:1;
  90556. + /** Reserved */
  90557. + unsigned reserved:1;
  90558. + /** Global Multi Count */
  90559. + unsigned gmc:2;
  90560. + /** Ignore Frame Number for ISOC EPs */
  90561. + unsigned ifrmnum:1;
  90562. + /** NAK on Babble */
  90563. + unsigned nakonbble:1;
  90564. + /** Enable Continue on BNA */
  90565. + unsigned encontonbna:1;
  90566. +
  90567. + unsigned reserved18_31:14;
  90568. + } b;
  90569. +} dctl_data_t;
  90570. +
  90571. +/**
  90572. + * This union represents the bit fields in the Device Status
  90573. + * Register. Read the register into the <i>d32</i> member then
  90574. + * set/clear the bits using the <i>b</i>it elements.
  90575. + */
  90576. +typedef union dsts_data {
  90577. + /** raw register data */
  90578. + uint32_t d32;
  90579. + /** register bits */
  90580. + struct {
  90581. + /** Suspend Status */
  90582. + unsigned suspsts:1;
  90583. + /** Enumerated Speed */
  90584. + unsigned enumspd:2;
  90585. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  90586. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  90587. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  90588. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  90589. + /** Erratic Error */
  90590. + unsigned errticerr:1;
  90591. + unsigned reserved4_7:4;
  90592. + /** Frame or Microframe Number of the received SOF */
  90593. + unsigned soffn:14;
  90594. + unsigned reserved22_31:10;
  90595. + } b;
  90596. +} dsts_data_t;
  90597. +
  90598. +/**
  90599. + * This union represents the bit fields in the Device IN EP Interrupt
  90600. + * Register and the Device IN EP Common Mask Register.
  90601. + *
  90602. + * - Read the register into the <i>d32</i> member then set/clear the
  90603. + * bits using the <i>b</i>it elements.
  90604. + */
  90605. +typedef union diepint_data {
  90606. + /** raw register data */
  90607. + uint32_t d32;
  90608. + /** register bits */
  90609. + struct {
  90610. + /** Transfer complete mask */
  90611. + unsigned xfercompl:1;
  90612. + /** Endpoint disable mask */
  90613. + unsigned epdisabled:1;
  90614. + /** AHB Error mask */
  90615. + unsigned ahberr:1;
  90616. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  90617. + unsigned timeout:1;
  90618. + /** IN Token received with TxF Empty mask */
  90619. + unsigned intktxfemp:1;
  90620. + /** IN Token Received with EP mismatch mask */
  90621. + unsigned intknepmis:1;
  90622. + /** IN Endpoint NAK Effective mask */
  90623. + unsigned inepnakeff:1;
  90624. + /** Reserved */
  90625. + unsigned emptyintr:1;
  90626. +
  90627. + unsigned txfifoundrn:1;
  90628. +
  90629. + /** BNA Interrupt mask */
  90630. + unsigned bna:1;
  90631. +
  90632. + unsigned reserved10_12:3;
  90633. + /** BNA Interrupt mask */
  90634. + unsigned nak:1;
  90635. +
  90636. + unsigned reserved14_31:18;
  90637. + } b;
  90638. +} diepint_data_t;
  90639. +
  90640. +/**
  90641. + * This union represents the bit fields in the Device IN EP
  90642. + * Common/Dedicated Interrupt Mask Register.
  90643. + */
  90644. +typedef union diepint_data diepmsk_data_t;
  90645. +
  90646. +/**
  90647. + * This union represents the bit fields in the Device OUT EP Interrupt
  90648. + * Registerand Device OUT EP Common Interrupt Mask Register.
  90649. + *
  90650. + * - Read the register into the <i>d32</i> member then set/clear the
  90651. + * bits using the <i>b</i>it elements.
  90652. + */
  90653. +typedef union doepint_data {
  90654. + /** raw register data */
  90655. + uint32_t d32;
  90656. + /** register bits */
  90657. + struct {
  90658. + /** Transfer complete */
  90659. + unsigned xfercompl:1;
  90660. + /** Endpoint disable */
  90661. + unsigned epdisabled:1;
  90662. + /** AHB Error */
  90663. + unsigned ahberr:1;
  90664. + /** Setup Phase Done (contorl EPs) */
  90665. + unsigned setup:1;
  90666. + /** OUT Token Received when Endpoint Disabled */
  90667. + unsigned outtknepdis:1;
  90668. +
  90669. + unsigned stsphsercvd:1;
  90670. + /** Back-to-Back SETUP Packets Received */
  90671. + unsigned back2backsetup:1;
  90672. +
  90673. + unsigned reserved7:1;
  90674. + /** OUT packet Error */
  90675. + unsigned outpkterr:1;
  90676. + /** BNA Interrupt */
  90677. + unsigned bna:1;
  90678. +
  90679. + unsigned reserved10:1;
  90680. + /** Packet Drop Status */
  90681. + unsigned pktdrpsts:1;
  90682. + /** Babble Interrupt */
  90683. + unsigned babble:1;
  90684. + /** NAK Interrupt */
  90685. + unsigned nak:1;
  90686. + /** NYET Interrupt */
  90687. + unsigned nyet:1;
  90688. + /** Bit indicating setup packet received */
  90689. + unsigned sr:1;
  90690. +
  90691. + unsigned reserved16_31:16;
  90692. + } b;
  90693. +} doepint_data_t;
  90694. +
  90695. +/**
  90696. + * This union represents the bit fields in the Device OUT EP
  90697. + * Common/Dedicated Interrupt Mask Register.
  90698. + */
  90699. +typedef union doepint_data doepmsk_data_t;
  90700. +
  90701. +/**
  90702. + * This union represents the bit fields in the Device All EP Interrupt
  90703. + * and Mask Registers.
  90704. + * - Read the register into the <i>d32</i> member then set/clear the
  90705. + * bits using the <i>b</i>it elements.
  90706. + */
  90707. +typedef union daint_data {
  90708. + /** raw register data */
  90709. + uint32_t d32;
  90710. + /** register bits */
  90711. + struct {
  90712. + /** IN Endpoint bits */
  90713. + unsigned in:16;
  90714. + /** OUT Endpoint bits */
  90715. + unsigned out:16;
  90716. + } ep;
  90717. + struct {
  90718. + /** IN Endpoint bits */
  90719. + unsigned inep0:1;
  90720. + unsigned inep1:1;
  90721. + unsigned inep2:1;
  90722. + unsigned inep3:1;
  90723. + unsigned inep4:1;
  90724. + unsigned inep5:1;
  90725. + unsigned inep6:1;
  90726. + unsigned inep7:1;
  90727. + unsigned inep8:1;
  90728. + unsigned inep9:1;
  90729. + unsigned inep10:1;
  90730. + unsigned inep11:1;
  90731. + unsigned inep12:1;
  90732. + unsigned inep13:1;
  90733. + unsigned inep14:1;
  90734. + unsigned inep15:1;
  90735. + /** OUT Endpoint bits */
  90736. + unsigned outep0:1;
  90737. + unsigned outep1:1;
  90738. + unsigned outep2:1;
  90739. + unsigned outep3:1;
  90740. + unsigned outep4:1;
  90741. + unsigned outep5:1;
  90742. + unsigned outep6:1;
  90743. + unsigned outep7:1;
  90744. + unsigned outep8:1;
  90745. + unsigned outep9:1;
  90746. + unsigned outep10:1;
  90747. + unsigned outep11:1;
  90748. + unsigned outep12:1;
  90749. + unsigned outep13:1;
  90750. + unsigned outep14:1;
  90751. + unsigned outep15:1;
  90752. + } b;
  90753. +} daint_data_t;
  90754. +
  90755. +/**
  90756. + * This union represents the bit fields in the Device IN Token Queue
  90757. + * Read Registers.
  90758. + * - Read the register into the <i>d32</i> member.
  90759. + * - READ-ONLY Register
  90760. + */
  90761. +typedef union dtknq1_data {
  90762. + /** raw register data */
  90763. + uint32_t d32;
  90764. + /** register bits */
  90765. + struct {
  90766. + /** In Token Queue Write Pointer */
  90767. + unsigned intknwptr:5;
  90768. + /** Reserved */
  90769. + unsigned reserved05_06:2;
  90770. + /** write pointer has wrapped. */
  90771. + unsigned wrap_bit:1;
  90772. + /** EP Numbers of IN Tokens 0 ... 4 */
  90773. + unsigned epnums0_5:24;
  90774. + } b;
  90775. +} dtknq1_data_t;
  90776. +
  90777. +/**
  90778. + * This union represents Threshold control Register
  90779. + * - Read and write the register into the <i>d32</i> member.
  90780. + * - READ-WRITABLE Register
  90781. + */
  90782. +typedef union dthrctl_data {
  90783. + /** raw register data */
  90784. + uint32_t d32;
  90785. + /** register bits */
  90786. + struct {
  90787. + /** non ISO Tx Thr. Enable */
  90788. + unsigned non_iso_thr_en:1;
  90789. + /** ISO Tx Thr. Enable */
  90790. + unsigned iso_thr_en:1;
  90791. + /** Tx Thr. Length */
  90792. + unsigned tx_thr_len:9;
  90793. + /** AHB Threshold ratio */
  90794. + unsigned ahb_thr_ratio:2;
  90795. + /** Reserved */
  90796. + unsigned reserved13_15:3;
  90797. + /** Rx Thr. Enable */
  90798. + unsigned rx_thr_en:1;
  90799. + /** Rx Thr. Length */
  90800. + unsigned rx_thr_len:9;
  90801. + unsigned reserved26:1;
  90802. + /** Arbiter Parking Enable*/
  90803. + unsigned arbprken:1;
  90804. + /** Reserved */
  90805. + unsigned reserved28_31:4;
  90806. + } b;
  90807. +} dthrctl_data_t;
  90808. +
  90809. +/**
  90810. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  90811. + * 900h-AFCh</i>
  90812. + *
  90813. + * There will be one set of endpoint registers per logical endpoint
  90814. + * implemented.
  90815. + *
  90816. + * <i>These registers are visible only in Device mode and must not be
  90817. + * accessed in Host mode, as the results are unknown.</i>
  90818. + */
  90819. +typedef struct dwc_otg_dev_in_ep_regs {
  90820. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  90821. + * (ep_num * 20h) + 00h</i> */
  90822. + volatile uint32_t diepctl;
  90823. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  90824. + uint32_t reserved04;
  90825. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  90826. + * (ep_num * 20h) + 08h</i> */
  90827. + volatile uint32_t diepint;
  90828. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  90829. + uint32_t reserved0C;
  90830. + /** Device IN Endpoint Transfer Size
  90831. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  90832. + volatile uint32_t dieptsiz;
  90833. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  90834. + * (ep_num * 20h) + 14h</i> */
  90835. + volatile uint32_t diepdma;
  90836. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  90837. + * (ep_num * 20h) + 18h</i> */
  90838. + volatile uint32_t dtxfsts;
  90839. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  90840. + * (ep_num * 20h) + 1Ch</i> */
  90841. + volatile uint32_t diepdmab;
  90842. +} dwc_otg_dev_in_ep_regs_t;
  90843. +
  90844. +/**
  90845. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  90846. + * B00h-CFCh</i>
  90847. + *
  90848. + * There will be one set of endpoint registers per logical endpoint
  90849. + * implemented.
  90850. + *
  90851. + * <i>These registers are visible only in Device mode and must not be
  90852. + * accessed in Host mode, as the results are unknown.</i>
  90853. + */
  90854. +typedef struct dwc_otg_dev_out_ep_regs {
  90855. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  90856. + * (ep_num * 20h) + 00h</i> */
  90857. + volatile uint32_t doepctl;
  90858. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  90859. + uint32_t reserved04;
  90860. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  90861. + * (ep_num * 20h) + 08h</i> */
  90862. + volatile uint32_t doepint;
  90863. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  90864. + uint32_t reserved0C;
  90865. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  90866. + * B00h + (ep_num * 20h) + 10h</i> */
  90867. + volatile uint32_t doeptsiz;
  90868. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  90869. + * + (ep_num * 20h) + 14h</i> */
  90870. + volatile uint32_t doepdma;
  90871. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  90872. + uint32_t unused;
  90873. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  90874. + * + (ep_num * 20h) + 1Ch</i> */
  90875. + uint32_t doepdmab;
  90876. +} dwc_otg_dev_out_ep_regs_t;
  90877. +
  90878. +/**
  90879. + * This union represents the bit fields in the Device EP Control
  90880. + * Register. Read the register into the <i>d32</i> member then
  90881. + * set/clear the bits using the <i>b</i>it elements.
  90882. + */
  90883. +typedef union depctl_data {
  90884. + /** raw register data */
  90885. + uint32_t d32;
  90886. + /** register bits */
  90887. + struct {
  90888. + /** Maximum Packet Size
  90889. + * IN/OUT EPn
  90890. + * IN/OUT EP0 - 2 bits
  90891. + * 2'b00: 64 Bytes
  90892. + * 2'b01: 32
  90893. + * 2'b10: 16
  90894. + * 2'b11: 8 */
  90895. + unsigned mps:11;
  90896. +#define DWC_DEP0CTL_MPS_64 0
  90897. +#define DWC_DEP0CTL_MPS_32 1
  90898. +#define DWC_DEP0CTL_MPS_16 2
  90899. +#define DWC_DEP0CTL_MPS_8 3
  90900. +
  90901. + /** Next Endpoint
  90902. + * IN EPn/IN EP0
  90903. + * OUT EPn/OUT EP0 - reserved */
  90904. + unsigned nextep:4;
  90905. +
  90906. + /** USB Active Endpoint */
  90907. + unsigned usbactep:1;
  90908. +
  90909. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  90910. + * This field contains the PID of the packet going to
  90911. + * be received or transmitted on this endpoint. The
  90912. + * application should program the PID of the first
  90913. + * packet going to be received or transmitted on this
  90914. + * endpoint , after the endpoint is
  90915. + * activated. Application use the SetD1PID and
  90916. + * SetD0PID fields of this register to program either
  90917. + * D0 or D1 PID.
  90918. + *
  90919. + * The encoding for this field is
  90920. + * - 0: D0
  90921. + * - 1: D1
  90922. + */
  90923. + unsigned dpid:1;
  90924. +
  90925. + /** NAK Status */
  90926. + unsigned naksts:1;
  90927. +
  90928. + /** Endpoint Type
  90929. + * 2'b00: Control
  90930. + * 2'b01: Isochronous
  90931. + * 2'b10: Bulk
  90932. + * 2'b11: Interrupt */
  90933. + unsigned eptype:2;
  90934. +
  90935. + /** Snoop Mode
  90936. + * OUT EPn/OUT EP0
  90937. + * IN EPn/IN EP0 - reserved */
  90938. + unsigned snp:1;
  90939. +
  90940. + /** Stall Handshake */
  90941. + unsigned stall:1;
  90942. +
  90943. + /** Tx Fifo Number
  90944. + * IN EPn/IN EP0
  90945. + * OUT EPn/OUT EP0 - reserved */
  90946. + unsigned txfnum:4;
  90947. +
  90948. + /** Clear NAK */
  90949. + unsigned cnak:1;
  90950. + /** Set NAK */
  90951. + unsigned snak:1;
  90952. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  90953. + * Writing to this field sets the Endpoint DPID (DPID)
  90954. + * field in this register to DATA0. Set Even
  90955. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  90956. + * Writing to this field sets the Even/Odd
  90957. + * (micro)frame (EO_FrNum) field to even (micro)
  90958. + * frame.
  90959. + */
  90960. + unsigned setd0pid:1;
  90961. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  90962. + * Writing to this field sets the Endpoint DPID (DPID)
  90963. + * field in this register to DATA1 Set Odd
  90964. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  90965. + * Writing to this field sets the Even/Odd
  90966. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  90967. + */
  90968. + unsigned setd1pid:1;
  90969. +
  90970. + /** Endpoint Disable */
  90971. + unsigned epdis:1;
  90972. + /** Endpoint Enable */
  90973. + unsigned epena:1;
  90974. + } b;
  90975. +} depctl_data_t;
  90976. +
  90977. +/**
  90978. + * This union represents the bit fields in the Device EP Transfer
  90979. + * Size Register. Read the register into the <i>d32</i> member then
  90980. + * set/clear the bits using the <i>b</i>it elements.
  90981. + */
  90982. +typedef union deptsiz_data {
  90983. + /** raw register data */
  90984. + uint32_t d32;
  90985. + /** register bits */
  90986. + struct {
  90987. + /** Transfer size */
  90988. + unsigned xfersize:19;
  90989. +/** Max packet count for EP (pow(2,10)-1) */
  90990. +#define MAX_PKT_CNT 1023
  90991. + /** Packet Count */
  90992. + unsigned pktcnt:10;
  90993. + /** Multi Count - Periodic IN endpoints */
  90994. + unsigned mc:2;
  90995. + unsigned reserved:1;
  90996. + } b;
  90997. +} deptsiz_data_t;
  90998. +
  90999. +/**
  91000. + * This union represents the bit fields in the Device EP 0 Transfer
  91001. + * Size Register. Read the register into the <i>d32</i> member then
  91002. + * set/clear the bits using the <i>b</i>it elements.
  91003. + */
  91004. +typedef union deptsiz0_data {
  91005. + /** raw register data */
  91006. + uint32_t d32;
  91007. + /** register bits */
  91008. + struct {
  91009. + /** Transfer size */
  91010. + unsigned xfersize:7;
  91011. + /** Reserved */
  91012. + unsigned reserved7_18:12;
  91013. + /** Packet Count */
  91014. + unsigned pktcnt:2;
  91015. + /** Reserved */
  91016. + unsigned reserved21_28:8;
  91017. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  91018. + unsigned supcnt:2;
  91019. + unsigned reserved31;
  91020. + } b;
  91021. +} deptsiz0_data_t;
  91022. +
  91023. +/////////////////////////////////////////////////
  91024. +// DMA Descriptor Specific Structures
  91025. +//
  91026. +
  91027. +/** Buffer status definitions */
  91028. +
  91029. +#define BS_HOST_READY 0x0
  91030. +#define BS_DMA_BUSY 0x1
  91031. +#define BS_DMA_DONE 0x2
  91032. +#define BS_HOST_BUSY 0x3
  91033. +
  91034. +/** Receive/Transmit status definitions */
  91035. +
  91036. +#define RTS_SUCCESS 0x0
  91037. +#define RTS_BUFFLUSH 0x1
  91038. +#define RTS_RESERVED 0x2
  91039. +#define RTS_BUFERR 0x3
  91040. +
  91041. +/**
  91042. + * This union represents the bit fields in the DMA Descriptor
  91043. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  91044. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  91045. + * <i>b_iso_in</i> elements.
  91046. + */
  91047. +typedef union dev_dma_desc_sts {
  91048. + /** raw register data */
  91049. + uint32_t d32;
  91050. + /** quadlet bits */
  91051. + struct {
  91052. + /** Received number of bytes */
  91053. + unsigned bytes:16;
  91054. + /** NAK bit - only for OUT EPs */
  91055. + unsigned nak:1;
  91056. + unsigned reserved17_22:6;
  91057. + /** Multiple Transfer - only for OUT EPs */
  91058. + unsigned mtrf:1;
  91059. + /** Setup Packet received - only for OUT EPs */
  91060. + unsigned sr:1;
  91061. + /** Interrupt On Complete */
  91062. + unsigned ioc:1;
  91063. + /** Short Packet */
  91064. + unsigned sp:1;
  91065. + /** Last */
  91066. + unsigned l:1;
  91067. + /** Receive Status */
  91068. + unsigned sts:2;
  91069. + /** Buffer Status */
  91070. + unsigned bs:2;
  91071. + } b;
  91072. +
  91073. +//#ifdef DWC_EN_ISOC
  91074. + /** iso out quadlet bits */
  91075. + struct {
  91076. + /** Received number of bytes */
  91077. + unsigned rxbytes:11;
  91078. +
  91079. + unsigned reserved11:1;
  91080. + /** Frame Number */
  91081. + unsigned framenum:11;
  91082. + /** Received ISO Data PID */
  91083. + unsigned pid:2;
  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 rxsts:2;
  91092. + /** Buffer Status */
  91093. + unsigned bs:2;
  91094. + } b_iso_out;
  91095. +
  91096. + /** iso in quadlet bits */
  91097. + struct {
  91098. + /** Transmited number of bytes */
  91099. + unsigned txbytes:12;
  91100. + /** Frame Number */
  91101. + unsigned framenum:11;
  91102. + /** Transmited ISO Data PID */
  91103. + unsigned pid:2;
  91104. + /** Interrupt On Complete */
  91105. + unsigned ioc:1;
  91106. + /** Short Packet */
  91107. + unsigned sp:1;
  91108. + /** Last */
  91109. + unsigned l:1;
  91110. + /** Transmit Status */
  91111. + unsigned txsts:2;
  91112. + /** Buffer Status */
  91113. + unsigned bs:2;
  91114. + } b_iso_in;
  91115. +//#endif /* DWC_EN_ISOC */
  91116. +} dev_dma_desc_sts_t;
  91117. +
  91118. +/**
  91119. + * DMA Descriptor structure
  91120. + *
  91121. + * DMA Descriptor structure contains two quadlets:
  91122. + * Status quadlet and Data buffer pointer.
  91123. + */
  91124. +typedef struct dwc_otg_dev_dma_desc {
  91125. + /** DMA Descriptor status quadlet */
  91126. + dev_dma_desc_sts_t status;
  91127. + /** DMA Descriptor data buffer pointer */
  91128. + uint32_t buf;
  91129. +} dwc_otg_dev_dma_desc_t;
  91130. +
  91131. +/**
  91132. + * The dwc_otg_dev_if structure contains information needed to manage
  91133. + * the DWC_otg controller acting in device mode. It represents the
  91134. + * programming view of the device-specific aspects of the controller.
  91135. + */
  91136. +typedef struct dwc_otg_dev_if {
  91137. + /** Pointer to device Global registers.
  91138. + * Device Global Registers starting at offset 800h
  91139. + */
  91140. + dwc_otg_device_global_regs_t *dev_global_regs;
  91141. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  91142. +
  91143. + /**
  91144. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  91145. + */
  91146. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  91147. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  91148. +#define DWC_EP_REG_OFFSET 0x20
  91149. +
  91150. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  91151. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  91152. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  91153. +
  91154. + /* Device configuration information */
  91155. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  91156. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  91157. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  91158. +
  91159. + /** Size of periodic FIFOs (Bytes) */
  91160. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  91161. +
  91162. + /** Size of Tx FIFOs (Bytes) */
  91163. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  91164. +
  91165. + /** Thresholding enable flags and length varaiables **/
  91166. + uint16_t rx_thr_en;
  91167. + uint16_t iso_tx_thr_en;
  91168. + uint16_t non_iso_tx_thr_en;
  91169. +
  91170. + uint16_t rx_thr_length;
  91171. + uint16_t tx_thr_length;
  91172. +
  91173. + /**
  91174. + * Pointers to the DMA Descriptors for EP0 Control
  91175. + * transfers (virtual and physical)
  91176. + */
  91177. +
  91178. + /** 2 descriptors for SETUP packets */
  91179. + dwc_dma_t dma_setup_desc_addr[2];
  91180. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  91181. +
  91182. + /** Pointer to Descriptor with latest SETUP packet */
  91183. + dwc_otg_dev_dma_desc_t *psetup;
  91184. +
  91185. + /** Index of current SETUP handler descriptor */
  91186. + uint32_t setup_desc_index;
  91187. +
  91188. + /** Descriptor for Data In or Status In phases */
  91189. + dwc_dma_t dma_in_desc_addr;
  91190. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  91191. +
  91192. + /** Descriptor for Data Out or Status Out phases */
  91193. + dwc_dma_t dma_out_desc_addr;
  91194. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  91195. +
  91196. + /** Setup Packet Detected - if set clear NAK when queueing */
  91197. + uint32_t spd;
  91198. + /** Isoc ep pointer on which incomplete happens */
  91199. + void *isoc_ep;
  91200. +
  91201. +} dwc_otg_dev_if_t;
  91202. +
  91203. +/////////////////////////////////////////////////
  91204. +// Host Mode Register Structures
  91205. +//
  91206. +/**
  91207. + * The Host Global Registers structure defines the size and relative
  91208. + * field offsets for the Host Mode Global Registers. Host Global
  91209. + * Registers offsets 400h-7FFh.
  91210. +*/
  91211. +typedef struct dwc_otg_host_global_regs {
  91212. + /** Host Configuration Register. <i>Offset: 400h</i> */
  91213. + volatile uint32_t hcfg;
  91214. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  91215. + volatile uint32_t hfir;
  91216. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  91217. + volatile uint32_t hfnum;
  91218. + /** Reserved. <i>Offset: 40Ch</i> */
  91219. + uint32_t reserved40C;
  91220. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  91221. + volatile uint32_t hptxsts;
  91222. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  91223. + volatile uint32_t haint;
  91224. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  91225. + volatile uint32_t haintmsk;
  91226. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  91227. + volatile uint32_t hflbaddr;
  91228. +} dwc_otg_host_global_regs_t;
  91229. +
  91230. +/**
  91231. + * This union represents the bit fields in the Host Configuration Register.
  91232. + * Read the register into the <i>d32</i> member then set/clear the bits using
  91233. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  91234. + */
  91235. +typedef union hcfg_data {
  91236. + /** raw register data */
  91237. + uint32_t d32;
  91238. +
  91239. + /** register bits */
  91240. + struct {
  91241. + /** FS/LS Phy Clock Select */
  91242. + unsigned fslspclksel:2;
  91243. +#define DWC_HCFG_30_60_MHZ 0
  91244. +#define DWC_HCFG_48_MHZ 1
  91245. +#define DWC_HCFG_6_MHZ 2
  91246. +
  91247. + /** FS/LS Only Support */
  91248. + unsigned fslssupp:1;
  91249. + unsigned reserved3_6:4;
  91250. + /** Enable 32-KHz Suspend Mode */
  91251. + unsigned ena32khzs:1;
  91252. + /** Resume Validation Periiod */
  91253. + unsigned resvalid:8;
  91254. + unsigned reserved16_22:7;
  91255. + /** Enable Scatter/gather DMA in Host mode */
  91256. + unsigned descdma:1;
  91257. + /** Frame List Entries */
  91258. + unsigned frlisten:2;
  91259. + /** Enable Periodic Scheduling */
  91260. + unsigned perschedena:1;
  91261. + unsigned reserved27_30:4;
  91262. + unsigned modechtimen:1;
  91263. + } b;
  91264. +} hcfg_data_t;
  91265. +
  91266. +/**
  91267. + * This union represents the bit fields in the Host Frame Remaing/Number
  91268. + * Register.
  91269. + */
  91270. +typedef union hfir_data {
  91271. + /** raw register data */
  91272. + uint32_t d32;
  91273. +
  91274. + /** register bits */
  91275. + struct {
  91276. + unsigned frint:16;
  91277. + unsigned hfirrldctrl:1;
  91278. + unsigned reserved:15;
  91279. + } b;
  91280. +} hfir_data_t;
  91281. +
  91282. +/**
  91283. + * This union represents the bit fields in the Host Frame Remaing/Number
  91284. + * Register.
  91285. + */
  91286. +typedef union hfnum_data {
  91287. + /** raw register data */
  91288. + uint32_t d32;
  91289. +
  91290. + /** register bits */
  91291. + struct {
  91292. + unsigned frnum:16;
  91293. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  91294. + unsigned frrem:16;
  91295. + } b;
  91296. +} hfnum_data_t;
  91297. +
  91298. +typedef union hptxsts_data {
  91299. + /** raw register data */
  91300. + uint32_t d32;
  91301. +
  91302. + /** register bits */
  91303. + struct {
  91304. + unsigned ptxfspcavail:16;
  91305. + unsigned ptxqspcavail:8;
  91306. + /** Top of the Periodic Transmit Request Queue
  91307. + * - bit 24 - Terminate (last entry for the selected channel)
  91308. + * - bits 26:25 - Token Type
  91309. + * - 2'b00 - Zero length
  91310. + * - 2'b01 - Ping
  91311. + * - 2'b10 - Disable
  91312. + * - bits 30:27 - Channel Number
  91313. + * - bit 31 - Odd/even microframe
  91314. + */
  91315. + unsigned ptxqtop_terminate:1;
  91316. + unsigned ptxqtop_token:2;
  91317. + unsigned ptxqtop_chnum:4;
  91318. + unsigned ptxqtop_odd:1;
  91319. + } b;
  91320. +} hptxsts_data_t;
  91321. +
  91322. +/**
  91323. + * This union represents the bit fields in the Host Port Control and Status
  91324. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91325. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91326. + * hprt0 register.
  91327. + */
  91328. +typedef union hprt0_data {
  91329. + /** raw register data */
  91330. + uint32_t d32;
  91331. + /** register bits */
  91332. + struct {
  91333. + unsigned prtconnsts:1;
  91334. + unsigned prtconndet:1;
  91335. + unsigned prtena:1;
  91336. + unsigned prtenchng:1;
  91337. + unsigned prtovrcurract:1;
  91338. + unsigned prtovrcurrchng:1;
  91339. + unsigned prtres:1;
  91340. + unsigned prtsusp:1;
  91341. + unsigned prtrst:1;
  91342. + unsigned reserved9:1;
  91343. + unsigned prtlnsts:2;
  91344. + unsigned prtpwr:1;
  91345. + unsigned prttstctl:4;
  91346. + unsigned prtspd:2;
  91347. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  91348. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  91349. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  91350. + unsigned reserved19_31:13;
  91351. + } b;
  91352. +} hprt0_data_t;
  91353. +
  91354. +/**
  91355. + * This union represents the bit fields in the Host All Interrupt
  91356. + * Register.
  91357. + */
  91358. +typedef union haint_data {
  91359. + /** raw register data */
  91360. + uint32_t d32;
  91361. + /** register bits */
  91362. + struct {
  91363. + unsigned ch0:1;
  91364. + unsigned ch1:1;
  91365. + unsigned ch2:1;
  91366. + unsigned ch3:1;
  91367. + unsigned ch4:1;
  91368. + unsigned ch5:1;
  91369. + unsigned ch6:1;
  91370. + unsigned ch7:1;
  91371. + unsigned ch8:1;
  91372. + unsigned ch9:1;
  91373. + unsigned ch10:1;
  91374. + unsigned ch11:1;
  91375. + unsigned ch12:1;
  91376. + unsigned ch13:1;
  91377. + unsigned ch14:1;
  91378. + unsigned ch15:1;
  91379. + unsigned reserved:16;
  91380. + } b;
  91381. +
  91382. + struct {
  91383. + unsigned chint:16;
  91384. + unsigned reserved:16;
  91385. + } b2;
  91386. +} haint_data_t;
  91387. +
  91388. +/**
  91389. + * This union represents the bit fields in the Host All Interrupt
  91390. + * Register.
  91391. + */
  91392. +typedef union haintmsk_data {
  91393. + /** raw register data */
  91394. + uint32_t d32;
  91395. + /** register bits */
  91396. + struct {
  91397. + unsigned ch0:1;
  91398. + unsigned ch1:1;
  91399. + unsigned ch2:1;
  91400. + unsigned ch3:1;
  91401. + unsigned ch4:1;
  91402. + unsigned ch5:1;
  91403. + unsigned ch6:1;
  91404. + unsigned ch7:1;
  91405. + unsigned ch8:1;
  91406. + unsigned ch9:1;
  91407. + unsigned ch10:1;
  91408. + unsigned ch11:1;
  91409. + unsigned ch12:1;
  91410. + unsigned ch13:1;
  91411. + unsigned ch14:1;
  91412. + unsigned ch15:1;
  91413. + unsigned reserved:16;
  91414. + } b;
  91415. +
  91416. + struct {
  91417. + unsigned chint:16;
  91418. + unsigned reserved:16;
  91419. + } b2;
  91420. +} haintmsk_data_t;
  91421. +
  91422. +/**
  91423. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  91424. + */
  91425. +typedef struct dwc_otg_hc_regs {
  91426. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  91427. + volatile uint32_t hcchar;
  91428. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  91429. + volatile uint32_t hcsplt;
  91430. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  91431. + volatile uint32_t hcint;
  91432. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  91433. + volatile uint32_t hcintmsk;
  91434. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  91435. + volatile uint32_t hctsiz;
  91436. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  91437. + volatile uint32_t hcdma;
  91438. + volatile uint32_t reserved;
  91439. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  91440. + volatile uint32_t hcdmab;
  91441. +} dwc_otg_hc_regs_t;
  91442. +
  91443. +/**
  91444. + * This union represents the bit fields in the Host Channel Characteristics
  91445. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91446. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91447. + * hcchar register.
  91448. + */
  91449. +typedef union hcchar_data {
  91450. + /** raw register data */
  91451. + uint32_t d32;
  91452. +
  91453. + /** register bits */
  91454. + struct {
  91455. + /** Maximum packet size in bytes */
  91456. + unsigned mps:11;
  91457. +
  91458. + /** Endpoint number */
  91459. + unsigned epnum:4;
  91460. +
  91461. + /** 0: OUT, 1: IN */
  91462. + unsigned epdir:1;
  91463. +
  91464. + unsigned reserved:1;
  91465. +
  91466. + /** 0: Full/high speed device, 1: Low speed device */
  91467. + unsigned lspddev:1;
  91468. +
  91469. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  91470. + unsigned eptype:2;
  91471. +
  91472. + /** Packets per frame for periodic transfers. 0 is reserved. */
  91473. + unsigned multicnt:2;
  91474. +
  91475. + /** Device address */
  91476. + unsigned devaddr:7;
  91477. +
  91478. + /**
  91479. + * Frame to transmit periodic transaction.
  91480. + * 0: even, 1: odd
  91481. + */
  91482. + unsigned oddfrm:1;
  91483. +
  91484. + /** Channel disable */
  91485. + unsigned chdis:1;
  91486. +
  91487. + /** Channel enable */
  91488. + unsigned chen:1;
  91489. + } b;
  91490. +} hcchar_data_t;
  91491. +
  91492. +typedef union hcsplt_data {
  91493. + /** raw register data */
  91494. + uint32_t d32;
  91495. +
  91496. + /** register bits */
  91497. + struct {
  91498. + /** Port Address */
  91499. + unsigned prtaddr:7;
  91500. +
  91501. + /** Hub Address */
  91502. + unsigned hubaddr:7;
  91503. +
  91504. + /** Transaction Position */
  91505. + unsigned xactpos:2;
  91506. +#define DWC_HCSPLIT_XACTPOS_MID 0
  91507. +#define DWC_HCSPLIT_XACTPOS_END 1
  91508. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  91509. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  91510. +
  91511. + /** Do Complete Split */
  91512. + unsigned compsplt:1;
  91513. +
  91514. + /** Reserved */
  91515. + unsigned reserved:14;
  91516. +
  91517. + /** Split Enble */
  91518. + unsigned spltena:1;
  91519. + } b;
  91520. +} hcsplt_data_t;
  91521. +
  91522. +/**
  91523. + * This union represents the bit fields in the Host All Interrupt
  91524. + * Register.
  91525. + */
  91526. +typedef union hcint_data {
  91527. + /** raw register data */
  91528. + uint32_t d32;
  91529. + /** register bits */
  91530. + struct {
  91531. + /** Transfer Complete */
  91532. + unsigned xfercomp:1;
  91533. + /** Channel Halted */
  91534. + unsigned chhltd:1;
  91535. + /** AHB Error */
  91536. + unsigned ahberr:1;
  91537. + /** STALL Response Received */
  91538. + unsigned stall:1;
  91539. + /** NAK Response Received */
  91540. + unsigned nak:1;
  91541. + /** ACK Response Received */
  91542. + unsigned ack:1;
  91543. + /** NYET Response Received */
  91544. + unsigned nyet:1;
  91545. + /** Transaction Err */
  91546. + unsigned xacterr:1;
  91547. + /** Babble Error */
  91548. + unsigned bblerr:1;
  91549. + /** Frame Overrun */
  91550. + unsigned frmovrun:1;
  91551. + /** Data Toggle Error */
  91552. + unsigned datatglerr:1;
  91553. + /** Buffer Not Available (only for DDMA mode) */
  91554. + unsigned bna:1;
  91555. + /** Exessive transaction error (only for DDMA mode) */
  91556. + unsigned xcs_xact:1;
  91557. + /** Frame List Rollover interrupt */
  91558. + unsigned frm_list_roll:1;
  91559. + /** Reserved */
  91560. + unsigned reserved14_31:18;
  91561. + } b;
  91562. +} hcint_data_t;
  91563. +
  91564. +/**
  91565. + * This union represents the bit fields in the Host Channel Interrupt Mask
  91566. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91567. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91568. + * hcintmsk register.
  91569. + */
  91570. +typedef union hcintmsk_data {
  91571. + /** raw register data */
  91572. + uint32_t d32;
  91573. +
  91574. + /** register bits */
  91575. + struct {
  91576. + unsigned xfercompl:1;
  91577. + unsigned chhltd:1;
  91578. + unsigned ahberr:1;
  91579. + unsigned stall:1;
  91580. + unsigned nak:1;
  91581. + unsigned ack:1;
  91582. + unsigned nyet:1;
  91583. + unsigned xacterr:1;
  91584. + unsigned bblerr:1;
  91585. + unsigned frmovrun:1;
  91586. + unsigned datatglerr:1;
  91587. + unsigned bna:1;
  91588. + unsigned xcs_xact:1;
  91589. + unsigned frm_list_roll:1;
  91590. + unsigned reserved14_31:18;
  91591. + } b;
  91592. +} hcintmsk_data_t;
  91593. +
  91594. +/**
  91595. + * This union represents the bit fields in the Host Channel Transfer Size
  91596. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91597. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91598. + * hcchar register.
  91599. + */
  91600. +
  91601. +typedef union hctsiz_data {
  91602. + /** raw register data */
  91603. + uint32_t d32;
  91604. +
  91605. + /** register bits */
  91606. + struct {
  91607. + /** Total transfer size in bytes */
  91608. + unsigned xfersize:19;
  91609. +
  91610. + /** Data packets to transfer */
  91611. + unsigned pktcnt:10;
  91612. +
  91613. + /**
  91614. + * Packet ID for next data packet
  91615. + * 0: DATA0
  91616. + * 1: DATA2
  91617. + * 2: DATA1
  91618. + * 3: MDATA (non-Control), SETUP (Control)
  91619. + */
  91620. + unsigned pid:2;
  91621. +#define DWC_HCTSIZ_DATA0 0
  91622. +#define DWC_HCTSIZ_DATA1 2
  91623. +#define DWC_HCTSIZ_DATA2 1
  91624. +#define DWC_HCTSIZ_MDATA 3
  91625. +#define DWC_HCTSIZ_SETUP 3
  91626. +
  91627. + /** Do PING protocol when 1 */
  91628. + unsigned dopng:1;
  91629. + } b;
  91630. +
  91631. + /** register bits */
  91632. + struct {
  91633. + /** Scheduling information */
  91634. + unsigned schinfo:8;
  91635. +
  91636. + /** Number of transfer descriptors.
  91637. + * Max value:
  91638. + * 64 in general,
  91639. + * 256 only for HS isochronous endpoint.
  91640. + */
  91641. + unsigned ntd:8;
  91642. +
  91643. + /** Data packets to transfer */
  91644. + unsigned reserved16_28:13;
  91645. +
  91646. + /**
  91647. + * Packet ID for next data packet
  91648. + * 0: DATA0
  91649. + * 1: DATA2
  91650. + * 2: DATA1
  91651. + * 3: MDATA (non-Control)
  91652. + */
  91653. + unsigned pid:2;
  91654. +
  91655. + /** Do PING protocol when 1 */
  91656. + unsigned dopng:1;
  91657. + } b_ddma;
  91658. +} hctsiz_data_t;
  91659. +
  91660. +/**
  91661. + * This union represents the bit fields in the Host DMA Address
  91662. + * Register used in Descriptor DMA mode.
  91663. + */
  91664. +typedef union hcdma_data {
  91665. + /** raw register data */
  91666. + uint32_t d32;
  91667. + /** register bits */
  91668. + struct {
  91669. + unsigned reserved0_2:3;
  91670. + /** Current Transfer Descriptor. Not used for ISOC */
  91671. + unsigned ctd:8;
  91672. + /** Start Address of Descriptor List */
  91673. + unsigned dma_addr:21;
  91674. + } b;
  91675. +} hcdma_data_t;
  91676. +
  91677. +/**
  91678. + * This union represents the bit fields in the DMA Descriptor
  91679. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  91680. + * set/clear the bits using the <i>b</i>it elements.
  91681. + */
  91682. +typedef union host_dma_desc_sts {
  91683. + /** raw register data */
  91684. + uint32_t d32;
  91685. + /** quadlet bits */
  91686. +
  91687. + /* for non-isochronous */
  91688. + struct {
  91689. + /** Number of bytes */
  91690. + unsigned n_bytes:17;
  91691. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  91692. + unsigned qtd_offset:6;
  91693. + /**
  91694. + * Set to request the core to jump to alternate QTD if
  91695. + * Short Packet received - only for IN EPs
  91696. + */
  91697. + unsigned a_qtd:1;
  91698. + /**
  91699. + * Setup Packet bit. When set indicates that buffer contains
  91700. + * setup packet.
  91701. + */
  91702. + unsigned sup:1;
  91703. + /** Interrupt On Complete */
  91704. + unsigned ioc:1;
  91705. + /** End of List */
  91706. + unsigned eol:1;
  91707. + unsigned reserved27:1;
  91708. + /** Rx/Tx Status */
  91709. + unsigned sts:2;
  91710. +#define DMA_DESC_STS_PKTERR 1
  91711. + unsigned reserved30:1;
  91712. + /** Active Bit */
  91713. + unsigned a:1;
  91714. + } b;
  91715. + /* for isochronous */
  91716. + struct {
  91717. + /** Number of bytes */
  91718. + unsigned n_bytes:12;
  91719. + unsigned reserved12_24:13;
  91720. + /** Interrupt On Complete */
  91721. + unsigned ioc:1;
  91722. + unsigned reserved26_27:2;
  91723. + /** Rx/Tx Status */
  91724. + unsigned sts:2;
  91725. + unsigned reserved30:1;
  91726. + /** Active Bit */
  91727. + unsigned a:1;
  91728. + } b_isoc;
  91729. +} host_dma_desc_sts_t;
  91730. +
  91731. +#define MAX_DMA_DESC_SIZE 131071
  91732. +#define MAX_DMA_DESC_NUM_GENERIC 64
  91733. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  91734. +#define MAX_FRLIST_EN_NUM 64
  91735. +/**
  91736. + * Host-mode DMA Descriptor structure
  91737. + *
  91738. + * DMA Descriptor structure contains two quadlets:
  91739. + * Status quadlet and Data buffer pointer.
  91740. + */
  91741. +typedef struct dwc_otg_host_dma_desc {
  91742. + /** DMA Descriptor status quadlet */
  91743. + host_dma_desc_sts_t status;
  91744. + /** DMA Descriptor data buffer pointer */
  91745. + uint32_t buf;
  91746. +} dwc_otg_host_dma_desc_t;
  91747. +
  91748. +/** OTG Host Interface Structure.
  91749. + *
  91750. + * The OTG Host Interface Structure structure contains information
  91751. + * needed to manage the DWC_otg controller acting in host mode. It
  91752. + * represents the programming view of the host-specific aspects of the
  91753. + * controller.
  91754. + */
  91755. +typedef struct dwc_otg_host_if {
  91756. + /** Host Global Registers starting at offset 400h.*/
  91757. + dwc_otg_host_global_regs_t *host_global_regs;
  91758. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  91759. +
  91760. + /** Host Port 0 Control and Status Register */
  91761. + volatile uint32_t *hprt0;
  91762. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  91763. +
  91764. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  91765. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  91766. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  91767. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  91768. +
  91769. + /* Host configuration information */
  91770. + /** Number of Host Channels (range: 1-16) */
  91771. + uint8_t num_host_channels;
  91772. + /** Periodic EPs supported (0: no, 1: yes) */
  91773. + uint8_t perio_eps_supported;
  91774. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  91775. + uint16_t perio_tx_fifo_size;
  91776. +
  91777. +} dwc_otg_host_if_t;
  91778. +
  91779. +/**
  91780. + * This union represents the bit fields in the Power and Clock Gating Control
  91781. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91782. + * bits using the <i>b</i>it elements.
  91783. + */
  91784. +typedef union pcgcctl_data {
  91785. + /** raw register data */
  91786. + uint32_t d32;
  91787. +
  91788. + /** register bits */
  91789. + struct {
  91790. + /** Stop Pclk */
  91791. + unsigned stoppclk:1;
  91792. + /** Gate Hclk */
  91793. + unsigned gatehclk:1;
  91794. + /** Power Clamp */
  91795. + unsigned pwrclmp:1;
  91796. + /** Reset Power Down Modules */
  91797. + unsigned rstpdwnmodule:1;
  91798. + /** Reserved */
  91799. + unsigned reserved:1;
  91800. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  91801. + unsigned enbl_sleep_gating:1;
  91802. + /** PHY In Sleep (PhySleep) */
  91803. + unsigned phy_in_sleep:1;
  91804. + /** Deep Sleep*/
  91805. + unsigned deep_sleep:1;
  91806. + unsigned resetaftsusp:1;
  91807. + unsigned restoremode:1;
  91808. + unsigned enbl_extnd_hiber:1;
  91809. + unsigned extnd_hiber_pwrclmp:1;
  91810. + unsigned extnd_hiber_switch:1;
  91811. + unsigned ess_reg_restored:1;
  91812. + unsigned prt_clk_sel:2;
  91813. + unsigned port_power:1;
  91814. + unsigned max_xcvrselect:2;
  91815. + unsigned max_termsel:1;
  91816. + unsigned mac_dev_addr:7;
  91817. + unsigned p2hd_dev_enum_spd:2;
  91818. + unsigned p2hd_prt_spd:2;
  91819. + unsigned if_dev_mode:1;
  91820. + } b;
  91821. +} pcgcctl_data_t;
  91822. +
  91823. +/**
  91824. + * This union represents the bit fields in the Global Data FIFO Software
  91825. + * Configuration Register. Read the register into the <i>d32</i> member then
  91826. + * set/clear the bits using the <i>b</i>it elements.
  91827. + */
  91828. +typedef union gdfifocfg_data {
  91829. + /* raw register data */
  91830. + uint32_t d32;
  91831. + /** register bits */
  91832. + struct {
  91833. + /** OTG Data FIFO depth */
  91834. + unsigned gdfifocfg:16;
  91835. + /** Start address of EP info controller */
  91836. + unsigned epinfobase:16;
  91837. + } b;
  91838. +} gdfifocfg_data_t;
  91839. +
  91840. +/**
  91841. + * This union represents the bit fields in the Global Power Down Register
  91842. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91843. + * bits using the <i>b</i>it elements.
  91844. + */
  91845. +typedef union gpwrdn_data {
  91846. + /* raw register data */
  91847. + uint32_t d32;
  91848. +
  91849. + /** register bits */
  91850. + struct {
  91851. + /** PMU Interrupt Select */
  91852. + unsigned pmuintsel:1;
  91853. + /** PMU Active */
  91854. + unsigned pmuactv:1;
  91855. + /** Restore */
  91856. + unsigned restore:1;
  91857. + /** Power Down Clamp */
  91858. + unsigned pwrdnclmp:1;
  91859. + /** Power Down Reset */
  91860. + unsigned pwrdnrstn:1;
  91861. + /** Power Down Switch */
  91862. + unsigned pwrdnswtch:1;
  91863. + /** Disable VBUS */
  91864. + unsigned dis_vbus:1;
  91865. + /** Line State Change */
  91866. + unsigned lnstschng:1;
  91867. + /** Line state change mask */
  91868. + unsigned lnstchng_msk:1;
  91869. + /** Reset Detected */
  91870. + unsigned rst_det:1;
  91871. + /** Reset Detect mask */
  91872. + unsigned rst_det_msk:1;
  91873. + /** Disconnect Detected */
  91874. + unsigned disconn_det:1;
  91875. + /** Disconnect Detect mask */
  91876. + unsigned disconn_det_msk:1;
  91877. + /** Connect Detected*/
  91878. + unsigned connect_det:1;
  91879. + /** Connect Detected Mask*/
  91880. + unsigned connect_det_msk:1;
  91881. + /** SRP Detected */
  91882. + unsigned srp_det:1;
  91883. + /** SRP Detect mask */
  91884. + unsigned srp_det_msk:1;
  91885. + /** Status Change Interrupt */
  91886. + unsigned sts_chngint:1;
  91887. + /** Status Change Interrupt Mask */
  91888. + unsigned sts_chngint_msk:1;
  91889. + /** Line State */
  91890. + unsigned linestate:2;
  91891. + /** Indicates current mode(status of IDDIG signal) */
  91892. + unsigned idsts:1;
  91893. + /** B Session Valid signal status*/
  91894. + unsigned bsessvld:1;
  91895. + /** ADP Event Detected */
  91896. + unsigned adp_int:1;
  91897. + /** Multi Valued ID pin */
  91898. + unsigned mult_val_id_bc:5;
  91899. + /** Reserved 24_31 */
  91900. + unsigned reserved29_31:3;
  91901. + } b;
  91902. +} gpwrdn_data_t;
  91903. +
  91904. +#endif
  91905. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/Makefile linux-raspberry-pi/drivers/usb/host/dwc_otg/Makefile
  91906. --- linux-3.12.13/drivers/usb/host/dwc_otg/Makefile 1970-01-01 01:00:00.000000000 +0100
  91907. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/Makefile 2014-03-11 17:51:27.000000000 +0100
  91908. @@ -0,0 +1,81 @@
  91909. +#
  91910. +# Makefile for DWC_otg Highspeed USB controller driver
  91911. +#
  91912. +
  91913. +ifneq ($(KERNELRELEASE),)
  91914. +
  91915. +# Use the BUS_INTERFACE variable to compile the software for either
  91916. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  91917. +ifeq ($(BUS_INTERFACE),)
  91918. +# BUS_INTERFACE = -DPCI_INTERFACE
  91919. +# BUS_INTERFACE = -DLM_INTERFACE
  91920. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  91921. +endif
  91922. +
  91923. +#EXTRA_CFLAGS += -DDEBUG
  91924. +#EXTRA_CFLAGS += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  91925. +
  91926. +# Use one of the following flags to compile the software in host-only or
  91927. +# device-only mode.
  91928. +#EXTRA_CFLAGS += -DDWC_HOST_ONLY
  91929. +#EXTRA_CFLAGS += -DDWC_DEVICE_ONLY
  91930. +
  91931. +EXTRA_CFLAGS += -Dlinux -DDWC_HS_ELECT_TST
  91932. +#EXTRA_CFLAGS += -DDWC_EN_ISOC
  91933. +EXTRA_CFLAGS += -I$(obj)/../dwc_common_port
  91934. +#EXTRA_CFLAGS += -I$(PORTLIB)
  91935. +EXTRA_CFLAGS += -DDWC_LINUX
  91936. +EXTRA_CFLAGS += $(CFI)
  91937. +EXTRA_CFLAGS += $(BUS_INTERFACE)
  91938. +#EXTRA_CFLAGS += -DDWC_DEV_SRPCAP
  91939. +
  91940. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  91941. +
  91942. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  91943. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  91944. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  91945. +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
  91946. +dwc_otg-objs += dwc_otg_adp.o
  91947. +dwc_otg-objs += dwc_otg_mphi_fix.o
  91948. +ifneq ($(CFI),)
  91949. +dwc_otg-objs += dwc_otg_cfi.o
  91950. +endif
  91951. +
  91952. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  91953. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  91954. +
  91955. +ifneq ($(kernrel3),2.6.20)
  91956. +EXTRA_CFLAGS += $(CPPFLAGS)
  91957. +endif
  91958. +
  91959. +else
  91960. +
  91961. +PWD := $(shell pwd)
  91962. +PORTLIB := $(PWD)/../dwc_common_port
  91963. +
  91964. +# Command paths
  91965. +CTAGS := $(CTAGS)
  91966. +DOXYGEN := $(DOXYGEN)
  91967. +
  91968. +default: portlib
  91969. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  91970. +
  91971. +install: default
  91972. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  91973. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  91974. +
  91975. +portlib:
  91976. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  91977. + cp $(PORTLIB)/Module.symvers $(PWD)/
  91978. +
  91979. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  91980. + $(DOXYGEN) doc/doxygen.cfg
  91981. +
  91982. +tags: $(wildcard *.[hc])
  91983. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  91984. +
  91985. +
  91986. +clean:
  91987. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  91988. +
  91989. +endif
  91990. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm linux-raspberry-pi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  91991. --- linux-3.12.13/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 1970-01-01 01:00:00.000000000 +0100
  91992. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 2014-03-11 17:51:27.000000000 +0100
  91993. @@ -0,0 +1,337 @@
  91994. +package dwc_otg_test;
  91995. +
  91996. +use strict;
  91997. +use Exporter ();
  91998. +
  91999. +use vars qw(@ISA @EXPORT
  92000. +$sysfsdir $paramdir $errors $params
  92001. +);
  92002. +
  92003. +@ISA = qw(Exporter);
  92004. +
  92005. +#
  92006. +# Globals
  92007. +#
  92008. +$sysfsdir = "/sys/devices/lm0";
  92009. +$paramdir = "/sys/module/dwc_otg";
  92010. +$errors = 0;
  92011. +
  92012. +$params = [
  92013. + {
  92014. + NAME => "otg_cap",
  92015. + DEFAULT => 0,
  92016. + ENUM => [],
  92017. + LOW => 0,
  92018. + HIGH => 2
  92019. + },
  92020. + {
  92021. + NAME => "dma_enable",
  92022. + DEFAULT => 0,
  92023. + ENUM => [],
  92024. + LOW => 0,
  92025. + HIGH => 1
  92026. + },
  92027. + {
  92028. + NAME => "dma_burst_size",
  92029. + DEFAULT => 32,
  92030. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  92031. + LOW => 1,
  92032. + HIGH => 256
  92033. + },
  92034. + {
  92035. + NAME => "host_speed",
  92036. + DEFAULT => 0,
  92037. + ENUM => [],
  92038. + LOW => 0,
  92039. + HIGH => 1
  92040. + },
  92041. + {
  92042. + NAME => "host_support_fs_ls_low_power",
  92043. + DEFAULT => 0,
  92044. + ENUM => [],
  92045. + LOW => 0,
  92046. + HIGH => 1
  92047. + },
  92048. + {
  92049. + NAME => "host_ls_low_power_phy_clk",
  92050. + DEFAULT => 0,
  92051. + ENUM => [],
  92052. + LOW => 0,
  92053. + HIGH => 1
  92054. + },
  92055. + {
  92056. + NAME => "dev_speed",
  92057. + DEFAULT => 0,
  92058. + ENUM => [],
  92059. + LOW => 0,
  92060. + HIGH => 1
  92061. + },
  92062. + {
  92063. + NAME => "enable_dynamic_fifo",
  92064. + DEFAULT => 1,
  92065. + ENUM => [],
  92066. + LOW => 0,
  92067. + HIGH => 1
  92068. + },
  92069. + {
  92070. + NAME => "data_fifo_size",
  92071. + DEFAULT => 8192,
  92072. + ENUM => [],
  92073. + LOW => 32,
  92074. + HIGH => 32768
  92075. + },
  92076. + {
  92077. + NAME => "dev_rx_fifo_size",
  92078. + DEFAULT => 1064,
  92079. + ENUM => [],
  92080. + LOW => 16,
  92081. + HIGH => 32768
  92082. + },
  92083. + {
  92084. + NAME => "dev_nperio_tx_fifo_size",
  92085. + DEFAULT => 1024,
  92086. + ENUM => [],
  92087. + LOW => 16,
  92088. + HIGH => 32768
  92089. + },
  92090. + {
  92091. + NAME => "dev_perio_tx_fifo_size_1",
  92092. + DEFAULT => 256,
  92093. + ENUM => [],
  92094. + LOW => 4,
  92095. + HIGH => 768
  92096. + },
  92097. + {
  92098. + NAME => "dev_perio_tx_fifo_size_2",
  92099. + DEFAULT => 256,
  92100. + ENUM => [],
  92101. + LOW => 4,
  92102. + HIGH => 768
  92103. + },
  92104. + {
  92105. + NAME => "dev_perio_tx_fifo_size_3",
  92106. + DEFAULT => 256,
  92107. + ENUM => [],
  92108. + LOW => 4,
  92109. + HIGH => 768
  92110. + },
  92111. + {
  92112. + NAME => "dev_perio_tx_fifo_size_4",
  92113. + DEFAULT => 256,
  92114. + ENUM => [],
  92115. + LOW => 4,
  92116. + HIGH => 768
  92117. + },
  92118. + {
  92119. + NAME => "dev_perio_tx_fifo_size_5",
  92120. + DEFAULT => 256,
  92121. + ENUM => [],
  92122. + LOW => 4,
  92123. + HIGH => 768
  92124. + },
  92125. + {
  92126. + NAME => "dev_perio_tx_fifo_size_6",
  92127. + DEFAULT => 256,
  92128. + ENUM => [],
  92129. + LOW => 4,
  92130. + HIGH => 768
  92131. + },
  92132. + {
  92133. + NAME => "dev_perio_tx_fifo_size_7",
  92134. + DEFAULT => 256,
  92135. + ENUM => [],
  92136. + LOW => 4,
  92137. + HIGH => 768
  92138. + },
  92139. + {
  92140. + NAME => "dev_perio_tx_fifo_size_8",
  92141. + DEFAULT => 256,
  92142. + ENUM => [],
  92143. + LOW => 4,
  92144. + HIGH => 768
  92145. + },
  92146. + {
  92147. + NAME => "dev_perio_tx_fifo_size_9",
  92148. + DEFAULT => 256,
  92149. + ENUM => [],
  92150. + LOW => 4,
  92151. + HIGH => 768
  92152. + },
  92153. + {
  92154. + NAME => "dev_perio_tx_fifo_size_10",
  92155. + DEFAULT => 256,
  92156. + ENUM => [],
  92157. + LOW => 4,
  92158. + HIGH => 768
  92159. + },
  92160. + {
  92161. + NAME => "dev_perio_tx_fifo_size_11",
  92162. + DEFAULT => 256,
  92163. + ENUM => [],
  92164. + LOW => 4,
  92165. + HIGH => 768
  92166. + },
  92167. + {
  92168. + NAME => "dev_perio_tx_fifo_size_12",
  92169. + DEFAULT => 256,
  92170. + ENUM => [],
  92171. + LOW => 4,
  92172. + HIGH => 768
  92173. + },
  92174. + {
  92175. + NAME => "dev_perio_tx_fifo_size_13",
  92176. + DEFAULT => 256,
  92177. + ENUM => [],
  92178. + LOW => 4,
  92179. + HIGH => 768
  92180. + },
  92181. + {
  92182. + NAME => "dev_perio_tx_fifo_size_14",
  92183. + DEFAULT => 256,
  92184. + ENUM => [],
  92185. + LOW => 4,
  92186. + HIGH => 768
  92187. + },
  92188. + {
  92189. + NAME => "dev_perio_tx_fifo_size_15",
  92190. + DEFAULT => 256,
  92191. + ENUM => [],
  92192. + LOW => 4,
  92193. + HIGH => 768
  92194. + },
  92195. + {
  92196. + NAME => "host_rx_fifo_size",
  92197. + DEFAULT => 1024,
  92198. + ENUM => [],
  92199. + LOW => 16,
  92200. + HIGH => 32768
  92201. + },
  92202. + {
  92203. + NAME => "host_nperio_tx_fifo_size",
  92204. + DEFAULT => 1024,
  92205. + ENUM => [],
  92206. + LOW => 16,
  92207. + HIGH => 32768
  92208. + },
  92209. + {
  92210. + NAME => "host_perio_tx_fifo_size",
  92211. + DEFAULT => 1024,
  92212. + ENUM => [],
  92213. + LOW => 16,
  92214. + HIGH => 32768
  92215. + },
  92216. + {
  92217. + NAME => "max_transfer_size",
  92218. + DEFAULT => 65535,
  92219. + ENUM => [],
  92220. + LOW => 2047,
  92221. + HIGH => 65535
  92222. + },
  92223. + {
  92224. + NAME => "max_packet_count",
  92225. + DEFAULT => 511,
  92226. + ENUM => [],
  92227. + LOW => 15,
  92228. + HIGH => 511
  92229. + },
  92230. + {
  92231. + NAME => "host_channels",
  92232. + DEFAULT => 12,
  92233. + ENUM => [],
  92234. + LOW => 1,
  92235. + HIGH => 16
  92236. + },
  92237. + {
  92238. + NAME => "dev_endpoints",
  92239. + DEFAULT => 6,
  92240. + ENUM => [],
  92241. + LOW => 1,
  92242. + HIGH => 15
  92243. + },
  92244. + {
  92245. + NAME => "phy_type",
  92246. + DEFAULT => 1,
  92247. + ENUM => [],
  92248. + LOW => 0,
  92249. + HIGH => 2
  92250. + },
  92251. + {
  92252. + NAME => "phy_utmi_width",
  92253. + DEFAULT => 16,
  92254. + ENUM => [8, 16],
  92255. + LOW => 8,
  92256. + HIGH => 16
  92257. + },
  92258. + {
  92259. + NAME => "phy_ulpi_ddr",
  92260. + DEFAULT => 0,
  92261. + ENUM => [],
  92262. + LOW => 0,
  92263. + HIGH => 1
  92264. + },
  92265. + ];
  92266. +
  92267. +
  92268. +#
  92269. +#
  92270. +sub check_arch {
  92271. + $_ = `uname -m`;
  92272. + chomp;
  92273. + unless (m/armv4tl/) {
  92274. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  92275. + return 0;
  92276. + }
  92277. + return 1;
  92278. +}
  92279. +
  92280. +#
  92281. +#
  92282. +sub load_module {
  92283. + my $params = shift;
  92284. + print "\nRemoving Module\n";
  92285. + system "rmmod dwc_otg";
  92286. + print "Loading Module\n";
  92287. + if ($params ne "") {
  92288. + print "Module Parameters: $params\n";
  92289. + }
  92290. + if (system("modprobe dwc_otg $params")) {
  92291. + warn "Unable to load module\n";
  92292. + return 0;
  92293. + }
  92294. + return 1;
  92295. +}
  92296. +
  92297. +#
  92298. +#
  92299. +sub test_status {
  92300. + my $arg = shift;
  92301. +
  92302. + print "\n";
  92303. +
  92304. + if (defined $arg) {
  92305. + warn "WARNING: $arg\n";
  92306. + }
  92307. +
  92308. + if ($errors > 0) {
  92309. + warn "TEST FAILED with $errors errors\n";
  92310. + return 0;
  92311. + } else {
  92312. + print "TEST PASSED\n";
  92313. + return 0 if (defined $arg);
  92314. + }
  92315. + return 1;
  92316. +}
  92317. +
  92318. +#
  92319. +#
  92320. +@EXPORT = qw(
  92321. +$sysfsdir
  92322. +$paramdir
  92323. +$params
  92324. +$errors
  92325. +check_arch
  92326. +load_module
  92327. +test_status
  92328. +);
  92329. +
  92330. +1;
  92331. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/test/Makefile linux-raspberry-pi/drivers/usb/host/dwc_otg/test/Makefile
  92332. --- linux-3.12.13/drivers/usb/host/dwc_otg/test/Makefile 1970-01-01 01:00:00.000000000 +0100
  92333. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/Makefile 2014-03-11 17:33:06.000000000 +0100
  92334. @@ -0,0 +1,16 @@
  92335. +
  92336. +PERL=/usr/bin/perl
  92337. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  92338. +
  92339. +.PHONY : test
  92340. +test : perl_tests
  92341. +
  92342. +perl_tests :
  92343. + @echo
  92344. + @echo Running perl tests
  92345. + @for test in $(PL_TESTS); do \
  92346. + if $(PERL) ./$$test ; then \
  92347. + echo "=======> $$test, PASSED" ; \
  92348. + else echo "=======> $$test, FAILED" ; \
  92349. + fi \
  92350. + done
  92351. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/test/test_mod_param.pl linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  92352. --- linux-3.12.13/drivers/usb/host/dwc_otg/test/test_mod_param.pl 1970-01-01 01:00:00.000000000 +0100
  92353. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_mod_param.pl 2014-03-11 17:51:27.000000000 +0100
  92354. @@ -0,0 +1,133 @@
  92355. +#!/usr/bin/perl -w
  92356. +#
  92357. +# Run this program on the integrator.
  92358. +#
  92359. +# - Tests module parameter default values.
  92360. +# - Tests setting of valid module parameter values via modprobe.
  92361. +# - Tests invalid module parameter values.
  92362. +# -----------------------------------------------------------------------------
  92363. +use strict;
  92364. +use dwc_otg_test;
  92365. +
  92366. +check_arch() or die;
  92367. +
  92368. +#
  92369. +#
  92370. +sub test {
  92371. + my ($param,$expected) = @_;
  92372. + my $value = get($param);
  92373. +
  92374. + if ($value == $expected) {
  92375. + print "$param = $value, okay\n";
  92376. + }
  92377. +
  92378. + else {
  92379. + warn "ERROR: value of $param != $expected, $value\n";
  92380. + $errors ++;
  92381. + }
  92382. +}
  92383. +
  92384. +#
  92385. +#
  92386. +sub get {
  92387. + my $param = shift;
  92388. + my $tmp = `cat $paramdir/$param`;
  92389. + chomp $tmp;
  92390. + return $tmp;
  92391. +}
  92392. +
  92393. +#
  92394. +#
  92395. +sub test_main {
  92396. +
  92397. + print "\nTesting Module Parameters\n";
  92398. +
  92399. + load_module("") or die;
  92400. +
  92401. + # Test initial values
  92402. + print "\nTesting Default Values\n";
  92403. + foreach (@{$params}) {
  92404. + test ($_->{NAME}, $_->{DEFAULT});
  92405. + }
  92406. +
  92407. + # Test low value
  92408. + print "\nTesting Low Value\n";
  92409. + my $cmd_params = "";
  92410. + foreach (@{$params}) {
  92411. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  92412. + }
  92413. + load_module($cmd_params) or die;
  92414. +
  92415. + foreach (@{$params}) {
  92416. + test ($_->{NAME}, $_->{LOW});
  92417. + }
  92418. +
  92419. + # Test high value
  92420. + print "\nTesting High Value\n";
  92421. + $cmd_params = "";
  92422. + foreach (@{$params}) {
  92423. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  92424. + }
  92425. + load_module($cmd_params) or die;
  92426. +
  92427. + foreach (@{$params}) {
  92428. + test ($_->{NAME}, $_->{HIGH});
  92429. + }
  92430. +
  92431. + # Test Enum
  92432. + print "\nTesting Enumerated\n";
  92433. + foreach (@{$params}) {
  92434. + if (defined $_->{ENUM}) {
  92435. + my $value;
  92436. + foreach $value (@{$_->{ENUM}}) {
  92437. + $cmd_params = "$_->{NAME}=$value";
  92438. + load_module($cmd_params) or die;
  92439. + test ($_->{NAME}, $value);
  92440. + }
  92441. + }
  92442. + }
  92443. +
  92444. + # Test Invalid Values
  92445. + print "\nTesting Invalid Values\n";
  92446. + $cmd_params = "";
  92447. + foreach (@{$params}) {
  92448. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  92449. + }
  92450. + load_module($cmd_params) or die;
  92451. +
  92452. + foreach (@{$params}) {
  92453. + test ($_->{NAME}, $_->{DEFAULT});
  92454. + }
  92455. +
  92456. + $cmd_params = "";
  92457. + foreach (@{$params}) {
  92458. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  92459. + }
  92460. + load_module($cmd_params) or die;
  92461. +
  92462. + foreach (@{$params}) {
  92463. + test ($_->{NAME}, $_->{DEFAULT});
  92464. + }
  92465. +
  92466. + print "\nTesting Enumerated\n";
  92467. + foreach (@{$params}) {
  92468. + if (defined $_->{ENUM}) {
  92469. + my $value;
  92470. + foreach $value (@{$_->{ENUM}}) {
  92471. + $value = $value + 1;
  92472. + $cmd_params = "$_->{NAME}=$value";
  92473. + load_module($cmd_params) or die;
  92474. + test ($_->{NAME}, $_->{DEFAULT});
  92475. + $value = $value - 2;
  92476. + $cmd_params = "$_->{NAME}=$value";
  92477. + load_module($cmd_params) or die;
  92478. + test ($_->{NAME}, $_->{DEFAULT});
  92479. + }
  92480. + }
  92481. + }
  92482. +
  92483. + test_status() or die;
  92484. +}
  92485. +
  92486. +test_main();
  92487. +0;
  92488. diff -Nur linux-3.12.13/drivers/usb/host/dwc_otg/test/test_sysfs.pl linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  92489. --- linux-3.12.13/drivers/usb/host/dwc_otg/test/test_sysfs.pl 1970-01-01 01:00:00.000000000 +0100
  92490. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_sysfs.pl 2014-03-11 17:51:27.000000000 +0100
  92491. @@ -0,0 +1,193 @@
  92492. +#!/usr/bin/perl -w
  92493. +#
  92494. +# Run this program on the integrator
  92495. +# - Tests select sysfs attributes.
  92496. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  92497. +# -----------------------------------------------------------------------------
  92498. +use strict;
  92499. +use dwc_otg_test;
  92500. +
  92501. +check_arch() or die;
  92502. +
  92503. +#
  92504. +#
  92505. +sub test {
  92506. + my ($attr,$expected) = @_;
  92507. + my $string = get($attr);
  92508. +
  92509. + if ($string eq $expected) {
  92510. + printf("$attr = $string, okay\n");
  92511. + }
  92512. + else {
  92513. + warn "ERROR: value of $attr != $expected, $string\n";
  92514. + $errors ++;
  92515. + }
  92516. +}
  92517. +
  92518. +#
  92519. +#
  92520. +sub set {
  92521. + my ($reg, $value) = @_;
  92522. + system "echo $value > $sysfsdir/$reg";
  92523. +}
  92524. +
  92525. +#
  92526. +#
  92527. +sub get {
  92528. + my $attr = shift;
  92529. + my $string = `cat $sysfsdir/$attr`;
  92530. + chomp $string;
  92531. + if ($string =~ m/\s\=\s/) {
  92532. + my $tmp;
  92533. + ($tmp, $string) = split /\s=\s/, $string;
  92534. + }
  92535. + return $string;
  92536. +}
  92537. +
  92538. +#
  92539. +#
  92540. +sub test_main {
  92541. + print("\nTesting Sysfs Attributes\n");
  92542. +
  92543. + load_module("") or die;
  92544. +
  92545. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  92546. + print("\nTesting Default Values\n");
  92547. +
  92548. + test("regoffset", "0xffffffff");
  92549. + test("regvalue", "invalid offset");
  92550. + test("guid", "0x12345678"); # this will fail if it has been changed
  92551. + test("gsnpsid", "0x4f54200a");
  92552. +
  92553. + # Test operation of regoffset/regvalue
  92554. + print("\nTesting regoffset\n");
  92555. + set('regoffset', '5a5a5a5a');
  92556. + test("regoffset", "0xffffffff");
  92557. +
  92558. + set('regoffset', '0');
  92559. + test("regoffset", "0x00000000");
  92560. +
  92561. + set('regoffset', '40000');
  92562. + test("regoffset", "0x00000000");
  92563. +
  92564. + set('regoffset', '3ffff');
  92565. + test("regoffset", "0x0003ffff");
  92566. +
  92567. + set('regoffset', '1');
  92568. + test("regoffset", "0x00000001");
  92569. +
  92570. + print("\nTesting regvalue\n");
  92571. + set('regoffset', '3c');
  92572. + test("regvalue", "0x12345678");
  92573. + set('regvalue', '5a5a5a5a');
  92574. + test("regvalue", "0x5a5a5a5a");
  92575. + set('regvalue','a5a5a5a5');
  92576. + test("regvalue", "0xa5a5a5a5");
  92577. + set('guid','12345678');
  92578. +
  92579. + # Test HNP Capable
  92580. + print("\nTesting HNP Capable bit\n");
  92581. + set('hnpcapable', '1');
  92582. + test("hnpcapable", "0x1");
  92583. + set('hnpcapable','0');
  92584. + test("hnpcapable", "0x0");
  92585. +
  92586. + set('regoffset','0c');
  92587. +
  92588. + my $old = get('gusbcfg');
  92589. + print("setting hnpcapable\n");
  92590. + set('hnpcapable', '1');
  92591. + test("hnpcapable", "0x1");
  92592. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  92593. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  92594. +
  92595. + $old = get('gusbcfg');
  92596. + print("clearing hnpcapable\n");
  92597. + set('hnpcapable', '0');
  92598. + test("hnpcapable", "0x0");
  92599. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  92600. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  92601. +
  92602. + # Test SRP Capable
  92603. + print("\nTesting SRP Capable bit\n");
  92604. + set('srpcapable', '1');
  92605. + test("srpcapable", "0x1");
  92606. + set('srpcapable','0');
  92607. + test("srpcapable", "0x0");
  92608. +
  92609. + set('regoffset','0c');
  92610. +
  92611. + $old = get('gusbcfg');
  92612. + print("setting srpcapable\n");
  92613. + set('srpcapable', '1');
  92614. + test("srpcapable", "0x1");
  92615. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  92616. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  92617. +
  92618. + $old = get('gusbcfg');
  92619. + print("clearing srpcapable\n");
  92620. + set('srpcapable', '0');
  92621. + test("srpcapable", "0x0");
  92622. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  92623. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  92624. +
  92625. + # Test GGPIO
  92626. + print("\nTesting GGPIO\n");
  92627. + set('ggpio','5a5a5a5a');
  92628. + test('ggpio','0x5a5a0000');
  92629. + set('ggpio','a5a5a5a5');
  92630. + test('ggpio','0xa5a50000');
  92631. + set('ggpio','11110000');
  92632. + test('ggpio','0x11110000');
  92633. + set('ggpio','00001111');
  92634. + test('ggpio','0x00000000');
  92635. +
  92636. + # Test DEVSPEED
  92637. + print("\nTesting DEVSPEED\n");
  92638. + set('regoffset','800');
  92639. + $old = get('regvalue');
  92640. + set('devspeed','0');
  92641. + test('devspeed','0x0');
  92642. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  92643. + set('devspeed','1');
  92644. + test('devspeed','0x1');
  92645. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  92646. + set('devspeed','2');
  92647. + test('devspeed','0x2');
  92648. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  92649. + set('devspeed','3');
  92650. + test('devspeed','0x3');
  92651. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  92652. + set('devspeed','4');
  92653. + test('devspeed','0x0');
  92654. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  92655. + set('devspeed','5');
  92656. + test('devspeed','0x1');
  92657. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  92658. +
  92659. +
  92660. + # mode Returns the current mode:0 for device mode1 for host mode Read
  92661. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  92662. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  92663. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  92664. + # bussuspend Suspend the USB bus. Read/Write
  92665. + # busconnected Get the connection status of the bus Read
  92666. +
  92667. + # gotgctl Get or set the Core Control Status Register. Read/Write
  92668. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  92669. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  92670. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  92671. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  92672. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  92673. + ## guid Get or set the value of the User ID Register Read/Write
  92674. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  92675. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  92676. + # enumspeed Gets the device enumeration Speed. Read
  92677. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  92678. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  92679. +
  92680. + test_status("TEST NYI") or die;
  92681. +}
  92682. +
  92683. +test_main();
  92684. +0;
  92685. diff -Nur linux-3.12.13/drivers/usb/host/Kconfig linux-raspberry-pi/drivers/usb/host/Kconfig
  92686. --- linux-3.12.13/drivers/usb/host/Kconfig 2014-02-22 22:32:50.000000000 +0100
  92687. +++ linux-raspberry-pi/drivers/usb/host/Kconfig 2014-03-11 17:51:27.000000000 +0100
  92688. @@ -650,6 +650,19 @@
  92689. To compile this driver a module, choose M here: the module
  92690. will be called "hwa-hc".
  92691. +config USB_DWCOTG
  92692. + tristate "Synopsis DWC host support"
  92693. + depends on USB
  92694. + help
  92695. + The Synopsis DWC controller is a dual-role
  92696. + host/peripheral/OTG ("On The Go") USB controllers.
  92697. +
  92698. + Enable this option to support this IP in host controller mode.
  92699. + If unsure, say N.
  92700. +
  92701. + To compile this driver as a module, choose M here: the
  92702. + modules built will be called dwc_otg and dwc_common_port.
  92703. +
  92704. config USB_IMX21_HCD
  92705. tristate "i.MX21 HCD support"
  92706. depends on ARM && ARCH_MXC
  92707. diff -Nur linux-3.12.13/drivers/usb/host/Makefile linux-raspberry-pi/drivers/usb/host/Makefile
  92708. --- linux-3.12.13/drivers/usb/host/Makefile 2014-02-22 22:32:50.000000000 +0100
  92709. +++ linux-raspberry-pi/drivers/usb/host/Makefile 2014-03-11 17:51:27.000000000 +0100
  92710. @@ -56,6 +56,8 @@
  92711. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  92712. obj-$(CONFIG_USB_ISP1760_HCD) += isp1760.o
  92713. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  92714. +
  92715. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  92716. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  92717. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
  92718. obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o
  92719. diff -Nur linux-3.12.13/drivers/usb/Makefile linux-raspberry-pi/drivers/usb/Makefile
  92720. --- linux-3.12.13/drivers/usb/Makefile 2014-02-22 22:32:50.000000000 +0100
  92721. +++ linux-raspberry-pi/drivers/usb/Makefile 2014-03-11 17:51:27.000000000 +0100
  92722. @@ -23,6 +23,7 @@
  92723. obj-$(CONFIG_USB_R8A66597_HCD) += host/
  92724. obj-$(CONFIG_USB_HWA_HCD) += host/
  92725. obj-$(CONFIG_USB_ISP1760_HCD) += host/
  92726. +obj-$(CONFIG_USB_DWCOTG) += host/
  92727. obj-$(CONFIG_USB_IMX21_HCD) += host/
  92728. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += host/
  92729. obj-$(CONFIG_USB_FUSBH200_HCD) += host/
  92730. diff -Nur linux-3.12.13/drivers/video/bcm2708_fb.c linux-raspberry-pi/drivers/video/bcm2708_fb.c
  92731. --- linux-3.12.13/drivers/video/bcm2708_fb.c 1970-01-01 01:00:00.000000000 +0100
  92732. +++ linux-raspberry-pi/drivers/video/bcm2708_fb.c 2014-03-11 17:51:27.000000000 +0100
  92733. @@ -0,0 +1,765 @@
  92734. +/*
  92735. + * linux/drivers/video/bcm2708_fb.c
  92736. + *
  92737. + * Copyright (C) 2010 Broadcom
  92738. + *
  92739. + * This file is subject to the terms and conditions of the GNU General Public
  92740. + * License. See the file COPYING in the main directory of this archive
  92741. + * for more details.
  92742. + *
  92743. + * Broadcom simple framebuffer driver
  92744. + *
  92745. + * This file is derived from cirrusfb.c
  92746. + * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  92747. + *
  92748. + */
  92749. +#include <linux/module.h>
  92750. +#include <linux/kernel.h>
  92751. +#include <linux/errno.h>
  92752. +#include <linux/string.h>
  92753. +#include <linux/slab.h>
  92754. +#include <linux/mm.h>
  92755. +#include <linux/fb.h>
  92756. +#include <linux/init.h>
  92757. +#include <linux/interrupt.h>
  92758. +#include <linux/ioport.h>
  92759. +#include <linux/list.h>
  92760. +#include <linux/platform_device.h>
  92761. +#include <linux/clk.h>
  92762. +#include <linux/printk.h>
  92763. +#include <linux/console.h>
  92764. +#include <linux/debugfs.h>
  92765. +
  92766. +#include <mach/dma.h>
  92767. +#include <mach/platform.h>
  92768. +#include <mach/vcio.h>
  92769. +
  92770. +#include <asm/sizes.h>
  92771. +#include <linux/io.h>
  92772. +#include <linux/dma-mapping.h>
  92773. +
  92774. +#ifdef BCM2708_FB_DEBUG
  92775. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  92776. +#else
  92777. +#define print_debug(fmt,...)
  92778. +#endif
  92779. +
  92780. +/* This is limited to 16 characters when displayed by X startup */
  92781. +static const char *bcm2708_name = "BCM2708 FB";
  92782. +
  92783. +#define DRIVER_NAME "bcm2708_fb"
  92784. +
  92785. +static int fbwidth = 800; /* module parameter */
  92786. +static int fbheight = 480; /* module parameter */
  92787. +static int fbdepth = 16; /* module parameter */
  92788. +static int fbswap = 0; /* module parameter */
  92789. +
  92790. +static u32 dma_busy_wait_threshold = 1<<15;
  92791. +module_param(dma_busy_wait_threshold, int, 0644);
  92792. +MODULE_PARM_DESC(dma_busy_wait_threshold, "Busy-wait for DMA completion below this area");
  92793. +
  92794. +/* this data structure describes each frame buffer device we find */
  92795. +
  92796. +struct fbinfo_s {
  92797. + u32 xres, yres, xres_virtual, yres_virtual;
  92798. + u32 pitch, bpp;
  92799. + u32 xoffset, yoffset;
  92800. + u32 base;
  92801. + u32 screen_size;
  92802. + u16 cmap[256];
  92803. +};
  92804. +
  92805. +struct bcm2708_fb_stats {
  92806. + struct debugfs_regset32 regset;
  92807. + u32 dma_copies;
  92808. + u32 dma_irqs;
  92809. +};
  92810. +
  92811. +struct bcm2708_fb {
  92812. + struct fb_info fb;
  92813. + struct platform_device *dev;
  92814. + struct fbinfo_s *info;
  92815. + dma_addr_t dma;
  92816. + u32 cmap[16];
  92817. + int dma_chan;
  92818. + int dma_irq;
  92819. + void __iomem *dma_chan_base;
  92820. + void *cb_base; /* DMA control blocks */
  92821. + dma_addr_t cb_handle;
  92822. + struct dentry *debugfs_dir;
  92823. + wait_queue_head_t dma_waitq;
  92824. + struct bcm2708_fb_stats stats;
  92825. +};
  92826. +
  92827. +#define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
  92828. +
  92829. +static void bcm2708_fb_debugfs_deinit(struct bcm2708_fb *fb)
  92830. +{
  92831. + debugfs_remove_recursive(fb->debugfs_dir);
  92832. + fb->debugfs_dir = NULL;
  92833. +}
  92834. +
  92835. +static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
  92836. +{
  92837. + static struct debugfs_reg32 stats_registers[] = {
  92838. + {
  92839. + "dma_copies",
  92840. + offsetof(struct bcm2708_fb_stats, dma_copies)
  92841. + },
  92842. + {
  92843. + "dma_irqs",
  92844. + offsetof(struct bcm2708_fb_stats, dma_irqs)
  92845. + },
  92846. + };
  92847. +
  92848. + fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
  92849. + if (!fb->debugfs_dir) {
  92850. + pr_warn("%s: could not create debugfs entry\n",
  92851. + __func__);
  92852. + return -EFAULT;
  92853. + }
  92854. +
  92855. + fb->stats.regset.regs = stats_registers;
  92856. + fb->stats.regset.nregs = ARRAY_SIZE(stats_registers);
  92857. + fb->stats.regset.base = &fb->stats;
  92858. +
  92859. + if (!debugfs_create_regset32(
  92860. + "stats", 0444, fb->debugfs_dir, &fb->stats.regset)) {
  92861. + pr_warn("%s: could not create statistics registers\n",
  92862. + __func__);
  92863. + goto fail;
  92864. + }
  92865. + return 0;
  92866. +
  92867. +fail:
  92868. + bcm2708_fb_debugfs_deinit(fb);
  92869. + return -EFAULT;
  92870. +}
  92871. +
  92872. +static int bcm2708_fb_set_bitfields(struct fb_var_screeninfo *var)
  92873. +{
  92874. + int ret = 0;
  92875. +
  92876. + memset(&var->transp, 0, sizeof(var->transp));
  92877. +
  92878. + var->red.msb_right = 0;
  92879. + var->green.msb_right = 0;
  92880. + var->blue.msb_right = 0;
  92881. +
  92882. + switch (var->bits_per_pixel) {
  92883. + case 1:
  92884. + case 2:
  92885. + case 4:
  92886. + case 8:
  92887. + var->red.length = var->bits_per_pixel;
  92888. + var->red.offset = 0;
  92889. + var->green.length = var->bits_per_pixel;
  92890. + var->green.offset = 0;
  92891. + var->blue.length = var->bits_per_pixel;
  92892. + var->blue.offset = 0;
  92893. + break;
  92894. + case 16:
  92895. + var->red.length = 5;
  92896. + var->blue.length = 5;
  92897. + /*
  92898. + * Green length can be 5 or 6 depending whether
  92899. + * we're operating in RGB555 or RGB565 mode.
  92900. + */
  92901. + if (var->green.length != 5 && var->green.length != 6)
  92902. + var->green.length = 6;
  92903. + break;
  92904. + case 24:
  92905. + var->red.length = 8;
  92906. + var->blue.length = 8;
  92907. + var->green.length = 8;
  92908. + break;
  92909. + case 32:
  92910. + var->red.length = 8;
  92911. + var->green.length = 8;
  92912. + var->blue.length = 8;
  92913. + var->transp.length = 8;
  92914. + break;
  92915. + default:
  92916. + ret = -EINVAL;
  92917. + break;
  92918. + }
  92919. +
  92920. + /*
  92921. + * >= 16bpp displays have separate colour component bitfields
  92922. + * encoded in the pixel data. Calculate their position from
  92923. + * the bitfield length defined above.
  92924. + */
  92925. + if (ret == 0 && var->bits_per_pixel >= 24 && fbswap) {
  92926. + var->blue.offset = 0;
  92927. + var->green.offset = var->blue.offset + var->blue.length;
  92928. + var->red.offset = var->green.offset + var->green.length;
  92929. + var->transp.offset = var->red.offset + var->red.length;
  92930. + } else if (ret == 0 && var->bits_per_pixel >= 24) {
  92931. + var->red.offset = 0;
  92932. + var->green.offset = var->red.offset + var->red.length;
  92933. + var->blue.offset = var->green.offset + var->green.length;
  92934. + var->transp.offset = var->blue.offset + var->blue.length;
  92935. + } else if (ret == 0 && var->bits_per_pixel >= 16) {
  92936. + var->blue.offset = 0;
  92937. + var->green.offset = var->blue.offset + var->blue.length;
  92938. + var->red.offset = var->green.offset + var->green.length;
  92939. + var->transp.offset = var->red.offset + var->red.length;
  92940. + }
  92941. +
  92942. + return ret;
  92943. +}
  92944. +
  92945. +static int bcm2708_fb_check_var(struct fb_var_screeninfo *var,
  92946. + struct fb_info *info)
  92947. +{
  92948. + /* info input, var output */
  92949. + int yres;
  92950. +
  92951. + /* info input, var output */
  92952. + print_debug("bcm2708_fb_check_var info(%p) %dx%d (%dx%d), %d, %d\n", info,
  92953. + info->var.xres, info->var.yres, info->var.xres_virtual,
  92954. + info->var.yres_virtual, (int)info->screen_size,
  92955. + info->var.bits_per_pixel);
  92956. + print_debug("bcm2708_fb_check_var var(%p) %dx%d (%dx%d), %d\n", var,
  92957. + var->xres, var->yres, var->xres_virtual, var->yres_virtual,
  92958. + var->bits_per_pixel);
  92959. +
  92960. + if (!var->bits_per_pixel)
  92961. + var->bits_per_pixel = 16;
  92962. +
  92963. + if (bcm2708_fb_set_bitfields(var) != 0) {
  92964. + pr_err("bcm2708_fb_check_var: invalid bits_per_pixel %d\n",
  92965. + var->bits_per_pixel);
  92966. + return -EINVAL;
  92967. + }
  92968. +
  92969. +
  92970. + if (var->xres_virtual < var->xres)
  92971. + var->xres_virtual = var->xres;
  92972. + /* use highest possible virtual resolution */
  92973. + if (var->yres_virtual == -1) {
  92974. + var->yres_virtual = 480;
  92975. +
  92976. + pr_err
  92977. + ("bcm2708_fb_check_var: virtual resolution set to maximum of %dx%d\n",
  92978. + var->xres_virtual, var->yres_virtual);
  92979. + }
  92980. + if (var->yres_virtual < var->yres)
  92981. + var->yres_virtual = var->yres;
  92982. +
  92983. + if (var->xoffset < 0)
  92984. + var->xoffset = 0;
  92985. + if (var->yoffset < 0)
  92986. + var->yoffset = 0;
  92987. +
  92988. + /* truncate xoffset and yoffset to maximum if too high */
  92989. + if (var->xoffset > var->xres_virtual - var->xres)
  92990. + var->xoffset = var->xres_virtual - var->xres - 1;
  92991. + if (var->yoffset > var->yres_virtual - var->yres)
  92992. + var->yoffset = var->yres_virtual - var->yres - 1;
  92993. +
  92994. + yres = var->yres;
  92995. + if (var->vmode & FB_VMODE_DOUBLE)
  92996. + yres *= 2;
  92997. + else if (var->vmode & FB_VMODE_INTERLACED)
  92998. + yres = (yres + 1) / 2;
  92999. +
  93000. + if (var->xres * yres > 1920 * 1200) {
  93001. + pr_err("bcm2708_fb_check_var: ERROR: Pixel size >= 1920x1200; "
  93002. + "special treatment required! (TODO)\n");
  93003. + return -EINVAL;
  93004. + }
  93005. +
  93006. + return 0;
  93007. +}
  93008. +
  93009. +static int bcm2708_fb_set_par(struct fb_info *info)
  93010. +{
  93011. + uint32_t val = 0;
  93012. + struct bcm2708_fb *fb = to_bcm2708(info);
  93013. + volatile struct fbinfo_s *fbinfo = fb->info;
  93014. + fbinfo->xres = info->var.xres;
  93015. + fbinfo->yres = info->var.yres;
  93016. + fbinfo->xres_virtual = info->var.xres_virtual;
  93017. + fbinfo->yres_virtual = info->var.yres_virtual;
  93018. + fbinfo->bpp = info->var.bits_per_pixel;
  93019. + fbinfo->xoffset = info->var.xoffset;
  93020. + fbinfo->yoffset = info->var.yoffset;
  93021. + fbinfo->base = 0; /* filled in by VC */
  93022. + fbinfo->pitch = 0; /* filled in by VC */
  93023. +
  93024. + print_debug("bcm2708_fb_set_par info(%p) %dx%d (%dx%d), %d, %d\n", info,
  93025. + info->var.xres, info->var.yres, info->var.xres_virtual,
  93026. + info->var.yres_virtual, (int)info->screen_size,
  93027. + info->var.bits_per_pixel);
  93028. +
  93029. + /* ensure last write to fbinfo is visible to GPU */
  93030. + wmb();
  93031. +
  93032. + /* inform vc about new framebuffer */
  93033. + bcm_mailbox_write(MBOX_CHAN_FB, fb->dma);
  93034. +
  93035. + /* TODO: replace fb driver with vchiq version */
  93036. + /* wait for response */
  93037. + bcm_mailbox_read(MBOX_CHAN_FB, &val);
  93038. +
  93039. + /* ensure GPU writes are visible to us */
  93040. + rmb();
  93041. +
  93042. + if (val == 0) {
  93043. + fb->fb.fix.line_length = fbinfo->pitch;
  93044. +
  93045. + if (info->var.bits_per_pixel <= 8)
  93046. + fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  93047. + else
  93048. + fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  93049. +
  93050. + fb->fb.fix.smem_start = fbinfo->base;
  93051. + fb->fb.fix.smem_len = fbinfo->pitch * fbinfo->yres_virtual;
  93052. + fb->fb.screen_size = fbinfo->screen_size;
  93053. + if (fb->fb.screen_base)
  93054. + iounmap(fb->fb.screen_base);
  93055. + fb->fb.screen_base =
  93056. + (void *)ioremap_wc(fb->fb.fix.smem_start, fb->fb.screen_size);
  93057. + if (!fb->fb.screen_base) {
  93058. + /* the console may currently be locked */
  93059. + console_trylock();
  93060. + console_unlock();
  93061. +
  93062. + BUG(); /* what can we do here */
  93063. + }
  93064. + }
  93065. + print_debug
  93066. + ("BCM2708FB: start = %p,%p width=%d, height=%d, bpp=%d, pitch=%d size=%d success=%d\n",
  93067. + (void *)fb->fb.screen_base, (void *)fb->fb.fix.smem_start,
  93068. + fbinfo->xres, fbinfo->yres, fbinfo->bpp,
  93069. + fbinfo->pitch, (int)fb->fb.screen_size, val);
  93070. +
  93071. + return val;
  93072. +}
  93073. +
  93074. +static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  93075. +{
  93076. + unsigned int mask = (1 << bf->length) - 1;
  93077. +
  93078. + return (val >> (16 - bf->length) & mask) << bf->offset;
  93079. +}
  93080. +
  93081. +
  93082. +static int bcm2708_fb_setcolreg(unsigned int regno, unsigned int red,
  93083. + unsigned int green, unsigned int blue,
  93084. + unsigned int transp, struct fb_info *info)
  93085. +{
  93086. + struct bcm2708_fb *fb = to_bcm2708(info);
  93087. +
  93088. + /*print_debug("BCM2708FB: setcolreg %d:(%02x,%02x,%02x,%02x) %x\n", regno, red, green, blue, transp, fb->fb.fix.visual);*/
  93089. + if (fb->fb.var.bits_per_pixel <= 8) {
  93090. + if (regno < 256) {
  93091. + /* blue [0:4], green [5:10], red [11:15] */
  93092. + fb->info->cmap[regno] = ((red >> (16-5)) & 0x1f) << 11 |
  93093. + ((green >> (16-6)) & 0x3f) << 5 |
  93094. + ((blue >> (16-5)) & 0x1f) << 0;
  93095. + }
  93096. + /* 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 */
  93097. + /* So just call it for what looks like the last colour in a list for now. */
  93098. + if (regno == 15 || regno == 255)
  93099. + bcm2708_fb_set_par(info);
  93100. + } else if (regno < 16) {
  93101. + fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  93102. + convert_bitfield(blue, &fb->fb.var.blue) |
  93103. + convert_bitfield(green, &fb->fb.var.green) |
  93104. + convert_bitfield(red, &fb->fb.var.red);
  93105. + }
  93106. + return regno > 255;
  93107. +}
  93108. +
  93109. +static int bcm2708_fb_blank(int blank_mode, struct fb_info *info)
  93110. +{
  93111. + /*print_debug("bcm2708_fb_blank\n"); */
  93112. + return -1;
  93113. +}
  93114. +
  93115. +static void bcm2708_fb_fillrect(struct fb_info *info,
  93116. + const struct fb_fillrect *rect)
  93117. +{
  93118. + /* (is called) print_debug("bcm2708_fb_fillrect\n"); */
  93119. + cfb_fillrect(info, rect);
  93120. +}
  93121. +
  93122. +/* A helper function for configuring dma control block */
  93123. +static void set_dma_cb(struct bcm2708_dma_cb *cb,
  93124. + int burst_size,
  93125. + dma_addr_t dst,
  93126. + int dst_stride,
  93127. + dma_addr_t src,
  93128. + int src_stride,
  93129. + int w,
  93130. + int h)
  93131. +{
  93132. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  93133. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  93134. + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
  93135. + cb->dst = dst;
  93136. + cb->src = src;
  93137. + /*
  93138. + * This is not really obvious from the DMA documentation,
  93139. + * but the top 16 bits must be programmmed to "height -1"
  93140. + * and not "height" in 2D mode.
  93141. + */
  93142. + cb->length = ((h - 1) << 16) | w;
  93143. + cb->stride = ((dst_stride - w) << 16) | (u16)(src_stride - w);
  93144. + cb->pad[0] = 0;
  93145. + cb->pad[1] = 0;
  93146. +}
  93147. +
  93148. +static void bcm2708_fb_copyarea(struct fb_info *info,
  93149. + const struct fb_copyarea *region)
  93150. +{
  93151. + struct bcm2708_fb *fb = to_bcm2708(info);
  93152. + struct bcm2708_dma_cb *cb = fb->cb_base;
  93153. + int bytes_per_pixel = (info->var.bits_per_pixel + 7) >> 3;
  93154. + /* Channel 0 supports larger bursts and is a bit faster */
  93155. + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
  93156. + int pixels = region->width * region->height;
  93157. +
  93158. + /* Fallback to cfb_copyarea() if we don't like something */
  93159. + if (bytes_per_pixel > 4 ||
  93160. + info->var.xres * info->var.yres > 1920 * 1200 ||
  93161. + region->width <= 0 || region->width > info->var.xres ||
  93162. + region->height <= 0 || region->height > info->var.yres ||
  93163. + region->sx < 0 || region->sx >= info->var.xres ||
  93164. + region->sy < 0 || region->sy >= info->var.yres ||
  93165. + region->dx < 0 || region->dx >= info->var.xres ||
  93166. + region->dy < 0 || region->dy >= info->var.yres ||
  93167. + region->sx + region->width > info->var.xres ||
  93168. + region->dx + region->width > info->var.xres ||
  93169. + region->sy + region->height > info->var.yres ||
  93170. + region->dy + region->height > info->var.yres) {
  93171. + cfb_copyarea(info, region);
  93172. + return;
  93173. + }
  93174. +
  93175. + if (region->dy == region->sy && region->dx > region->sx) {
  93176. + /*
  93177. + * A difficult case of overlapped copy. Because DMA can't
  93178. + * copy individual scanlines in backwards direction, we need
  93179. + * two-pass processing. We do it by programming a chain of dma
  93180. + * control blocks in the first 16K part of the buffer and use
  93181. + * the remaining 48K as the intermediate temporary scratch
  93182. + * buffer. The buffer size is sufficient to handle up to
  93183. + * 1920x1200 resolution at 32bpp pixel depth.
  93184. + */
  93185. + int y;
  93186. + dma_addr_t control_block_pa = fb->cb_handle;
  93187. + dma_addr_t scratchbuf = fb->cb_handle + 16 * 1024;
  93188. + int scanline_size = bytes_per_pixel * region->width;
  93189. + int scanlines_per_cb = (64 * 1024 - 16 * 1024) / scanline_size;
  93190. +
  93191. + for (y = 0; y < region->height; y += scanlines_per_cb) {
  93192. + dma_addr_t src =
  93193. + fb->fb.fix.smem_start +
  93194. + bytes_per_pixel * region->sx +
  93195. + (region->sy + y) * fb->fb.fix.line_length;
  93196. + dma_addr_t dst =
  93197. + fb->fb.fix.smem_start +
  93198. + bytes_per_pixel * region->dx +
  93199. + (region->dy + y) * fb->fb.fix.line_length;
  93200. +
  93201. + if (region->height - y < scanlines_per_cb)
  93202. + scanlines_per_cb = region->height - y;
  93203. +
  93204. + set_dma_cb(cb, burst_size, scratchbuf, scanline_size,
  93205. + src, fb->fb.fix.line_length,
  93206. + scanline_size, scanlines_per_cb);
  93207. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  93208. + cb->next = control_block_pa;
  93209. + cb++;
  93210. +
  93211. + set_dma_cb(cb, burst_size, dst, fb->fb.fix.line_length,
  93212. + scratchbuf, scanline_size,
  93213. + scanline_size, scanlines_per_cb);
  93214. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  93215. + cb->next = control_block_pa;
  93216. + cb++;
  93217. + }
  93218. + /* move the pointer back to the last dma control block */
  93219. + cb--;
  93220. + } else {
  93221. + /* A single dma control block is enough. */
  93222. + int sy, dy, stride;
  93223. + if (region->dy <= region->sy) {
  93224. + /* processing from top to bottom */
  93225. + dy = region->dy;
  93226. + sy = region->sy;
  93227. + stride = fb->fb.fix.line_length;
  93228. + } else {
  93229. + /* processing from bottom to top */
  93230. + dy = region->dy + region->height - 1;
  93231. + sy = region->sy + region->height - 1;
  93232. + stride = -fb->fb.fix.line_length;
  93233. + }
  93234. + set_dma_cb(cb, burst_size,
  93235. + fb->fb.fix.smem_start + dy * fb->fb.fix.line_length +
  93236. + bytes_per_pixel * region->dx,
  93237. + stride,
  93238. + fb->fb.fix.smem_start + sy * fb->fb.fix.line_length +
  93239. + bytes_per_pixel * region->sx,
  93240. + stride,
  93241. + region->width * bytes_per_pixel,
  93242. + region->height);
  93243. + }
  93244. +
  93245. + /* end of dma control blocks chain */
  93246. + cb->next = 0;
  93247. +
  93248. +
  93249. + if (pixels < dma_busy_wait_threshold) {
  93250. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  93251. + bcm_dma_wait_idle(fb->dma_chan_base);
  93252. + } else {
  93253. + void __iomem *dma_chan = fb->dma_chan_base;
  93254. + cb->info |= BCM2708_DMA_INT_EN;
  93255. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  93256. + while (bcm_dma_is_busy(dma_chan)) {
  93257. + wait_event_interruptible(
  93258. + fb->dma_waitq,
  93259. + !bcm_dma_is_busy(dma_chan));
  93260. + }
  93261. + fb->stats.dma_irqs++;
  93262. + }
  93263. + fb->stats.dma_copies++;
  93264. +}
  93265. +
  93266. +static void bcm2708_fb_imageblit(struct fb_info *info,
  93267. + const struct fb_image *image)
  93268. +{
  93269. + /* (is called) print_debug("bcm2708_fb_imageblit\n"); */
  93270. + cfb_imageblit(info, image);
  93271. +}
  93272. +
  93273. +static irqreturn_t bcm2708_fb_dma_irq(int irq, void *cxt)
  93274. +{
  93275. + struct bcm2708_fb *fb = cxt;
  93276. +
  93277. + /* FIXME: should read status register to check if this is
  93278. + * actually interrupting us or not, in case this interrupt
  93279. + * ever becomes shared amongst several DMA channels
  93280. + *
  93281. + * readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_IRQ;
  93282. + */
  93283. +
  93284. + /* acknowledge the interrupt */
  93285. + writel(BCM2708_DMA_INT, fb->dma_chan_base + BCM2708_DMA_CS);
  93286. +
  93287. + wake_up(&fb->dma_waitq);
  93288. + return IRQ_HANDLED;
  93289. +}
  93290. +
  93291. +static struct fb_ops bcm2708_fb_ops = {
  93292. + .owner = THIS_MODULE,
  93293. + .fb_check_var = bcm2708_fb_check_var,
  93294. + .fb_set_par = bcm2708_fb_set_par,
  93295. + .fb_setcolreg = bcm2708_fb_setcolreg,
  93296. + .fb_blank = bcm2708_fb_blank,
  93297. + .fb_fillrect = bcm2708_fb_fillrect,
  93298. + .fb_copyarea = bcm2708_fb_copyarea,
  93299. + .fb_imageblit = bcm2708_fb_imageblit,
  93300. +};
  93301. +
  93302. +static int bcm2708_fb_register(struct bcm2708_fb *fb)
  93303. +{
  93304. + int ret;
  93305. + dma_addr_t dma;
  93306. + void *mem;
  93307. +
  93308. + mem =
  93309. + dma_alloc_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), &dma,
  93310. + GFP_KERNEL);
  93311. +
  93312. + if (NULL == mem) {
  93313. + pr_err(": unable to allocate fbinfo buffer\n");
  93314. + ret = -ENOMEM;
  93315. + } else {
  93316. + fb->info = (struct fbinfo_s *)mem;
  93317. + fb->dma = dma;
  93318. + }
  93319. + fb->fb.fbops = &bcm2708_fb_ops;
  93320. + fb->fb.flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_COPYAREA;
  93321. + fb->fb.pseudo_palette = fb->cmap;
  93322. +
  93323. + strncpy(fb->fb.fix.id, bcm2708_name, sizeof(fb->fb.fix.id));
  93324. + fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  93325. + fb->fb.fix.type_aux = 0;
  93326. + fb->fb.fix.xpanstep = 0;
  93327. + fb->fb.fix.ypanstep = 0;
  93328. + fb->fb.fix.ywrapstep = 0;
  93329. + fb->fb.fix.accel = FB_ACCEL_NONE;
  93330. +
  93331. + fb->fb.var.xres = fbwidth;
  93332. + fb->fb.var.yres = fbheight;
  93333. + fb->fb.var.xres_virtual = fbwidth;
  93334. + fb->fb.var.yres_virtual = fbheight;
  93335. + fb->fb.var.bits_per_pixel = fbdepth;
  93336. + fb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  93337. + fb->fb.var.activate = FB_ACTIVATE_NOW;
  93338. + fb->fb.var.nonstd = 0;
  93339. + fb->fb.var.height = -1; /* height of picture in mm */
  93340. + fb->fb.var.width = -1; /* width of picture in mm */
  93341. + fb->fb.var.accel_flags = 0;
  93342. +
  93343. + fb->fb.monspecs.hfmin = 0;
  93344. + fb->fb.monspecs.hfmax = 100000;
  93345. + fb->fb.monspecs.vfmin = 0;
  93346. + fb->fb.monspecs.vfmax = 400;
  93347. + fb->fb.monspecs.dclkmin = 1000000;
  93348. + fb->fb.monspecs.dclkmax = 100000000;
  93349. +
  93350. + bcm2708_fb_set_bitfields(&fb->fb.var);
  93351. + init_waitqueue_head(&fb->dma_waitq);
  93352. +
  93353. + /*
  93354. + * Allocate colourmap.
  93355. + */
  93356. +
  93357. + fb_set_var(&fb->fb, &fb->fb.var);
  93358. +
  93359. + print_debug("BCM2708FB: registering framebuffer (%dx%d@%d) (%d)\n", fbwidth
  93360. + fbheight, fbdepth, fbswap);
  93361. +
  93362. + ret = register_framebuffer(&fb->fb);
  93363. + print_debug("BCM2708FB: register framebuffer (%d)\n", ret);
  93364. + if (ret == 0)
  93365. + goto out;
  93366. +
  93367. + print_debug("BCM2708FB: cannot register framebuffer (%d)\n", ret);
  93368. +out:
  93369. + return ret;
  93370. +}
  93371. +
  93372. +static int bcm2708_fb_probe(struct platform_device *dev)
  93373. +{
  93374. + struct bcm2708_fb *fb;
  93375. + int ret;
  93376. +
  93377. + fb = kzalloc(sizeof(struct bcm2708_fb), GFP_KERNEL);
  93378. + if (!fb) {
  93379. + dev_err(&dev->dev,
  93380. + "could not allocate new bcm2708_fb struct\n");
  93381. + ret = -ENOMEM;
  93382. + goto free_region;
  93383. + }
  93384. +
  93385. + bcm2708_fb_debugfs_init(fb);
  93386. +
  93387. +
  93388. + bcm2708_fb_debugfs_init(fb);
  93389. +
  93390. + fb->cb_base = dma_alloc_writecombine(&dev->dev, SZ_64K,
  93391. + &fb->cb_handle, GFP_KERNEL);
  93392. + if (!fb->cb_base) {
  93393. + dev_err(&dev->dev, "cannot allocate DMA CBs\n");
  93394. + ret = -ENOMEM;
  93395. + goto free_fb;
  93396. + }
  93397. +
  93398. + pr_info("BCM2708FB: allocated DMA memory %08x\n",
  93399. + fb->cb_handle);
  93400. +
  93401. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_BULK,
  93402. + &fb->dma_chan_base, &fb->dma_irq);
  93403. + if (ret < 0) {
  93404. + dev_err(&dev->dev, "couldn't allocate a DMA channel\n");
  93405. + goto free_cb;
  93406. + }
  93407. + fb->dma_chan = ret;
  93408. +
  93409. + ret = request_irq(fb->dma_irq, bcm2708_fb_dma_irq,
  93410. + 0, "bcm2708_fb dma", fb);
  93411. + if (ret) {
  93412. + pr_err("%s: failed to request DMA irq\n", __func__);
  93413. + goto free_dma_chan;
  93414. + }
  93415. +
  93416. +
  93417. + pr_info("BCM2708FB: allocated DMA channel %d @ %p\n",
  93418. + fb->dma_chan, fb->dma_chan_base);
  93419. +
  93420. + fb->dev = dev;
  93421. +
  93422. + ret = bcm2708_fb_register(fb);
  93423. + if (ret == 0) {
  93424. + platform_set_drvdata(dev, fb);
  93425. + goto out;
  93426. + }
  93427. +
  93428. +free_dma_chan:
  93429. + bcm_dma_chan_free(fb->dma_chan);
  93430. +free_cb:
  93431. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  93432. +free_fb:
  93433. + kfree(fb);
  93434. +free_region:
  93435. + dev_err(&dev->dev, "probe failed, err %d\n", ret);
  93436. +out:
  93437. + return ret;
  93438. +}
  93439. +
  93440. +static int bcm2708_fb_remove(struct platform_device *dev)
  93441. +{
  93442. + struct bcm2708_fb *fb = platform_get_drvdata(dev);
  93443. +
  93444. + platform_set_drvdata(dev, NULL);
  93445. +
  93446. + if (fb->fb.screen_base)
  93447. + iounmap(fb->fb.screen_base);
  93448. + unregister_framebuffer(&fb->fb);
  93449. +
  93450. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  93451. + bcm_dma_chan_free(fb->dma_chan);
  93452. +
  93453. + dma_free_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), (void *)fb->info,
  93454. + fb->dma);
  93455. + bcm2708_fb_debugfs_deinit(fb);
  93456. +
  93457. + free_irq(fb->dma_irq, fb);
  93458. +
  93459. + kfree(fb);
  93460. +
  93461. + return 0;
  93462. +}
  93463. +
  93464. +static struct platform_driver bcm2708_fb_driver = {
  93465. + .probe = bcm2708_fb_probe,
  93466. + .remove = bcm2708_fb_remove,
  93467. + .driver = {
  93468. + .name = DRIVER_NAME,
  93469. + .owner = THIS_MODULE,
  93470. + },
  93471. +};
  93472. +
  93473. +static int __init bcm2708_fb_init(void)
  93474. +{
  93475. + return platform_driver_register(&bcm2708_fb_driver);
  93476. +}
  93477. +
  93478. +module_init(bcm2708_fb_init);
  93479. +
  93480. +static void __exit bcm2708_fb_exit(void)
  93481. +{
  93482. + platform_driver_unregister(&bcm2708_fb_driver);
  93483. +}
  93484. +
  93485. +module_exit(bcm2708_fb_exit);
  93486. +
  93487. +module_param(fbwidth, int, 0644);
  93488. +module_param(fbheight, int, 0644);
  93489. +module_param(fbdepth, int, 0644);
  93490. +module_param(fbswap, int, 0644);
  93491. +
  93492. +MODULE_DESCRIPTION("BCM2708 framebuffer driver");
  93493. +MODULE_LICENSE("GPL");
  93494. +
  93495. +MODULE_PARM_DESC(fbwidth, "Width of ARM Framebuffer");
  93496. +MODULE_PARM_DESC(fbheight, "Height of ARM Framebuffer");
  93497. +MODULE_PARM_DESC(fbdepth, "Bit depth of ARM Framebuffer");
  93498. +MODULE_PARM_DESC(fbswap, "Swap order of red and blue in 24 and 32 bit modes");
  93499. diff -Nur linux-3.12.13/drivers/video/cfbimgblt.c linux-raspberry-pi/drivers/video/cfbimgblt.c
  93500. --- linux-3.12.13/drivers/video/cfbimgblt.c 2014-02-22 22:32:50.000000000 +0100
  93501. +++ linux-raspberry-pi/drivers/video/cfbimgblt.c 2014-03-11 17:33:07.000000000 +0100
  93502. @@ -28,6 +28,11 @@
  93503. *
  93504. * Also need to add code to deal with cards endians that are different than
  93505. * the native cpu endians. I also need to deal with MSB position in the word.
  93506. + * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  93507. + * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  93508. + * significantly faster than the previous implementation.
  93509. + * - Simplify the fast/slow_imageblit selection code, avoiding integer
  93510. + * divides.
  93511. */
  93512. #include <linux/module.h>
  93513. #include <linux/string.h>
  93514. @@ -262,6 +267,133 @@
  93515. }
  93516. }
  93517. +/*
  93518. + * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  93519. + * into the code, main loop unrolled.
  93520. + */
  93521. +
  93522. +static inline void fast_imageblit16(const struct fb_image *image,
  93523. + struct fb_info *p, u8 __iomem * dst1,
  93524. + u32 fgcolor, u32 bgcolor)
  93525. +{
  93526. + u32 fgx = fgcolor, bgx = bgcolor;
  93527. + u32 spitch = (image->width + 7) / 8;
  93528. + u32 end_mask, eorx;
  93529. + const char *s = image->data, *src;
  93530. + u32 __iomem *dst;
  93531. + const u32 *tab = NULL;
  93532. + int i, j, k;
  93533. +
  93534. + tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  93535. +
  93536. + fgx <<= 16;
  93537. + bgx <<= 16;
  93538. + fgx |= fgcolor;
  93539. + bgx |= bgcolor;
  93540. +
  93541. + eorx = fgx ^ bgx;
  93542. + k = image->width / 2;
  93543. +
  93544. + for (i = image->height; i--;) {
  93545. + dst = (u32 __iomem *) dst1;
  93546. + src = s;
  93547. +
  93548. + j = k;
  93549. + while (j >= 4) {
  93550. + u8 bits = *src;
  93551. + end_mask = tab[(bits >> 6) & 3];
  93552. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93553. + end_mask = tab[(bits >> 4) & 3];
  93554. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93555. + end_mask = tab[(bits >> 2) & 3];
  93556. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93557. + end_mask = tab[bits & 3];
  93558. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93559. + src++;
  93560. + j -= 4;
  93561. + }
  93562. + if (j != 0) {
  93563. + u8 bits = *src;
  93564. + end_mask = tab[(bits >> 6) & 3];
  93565. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93566. + if (j >= 2) {
  93567. + end_mask = tab[(bits >> 4) & 3];
  93568. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93569. + if (j == 3) {
  93570. + end_mask = tab[(bits >> 2) & 3];
  93571. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  93572. + }
  93573. + }
  93574. + }
  93575. + dst1 += p->fix.line_length;
  93576. + s += spitch;
  93577. + }
  93578. +}
  93579. +
  93580. +/*
  93581. + * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  93582. + * into the code, main loop unrolled.
  93583. + */
  93584. +
  93585. +static inline void fast_imageblit32(const struct fb_image *image,
  93586. + struct fb_info *p, u8 __iomem * dst1,
  93587. + u32 fgcolor, u32 bgcolor)
  93588. +{
  93589. + u32 fgx = fgcolor, bgx = bgcolor;
  93590. + u32 spitch = (image->width + 7) / 8;
  93591. + u32 end_mask, eorx;
  93592. + const char *s = image->data, *src;
  93593. + u32 __iomem *dst;
  93594. + const u32 *tab = NULL;
  93595. + int i, j, k;
  93596. +
  93597. + tab = cfb_tab32;
  93598. +
  93599. + eorx = fgx ^ bgx;
  93600. + k = image->width;
  93601. +
  93602. + for (i = image->height; i--;) {
  93603. + dst = (u32 __iomem *) dst1;
  93604. + src = s;
  93605. +
  93606. + j = k;
  93607. + while (j >= 8) {
  93608. + u8 bits = *src;
  93609. + end_mask = tab[(bits >> 7) & 1];
  93610. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93611. + end_mask = tab[(bits >> 6) & 1];
  93612. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93613. + end_mask = tab[(bits >> 5) & 1];
  93614. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93615. + end_mask = tab[(bits >> 4) & 1];
  93616. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93617. + end_mask = tab[(bits >> 3) & 1];
  93618. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93619. + end_mask = tab[(bits >> 2) & 1];
  93620. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93621. + end_mask = tab[(bits >> 1) & 1];
  93622. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93623. + end_mask = tab[bits & 1];
  93624. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93625. + src++;
  93626. + j -= 8;
  93627. + }
  93628. + if (j != 0) {
  93629. + u32 bits = (u32) * src;
  93630. + while (j > 1) {
  93631. + end_mask = tab[(bits >> 7) & 1];
  93632. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93633. + bits <<= 1;
  93634. + j--;
  93635. + }
  93636. + end_mask = tab[(bits >> 7) & 1];
  93637. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  93638. + }
  93639. + dst1 += p->fix.line_length;
  93640. + s += spitch;
  93641. + }
  93642. +}
  93643. +
  93644. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  93645. {
  93646. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  93647. @@ -294,11 +426,21 @@
  93648. bgcolor = image->bg_color;
  93649. }
  93650. - if (32 % bpp == 0 && !start_index && !pitch_index &&
  93651. - ((width & (32/bpp-1)) == 0) &&
  93652. - bpp >= 8 && bpp <= 32)
  93653. - fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  93654. - else
  93655. + if (!start_index && !pitch_index) {
  93656. + if (bpp == 32)
  93657. + fast_imageblit32(image, p, dst1, fgcolor,
  93658. + bgcolor);
  93659. + else if (bpp == 16 && (width & 1) == 0)
  93660. + fast_imageblit16(image, p, dst1, fgcolor,
  93661. + bgcolor);
  93662. + else if (bpp == 8 && (width & 3) == 0)
  93663. + fast_imageblit(image, p, dst1, fgcolor,
  93664. + bgcolor);
  93665. + else
  93666. + slow_imageblit(image, p, dst1, fgcolor,
  93667. + bgcolor,
  93668. + start_index, pitch_index);
  93669. + } else
  93670. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  93671. start_index, pitch_index);
  93672. } else
  93673. diff -Nur linux-3.12.13/drivers/video/fbmem.c linux-raspberry-pi/drivers/video/fbmem.c
  93674. --- linux-3.12.13/drivers/video/fbmem.c 2014-02-22 22:32:50.000000000 +0100
  93675. +++ linux-raspberry-pi/drivers/video/fbmem.c 2014-03-11 17:51:27.000000000 +0100
  93676. @@ -1083,6 +1083,25 @@
  93677. }
  93678. EXPORT_SYMBOL(fb_blank);
  93679. +static int fb_copyarea_user(struct fb_info *info,
  93680. + struct fb_copyarea *copy)
  93681. +{
  93682. + int ret = 0;
  93683. + if (!lock_fb_info(info))
  93684. + return -ENODEV;
  93685. + if (copy->dx + copy->width > info->var.xres ||
  93686. + copy->sx + copy->width > info->var.xres ||
  93687. + copy->dy + copy->height > info->var.yres ||
  93688. + copy->sy + copy->height > info->var.yres) {
  93689. + ret = -EINVAL;
  93690. + goto out;
  93691. + }
  93692. + info->fbops->fb_copyarea(info, copy);
  93693. +out:
  93694. + unlock_fb_info(info);
  93695. + return ret;
  93696. +}
  93697. +
  93698. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  93699. unsigned long arg)
  93700. {
  93701. @@ -1093,6 +1112,7 @@
  93702. struct fb_cmap cmap_from;
  93703. struct fb_cmap_user cmap;
  93704. struct fb_event event;
  93705. + struct fb_copyarea copy;
  93706. void __user *argp = (void __user *)arg;
  93707. long ret = 0;
  93708. @@ -1202,6 +1222,15 @@
  93709. console_unlock();
  93710. unlock_fb_info(info);
  93711. break;
  93712. + case FBIOCOPYAREA:
  93713. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  93714. + /* only provide this ioctl if it is accelerated */
  93715. + if (copy_from_user(&copy, argp, sizeof(copy)))
  93716. + return -EFAULT;
  93717. + ret = fb_copyarea_user(info, &copy);
  93718. + break;
  93719. + }
  93720. + /* fall through */
  93721. default:
  93722. if (!lock_fb_info(info))
  93723. return -ENODEV;
  93724. @@ -1356,6 +1385,7 @@
  93725. case FBIOPAN_DISPLAY:
  93726. case FBIOGET_CON2FBMAP:
  93727. case FBIOPUT_CON2FBMAP:
  93728. + case FBIOCOPYAREA:
  93729. arg = (unsigned long) compat_ptr(arg);
  93730. case FBIOBLANK:
  93731. ret = do_fb_ioctl(info, cmd, arg);
  93732. diff -Nur linux-3.12.13/drivers/video/Kconfig linux-raspberry-pi/drivers/video/Kconfig
  93733. --- linux-3.12.13/drivers/video/Kconfig 2014-02-22 22:32:50.000000000 +0100
  93734. +++ linux-raspberry-pi/drivers/video/Kconfig 2014-03-11 17:51:27.000000000 +0100
  93735. @@ -310,6 +310,20 @@
  93736. help
  93737. Support the Permedia2 FIFO disconnect feature.
  93738. +config FB_BCM2708
  93739. + tristate "BCM2708 framebuffer support"
  93740. + depends on FB && ARM
  93741. + select FB_CFB_FILLRECT
  93742. + select FB_CFB_COPYAREA
  93743. + select FB_CFB_IMAGEBLIT
  93744. + help
  93745. + This framebuffer device driver is for the BCM2708 framebuffer.
  93746. +
  93747. + If you want to compile this as a module (=code which can be
  93748. + inserted into and removed from the running kernel), say M
  93749. + here and read <file:Documentation/kbuild/modules.txt>. The module
  93750. + will be called bcm2708_fb.
  93751. +
  93752. config FB_ARMCLCD
  93753. tristate "ARM PrimeCell PL110 support"
  93754. depends on FB && ARM && ARM_AMBA
  93755. diff -Nur linux-3.12.13/drivers/video/logo/logo_linux_clut224.ppm linux-raspberry-pi/drivers/video/logo/logo_linux_clut224.ppm
  93756. --- linux-3.12.13/drivers/video/logo/logo_linux_clut224.ppm 2014-02-22 22:32:50.000000000 +0100
  93757. +++ linux-raspberry-pi/drivers/video/logo/logo_linux_clut224.ppm 2014-03-11 17:33:08.000000000 +0100
  93758. @@ -1,1604 +1,883 @@
  93759. P3
  93760. -# Standard 224-color Linux logo
  93761. -80 80
  93762. +63 80
  93763. 255
  93764. - 0 0 0 0 0 0 0 0 0 0 0 0
  93765. - 0 0 0 0 0 0 0 0 0 0 0 0
  93766. - 0 0 0 0 0 0 0 0 0 0 0 0
  93767. - 0 0 0 0 0 0 0 0 0 0 0 0
  93768. - 0 0 0 0 0 0 0 0 0 0 0 0
  93769. - 0 0 0 0 0 0 0 0 0 0 0 0
  93770. - 0 0 0 0 0 0 0 0 0 0 0 0
  93771. - 0 0 0 0 0 0 0 0 0 0 0 0
  93772. - 0 0 0 0 0 0 0 0 0 0 0 0
  93773. - 6 6 6 6 6 6 10 10 10 10 10 10
  93774. - 10 10 10 6 6 6 6 6 6 6 6 6
  93775. - 0 0 0 0 0 0 0 0 0 0 0 0
  93776. - 0 0 0 0 0 0 0 0 0 0 0 0
  93777. - 0 0 0 0 0 0 0 0 0 0 0 0
  93778. - 0 0 0 0 0 0 0 0 0 0 0 0
  93779. - 0 0 0 0 0 0 0 0 0 0 0 0
  93780. - 0 0 0 0 0 0 0 0 0 0 0 0
  93781. - 0 0 0 0 0 0 0 0 0 0 0 0
  93782. - 0 0 0 0 0 0 0 0 0 0 0 0
  93783. - 0 0 0 0 0 0 0 0 0 0 0 0
  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 6 6 6 10 10 10 14 14 14
  93793. - 22 22 22 26 26 26 30 30 30 34 34 34
  93794. - 30 30 30 30 30 30 26 26 26 18 18 18
  93795. - 14 14 14 10 10 10 6 6 6 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 1 0 0 1 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. - 6 6 6 14 14 14 26 26 26 42 42 42
  93813. - 54 54 54 66 66 66 78 78 78 78 78 78
  93814. - 78 78 78 74 74 74 66 66 66 54 54 54
  93815. - 42 42 42 26 26 26 18 18 18 10 10 10
  93816. - 6 6 6 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 1 0 0 0 0 0 0 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 10 10 10
  93832. - 22 22 22 42 42 42 66 66 66 86 86 86
  93833. - 66 66 66 38 38 38 38 38 38 22 22 22
  93834. - 26 26 26 34 34 34 54 54 54 66 66 66
  93835. - 86 86 86 70 70 70 46 46 46 26 26 26
  93836. - 14 14 14 6 6 6 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 1 0 0 1 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 10 10 10 26 26 26
  93852. - 50 50 50 82 82 82 58 58 58 6 6 6
  93853. - 2 2 6 2 2 6 2 2 6 2 2 6
  93854. - 2 2 6 2 2 6 2 2 6 2 2 6
  93855. - 6 6 6 54 54 54 86 86 86 66 66 66
  93856. - 38 38 38 18 18 18 6 6 6 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 0 0 0 0 0 0 0 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 6 6 6 22 22 22 50 50 50
  93872. - 78 78 78 34 34 34 2 2 6 2 2 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. - 2 2 6 2 2 6 6 6 6 70 70 70
  93876. - 78 78 78 46 46 46 22 22 22 6 6 6
  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 1 0 0 1 0 0 1 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. - 6 6 6 18 18 18 42 42 42 82 82 82
  93892. - 26 26 26 2 2 6 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 14 14 14
  93895. - 46 46 46 34 34 34 6 6 6 2 2 6
  93896. - 42 42 42 78 78 78 42 42 42 18 18 18
  93897. - 6 6 6 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 0 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. - 10 10 10 30 30 30 66 66 66 58 58 58
  93912. - 2 2 6 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 26 26 26
  93915. - 86 86 86 101 101 101 46 46 46 10 10 10
  93916. - 2 2 6 58 58 58 70 70 70 34 34 34
  93917. - 10 10 10 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 1 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. - 14 14 14 42 42 42 86 86 86 10 10 10
  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 30 30 30
  93935. - 94 94 94 94 94 94 58 58 58 26 26 26
  93936. - 2 2 6 6 6 6 78 78 78 54 54 54
  93937. - 22 22 22 6 6 6 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 0 0 0 0 0 0 0 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 6 6 6
  93951. - 22 22 22 62 62 62 62 62 62 2 2 6
  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 26 26 26
  93955. - 54 54 54 38 38 38 18 18 18 10 10 10
  93956. - 2 2 6 2 2 6 34 34 34 82 82 82
  93957. - 38 38 38 14 14 14 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 1 0 0 1 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. - 30 30 30 78 78 78 30 30 30 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 10 10 10
  93975. - 10 10 10 2 2 6 2 2 6 2 2 6
  93976. - 2 2 6 2 2 6 2 2 6 78 78 78
  93977. - 50 50 50 18 18 18 6 6 6 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 1 0 0 0 0 0 0 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 10 10 10
  93991. - 38 38 38 86 86 86 14 14 14 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 2 2 6
  93995. - 2 2 6 2 2 6 2 2 6 2 2 6
  93996. - 2 2 6 2 2 6 2 2 6 54 54 54
  93997. - 66 66 66 26 26 26 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 0 0 0 1 0 0 1 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 14 14 14
  94011. - 42 42 42 82 82 82 2 2 6 2 2 6
  94012. - 2 2 6 6 6 6 10 10 10 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 6 6 6
  94015. - 14 14 14 10 10 10 2 2 6 2 2 6
  94016. - 2 2 6 2 2 6 2 2 6 18 18 18
  94017. - 82 82 82 34 34 34 10 10 10 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 1 0 0 0 0 0 0 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. - 46 46 46 86 86 86 2 2 6 2 2 6
  94032. - 6 6 6 6 6 6 22 22 22 34 34 34
  94033. - 6 6 6 2 2 6 2 2 6 2 2 6
  94034. - 2 2 6 2 2 6 18 18 18 34 34 34
  94035. - 10 10 10 50 50 50 22 22 22 2 2 6
  94036. - 2 2 6 2 2 6 2 2 6 10 10 10
  94037. - 86 86 86 42 42 42 14 14 14 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 1 0 0 1 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. - 38 38 38 116 116 116 94 94 94 22 22 22
  94053. - 22 22 22 2 2 6 2 2 6 2 2 6
  94054. - 14 14 14 86 86 86 138 138 138 162 162 162
  94055. -154 154 154 38 38 38 26 26 26 6 6 6
  94056. - 2 2 6 2 2 6 2 2 6 2 2 6
  94057. - 86 86 86 46 46 46 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 0 0 0 0 0 0 0 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 14 14 14
  94072. -134 134 134 198 198 198 195 195 195 116 116 116
  94073. - 10 10 10 2 2 6 2 2 6 6 6 6
  94074. -101 98 89 187 187 187 210 210 210 218 218 218
  94075. -214 214 214 134 134 134 14 14 14 6 6 6
  94076. - 2 2 6 2 2 6 2 2 6 2 2 6
  94077. - 86 86 86 50 50 50 18 18 18 6 6 6
  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 1 0 0 0
  94085. - 0 0 1 0 0 1 0 0 1 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 54 54 54
  94092. -218 218 218 195 195 195 226 226 226 246 246 246
  94093. - 58 58 58 2 2 6 2 2 6 30 30 30
  94094. -210 210 210 253 253 253 174 174 174 123 123 123
  94095. -221 221 221 234 234 234 74 74 74 2 2 6
  94096. - 2 2 6 2 2 6 2 2 6 2 2 6
  94097. - 70 70 70 58 58 58 22 22 22 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 0 0 0 0
  94105. - 0 0 0 0 0 0 0 0 0 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 82 82 82 2 2 6 106 106 106
  94112. -170 170 170 26 26 26 86 86 86 226 226 226
  94113. -123 123 123 10 10 10 14 14 14 46 46 46
  94114. -231 231 231 190 190 190 6 6 6 70 70 70
  94115. - 90 90 90 238 238 238 158 158 158 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 1 0 0 0
  94125. - 0 0 1 0 0 1 0 0 1 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. - 42 42 42 86 86 86 6 6 6 116 116 116
  94132. -106 106 106 6 6 6 70 70 70 149 149 149
  94133. -128 128 128 18 18 18 38 38 38 54 54 54
  94134. -221 221 221 106 106 106 2 2 6 14 14 14
  94135. - 46 46 46 190 190 190 198 198 198 2 2 6
  94136. - 2 2 6 2 2 6 2 2 6 2 2 6
  94137. - 74 74 74 62 62 62 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 0 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 94 94 94 14 14 14 101 101 101
  94152. -128 128 128 2 2 6 18 18 18 116 116 116
  94153. -118 98 46 121 92 8 121 92 8 98 78 10
  94154. -162 162 162 106 106 106 2 2 6 2 2 6
  94155. - 2 2 6 195 195 195 195 195 195 6 6 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 1
  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 10 10 10
  94171. - 38 38 38 90 90 90 14 14 14 58 58 58
  94172. -210 210 210 26 26 26 54 38 6 154 114 10
  94173. -226 170 11 236 186 11 225 175 15 184 144 12
  94174. -215 174 15 175 146 61 37 26 9 2 2 6
  94175. - 70 70 70 246 246 246 138 138 138 2 2 6
  94176. - 2 2 6 2 2 6 2 2 6 2 2 6
  94177. - 70 70 70 66 66 66 26 26 26 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 0 0 0 0
  94185. - 0 0 0 0 0 0 0 0 0 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 86 86 86 14 14 14 10 10 10
  94192. -195 195 195 188 164 115 192 133 9 225 175 15
  94193. -239 182 13 234 190 10 232 195 16 232 200 30
  94194. -245 207 45 241 208 19 232 195 16 184 144 12
  94195. -218 194 134 211 206 186 42 42 42 2 2 6
  94196. - 2 2 6 2 2 6 2 2 6 2 2 6
  94197. - 50 50 50 74 74 74 30 30 30 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. - 34 34 34 86 86 86 14 14 14 2 2 6
  94212. -121 87 25 192 133 9 219 162 10 239 182 13
  94213. -236 186 11 232 195 16 241 208 19 244 214 54
  94214. -246 218 60 246 218 38 246 215 20 241 208 19
  94215. -241 208 19 226 184 13 121 87 25 2 2 6
  94216. - 2 2 6 2 2 6 2 2 6 2 2 6
  94217. - 50 50 50 82 82 82 34 34 34 10 10 10
  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 82 82 82 30 30 30 61 42 6
  94232. -180 123 7 206 145 10 230 174 11 239 182 13
  94233. -234 190 10 238 202 15 241 208 19 246 218 74
  94234. -246 218 38 246 215 20 246 215 20 246 215 20
  94235. -226 184 13 215 174 15 184 144 12 6 6 6
  94236. - 2 2 6 2 2 6 2 2 6 2 2 6
  94237. - 26 26 26 94 94 94 42 42 42 14 14 14
  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. - 30 30 30 78 78 78 50 50 50 104 69 6
  94252. -192 133 9 216 158 10 236 178 12 236 186 11
  94253. -232 195 16 241 208 19 244 214 54 245 215 43
  94254. -246 215 20 246 215 20 241 208 19 198 155 10
  94255. -200 144 11 216 158 10 156 118 10 2 2 6
  94256. - 2 2 6 2 2 6 2 2 6 2 2 6
  94257. - 6 6 6 90 90 90 54 54 54 18 18 18
  94258. - 6 6 6 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 46 46 46 22 22 22
  94272. -137 92 6 210 162 10 239 182 13 238 190 10
  94273. -238 202 15 241 208 19 246 215 20 246 215 20
  94274. -241 208 19 203 166 17 185 133 11 210 150 10
  94275. -216 158 10 210 150 10 102 78 10 2 2 6
  94276. - 6 6 6 54 54 54 14 14 14 2 2 6
  94277. - 2 2 6 62 62 62 74 74 74 30 30 30
  94278. - 10 10 10 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. - 34 34 34 78 78 78 50 50 50 6 6 6
  94292. - 94 70 30 139 102 15 190 146 13 226 184 13
  94293. -232 200 30 232 195 16 215 174 15 190 146 13
  94294. -168 122 10 192 133 9 210 150 10 213 154 11
  94295. -202 150 34 182 157 106 101 98 89 2 2 6
  94296. - 2 2 6 78 78 78 116 116 116 58 58 58
  94297. - 2 2 6 22 22 22 90 90 90 46 46 46
  94298. - 18 18 18 6 6 6 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. - 38 38 38 86 86 86 50 50 50 6 6 6
  94312. -128 128 128 174 154 114 156 107 11 168 122 10
  94313. -198 155 10 184 144 12 197 138 11 200 144 11
  94314. -206 145 10 206 145 10 197 138 11 188 164 115
  94315. -195 195 195 198 198 198 174 174 174 14 14 14
  94316. - 2 2 6 22 22 22 116 116 116 116 116 116
  94317. - 22 22 22 2 2 6 74 74 74 70 70 70
  94318. - 30 30 30 10 10 10 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 6 6 6 18 18 18
  94331. - 50 50 50 101 101 101 26 26 26 10 10 10
  94332. -138 138 138 190 190 190 174 154 114 156 107 11
  94333. -197 138 11 200 144 11 197 138 11 192 133 9
  94334. -180 123 7 190 142 34 190 178 144 187 187 187
  94335. -202 202 202 221 221 221 214 214 214 66 66 66
  94336. - 2 2 6 2 2 6 50 50 50 62 62 62
  94337. - 6 6 6 2 2 6 10 10 10 90 90 90
  94338. - 50 50 50 18 18 18 6 6 6 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 10 10 10 34 34 34
  94351. - 74 74 74 74 74 74 2 2 6 6 6 6
  94352. -144 144 144 198 198 198 190 190 190 178 166 146
  94353. -154 121 60 156 107 11 156 107 11 168 124 44
  94354. -174 154 114 187 187 187 190 190 190 210 210 210
  94355. -246 246 246 253 253 253 253 253 253 182 182 182
  94356. - 6 6 6 2 2 6 2 2 6 2 2 6
  94357. - 2 2 6 2 2 6 2 2 6 62 62 62
  94358. - 74 74 74 34 34 34 14 14 14 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 10 10 10 22 22 22 54 54 54
  94371. - 94 94 94 18 18 18 2 2 6 46 46 46
  94372. -234 234 234 221 221 221 190 190 190 190 190 190
  94373. -190 190 190 187 187 187 187 187 187 190 190 190
  94374. -190 190 190 195 195 195 214 214 214 242 242 242
  94375. -253 253 253 253 253 253 253 253 253 253 253 253
  94376. - 82 82 82 2 2 6 2 2 6 2 2 6
  94377. - 2 2 6 2 2 6 2 2 6 14 14 14
  94378. - 86 86 86 54 54 54 22 22 22 6 6 6
  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. - 6 6 6 18 18 18 46 46 46 90 90 90
  94391. - 46 46 46 18 18 18 6 6 6 182 182 182
  94392. -253 253 253 246 246 246 206 206 206 190 190 190
  94393. -190 190 190 190 190 190 190 190 190 190 190 190
  94394. -206 206 206 231 231 231 250 250 250 253 253 253
  94395. -253 253 253 253 253 253 253 253 253 253 253 253
  94396. -202 202 202 14 14 14 2 2 6 2 2 6
  94397. - 2 2 6 2 2 6 2 2 6 2 2 6
  94398. - 42 42 42 86 86 86 42 42 42 18 18 18
  94399. - 6 6 6 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 6 6 6
  94410. - 14 14 14 38 38 38 74 74 74 66 66 66
  94411. - 2 2 6 6 6 6 90 90 90 250 250 250
  94412. -253 253 253 253 253 253 238 238 238 198 198 198
  94413. -190 190 190 190 190 190 195 195 195 221 221 221
  94414. -246 246 246 253 253 253 253 253 253 253 253 253
  94415. -253 253 253 253 253 253 253 253 253 253 253 253
  94416. -253 253 253 82 82 82 2 2 6 2 2 6
  94417. - 2 2 6 2 2 6 2 2 6 2 2 6
  94418. - 2 2 6 78 78 78 70 70 70 34 34 34
  94419. - 14 14 14 6 6 6 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 14 14 14
  94430. - 34 34 34 66 66 66 78 78 78 6 6 6
  94431. - 2 2 6 18 18 18 218 218 218 253 253 253
  94432. -253 253 253 253 253 253 253 253 253 246 246 246
  94433. -226 226 226 231 231 231 246 246 246 253 253 253
  94434. -253 253 253 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 178 178 178 2 2 6 2 2 6
  94437. - 2 2 6 2 2 6 2 2 6 2 2 6
  94438. - 2 2 6 18 18 18 90 90 90 62 62 62
  94439. - 30 30 30 10 10 10 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 10 10 10 26 26 26
  94450. - 58 58 58 90 90 90 18 18 18 2 2 6
  94451. - 2 2 6 110 110 110 253 253 253 253 253 253
  94452. -253 253 253 253 253 253 253 253 253 253 253 253
  94453. -250 250 250 253 253 253 253 253 253 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 231 231 231 18 18 18 2 2 6
  94457. - 2 2 6 2 2 6 2 2 6 2 2 6
  94458. - 2 2 6 2 2 6 18 18 18 94 94 94
  94459. - 54 54 54 26 26 26 10 10 10 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 6 6 6 22 22 22 50 50 50
  94470. - 90 90 90 26 26 26 2 2 6 2 2 6
  94471. - 14 14 14 195 195 195 250 250 250 253 253 253
  94472. -253 253 253 253 253 253 253 253 253 253 253 253
  94473. -253 253 253 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. -250 250 250 242 242 242 54 54 54 2 2 6
  94477. - 2 2 6 2 2 6 2 2 6 2 2 6
  94478. - 2 2 6 2 2 6 2 2 6 38 38 38
  94479. - 86 86 86 50 50 50 22 22 22 6 6 6
  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. - 6 6 6 14 14 14 38 38 38 82 82 82
  94490. - 34 34 34 2 2 6 2 2 6 2 2 6
  94491. - 42 42 42 195 195 195 246 246 246 253 253 253
  94492. -253 253 253 253 253 253 253 253 253 250 250 250
  94493. -242 242 242 242 242 242 250 250 250 253 253 253
  94494. -253 253 253 253 253 253 253 253 253 253 253 253
  94495. -253 253 253 250 250 250 246 246 246 238 238 238
  94496. -226 226 226 231 231 231 101 101 101 6 6 6
  94497. - 2 2 6 2 2 6 2 2 6 2 2 6
  94498. - 2 2 6 2 2 6 2 2 6 2 2 6
  94499. - 38 38 38 82 82 82 42 42 42 14 14 14
  94500. - 6 6 6 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. - 10 10 10 26 26 26 62 62 62 66 66 66
  94510. - 2 2 6 2 2 6 2 2 6 6 6 6
  94511. - 70 70 70 170 170 170 206 206 206 234 234 234
  94512. -246 246 246 250 250 250 250 250 250 238 238 238
  94513. -226 226 226 231 231 231 238 238 238 250 250 250
  94514. -250 250 250 250 250 250 246 246 246 231 231 231
  94515. -214 214 214 206 206 206 202 202 202 202 202 202
  94516. -198 198 198 202 202 202 182 182 182 18 18 18
  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. - 2 2 6 62 62 62 66 66 66 30 30 30
  94520. - 10 10 10 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. - 14 14 14 42 42 42 82 82 82 18 18 18
  94530. - 2 2 6 2 2 6 2 2 6 10 10 10
  94531. - 94 94 94 182 182 182 218 218 218 242 242 242
  94532. -250 250 250 253 253 253 253 253 253 250 250 250
  94533. -234 234 234 253 253 253 253 253 253 253 253 253
  94534. -253 253 253 253 253 253 253 253 253 246 246 246
  94535. -238 238 238 226 226 226 210 210 210 202 202 202
  94536. -195 195 195 195 195 195 210 210 210 158 158 158
  94537. - 6 6 6 14 14 14 50 50 50 14 14 14
  94538. - 2 2 6 2 2 6 2 2 6 2 2 6
  94539. - 2 2 6 6 6 6 86 86 86 46 46 46
  94540. - 18 18 18 6 6 6 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 6 6 6
  94549. - 22 22 22 54 54 54 70 70 70 2 2 6
  94550. - 2 2 6 10 10 10 2 2 6 22 22 22
  94551. -166 166 166 231 231 231 250 250 250 253 253 253
  94552. -253 253 253 253 253 253 253 253 253 250 250 250
  94553. -242 242 242 253 253 253 253 253 253 253 253 253
  94554. -253 253 253 253 253 253 253 253 253 253 253 253
  94555. -253 253 253 253 253 253 253 253 253 246 246 246
  94556. -231 231 231 206 206 206 198 198 198 226 226 226
  94557. - 94 94 94 2 2 6 6 6 6 38 38 38
  94558. - 30 30 30 2 2 6 2 2 6 2 2 6
  94559. - 2 2 6 2 2 6 62 62 62 66 66 66
  94560. - 26 26 26 10 10 10 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 10 10 10
  94569. - 30 30 30 74 74 74 50 50 50 2 2 6
  94570. - 26 26 26 26 26 26 2 2 6 106 106 106
  94571. -238 238 238 253 253 253 253 253 253 253 253 253
  94572. -253 253 253 253 253 253 253 253 253 253 253 253
  94573. -253 253 253 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 253 253 253
  94576. -253 253 253 246 246 246 218 218 218 202 202 202
  94577. -210 210 210 14 14 14 2 2 6 2 2 6
  94578. - 30 30 30 22 22 22 2 2 6 2 2 6
  94579. - 2 2 6 2 2 6 18 18 18 86 86 86
  94580. - 42 42 42 14 14 14 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 14 14 14
  94589. - 42 42 42 90 90 90 22 22 22 2 2 6
  94590. - 42 42 42 2 2 6 18 18 18 218 218 218
  94591. -253 253 253 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 253 253 253 250 250 250 221 221 221
  94597. -218 218 218 101 101 101 2 2 6 14 14 14
  94598. - 18 18 18 38 38 38 10 10 10 2 2 6
  94599. - 2 2 6 2 2 6 2 2 6 78 78 78
  94600. - 58 58 58 22 22 22 6 6 6 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 6 6 6 18 18 18
  94609. - 54 54 54 82 82 82 2 2 6 26 26 26
  94610. - 22 22 22 2 2 6 123 123 123 253 253 253
  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 253 253 253 250 250 250
  94617. -238 238 238 198 198 198 6 6 6 38 38 38
  94618. - 58 58 58 26 26 26 38 38 38 2 2 6
  94619. - 2 2 6 2 2 6 2 2 6 46 46 46
  94620. - 78 78 78 30 30 30 10 10 10 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 10 10 10 30 30 30
  94629. - 74 74 74 58 58 58 2 2 6 42 42 42
  94630. - 2 2 6 22 22 22 231 231 231 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 250 250 250
  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 253 253 253
  94637. -253 253 253 246 246 246 46 46 46 38 38 38
  94638. - 42 42 42 14 14 14 38 38 38 14 14 14
  94639. - 2 2 6 2 2 6 2 2 6 6 6 6
  94640. - 86 86 86 46 46 46 14 14 14 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 6 6 6 14 14 14 42 42 42
  94649. - 90 90 90 18 18 18 18 18 18 26 26 26
  94650. - 2 2 6 116 116 116 253 253 253 253 253 253
  94651. -253 253 253 253 253 253 253 253 253 253 253 253
  94652. -253 253 253 253 253 253 250 250 250 238 238 238
  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 253 253 253 94 94 94 6 6 6
  94658. - 2 2 6 2 2 6 10 10 10 34 34 34
  94659. - 2 2 6 2 2 6 2 2 6 2 2 6
  94660. - 74 74 74 58 58 58 22 22 22 6 6 6
  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 10 10 10 26 26 26 66 66 66
  94669. - 82 82 82 2 2 6 38 38 38 6 6 6
  94670. - 14 14 14 210 210 210 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 246 246 246 242 242 242
  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 144 144 144 2 2 6
  94678. - 2 2 6 2 2 6 2 2 6 46 46 46
  94679. - 2 2 6 2 2 6 2 2 6 2 2 6
  94680. - 42 42 42 74 74 74 30 30 30 10 10 10
  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. - 6 6 6 14 14 14 42 42 42 90 90 90
  94689. - 26 26 26 6 6 6 42 42 42 2 2 6
  94690. - 74 74 74 250 250 250 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 242 242 242 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 182 182 182 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. - 10 10 10 86 86 86 38 38 38 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. - 10 10 10 26 26 26 66 66 66 82 82 82
  94709. - 2 2 6 22 22 22 18 18 18 2 2 6
  94710. -149 149 149 253 253 253 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 234 234 234 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 206 206 206 2 2 6
  94718. - 2 2 6 2 2 6 2 2 6 38 38 38
  94719. - 2 2 6 2 2 6 2 2 6 2 2 6
  94720. - 6 6 6 86 86 86 46 46 46 14 14 14
  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 6 6 6
  94728. - 18 18 18 46 46 46 86 86 86 18 18 18
  94729. - 2 2 6 34 34 34 10 10 10 6 6 6
  94730. -210 210 210 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 221 221 221 6 6 6
  94738. - 2 2 6 2 2 6 6 6 6 30 30 30
  94739. - 2 2 6 2 2 6 2 2 6 2 2 6
  94740. - 2 2 6 82 82 82 54 54 54 18 18 18
  94741. - 6 6 6 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 10 10 10
  94748. - 26 26 26 66 66 66 62 62 62 2 2 6
  94749. - 2 2 6 38 38 38 10 10 10 26 26 26
  94750. -238 238 238 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 231 231 231 238 238 238
  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 231 231 231 6 6 6
  94758. - 2 2 6 2 2 6 10 10 10 30 30 30
  94759. - 2 2 6 2 2 6 2 2 6 2 2 6
  94760. - 2 2 6 66 66 66 58 58 58 22 22 22
  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. - 38 38 38 78 78 78 6 6 6 2 2 6
  94769. - 2 2 6 46 46 46 14 14 14 42 42 42
  94770. -246 246 246 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 242 242 242
  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 234 234 234 10 10 10
  94778. - 2 2 6 2 2 6 22 22 22 14 14 14
  94779. - 2 2 6 2 2 6 2 2 6 2 2 6
  94780. - 2 2 6 66 66 66 62 62 62 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 6 6 6 18 18 18
  94788. - 50 50 50 74 74 74 2 2 6 2 2 6
  94789. - 14 14 14 70 70 70 34 34 34 62 62 62
  94790. -250 250 250 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 246 246 246
  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 14 14 14
  94798. - 2 2 6 2 2 6 30 30 30 2 2 6
  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. - 54 54 54 62 62 62 2 2 6 2 2 6
  94809. - 2 2 6 30 30 30 46 46 46 70 70 70
  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 226 226 226 10 10 10
  94818. - 2 2 6 6 6 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 58 58 58 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 22 22 22
  94828. - 58 58 58 62 62 62 2 2 6 2 2 6
  94829. - 2 2 6 2 2 6 30 30 30 78 78 78
  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 206 206 206 2 2 6
  94838. - 22 22 22 34 34 34 18 14 6 22 22 22
  94839. - 26 26 26 18 18 18 6 6 6 2 2 6
  94840. - 2 2 6 82 82 82 54 54 54 18 18 18
  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 26 26 26
  94848. - 62 62 62 106 106 106 74 54 14 185 133 11
  94849. -210 162 10 121 92 8 6 6 6 62 62 62
  94850. -238 238 238 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 158 158 158 18 18 18
  94858. - 14 14 14 2 2 6 2 2 6 2 2 6
  94859. - 6 6 6 18 18 18 66 66 66 38 38 38
  94860. - 6 6 6 94 94 94 50 50 50 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 6 6 6
  94867. - 10 10 10 10 10 10 18 18 18 38 38 38
  94868. - 78 78 78 142 134 106 216 158 10 242 186 14
  94869. -246 190 14 246 190 14 156 118 10 10 10 10
  94870. - 90 90 90 238 238 238 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 250 250 250
  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 246 230 190
  94877. -238 204 91 238 204 91 181 142 44 37 26 9
  94878. - 2 2 6 2 2 6 2 2 6 2 2 6
  94879. - 2 2 6 2 2 6 38 38 38 46 46 46
  94880. - 26 26 26 106 106 106 54 54 54 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 6 6 6 14 14 14 22 22 22
  94887. - 30 30 30 38 38 38 50 50 50 70 70 70
  94888. -106 106 106 190 142 34 226 170 11 242 186 14
  94889. -246 190 14 246 190 14 246 190 14 154 114 10
  94890. - 6 6 6 74 74 74 226 226 226 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 228 184 62
  94897. -241 196 14 241 208 19 232 195 16 38 30 10
  94898. - 2 2 6 2 2 6 2 2 6 2 2 6
  94899. - 2 2 6 6 6 6 30 30 30 26 26 26
  94900. -203 166 17 154 142 90 66 66 66 26 26 26
  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. - 6 6 6 18 18 18 38 38 38 58 58 58
  94907. - 78 78 78 86 86 86 101 101 101 123 123 123
  94908. -175 146 61 210 150 10 234 174 13 246 186 14
  94909. -246 190 14 246 190 14 246 190 14 238 190 10
  94910. -102 78 10 2 2 6 46 46 46 198 198 198
  94911. -253 253 253 253 253 253 253 253 253 253 253 253
  94912. -253 253 253 253 253 253 234 234 234 242 242 242
  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 224 178 62
  94917. -242 186 14 241 196 14 210 166 10 22 18 6
  94918. - 2 2 6 2 2 6 2 2 6 2 2 6
  94919. - 2 2 6 2 2 6 6 6 6 121 92 8
  94920. -238 202 15 232 195 16 82 82 82 34 34 34
  94921. - 10 10 10 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. - 14 14 14 38 38 38 70 70 70 154 122 46
  94927. -190 142 34 200 144 11 197 138 11 197 138 11
  94928. -213 154 11 226 170 11 242 186 14 246 190 14
  94929. -246 190 14 246 190 14 246 190 14 246 190 14
  94930. -225 175 15 46 32 6 2 2 6 22 22 22
  94931. -158 158 158 250 250 250 253 253 253 253 253 253
  94932. -253 253 253 253 253 253 253 253 253 253 253 253
  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 250 250 250 242 242 242 224 178 62
  94937. -239 182 13 236 186 11 213 154 11 46 32 6
  94938. - 2 2 6 2 2 6 2 2 6 2 2 6
  94939. - 2 2 6 2 2 6 61 42 6 225 175 15
  94940. -238 190 10 236 186 11 112 100 78 42 42 42
  94941. - 14 14 14 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 6 6 6
  94946. - 22 22 22 54 54 54 154 122 46 213 154 11
  94947. -226 170 11 230 174 11 226 170 11 226 170 11
  94948. -236 178 12 242 186 14 246 190 14 246 190 14
  94949. -246 190 14 246 190 14 246 190 14 246 190 14
  94950. -241 196 14 184 144 12 10 10 10 2 2 6
  94951. - 6 6 6 116 116 116 242 242 242 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 231 231 231 198 198 198 214 170 54
  94957. -236 178 12 236 178 12 210 150 10 137 92 6
  94958. - 18 14 6 2 2 6 2 2 6 2 2 6
  94959. - 6 6 6 70 47 6 200 144 11 236 178 12
  94960. -239 182 13 239 182 13 124 112 88 58 58 58
  94961. - 22 22 22 6 6 6 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 10 10 10
  94966. - 30 30 30 70 70 70 180 133 36 226 170 11
  94967. -239 182 13 242 186 14 242 186 14 246 186 14
  94968. -246 190 14 246 190 14 246 190 14 246 190 14
  94969. -246 190 14 246 190 14 246 190 14 246 190 14
  94970. -246 190 14 232 195 16 98 70 6 2 2 6
  94971. - 2 2 6 2 2 6 66 66 66 221 221 221
  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 206 206 206 198 198 198 214 166 58
  94977. -230 174 11 230 174 11 216 158 10 192 133 9
  94978. -163 110 8 116 81 8 102 78 10 116 81 8
  94979. -167 114 7 197 138 11 226 170 11 239 182 13
  94980. -242 186 14 242 186 14 162 146 94 78 78 78
  94981. - 34 34 34 14 14 14 6 6 6 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 6 6 6
  94986. - 30 30 30 78 78 78 190 142 34 226 170 11
  94987. -239 182 13 246 190 14 246 190 14 246 190 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 241 196 14 203 166 17 22 18 6
  94991. - 2 2 6 2 2 6 2 2 6 38 38 38
  94992. -218 218 218 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. -250 250 250 206 206 206 198 198 198 202 162 69
  94997. -226 170 11 236 178 12 224 166 10 210 150 10
  94998. -200 144 11 197 138 11 192 133 9 197 138 11
  94999. -210 150 10 226 170 11 242 186 14 246 190 14
  95000. -246 190 14 246 186 14 225 175 15 124 112 88
  95001. - 62 62 62 30 30 30 14 14 14 6 6 6
  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 10 10 10
  95006. - 30 30 30 78 78 78 174 135 50 224 166 10
  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 246 190 14 241 196 14 139 102 15
  95011. - 2 2 6 2 2 6 2 2 6 2 2 6
  95012. - 78 78 78 250 250 250 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 214 214 214 198 198 198 190 150 46
  95017. -219 162 10 236 178 12 234 174 13 224 166 10
  95018. -216 158 10 213 154 11 213 154 11 216 158 10
  95019. -226 170 11 239 182 13 246 190 14 246 190 14
  95020. -246 190 14 246 190 14 242 186 14 206 162 42
  95021. -101 101 101 58 58 58 30 30 30 14 14 14
  95022. - 6 6 6 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 74 74 74 174 135 50 216 158 10
  95027. -236 178 12 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 226 184 13
  95031. - 61 42 6 2 2 6 2 2 6 2 2 6
  95032. - 22 22 22 238 238 238 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. -253 253 253 226 226 226 187 187 187 180 133 36
  95037. -216 158 10 236 178 12 239 182 13 236 178 12
  95038. -230 174 11 226 170 11 226 170 11 230 174 11
  95039. -236 178 12 242 186 14 246 190 14 246 190 14
  95040. -246 190 14 246 190 14 246 186 14 239 182 13
  95041. -206 162 42 106 106 106 66 66 66 34 34 34
  95042. - 14 14 14 6 6 6 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 6 6 6
  95046. - 26 26 26 70 70 70 163 133 67 213 154 11
  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 246 190 14 241 196 14
  95051. -190 146 13 18 14 6 2 2 6 2 2 6
  95052. - 46 46 46 246 246 246 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 221 221 221 86 86 86 156 107 11
  95057. -216 158 10 236 178 12 242 186 14 246 186 14
  95058. -242 186 14 239 182 13 239 182 13 242 186 14
  95059. -242 186 14 246 186 14 246 190 14 246 190 14
  95060. -246 190 14 246 190 14 246 190 14 246 190 14
  95061. -242 186 14 225 175 15 142 122 72 66 66 66
  95062. - 30 30 30 10 10 10 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 210 150 10
  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 246 190 14
  95071. -232 195 16 121 92 8 34 34 34 106 106 106
  95072. -221 221 221 253 253 253 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. -242 242 242 82 82 82 18 14 6 163 110 8
  95077. -216 158 10 236 178 12 242 186 14 246 190 14
  95078. -246 190 14 246 190 14 246 190 14 246 190 14
  95079. -246 190 14 246 190 14 246 190 14 246 190 14
  95080. -246 190 14 246 190 14 246 190 14 246 190 14
  95081. -246 190 14 246 190 14 242 186 14 163 133 67
  95082. - 46 46 46 18 18 18 6 6 6 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 10 10 10
  95086. - 30 30 30 78 78 78 163 133 67 210 150 10
  95087. -236 178 12 246 186 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. -241 196 14 215 174 15 190 178 144 253 253 253
  95092. -253 253 253 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 218 218 218
  95096. - 58 58 58 2 2 6 22 18 6 167 114 7
  95097. -216 158 10 236 178 12 246 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 186 14 242 186 14 190 150 46
  95102. - 54 54 54 22 22 22 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 14 14 14
  95106. - 38 38 38 86 86 86 180 133 36 213 154 11
  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. -246 190 14 232 195 16 190 146 13 214 214 214
  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 250 250 250 170 170 170 26 26 26
  95116. - 2 2 6 2 2 6 37 26 9 163 110 8
  95117. -219 162 10 239 182 13 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 186 14 236 178 12 224 166 10 142 122 72
  95122. - 46 46 46 18 18 18 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 6 6 6 18 18 18
  95126. - 50 50 50 109 106 95 192 133 9 224 166 10
  95127. -242 186 14 246 190 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. -242 186 14 226 184 13 210 162 10 142 110 46
  95132. -226 226 226 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. -198 198 198 66 66 66 2 2 6 2 2 6
  95136. - 2 2 6 2 2 6 50 34 6 156 107 11
  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 242 186 14
  95141. -234 174 13 213 154 11 154 122 46 66 66 66
  95142. - 30 30 30 10 10 10 0 0 0 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 22 22 22
  95146. - 58 58 58 154 121 60 206 145 10 234 174 13
  95147. -242 186 14 246 186 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. -246 186 14 236 178 12 210 162 10 163 110 8
  95152. - 61 42 6 138 138 138 218 218 218 250 250 250
  95153. -253 253 253 253 253 253 253 253 253 250 250 250
  95154. -242 242 242 210 210 210 144 144 144 66 66 66
  95155. - 6 6 6 2 2 6 2 2 6 2 2 6
  95156. - 2 2 6 2 2 6 61 42 6 163 110 8
  95157. -216 158 10 236 178 12 246 190 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 239 182 13 230 174 11 216 158 10
  95161. -190 142 34 124 112 88 70 70 70 38 38 38
  95162. - 18 18 18 6 6 6 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. - 62 62 62 168 124 44 206 145 10 224 166 10
  95167. -236 178 12 239 182 13 242 186 14 242 186 14
  95168. -246 186 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 190 14 236 178 12 216 158 10 175 118 6
  95172. - 80 54 7 2 2 6 6 6 6 30 30 30
  95173. - 54 54 54 62 62 62 50 50 50 38 38 38
  95174. - 14 14 14 2 2 6 2 2 6 2 2 6
  95175. - 2 2 6 2 2 6 2 2 6 2 2 6
  95176. - 2 2 6 6 6 6 80 54 7 167 114 7
  95177. -213 154 11 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 242 186 14 239 182 13 239 182 13
  95180. -230 174 11 210 150 10 174 135 50 124 112 88
  95181. - 82 82 82 54 54 54 34 34 34 18 18 18
  95182. - 6 6 6 0 0 0 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 18 18 18
  95186. - 50 50 50 158 118 36 192 133 9 200 144 11
  95187. -216 158 10 219 162 10 224 166 10 226 170 11
  95188. -230 174 11 236 178 12 239 182 13 239 182 13
  95189. -242 186 14 246 186 14 246 190 14 246 190 14
  95190. -246 190 14 246 190 14 246 190 14 246 190 14
  95191. -246 186 14 230 174 11 210 150 10 163 110 8
  95192. -104 69 6 10 10 10 2 2 6 2 2 6
  95193. - 2 2 6 2 2 6 2 2 6 2 2 6
  95194. - 2 2 6 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 91 60 6 167 114 7
  95197. -206 145 10 230 174 11 242 186 14 246 190 14
  95198. -246 190 14 246 190 14 246 186 14 242 186 14
  95199. -239 182 13 230 174 11 224 166 10 213 154 11
  95200. -180 133 36 124 112 88 86 86 86 58 58 58
  95201. - 38 38 38 22 22 22 10 10 10 6 6 6
  95202. - 0 0 0 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 0 0 0 14 14 14
  95206. - 34 34 34 70 70 70 138 110 50 158 118 36
  95207. -167 114 7 180 123 7 192 133 9 197 138 11
  95208. -200 144 11 206 145 10 213 154 11 219 162 10
  95209. -224 166 10 230 174 11 239 182 13 242 186 14
  95210. -246 186 14 246 186 14 246 186 14 246 186 14
  95211. -239 182 13 216 158 10 185 133 11 152 99 6
  95212. -104 69 6 18 14 6 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 80 54 7 152 99 6
  95217. -192 133 9 219 162 10 236 178 12 239 182 13
  95218. -246 186 14 242 186 14 239 182 13 236 178 12
  95219. -224 166 10 206 145 10 192 133 9 154 121 60
  95220. - 94 94 94 62 62 62 42 42 42 22 22 22
  95221. - 14 14 14 6 6 6 0 0 0 0 0 0
  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 6 6 6
  95226. - 18 18 18 34 34 34 58 58 58 78 78 78
  95227. -101 98 89 124 112 88 142 110 46 156 107 11
  95228. -163 110 8 167 114 7 175 118 6 180 123 7
  95229. -185 133 11 197 138 11 210 150 10 219 162 10
  95230. -226 170 11 236 178 12 236 178 12 234 174 13
  95231. -219 162 10 197 138 11 163 110 8 130 83 6
  95232. - 91 60 6 10 10 10 2 2 6 2 2 6
  95233. - 18 18 18 38 38 38 38 38 38 38 38 38
  95234. - 38 38 38 38 38 38 38 38 38 38 38 38
  95235. - 38 38 38 38 38 38 26 26 26 2 2 6
  95236. - 2 2 6 6 6 6 70 47 6 137 92 6
  95237. -175 118 6 200 144 11 219 162 10 230 174 11
  95238. -234 174 13 230 174 11 219 162 10 210 150 10
  95239. -192 133 9 163 110 8 124 112 88 82 82 82
  95240. - 50 50 50 30 30 30 14 14 14 6 6 6
  95241. - 0 0 0 0 0 0 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 0 0 0
  95246. - 6 6 6 14 14 14 22 22 22 34 34 34
  95247. - 42 42 42 58 58 58 74 74 74 86 86 86
  95248. -101 98 89 122 102 70 130 98 46 121 87 25
  95249. -137 92 6 152 99 6 163 110 8 180 123 7
  95250. -185 133 11 197 138 11 206 145 10 200 144 11
  95251. -180 123 7 156 107 11 130 83 6 104 69 6
  95252. - 50 34 6 54 54 54 110 110 110 101 98 89
  95253. - 86 86 86 82 82 82 78 78 78 78 78 78
  95254. - 78 78 78 78 78 78 78 78 78 78 78 78
  95255. - 78 78 78 82 82 82 86 86 86 94 94 94
  95256. -106 106 106 101 101 101 86 66 34 124 80 6
  95257. -156 107 11 180 123 7 192 133 9 200 144 11
  95258. -206 145 10 200 144 11 192 133 9 175 118 6
  95259. -139 102 15 109 106 95 70 70 70 42 42 42
  95260. - 22 22 22 10 10 10 0 0 0 0 0 0
  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. - 0 0 0 0 0 0 6 6 6 10 10 10
  95267. - 14 14 14 22 22 22 30 30 30 38 38 38
  95268. - 50 50 50 62 62 62 74 74 74 90 90 90
  95269. -101 98 89 112 100 78 121 87 25 124 80 6
  95270. -137 92 6 152 99 6 152 99 6 152 99 6
  95271. -138 86 6 124 80 6 98 70 6 86 66 30
  95272. -101 98 89 82 82 82 58 58 58 46 46 46
  95273. - 38 38 38 34 34 34 34 34 34 34 34 34
  95274. - 34 34 34 34 34 34 34 34 34 34 34 34
  95275. - 34 34 34 34 34 34 38 38 38 42 42 42
  95276. - 54 54 54 82 82 82 94 86 76 91 60 6
  95277. -134 86 6 156 107 11 167 114 7 175 118 6
  95278. -175 118 6 167 114 7 152 99 6 121 87 25
  95279. -101 98 89 62 62 62 34 34 34 18 18 18
  95280. - 6 6 6 0 0 0 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 0 0 0 0 0 0
  95287. - 0 0 0 6 6 6 6 6 6 10 10 10
  95288. - 18 18 18 22 22 22 30 30 30 42 42 42
  95289. - 50 50 50 66 66 66 86 86 86 101 98 89
  95290. -106 86 58 98 70 6 104 69 6 104 69 6
  95291. -104 69 6 91 60 6 82 62 34 90 90 90
  95292. - 62 62 62 38 38 38 22 22 22 14 14 14
  95293. - 10 10 10 10 10 10 10 10 10 10 10 10
  95294. - 10 10 10 10 10 10 6 6 6 10 10 10
  95295. - 10 10 10 10 10 10 10 10 10 14 14 14
  95296. - 22 22 22 42 42 42 70 70 70 89 81 66
  95297. - 80 54 7 104 69 6 124 80 6 137 92 6
  95298. -134 86 6 116 81 8 100 82 52 86 86 86
  95299. - 58 58 58 30 30 30 14 14 14 6 6 6
  95300. - 0 0 0 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 0 0 0 0 0 0 0 0 0
  95308. - 0 0 0 6 6 6 10 10 10 14 14 14
  95309. - 18 18 18 26 26 26 38 38 38 54 54 54
  95310. - 70 70 70 86 86 86 94 86 76 89 81 66
  95311. - 89 81 66 86 86 86 74 74 74 50 50 50
  95312. - 30 30 30 14 14 14 6 6 6 0 0 0
  95313. - 0 0 0 0 0 0 0 0 0 0 0 0
  95314. - 0 0 0 0 0 0 0 0 0 0 0 0
  95315. - 0 0 0 0 0 0 0 0 0 0 0 0
  95316. - 6 6 6 18 18 18 34 34 34 58 58 58
  95317. - 82 82 82 89 81 66 89 81 66 89 81 66
  95318. - 94 86 66 94 86 76 74 74 74 50 50 50
  95319. - 26 26 26 14 14 14 6 6 6 0 0 0
  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 0 0 0 0 0 0 0 0 0
  95329. - 6 6 6 6 6 6 14 14 14 18 18 18
  95330. - 30 30 30 38 38 38 46 46 46 54 54 54
  95331. - 50 50 50 42 42 42 30 30 30 18 18 18
  95332. - 10 10 10 0 0 0 0 0 0 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. - 0 0 0 6 6 6 14 14 14 26 26 26
  95337. - 38 38 38 50 50 50 58 58 58 58 58 58
  95338. - 54 54 54 42 42 42 30 30 30 18 18 18
  95339. - 10 10 10 0 0 0 0 0 0 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. - 0 0 0 0 0 0 0 0 0 6 6 6
  95350. - 6 6 6 10 10 10 14 14 14 18 18 18
  95351. - 18 18 18 14 14 14 10 10 10 6 6 6
  95352. - 0 0 0 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 0 0 0 0 0 0 6 6 6
  95357. - 14 14 14 18 18 18 22 22 22 22 22 22
  95358. - 18 18 18 14 14 14 10 10 10 6 6 6
  95359. - 0 0 0 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 0 0 0 0 0 0
  95365. +0 0 0 0 0 0 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 0 0 0 0 0 0
  95367. +0 0 0 0 0 0 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 0 0 0 0 0 0
  95369. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95370. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95371. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95372. +0 0 0 0 0 0 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 0 0 0 0 0 0
  95374. +0 0 0 0 0 0 0 0 0
  95375. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95376. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95377. +0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0
  95378. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95379. +0 0 0 0 0 0 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 0 0 0 0 0 0
  95381. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95382. +0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
  95383. +0 1 0 0 0 0 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
  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 1 1 0
  95388. +10 15 3 2 3 1 12 18 4 42 61 14 19 27 6 11 16 4
  95389. +38 55 13 10 15 3 3 4 1 10 15 3 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 2 3 1
  95393. +12 18 4 1 1 0 23 34 8 31 45 11 10 15 3 32 47 11
  95394. +34 49 12 3 4 1 3 4 1 3 4 1 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
  95397. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95398. +0 0 0 0 0 0 10 15 3 29 42 10 26 37 9 12 18 4
  95399. +55 80 19 81 118 28 55 80 19 92 132 31 106 153 36 69 100 23
  95400. +100 144 34 80 116 27 42 61 14 81 118 28 23 34 8 27 40 9
  95401. +15 21 5 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 0 0 0 0 0 0 0 0 0
  95403. +0 0 0 0 0 0 1 1 0 29 42 10 15 21 5 50 72 17
  95404. +74 107 25 45 64 15 102 148 35 80 116 27 84 121 28 111 160 38
  95405. +69 100 23 65 94 22 81 118 28 29 42 10 17 25 6 29 42 10
  95406. +23 34 8 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  95407. +0 0 0 0 0 0 0 0 0
  95408. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  95409. +15 21 5 15 21 5 34 49 12 101 146 34 111 161 38 97 141 33
  95410. +97 141 33 119 172 41 117 170 40 116 167 40 118 170 40 118 171 40
  95411. +117 169 40 118 170 40 111 160 38 118 170 40 96 138 32 89 128 30
  95412. +81 118 28 11 16 4 10 15 3 1 1 0 0 0 0 0 0 0
  95413. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95414. +3 4 1 3 4 1 34 49 12 101 146 34 79 115 27 111 160 38
  95415. +114 165 39 113 163 39 118 170 40 117 169 40 118 171 40 117 169 40
  95416. +116 167 40 119 172 41 113 163 39 92 132 31 105 151 36 113 163 39
  95417. +75 109 26 19 27 6 16 23 5 11 16 4 0 1 0 0 0 0
  95418. +0 0 0 0 0 0 0 0 0
  95419. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  95420. +80 116 27 106 153 36 105 151 36 114 165 39 118 170 40 118 171 40
  95421. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95422. +117 169 40 117 169 40 117 170 40 117 169 40 118 170 40 118 170 40
  95423. +117 170 40 75 109 26 75 109 26 34 49 12 0 0 0 0 0 0
  95424. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  95425. +64 92 22 65 94 22 100 144 34 118 171 40 118 170 40 117 169 40
  95426. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95427. +117 169 40 117 169 40 117 169 40 118 171 41 118 170 40 117 169 40
  95428. +109 158 37 105 151 36 104 150 35 47 69 16 0 0 0 0 0 0
  95429. +0 0 0 0 0 0 0 0 0
  95430. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95431. +42 61 14 115 167 39 118 170 40 117 169 40 117 169 40 117 169 40
  95432. +117 170 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  95433. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95434. +117 169 40 117 169 40 118 170 40 96 138 32 17 25 6 0 0 0
  95435. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47 69 16
  95436. +114 165 39 117 168 40 117 170 40 117 169 40 117 169 40 117 169 40
  95437. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95438. +117 169 40 117 169 40 118 170 40 117 169 40 117 169 40 117 169 40
  95439. +117 170 40 119 172 41 96 138 32 12 18 4 0 0 0 0 0 0
  95440. +0 0 0 0 0 0 0 0 0
  95441. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  95442. +32 47 11 105 151 36 118 170 40 117 169 40 117 169 40 116 168 40
  95443. +109 157 37 111 160 38 117 169 40 118 171 40 117 169 40 117 169 40
  95444. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95445. +117 169 40 117 169 40 117 169 40 118 171 40 69 100 23 2 3 1
  95446. +0 0 0 0 0 0 0 0 0 0 0 0 19 27 6 101 146 34
  95447. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95448. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 170 40
  95449. +118 171 40 115 166 39 107 154 36 111 161 38 117 169 40 117 169 40
  95450. +117 169 40 118 171 40 75 109 26 19 27 6 2 3 1 0 0 0
  95451. +0 0 0 0 0 0 0 0 0
  95452. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 23 5
  95453. +89 128 30 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95454. +111 160 38 92 132 31 79 115 27 96 138 32 115 166 39 119 171 41
  95455. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95456. +117 169 40 117 169 40 117 169 40 118 170 40 109 157 37 26 37 9
  95457. +0 0 0 0 0 0 0 0 0 0 0 0 64 92 22 118 171 40
  95458. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95459. +117 169 40 117 169 40 117 169 40 118 170 40 118 171 40 109 157 37
  95460. +89 128 30 81 118 28 100 144 34 115 166 39 117 169 40 117 169 40
  95461. +117 169 40 117 170 40 113 163 39 60 86 20 1 1 0 0 0 0
  95462. +0 0 0 0 0 0 0 0 0
  95463. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95464. +27 40 9 96 138 32 118 170 40 117 169 40 117 169 40 117 169 40
  95465. +117 170 40 117 169 40 101 146 34 67 96 23 55 80 19 84 121 28
  95466. +113 163 39 119 171 41 117 169 40 117 169 40 117 169 40 117 169 40
  95467. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 65 94 22
  95468. +0 0 0 0 0 0 0 0 0 15 21 5 101 146 34 118 171 40
  95469. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95470. +117 169 40 118 170 40 118 171 40 104 150 35 69 100 23 53 76 18
  95471. +81 118 28 111 160 38 118 170 40 117 169 40 117 169 40 117 169 40
  95472. +117 169 40 114 165 39 69 100 23 10 15 3 0 0 0 0 0 0
  95473. +0 0 0 0 0 0 0 0 0
  95474. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  95475. +31 45 11 77 111 26 117 169 40 117 169 40 117 169 40 117 169 40
  95476. +117 169 40 117 169 40 118 170 40 116 168 40 92 132 31 47 69 16
  95477. +38 55 13 81 118 28 113 163 39 119 171 41 117 169 40 117 169 40
  95478. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 41 92 132 31
  95479. +10 15 3 0 0 0 0 0 0 36 52 12 115 166 39 117 169 40
  95480. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  95481. +118 171 40 102 148 35 64 92 22 34 49 12 65 94 22 106 153 36
  95482. +118 171 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  95483. +118 170 40 107 154 36 55 80 19 15 21 5 0 0 0 0 0 0
  95484. +0 0 0 0 0 0 0 0 0
  95485. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95486. +29 42 10 101 146 34 118 171 40 117 169 40 117 169 40 117 169 40
  95487. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 113 163 39
  95488. +75 109 26 27 40 9 36 52 12 89 128 30 116 167 40 118 171 40
  95489. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 104 150 35
  95490. +16 23 5 0 0 0 0 0 0 53 76 18 118 171 40 117 169 40
  95491. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 109 157 37
  95492. +67 96 23 23 34 8 42 61 14 96 138 32 118 170 40 118 170 40
  95493. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95494. +117 169 40 117 169 40 74 107 25 10 15 3 0 0 0 0 0 0
  95495. +0 0 0 0 0 0 0 0 0
  95496. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95497. +0 0 0 31 45 11 101 146 34 118 170 40 117 169 40 117 169 40
  95498. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95499. +119 171 41 102 148 35 47 69 16 14 20 5 50 72 17 102 148 35
  95500. +118 171 40 117 169 40 117 169 40 117 169 40 118 170 40 102 148 35
  95501. +15 21 5 0 0 0 0 0 0 50 72 17 118 170 40 117 169 40
  95502. +117 169 40 117 169 40 118 170 40 116 167 40 84 121 28 27 40 9
  95503. +19 27 6 74 107 25 114 165 39 118 171 40 117 169 40 117 169 40
  95504. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95505. +117 169 40 75 109 26 10 15 4 0 0 0 0 0 0 0 0 0
  95506. +0 0 0 0 0 0 0 0 0
  95507. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95508. +0 0 0 38 55 13 102 148 35 118 171 40 117 169 40 117 169 40
  95509. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95510. +117 169 40 118 170 40 115 167 39 77 111 26 17 25 6 19 27 6
  95511. +77 111 26 115 166 39 118 170 40 117 169 40 119 172 41 81 118 28
  95512. +3 4 1 0 0 0 0 0 0 27 40 9 111 160 38 118 170 40
  95513. +117 169 40 118 171 40 105 151 36 50 72 17 10 15 3 38 55 13
  95514. +100 144 34 118 171 40 117 169 40 117 169 40 117 169 40 117 169 40
  95515. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95516. +117 169 40 79 115 27 15 21 5 0 0 0 0 0 0 0 0 0
  95517. +0 0 0 0 0 0 0 0 0
  95518. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95519. +0 0 0 10 15 3 64 92 22 111 160 38 117 169 40 117 169 40
  95520. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95521. +117 169 40 117 169 40 117 169 40 118 171 40 96 138 32 32 47 11
  95522. +3 4 1 50 72 17 107 154 36 120 173 41 105 151 36 31 45 11
  95523. +0 0 0 0 0 0 0 0 0 3 4 1 65 94 22 117 169 40
  95524. +118 170 40 89 128 30 26 37 9 3 4 1 60 86 20 111 161 38
  95525. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95526. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95527. +97 141 33 36 52 12 1 1 0 0 0 0 0 0 0 0 0 0
  95528. +0 0 0 0 0 0 0 0 0
  95529. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95530. +0 0 0 0 0 0 14 20 5 75 109 26 117 168 40 117 169 40
  95531. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95532. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 107 154 36
  95533. +45 64 15 2 3 1 31 45 11 75 109 26 32 47 11 0 1 0
  95534. +0 0 0 0 0 0 0 0 0 0 0 0 10 15 3 55 80 19
  95535. +65 94 22 11 16 4 11 16 4 75 109 26 116 168 40 118 170 40
  95536. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95537. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 107 154 36
  95538. +47 69 16 3 4 1 0 0 0 0 0 0 0 0 0 0 0 0
  95539. +0 0 0 0 0 0 0 0 0
  95540. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95541. +0 0 0 0 0 0 12 18 4 69 100 23 111 161 38 118 171 40
  95542. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95543. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  95544. +111 160 38 50 72 17 2 3 1 2 3 1 0 0 0 0 0 0
  95545. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  95546. +1 1 0 12 18 4 81 118 28 118 170 40 117 169 40 117 169 40
  95547. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95548. +117 169 40 117 169 40 117 169 40 117 170 40 118 171 40 101 146 34
  95549. +42 61 14 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  95550. +0 0 0 0 0 0 0 0 0
  95551. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95552. +0 0 0 0 0 0 0 0 0 3 4 1 36 52 12 89 128 30
  95553. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95554. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95555. +118 171 41 101 146 34 14 20 5 0 0 0 0 0 0 0 0 0
  95556. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95557. +0 0 0 47 69 16 118 170 40 117 169 40 117 169 40 117 169 40
  95558. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95559. +117 169 40 117 169 40 117 170 40 111 160 38 69 100 23 19 27 6
  95560. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95561. +0 0 0 0 0 0 0 0 0
  95562. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95563. +0 0 0 0 0 0 0 0 0 0 0 0 11 16 4 69 100 23
  95564. +115 167 39 119 172 41 117 169 40 117 169 40 117 169 40 117 169 40
  95565. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95566. +119 172 41 75 109 26 3 4 1 0 0 0 0 0 0 0 0 0
  95567. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95568. +0 0 0 23 34 8 106 153 36 118 170 40 117 169 40 117 169 40
  95569. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95570. +117 169 40 118 170 40 119 172 41 105 151 36 42 61 14 2 3 1
  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
  95573. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95574. +0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 15 21 5
  95575. +45 64 15 80 116 27 114 165 39 118 170 40 117 169 40 117 169 40
  95576. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 119 172 41
  95577. +97 141 33 20 30 7 0 0 0 0 0 0 0 0 0 0 0 0
  95578. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95579. +0 0 0 1 1 0 53 76 18 114 165 39 118 171 40 117 169 40
  95580. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95581. +118 171 40 104 150 35 64 92 22 31 45 11 10 15 3 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
  95584. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95585. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95586. +0 0 0 36 52 12 97 141 33 109 158 37 113 163 39 116 168 40
  95587. +117 169 40 117 170 40 118 170 40 119 172 41 115 167 39 84 121 28
  95588. +23 34 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95589. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95590. +0 0 0 0 0 0 3 4 1 50 72 17 102 148 35 118 171 40
  95591. +119 171 41 118 170 40 117 169 40 117 169 40 115 166 39 111 161 38
  95592. +109 157 37 79 115 27 12 18 4 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
  95595. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95596. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95597. +0 0 0 3 4 1 15 21 5 23 34 8 45 64 15 106 153 36
  95598. +116 167 40 111 160 38 101 146 34 79 115 27 42 61 14 10 15 3
  95599. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95600. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95601. +0 0 0 0 0 0 0 0 0 1 1 0 20 30 7 60 86 20
  95602. +89 128 30 106 153 36 113 163 39 117 169 40 84 121 28 29 42 10
  95603. +19 27 6 10 15 3 2 3 1 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
  95606. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95607. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95608. +0 0 0 0 0 0 0 0 0 0 0 0 16 23 5 38 55 13
  95609. +36 52 12 26 37 9 12 18 4 2 3 1 0 0 0 0 0 0
  95610. +0 0 0 0 0 0 0 0 0 1 0 0 19 2 7 52 5 18
  95611. +78 7 27 88 8 31 81 7 29 56 5 19 25 2 9 3 0 1
  95612. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95613. +3 4 1 19 27 6 31 45 11 38 55 13 32 47 11 3 4 1
  95614. +0 0 0 0 0 0 0 0 0 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
  95617. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95618. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95619. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  95620. +9 0 3 12 1 4 9 0 3 4 0 1 0 0 0 0 0 0
  95621. +0 0 0 0 0 0 28 3 10 99 9 35 156 14 55 182 16 64
  95622. +189 17 66 190 17 67 189 17 66 184 17 65 166 15 58 118 13 41
  95623. +45 4 16 3 0 1 0 0 0 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 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
  95628. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95629. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95630. +0 0 0 0 0 0 11 1 4 52 5 18 101 9 35 134 12 47
  95631. +151 14 53 154 14 54 151 14 53 113 10 40 11 1 4 0 0 0
  95632. +3 0 1 67 6 24 159 14 56 190 17 67 190 17 67 188 17 66
  95633. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 191 17 67
  95634. +174 16 61 101 9 35 14 1 5 0 0 0 35 3 12 108 10 38
  95635. +122 11 43 122 11 43 112 10 39 87 8 30 50 5 17 13 1 5
  95636. +0 0 0 0 0 0 0 0 0 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
  95639. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95640. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95641. +3 0 1 56 5 19 141 13 49 182 16 64 191 17 67 191 17 67
  95642. +190 17 67 190 17 67 191 17 67 113 10 40 3 0 1 1 0 0
  95643. +79 7 28 180 16 63 190 17 67 188 17 66 188 17 66 188 17 66
  95644. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95645. +189 17 66 188 17 66 122 11 43 11 1 4 41 4 14 176 16 62
  95646. +191 17 67 191 17 67 191 17 67 190 17 67 181 16 63 146 13 51
  95647. +75 7 26 10 1 4 0 0 0 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
  95650. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95651. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 1 2
  95652. +90 8 32 178 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  95653. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 41 4 14
  95654. +173 16 61 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95655. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95656. +188 17 66 188 17 66 188 17 66 88 8 31 1 0 0 89 8 31
  95657. +185 17 65 189 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  95658. +186 17 65 124 11 43 25 2 9 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
  95661. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95662. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 89 8 31
  95663. +184 17 65 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95664. +190 17 67 151 14 53 34 3 12 0 0 0 0 0 0 79 7 28
  95665. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95666. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95667. +188 17 66 188 17 66 191 17 67 146 13 51 9 1 3 7 1 2
  95668. +108 10 38 187 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  95669. +188 17 66 190 17 67 141 13 49 22 2 8 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
  95672. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95673. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 176 16 62
  95674. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95675. +151 14 53 38 3 13 0 0 0 0 0 0 0 0 0 50 5 17
  95676. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95677. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95678. +188 17 66 188 17 66 191 17 67 141 13 49 7 1 3 0 0 0
  95679. +11 1 4 112 10 39 187 17 66 189 17 66 188 17 66 188 17 66
  95680. +188 17 66 188 17 66 190 17 67 113 10 40 5 0 2 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
  95683. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95684. +0 0 0 0 0 0 0 0 0 7 1 3 132 12 46 191 17 67
  95685. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 146 13 51
  95686. +35 3 12 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  95687. +101 9 35 185 17 65 190 17 67 188 17 66 188 17 66 188 17 66
  95688. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95689. +188 17 66 190 17 67 180 16 63 67 6 24 0 0 0 0 0 0
  95690. +0 0 0 11 1 4 108 10 38 186 17 65 189 17 66 188 17 66
  95691. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 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
  95694. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95695. +0 0 0 0 0 0 0 0 0 44 4 15 177 16 62 189 17 66
  95696. +188 17 66 188 17 66 189 17 66 189 17 66 134 12 47 28 3 10
  95697. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95698. +8 1 3 79 7 28 159 14 56 188 17 66 191 17 67 190 17 67
  95699. +189 17 66 189 17 66 189 17 66 189 17 66 190 17 67 191 17 67
  95700. +188 17 66 158 14 55 72 7 25 4 0 1 0 0 0 0 0 0
  95701. +0 0 0 0 0 0 8 1 3 95 9 33 182 16 64 189 17 67
  95702. +188 17 66 188 17 66 188 17 66 191 17 67 122 11 43 3 0 1
  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
  95705. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95706. +0 0 0 0 0 0 0 0 0 88 8 31 190 17 67 188 17 66
  95707. +188 17 66 189 17 66 185 17 65 113 10 40 18 2 6 0 0 0
  95708. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95709. +0 0 0 1 0 0 24 2 8 77 7 27 124 11 43 154 14 54
  95710. +168 15 59 173 16 61 173 16 61 168 15 59 154 14 54 124 11 43
  95711. +77 7 27 22 2 8 0 0 0 0 0 0 0 0 0 0 0 0
  95712. +0 0 0 0 0 0 0 0 0 5 0 2 77 7 27 173 16 61
  95713. +190 17 67 188 17 66 188 17 66 190 17 67 164 15 57 23 2 8
  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
  95716. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95717. +0 0 0 0 0 0 1 0 0 118 13 41 191 17 67 188 17 66
  95718. +190 17 67 174 16 61 87 8 30 8 1 3 0 0 0 0 0 0
  95719. +0 0 0 0 0 0 10 1 4 29 3 10 40 4 14 36 3 13
  95720. +18 2 6 2 0 1 0 0 0 0 0 0 3 0 1 14 1 5
  95721. +26 2 9 33 3 11 32 3 11 25 2 9 13 1 5 3 0 1
  95722. +0 0 0 14 1 5 56 5 19 95 9 33 109 10 38 101 9 35
  95723. +77 7 27 35 3 12 5 0 2 0 0 0 1 0 0 56 5 19
  95724. +156 14 55 190 17 67 188 17 66 188 17 66 182 16 64 50 5 17
  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
  95727. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95728. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 189 17 66
  95729. +151 14 53 52 5 18 2 0 1 0 0 0 0 0 0 1 0 0
  95730. +28 3 10 90 8 32 146 13 51 170 15 60 178 16 62 174 16 61
  95731. +158 14 55 112 10 39 40 4 14 1 0 0 0 0 0 0 0 0
  95732. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  95733. +56 5 19 146 13 51 183 17 64 191 17 67 191 17 67 191 17 67
  95734. +188 17 66 173 16 61 122 11 43 41 4 14 1 0 0 0 0 0
  95735. +30 3 10 124 11 43 185 17 65 190 17 67 187 17 66 67 6 24
  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 0 0 0
  95738. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95739. +0 0 0 0 0 0 6 1 2 134 12 47 168 15 59 99 9 35
  95740. +21 2 7 0 0 0 0 0 0 0 0 0 6 1 2 77 7 27
  95741. +162 15 57 190 17 67 191 17 67 189 17 66 189 17 66 189 17 66
  95742. +190 17 67 191 17 67 169 15 59 75 7 26 3 0 1 0 0 0
  95743. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 79 7 28
  95744. +178 16 62 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95745. +188 17 66 189 17 66 191 17 67 170 15 60 79 7 28 5 0 2
  95746. +0 0 0 10 1 3 78 7 27 159 14 56 188 17 66 75 7 26
  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 0 0 0
  95749. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95750. +0 0 0 0 0 0 1 0 0 35 3 12 29 3 10 2 0 1
  95751. +0 0 0 0 0 0 0 0 0 9 1 3 101 9 35 183 17 64
  95752. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95753. +188 17 66 188 17 66 190 17 67 178 16 63 67 6 23 0 0 0
  95754. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 174 16 61
  95755. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95756. +188 17 66 188 17 66 188 17 66 190 17 67 182 16 64 89 8 31
  95757. +4 0 1 0 0 0 0 0 0 25 2 9 73 7 26 31 3 11
  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 0 0 0
  95760. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95761. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95762. +0 0 0 0 0 0 4 0 1 98 9 34 187 17 66 189 17 66
  95763. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95764. +188 17 66 188 17 66 188 17 66 190 17 67 158 14 55 25 2 9
  95765. +0 0 0 0 0 0 0 0 0 8 1 3 134 12 47 191 17 67
  95766. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95767. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 180 16 63
  95768. +68 6 24 0 0 0 0 0 0 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 0 0 0
  95771. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95772. +0 0 0 6 1 2 19 2 7 3 0 1 0 0 0 0 0 0
  95773. +0 0 0 0 0 0 65 6 23 180 16 63 189 17 66 188 17 66
  95774. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95775. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 83 8 29
  95776. +0 0 0 0 0 0 0 0 0 41 4 14 177 16 62 189 17 66
  95777. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95778. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95779. +159 14 56 28 3 10 0 0 0 0 0 0 0 0 0 23 2 8
  95780. +41 4 14 5 0 2 0 0 0 0 0 0 0 0 0 0 0 0
  95781. +0 0 0 0 0 0 0 0 0
  95782. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95783. +23 2 8 113 10 40 159 14 56 65 6 23 0 0 0 0 0 0
  95784. +0 0 0 16 1 6 146 13 51 191 17 67 188 17 66 188 17 66
  95785. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95786. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 132 12 46
  95787. +5 0 2 0 0 0 0 0 0 77 7 27 189 17 66 188 17 66
  95788. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95789. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95790. +190 17 67 98 9 34 0 0 0 0 0 0 12 1 4 134 12 47
  95791. +178 16 63 108 10 38 16 1 6 0 0 0 0 0 0 0 0 0
  95792. +0 0 0 0 0 0 0 0 0
  95793. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 3 10
  95794. +141 13 49 190 17 67 191 17 67 134 12 47 6 1 2 0 0 0
  95795. +0 0 0 68 6 24 186 17 65 188 17 66 188 17 66 188 17 66
  95796. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95797. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 156 14 55
  95798. +14 1 5 0 0 0 0 0 0 98 9 34 191 17 67 188 17 66
  95799. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95800. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95801. +190 17 67 156 14 55 19 2 7 0 0 0 47 4 16 181 16 63
  95802. +190 17 67 189 17 66 126 14 44 17 2 6 0 0 0 0 0 0
  95803. +0 0 0 0 0 0 0 0 0
  95804. +0 0 0 0 0 0 0 0 0 0 0 0 16 1 6 134 12 47
  95805. +191 17 67 188 17 66 190 17 67 162 15 57 19 2 7 0 0 0
  95806. +3 0 1 123 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  95807. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95808. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 163 15 57
  95809. +20 2 7 0 0 0 0 0 0 101 9 35 191 17 67 188 17 66
  95810. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95811. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95812. +188 17 66 182 16 64 52 5 18 0 0 0 73 7 26 188 17 66
  95813. +188 17 66 188 17 66 189 17 66 109 10 38 5 0 2 0 0 0
  95814. +0 0 0 0 0 0 0 0 0
  95815. +0 0 0 0 0 0 0 0 0 0 0 0 95 9 33 189 17 66
  95816. +188 17 66 188 17 66 189 17 66 171 15 60 29 3 10 0 0 0
  95817. +16 1 6 156 14 55 190 17 67 188 17 66 188 17 66 188 17 66
  95818. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95819. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 158 14 55
  95820. +17 2 6 0 0 0 0 0 0 85 8 30 190 17 67 188 17 66
  95821. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95822. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95823. +188 17 66 189 17 66 81 7 29 0 0 0 85 8 30 190 17 67
  95824. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  95825. +0 0 0 0 0 0 0 0 0
  95826. +0 0 0 0 0 0 0 0 0 25 2 9 162 15 57 190 17 67
  95827. +188 17 66 188 17 66 189 17 66 173 16 61 31 3 11 0 0 0
  95828. +30 3 10 171 15 60 189 17 66 188 17 66 188 17 66 188 17 66
  95829. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95830. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 141 13 49
  95831. +7 1 2 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  95832. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95833. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95834. +188 17 66 191 17 67 98 9 34 0 0 0 88 8 31 190 17 67
  95835. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 5 0 2
  95836. +0 0 0 0 0 0 0 0 0
  95837. +0 0 0 0 0 0 0 0 0 68 6 24 187 17 66 188 17 66
  95838. +188 17 66 188 17 66 189 17 66 170 15 60 28 3 10 0 0 0
  95839. +34 3 12 174 16 61 189 17 66 188 17 66 188 17 66 188 17 66
  95840. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95841. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 101 9 35
  95842. +0 0 0 0 0 0 0 0 0 21 2 7 159 14 56 190 17 67
  95843. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95844. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95845. +188 17 66 191 17 67 98 9 34 0 0 0 81 7 29 189 17 66
  95846. +188 17 66 188 17 66 188 17 66 189 17 66 168 15 59 28 3 10
  95847. +0 0 0 0 0 0 0 0 0
  95848. +0 0 0 0 0 0 0 0 0 109 10 38 191 17 67 188 17 66
  95849. +188 17 66 188 17 66 190 17 67 163 15 57 21 2 7 0 0 0
  95850. +26 2 9 168 15 59 189 17 66 188 17 66 188 17 66 188 17 66
  95851. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95852. +188 17 66 188 17 66 188 17 66 189 17 66 180 16 63 47 4 16
  95853. +0 0 0 0 0 0 0 0 0 0 0 0 108 10 38 190 17 67
  95854. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95855. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95856. +188 17 66 189 17 66 78 7 27 0 0 0 68 6 24 187 17 66
  95857. +188 17 66 188 17 66 188 17 66 188 17 66 183 17 64 56 5 19
  95858. +0 0 0 0 0 0 0 0 0
  95859. +0 0 0 0 0 0 3 0 1 131 12 46 191 17 67 188 17 66
  95860. +188 17 66 188 17 66 190 17 67 151 14 53 12 1 4 0 0 0
  95861. +11 1 4 146 13 51 190 17 67 188 17 66 188 17 66 188 17 66
  95862. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95863. +188 17 66 188 17 66 188 17 66 191 17 67 126 14 44 7 1 2
  95864. +0 0 0 0 0 0 0 0 0 0 0 0 32 3 11 164 15 58
  95865. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95866. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95867. +189 17 66 178 16 62 44 4 15 0 0 0 50 5 17 182 16 64
  95868. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  95869. +0 0 0 0 0 0 0 0 0
  95870. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 188 17 66
  95871. +188 17 66 188 17 66 191 17 67 131 12 46 3 0 1 0 0 0
  95872. +0 0 0 101 9 35 190 17 67 188 17 66 188 17 66 188 17 66
  95873. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95874. +188 17 66 188 17 66 190 17 67 170 15 60 44 4 15 0 0 0
  95875. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77 7 27
  95876. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95877. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95878. +191 17 67 134 12 47 9 1 3 0 0 0 31 3 11 171 15 60
  95879. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  95880. +0 0 0 0 0 0 0 0 0
  95881. +0 0 0 0 0 0 2 0 1 124 11 43 191 17 67 188 17 66
  95882. +188 17 66 188 17 66 191 17 67 101 9 35 0 0 0 0 0 0
  95883. +0 0 0 35 3 12 168 15 59 190 17 67 188 17 66 188 17 66
  95884. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95885. +188 17 66 189 17 66 182 16 64 77 7 27 0 0 0 0 0 0
  95886. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 1 2
  95887. +99 9 35 185 17 65 189 17 66 188 17 66 188 17 66 188 17 66
  95888. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  95889. +177 16 62 56 5 19 0 0 0 0 0 0 13 1 5 151 14 53
  95890. +190 17 67 188 17 66 188 17 66 188 17 66 185 17 65 56 5 19
  95891. +0 0 0 0 0 0 0 0 0
  95892. +0 0 0 0 0 0 0 0 0 99 9 35 191 17 67 188 17 66
  95893. +188 17 66 188 17 66 186 17 65 65 6 23 0 0 0 0 0 0
  95894. +0 0 0 0 0 0 79 7 28 182 16 64 190 17 67 188 17 66
  95895. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95896. +191 17 67 177 16 62 83 8 29 4 0 1 0 0 0 0 0 0
  95897. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95898. +8 1 3 89 8 31 175 16 62 191 17 67 189 17 66 188 17 66
  95899. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 181 16 63
  95900. +85 8 30 3 0 1 0 0 0 0 0 0 1 0 0 118 13 41
  95901. +191 17 67 188 17 66 188 17 66 189 17 66 173 16 61 34 3 12
  95902. +0 0 0 0 0 0 0 0 0
  95903. +0 0 0 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  95904. +188 17 66 189 17 66 169 15 59 30 3 10 0 0 0 0 0 0
  95905. +0 0 0 0 0 0 5 0 2 83 8 29 173 16 61 191 17 67
  95906. +190 17 67 189 17 66 189 17 66 190 17 67 191 17 67 187 17 66
  95907. +151 14 53 56 5 19 3 0 1 0 0 0 16 1 6 50 5 17
  95908. +79 7 28 95 9 33 95 9 33 75 7 26 41 4 14 10 1 4
  95909. +0 0 0 2 0 1 50 5 17 132 12 46 178 16 62 190 17 67
  95910. +191 17 67 191 17 67 191 17 67 186 17 65 154 14 54 68 6 24
  95911. +4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  95912. +187 17 66 188 17 66 188 17 66 191 17 67 141 13 49 9 1 3
  95913. +0 0 0 0 0 0 0 0 0
  95914. +0 0 0 0 0 0 0 0 0 14 1 5 151 14 53 190 17 67
  95915. +188 17 66 191 17 67 131 12 46 5 0 2 0 0 0 0 0 0
  95916. +0 0 0 0 0 0 0 0 0 2 0 1 44 4 15 113 10 40
  95917. +156 14 55 173 16 61 174 16 61 164 15 58 134 12 47 77 7 27
  95918. +18 2 6 0 0 0 16 1 6 85 8 30 151 14 53 182 16 64
  95919. +189 17 66 191 17 67 190 17 67 188 17 66 177 16 62 141 13 49
  95920. +68 6 24 8 1 3 0 0 0 8 1 3 44 4 15 88 8 31
  95921. +113 10 40 122 11 43 108 10 38 67 6 24 20 2 7 0 0 0
  95922. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 3 10
  95923. +166 15 58 190 17 67 188 17 66 187 17 66 79 7 28 0 0 0
  95924. +0 0 0 0 0 0 0 0 0
  95925. +0 0 0 0 0 0 0 0 0 0 0 0 73 7 26 185 17 65
  95926. +189 17 66 184 17 65 65 6 23 0 0 0 0 0 0 0 0 0
  95927. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1
  95928. +17 2 6 32 3 11 34 3 12 22 2 8 6 1 2 0 0 0
  95929. +0 0 0 38 3 13 141 13 49 188 17 66 190 17 67 188 17 66
  95930. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  95931. +184 17 65 122 11 43 21 2 7 0 0 0 0 0 0 0 0 0
  95932. +0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95933. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  95934. +108 10 38 191 17 67 191 17 67 141 13 49 16 1 6 0 0 0
  95935. +0 0 0 0 0 0 0 0 0
  95936. +0 0 0 0 0 0 0 0 0 0 0 0 8 1 3 112 10 39
  95937. +186 17 65 124 11 43 10 1 4 0 0 0 0 0 0 0 0 0
  95938. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95939. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95940. +36 3 13 156 14 55 191 17 67 188 17 66 188 17 66 188 17 66
  95941. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95942. +189 17 66 190 17 67 134 12 47 18 2 6 0 0 0 0 0 0
  95943. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95944. +0 0 0 7 1 2 41 4 14 75 7 26 66 5 23 19 2 7
  95945. +26 2 9 144 13 50 154 14 54 40 4 14 0 0 0 0 0 0
  95946. +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 13 1 5
  95948. +56 5 19 19 2 7 0 0 0 7 1 2 29 3 10 35 3 12
  95949. +19 2 7 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  95950. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  95951. +134 12 47 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95952. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95953. +188 17 66 188 17 66 189 17 67 108 10 38 3 0 1 0 0 0
  95954. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  95955. +40 4 14 124 11 43 177 16 62 188 17 66 187 17 66 144 13 50
  95956. +24 2 8 17 2 6 22 2 8 0 0 0 0 0 0 0 0 0
  95957. +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 19 2 7 122 11 43 171 15 60 175 16 62
  95960. +159 14 56 112 10 39 40 4 14 2 0 1 0 0 0 0 0 0
  95961. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  95962. +186 17 65 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95963. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95964. +188 17 66 188 17 66 189 17 66 174 16 61 41 4 14 0 0 0
  95965. +0 0 0 0 0 0 0 0 0 0 0 0 3 0 1 72 7 25
  95966. +168 15 59 191 17 67 189 17 66 188 17 66 188 17 66 190 17 67
  95967. +95 9 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95968. +0 0 0 0 0 0 0 0 0
  95969. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95970. +0 0 0 0 0 0 95 9 33 191 17 67 189 17 66 189 17 66
  95971. +190 17 67 191 17 67 171 15 60 90 8 32 12 1 4 0 0 0
  95972. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 132 12 46
  95973. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95974. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95975. +188 17 66 188 17 66 188 17 66 190 17 67 98 9 34 0 0 0
  95976. +0 0 0 0 0 0 0 0 0 5 0 2 88 8 31 180 16 63
  95977. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 191 17 67
  95978. +146 13 51 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  95979. +0 0 0 0 0 0 0 0 0
  95980. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95981. +0 0 0 9 1 3 144 13 50 191 17 67 188 17 66 188 17 66
  95982. +188 17 66 188 17 66 189 17 66 187 17 66 123 11 43 20 2 7
  95983. +0 0 0 0 0 0 0 0 0 0 0 0 21 2 7 163 15 57
  95984. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95985. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95986. +188 17 66 188 17 66 188 17 66 191 17 67 134 12 47 5 0 2
  95987. +0 0 0 0 0 0 3 0 1 88 8 31 182 16 64 189 17 66
  95988. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  95989. +171 15 60 31 3 11 0 0 0 0 0 0 0 0 0 0 0 0
  95990. +0 0 0 0 0 0 0 0 0
  95991. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95992. +0 0 0 20 2 7 162 15 57 190 17 67 188 17 66 188 17 66
  95993. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 132 12 46
  95994. +20 2 7 0 0 0 0 0 0 0 0 0 32 3 11 173 16 61
  95995. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95996. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95997. +188 17 66 188 17 66 188 17 66 190 17 67 151 14 53 12 1 4
  95998. +0 0 0 0 0 0 72 7 25 180 16 63 189 17 66 188 17 66
  95999. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96000. +181 16 63 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  96001. +0 0 0 0 0 0 0 0 0
  96002. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96003. +0 0 0 21 2 7 163 15 57 190 17 67 188 17 66 188 17 66
  96004. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  96005. +122 11 43 9 1 3 0 0 0 0 0 0 30 3 10 171 15 60
  96006. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96007. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96008. +188 17 66 188 17 66 188 17 66 190 17 67 146 13 51 10 1 4
  96009. +0 0 0 38 3 13 166 15 58 190 17 67 188 17 66 188 17 66
  96010. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96011. +183 17 64 52 5 18 0 0 0 0 0 0 0 0 0 0 0 0
  96012. +0 0 0 0 0 0 0 0 0
  96013. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96014. +0 0 0 13 1 5 154 14 54 190 17 67 188 17 66 188 17 66
  96015. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96016. +186 17 65 79 7 28 0 0 0 0 0 0 14 1 5 156 14 54
  96017. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96018. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96019. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 2 0 1
  96020. +5 0 2 122 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  96021. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96022. +182 16 64 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  96023. +0 0 0 0 0 0 0 0 0
  96024. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96025. +0 0 0 3 0 1 126 14 44 191 17 67 188 17 66 188 17 66
  96026. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96027. +190 17 67 158 14 55 23 2 8 0 0 0 1 0 0 113 10 40
  96028. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96029. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96030. +188 17 66 188 17 66 188 17 66 188 17 66 78 7 27 0 0 0
  96031. +47 4 16 177 16 62 189 17 66 188 17 66 188 17 66 188 17 66
  96032. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  96033. +173 16 61 34 3 12 0 0 0 0 0 0 0 0 0 0 0 0
  96034. +0 0 0 0 0 0 0 0 0
  96035. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96036. +0 0 0 0 0 0 85 8 30 189 17 66 188 17 66 188 17 66
  96037. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96038. +188 17 66 188 17 66 79 7 28 0 0 0 0 0 0 47 4 16
  96039. +175 16 62 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96040. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96041. +188 17 66 188 17 66 190 17 67 156 14 55 22 2 8 0 0 0
  96042. +109 10 38 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  96043. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  96044. +151 14 53 13 1 5 0 0 0 0 0 0 0 0 0 0 0 0
  96045. +0 0 0 0 0 0 0 0 0
  96046. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96047. +0 0 0 0 0 0 35 3 12 173 16 61 189 17 66 188 17 66
  96048. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96049. +188 17 66 191 17 67 134 12 47 7 1 2 0 0 0 3 0 1
  96050. +99 9 35 188 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  96051. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96052. +188 17 66 189 17 66 181 16 63 68 6 24 0 0 0 18 2 6
  96053. +156 14 55 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  96054. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  96055. +101 9 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96056. +0 0 0 0 0 0 0 0 0
  96057. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96058. +0 0 0 0 0 0 3 0 1 118 13 41 191 17 67 188 17 66
  96059. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96060. +188 17 66 189 17 66 168 15 59 28 3 10 0 0 0 0 0 0
  96061. +12 1 4 113 10 40 187 17 66 189 17 67 188 17 66 188 17 66
  96062. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96063. +190 17 67 180 16 63 88 8 31 4 0 1 0 0 0 47 4 16
  96064. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96065. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 168 15 59
  96066. +36 3 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96067. +0 0 0 0 0 0 0 0 0
  96068. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96069. +0 0 0 0 0 0 0 0 0 38 3 13 164 15 58 190 17 67
  96070. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96071. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  96072. +0 0 0 11 1 4 90 8 32 169 15 59 190 17 67 190 17 67
  96073. +189 17 66 189 17 66 189 17 66 189 17 66 191 17 67 189 17 66
  96074. +158 14 55 68 6 24 4 0 1 0 0 0 0 0 0 73 7 26
  96075. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96076. +188 17 66 188 17 66 188 17 66 189 17 66 185 17 65 83 8 29
  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 0 0 0
  96079. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96080. +0 0 0 0 0 0 0 0 0 0 0 0 65 6 23 174 16 61
  96081. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96082. +188 17 66 188 17 66 185 17 65 56 5 19 0 0 0 0 0 0
  96083. +0 0 0 0 0 0 2 0 1 35 3 12 99 9 35 146 13 51
  96084. +170 15 60 177 16 62 177 16 62 166 15 58 141 13 49 85 8 30
  96085. +24 2 8 0 0 0 0 0 0 0 0 0 0 0 0 85 8 30
  96086. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96087. +188 17 66 188 17 66 188 17 66 189 17 66 112 10 39 8 1 3
  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
  96090. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96091. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 68 6 24
  96092. +170 15 60 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  96093. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  96094. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 11 1 4
  96095. +28 3 10 40 4 14 38 3 13 25 2 9 8 1 3 0 0 0
  96096. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 78 7 27
  96097. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96098. +188 17 66 189 17 66 187 17 66 113 10 40 14 1 5 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
  96101. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96102. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  96103. +47 4 16 141 13 49 186 17 65 191 17 67 190 17 67 189 17 66
  96104. +189 17 66 191 17 67 156 14 55 20 2 7 0 0 0 0 0 0
  96105. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96106. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96107. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 4 15
  96108. +178 16 62 190 17 67 188 17 66 188 17 66 188 17 66 190 17 67
  96109. +191 17 67 173 16 61 90 8 32 10 1 4 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
  96112. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96113. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96114. +0 0 0 14 1 5 68 6 24 131 12 46 162 15 57 174 16 61
  96115. +171 15 60 146 13 51 56 5 19 0 0 0 0 0 0 0 0 0
  96116. +0 0 0 0 0 0 0 0 0 3 0 1 14 1 5 29 3 10
  96117. +41 4 14 47 4 16 50 5 17 45 4 16 34 3 12 18 2 6
  96118. +5 0 2 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  96119. +90 8 32 169 15 59 185 17 65 187 17 66 182 16 64 163 15 57
  96120. +113 10 40 41 4 14 2 0 1 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
  96123. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96124. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96125. +0 0 0 0 0 0 0 0 0 5 0 2 21 2 7 34 3 12
  96126. +29 3 10 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  96127. +3 0 1 32 3 11 79 7 28 124 11 43 154 14 54 171 15 60
  96128. +180 16 63 182 16 64 182 16 64 180 16 63 174 16 61 159 14 56
  96129. +132 12 46 88 8 31 34 3 12 3 0 1 0 0 0 0 0 0
  96130. +3 0 1 29 3 10 56 5 19 65 6 23 50 5 17 23 2 8
  96131. +3 0 1 0 0 0 0 0 0 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
  96134. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96135. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96136. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96137. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25 2 9
  96138. +109 10 38 169 15 59 189 17 66 191 17 67 190 17 67 189 17 66
  96139. +189 17 66 188 17 66 188 17 66 188 17 66 189 17 66 190 17 67
  96140. +191 17 67 190 17 67 171 15 60 98 9 34 10 1 3 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 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
  96145. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96146. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96147. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96148. +0 0 0 0 0 0 0 0 0 0 0 0 14 1 5 141 13 49
  96149. +191 17 67 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96150. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96151. +188 17 66 188 17 66 189 17 67 186 17 65 65 6 23 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 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
  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 0 0 0
  96158. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96159. +0 0 0 0 0 0 0 0 0 0 0 0 23 2 8 166 15 58
  96160. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96161. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96162. +188 17 66 188 17 66 189 17 66 176 16 62 45 4 16 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 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
  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 0 0 0 0 0 0
  96169. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96170. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 83 8 29
  96171. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96172. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96173. +188 17 66 189 17 66 185 17 65 95 9 33 3 0 1 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 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
  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 0 0 0 0 0 0
  96180. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96181. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  96182. +85 8 30 176 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  96183. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96184. +191 17 67 180 16 63 95 9 33 7 1 3 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 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
  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 0 0 0 0 0 0
  96191. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96192. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96193. +2 0 1 52 5 18 141 13 49 185 17 65 191 17 67 189 17 67
  96194. +189 17 66 188 17 66 188 17 66 189 17 66 191 17 67 187 17 66
  96195. +146 13 51 56 5 19 4 0 1 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 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
  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 0 0 0
  96202. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96203. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96204. +0 0 0 0 0 0 14 1 5 68 6 24 131 12 46 166 15 58
  96205. +180 16 63 183 17 64 180 16 63 168 15 59 134 12 47 75 7 26
  96206. +17 2 6 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 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
  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. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96214. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96215. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 24 2 8
  96216. +44 4 15 52 5 18 45 4 16 26 2 9 6 1 2 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 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
  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 0 0 0 0 0 0 0 0 0 0 0 0
  96225. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96226. +0 0 0 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 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
  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 0 0 0 0 0 0
  96236. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 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
  96244. diff -Nur linux-3.12.13/drivers/video/Makefile linux-raspberry-pi/drivers/video/Makefile
  96245. --- linux-3.12.13/drivers/video/Makefile 2014-02-22 22:32:50.000000000 +0100
  96246. +++ linux-raspberry-pi/drivers/video/Makefile 2014-03-11 17:33:07.000000000 +0100
  96247. @@ -100,6 +100,7 @@
  96248. obj-$(CONFIG_FB_VOODOO1) += sstfb.o
  96249. obj-$(CONFIG_FB_ARMCLCD) += amba-clcd.o
  96250. obj-$(CONFIG_FB_GOLDFISH) += goldfishfb.o
  96251. +obj-$(CONFIG_FB_BCM2708) += bcm2708_fb.o
  96252. obj-$(CONFIG_FB_68328) += 68328fb.o
  96253. obj-$(CONFIG_FB_GBE) += gbefb.o
  96254. obj-$(CONFIG_FB_CIRRUS) += cirrusfb.o
  96255. diff -Nur linux-3.12.13/drivers/w1/masters/w1-gpio.c linux-raspberry-pi/drivers/w1/masters/w1-gpio.c
  96256. --- linux-3.12.13/drivers/w1/masters/w1-gpio.c 2014-02-22 22:32:50.000000000 +0100
  96257. +++ linux-raspberry-pi/drivers/w1/masters/w1-gpio.c 2014-03-11 17:51:32.000000000 +0100
  96258. @@ -22,6 +22,9 @@
  96259. #include "../w1.h"
  96260. #include "../w1_int.h"
  96261. +static int w1_gpio_pullup = 0;
  96262. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  96263. +
  96264. static void w1_gpio_write_bit_dir(void *data, u8 bit)
  96265. {
  96266. struct w1_gpio_platform_data *pdata = data;
  96267. @@ -46,6 +49,16 @@
  96268. return gpio_get_value(pdata->pin) ? 1 : 0;
  96269. }
  96270. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  96271. +{
  96272. + struct w1_gpio_platform_data *pdata = data;
  96273. +
  96274. + if (on)
  96275. + gpio_direction_output(pdata->pin, 1);
  96276. + else
  96277. + gpio_direction_input(pdata->pin);
  96278. +}
  96279. +
  96280. #if defined(CONFIG_OF)
  96281. static struct of_device_id w1_gpio_dt_ids[] = {
  96282. { .compatible = "w1-gpio" },
  96283. @@ -127,6 +140,13 @@
  96284. master->write_bit = w1_gpio_write_bit_dir;
  96285. }
  96286. + if (w1_gpio_pullup)
  96287. + if (pdata->is_open_drain)
  96288. + printk(KERN_ERR "w1-gpio 'pullup' option "
  96289. + "doesn't work with open drain GPIO\n");
  96290. + else
  96291. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  96292. +
  96293. err = w1_add_master_device(master);
  96294. if (err) {
  96295. dev_err(&pdev->dev, "w1_add_master device failed\n");
  96296. diff -Nur linux-3.12.13/drivers/w1/w1.h linux-raspberry-pi/drivers/w1/w1.h
  96297. --- linux-3.12.13/drivers/w1/w1.h 2014-02-22 22:32:50.000000000 +0100
  96298. +++ linux-raspberry-pi/drivers/w1/w1.h 2014-03-11 17:33:12.000000000 +0100
  96299. @@ -148,6 +148,12 @@
  96300. */
  96301. u8 (*set_pullup)(void *, int);
  96302. + /**
  96303. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  96304. + * @return -1=Error, 0=completed
  96305. + */
  96306. + void (*bitbang_pullup) (void *, u8);
  96307. +
  96308. /** Really nice hardware can handles the different types of ROM search
  96309. * w1_master* is passed to the slave found callback.
  96310. */
  96311. diff -Nur linux-3.12.13/drivers/w1/w1_int.c linux-raspberry-pi/drivers/w1/w1_int.c
  96312. --- linux-3.12.13/drivers/w1/w1_int.c 2014-02-22 22:32:50.000000000 +0100
  96313. +++ linux-raspberry-pi/drivers/w1/w1_int.c 2014-03-11 17:33:12.000000000 +0100
  96314. @@ -117,19 +117,21 @@
  96315. printk(KERN_ERR "w1_add_master_device: invalid function set\n");
  96316. return(-EINVAL);
  96317. }
  96318. - /* While it would be electrically possible to make a device that
  96319. - * generated a strong pullup in bit bang mode, only hardware that
  96320. - * controls 1-wire time frames are even expected to support a strong
  96321. - * pullup. w1_io.c would need to support calling set_pullup before
  96322. - * the last write_bit operation of a w1_write_8 which it currently
  96323. - * doesn't.
  96324. - */
  96325. +
  96326. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  96327. + * and takes care of timing itself */
  96328. if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  96329. printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  96330. "write_byte or touch_bit, disabling\n");
  96331. master->set_pullup = NULL;
  96332. }
  96333. + if (master->set_pullup && master->bitbang_pullup) {
  96334. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  96335. + "be set when bitbang_pullup is used, disabling\n");
  96336. + master->set_pullup = NULL;
  96337. + }
  96338. +
  96339. /* Lock until the device is added (or not) to w1_masters. */
  96340. mutex_lock(&w1_mlock);
  96341. /* Search for the first available id (starting at 1). */
  96342. diff -Nur linux-3.12.13/drivers/w1/w1_io.c linux-raspberry-pi/drivers/w1/w1_io.c
  96343. --- linux-3.12.13/drivers/w1/w1_io.c 2014-02-22 22:32:50.000000000 +0100
  96344. +++ linux-raspberry-pi/drivers/w1/w1_io.c 2014-03-11 17:33:12.000000000 +0100
  96345. @@ -127,10 +127,22 @@
  96346. static void w1_post_write(struct w1_master *dev)
  96347. {
  96348. if (dev->pullup_duration) {
  96349. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  96350. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  96351. - else
  96352. + if (dev->enable_pullup) {
  96353. + if (dev->bus_master->set_pullup) {
  96354. + dev->bus_master->set_pullup(dev->
  96355. + bus_master->data,
  96356. + 0);
  96357. + } else if (dev->bus_master->bitbang_pullup) {
  96358. + dev->bus_master->
  96359. + bitbang_pullup(dev->bus_master->data, 1);
  96360. msleep(dev->pullup_duration);
  96361. + dev->bus_master->
  96362. + bitbang_pullup(dev->bus_master->data, 0);
  96363. + }
  96364. + } else {
  96365. + msleep(dev->pullup_duration);
  96366. + }
  96367. +
  96368. dev->pullup_duration = 0;
  96369. }
  96370. }
  96371. diff -Nur linux-3.12.13/drivers/watchdog/bcm2708_wdog.c linux-raspberry-pi/drivers/watchdog/bcm2708_wdog.c
  96372. --- linux-3.12.13/drivers/watchdog/bcm2708_wdog.c 1970-01-01 01:00:00.000000000 +0100
  96373. +++ linux-raspberry-pi/drivers/watchdog/bcm2708_wdog.c 2014-03-11 17:51:32.000000000 +0100
  96374. @@ -0,0 +1,384 @@
  96375. +/*
  96376. + * Broadcom BCM2708 watchdog driver.
  96377. + *
  96378. + * (c) Copyright 2010 Broadcom Europe Ltd
  96379. + *
  96380. + * This program is free software; you can redistribute it and/or
  96381. + * modify it under the terms of the GNU General Public License
  96382. + * as published by the Free Software Foundation; either version
  96383. + * 2 of the License, or (at your option) any later version.
  96384. + *
  96385. + * BCM2708 watchdog driver. Loosely based on wdt driver.
  96386. + */
  96387. +
  96388. +#include <linux/interrupt.h>
  96389. +#include <linux/module.h>
  96390. +#include <linux/moduleparam.h>
  96391. +#include <linux/types.h>
  96392. +#include <linux/miscdevice.h>
  96393. +#include <linux/watchdog.h>
  96394. +#include <linux/fs.h>
  96395. +#include <linux/ioport.h>
  96396. +#include <linux/notifier.h>
  96397. +#include <linux/reboot.h>
  96398. +#include <linux/init.h>
  96399. +#include <linux/io.h>
  96400. +#include <linux/uaccess.h>
  96401. +#include <mach/platform.h>
  96402. +
  96403. +#include <asm/system.h>
  96404. +
  96405. +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  96406. +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  96407. +
  96408. +static unsigned long wdog_is_open;
  96409. +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
  96410. +static char expect_close;
  96411. +
  96412. +/*
  96413. + * Module parameters
  96414. + */
  96415. +
  96416. +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
  96417. +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
  96418. +
  96419. +module_param(heartbeat, int, 0);
  96420. +MODULE_PARM_DESC(heartbeat,
  96421. + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  96422. + __MODULE_STRING(WD_TIMO) ")");
  96423. +
  96424. +static int nowayout = WATCHDOG_NOWAYOUT;
  96425. +module_param(nowayout, int, 0);
  96426. +MODULE_PARM_DESC(nowayout,
  96427. + "Watchdog cannot be stopped once started (default="
  96428. + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  96429. +
  96430. +static DEFINE_SPINLOCK(wdog_lock);
  96431. +
  96432. +/**
  96433. + * Start the watchdog driver.
  96434. + */
  96435. +
  96436. +static int wdog_start(unsigned long timeout)
  96437. +{
  96438. + uint32_t cur;
  96439. + unsigned long flags;
  96440. + spin_lock_irqsave(&wdog_lock, flags);
  96441. +
  96442. + /* enable the watchdog */
  96443. + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
  96444. + __io_address(PM_WDOG));
  96445. + cur = ioread32(__io_address(PM_RSTC));
  96446. + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  96447. + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
  96448. +
  96449. + spin_unlock_irqrestore(&wdog_lock, flags);
  96450. + return 0;
  96451. +}
  96452. +
  96453. +/**
  96454. + * Stop the watchdog driver.
  96455. + */
  96456. +
  96457. +static int wdog_stop(void)
  96458. +{
  96459. + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
  96460. + printk(KERN_INFO "watchdog stopped\n");
  96461. + return 0;
  96462. +}
  96463. +
  96464. +/**
  96465. + * Reload counter one with the watchdog heartbeat. We don't bother
  96466. + * reloading the cascade counter.
  96467. + */
  96468. +
  96469. +static void wdog_ping(void)
  96470. +{
  96471. + wdog_start(wdog_ticks);
  96472. +}
  96473. +
  96474. +/**
  96475. + * @t: the new heartbeat value that needs to be set.
  96476. + *
  96477. + * Set a new heartbeat value for the watchdog device. If the heartbeat
  96478. + * value is incorrect we keep the old value and return -EINVAL. If
  96479. + * successful we return 0.
  96480. + */
  96481. +
  96482. +static int wdog_set_heartbeat(int t)
  96483. +{
  96484. + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
  96485. + return -EINVAL;
  96486. +
  96487. + heartbeat = t;
  96488. + wdog_ticks = SECS_TO_WDOG_TICKS(t);
  96489. + return 0;
  96490. +}
  96491. +
  96492. +/**
  96493. + * @file: file handle to the watchdog
  96494. + * @buf: buffer to write (unused as data does not matter here
  96495. + * @count: count of bytes
  96496. + * @ppos: pointer to the position to write. No seeks allowed
  96497. + *
  96498. + * A write to a watchdog device is defined as a keepalive signal.
  96499. + *
  96500. + * if 'nowayout' is set then normally a close() is ignored. But
  96501. + * if you write 'V' first then the close() will stop the timer.
  96502. + */
  96503. +
  96504. +static ssize_t wdog_write(struct file *file, const char __user *buf,
  96505. + size_t count, loff_t *ppos)
  96506. +{
  96507. + if (count) {
  96508. + if (!nowayout) {
  96509. + size_t i;
  96510. +
  96511. + /* In case it was set long ago */
  96512. + expect_close = 0;
  96513. +
  96514. + for (i = 0; i != count; i++) {
  96515. + char c;
  96516. + if (get_user(c, buf + i))
  96517. + return -EFAULT;
  96518. + if (c == 'V')
  96519. + expect_close = 42;
  96520. + }
  96521. + }
  96522. + wdog_ping();
  96523. + }
  96524. + return count;
  96525. +}
  96526. +
  96527. +static int wdog_get_status(void)
  96528. +{
  96529. + unsigned long flags;
  96530. + int status = 0;
  96531. + spin_lock_irqsave(&wdog_lock, flags);
  96532. + /* FIXME: readback reset reason */
  96533. + spin_unlock_irqrestore(&wdog_lock, flags);
  96534. + return status;
  96535. +}
  96536. +
  96537. +static uint32_t wdog_get_remaining(void)
  96538. +{
  96539. + uint32_t ret = ioread32(__io_address(PM_WDOG));
  96540. + return ret & PM_WDOG_TIME_SET;
  96541. +}
  96542. +
  96543. +/**
  96544. + * @file: file handle to the device
  96545. + * @cmd: watchdog command
  96546. + * @arg: argument pointer
  96547. + *
  96548. + * The watchdog API defines a common set of functions for all watchdogs
  96549. + * according to their available features. We only actually usefully support
  96550. + * querying capabilities and current status.
  96551. + */
  96552. +
  96553. +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  96554. +{
  96555. + void __user *argp = (void __user *)arg;
  96556. + int __user *p = argp;
  96557. + int new_heartbeat;
  96558. + int status;
  96559. + int options;
  96560. + uint32_t remaining;
  96561. +
  96562. + struct watchdog_info ident = {
  96563. + .options = WDIOF_SETTIMEOUT|
  96564. + WDIOF_MAGICCLOSE|
  96565. + WDIOF_KEEPALIVEPING,
  96566. + .firmware_version = 1,
  96567. + .identity = "BCM2708",
  96568. + };
  96569. +
  96570. + switch (cmd) {
  96571. + case WDIOC_GETSUPPORT:
  96572. + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  96573. + case WDIOC_GETSTATUS:
  96574. + status = wdog_get_status();
  96575. + return put_user(status, p);
  96576. + case WDIOC_GETBOOTSTATUS:
  96577. + return put_user(0, p);
  96578. + case WDIOC_KEEPALIVE:
  96579. + wdog_ping();
  96580. + return 0;
  96581. + case WDIOC_SETTIMEOUT:
  96582. + if (get_user(new_heartbeat, p))
  96583. + return -EFAULT;
  96584. + if (wdog_set_heartbeat(new_heartbeat))
  96585. + return -EINVAL;
  96586. + wdog_ping();
  96587. + /* Fall */
  96588. + case WDIOC_GETTIMEOUT:
  96589. + return put_user(heartbeat, p);
  96590. + case WDIOC_GETTIMELEFT:
  96591. + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
  96592. + return put_user(remaining, p);
  96593. + case WDIOC_SETOPTIONS:
  96594. + if (get_user(options, p))
  96595. + return -EFAULT;
  96596. + if (options & WDIOS_DISABLECARD)
  96597. + wdog_stop();
  96598. + if (options & WDIOS_ENABLECARD)
  96599. + wdog_start(wdog_ticks);
  96600. + return 0;
  96601. + default:
  96602. + return -ENOTTY;
  96603. + }
  96604. +}
  96605. +
  96606. +/**
  96607. + * @inode: inode of device
  96608. + * @file: file handle to device
  96609. + *
  96610. + * The watchdog device has been opened. The watchdog device is single
  96611. + * open and on opening we load the counters.
  96612. + */
  96613. +
  96614. +static int wdog_open(struct inode *inode, struct file *file)
  96615. +{
  96616. + if (test_and_set_bit(0, &wdog_is_open))
  96617. + return -EBUSY;
  96618. + /*
  96619. + * Activate
  96620. + */
  96621. + wdog_start(wdog_ticks);
  96622. + return nonseekable_open(inode, file);
  96623. +}
  96624. +
  96625. +/**
  96626. + * @inode: inode to board
  96627. + * @file: file handle to board
  96628. + *
  96629. + * The watchdog has a configurable API. There is a religious dispute
  96630. + * between people who want their watchdog to be able to shut down and
  96631. + * those who want to be sure if the watchdog manager dies the machine
  96632. + * reboots. In the former case we disable the counters, in the latter
  96633. + * case you have to open it again very soon.
  96634. + */
  96635. +
  96636. +static int wdog_release(struct inode *inode, struct file *file)
  96637. +{
  96638. + if (expect_close == 42) {
  96639. + wdog_stop();
  96640. + } else {
  96641. + printk(KERN_CRIT
  96642. + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  96643. + wdog_ping();
  96644. + }
  96645. + clear_bit(0, &wdog_is_open);
  96646. + expect_close = 0;
  96647. + return 0;
  96648. +}
  96649. +
  96650. +/**
  96651. + * @this: our notifier block
  96652. + * @code: the event being reported
  96653. + * @unused: unused
  96654. + *
  96655. + * Our notifier is called on system shutdowns. Turn the watchdog
  96656. + * off so that it does not fire during the next reboot.
  96657. + */
  96658. +
  96659. +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
  96660. + void *unused)
  96661. +{
  96662. + if (code == SYS_DOWN || code == SYS_HALT)
  96663. + wdog_stop();
  96664. + return NOTIFY_DONE;
  96665. +}
  96666. +
  96667. +/*
  96668. + * Kernel Interfaces
  96669. + */
  96670. +
  96671. +
  96672. +static const struct file_operations wdog_fops = {
  96673. + .owner = THIS_MODULE,
  96674. + .llseek = no_llseek,
  96675. + .write = wdog_write,
  96676. + .unlocked_ioctl = wdog_ioctl,
  96677. + .open = wdog_open,
  96678. + .release = wdog_release,
  96679. +};
  96680. +
  96681. +static struct miscdevice wdog_miscdev = {
  96682. + .minor = WATCHDOG_MINOR,
  96683. + .name = "watchdog",
  96684. + .fops = &wdog_fops,
  96685. +};
  96686. +
  96687. +/*
  96688. + * The WDT card needs to learn about soft shutdowns in order to
  96689. + * turn the timebomb registers off.
  96690. + */
  96691. +
  96692. +static struct notifier_block wdog_notifier = {
  96693. + .notifier_call = wdog_notify_sys,
  96694. +};
  96695. +
  96696. +/**
  96697. + * cleanup_module:
  96698. + *
  96699. + * Unload the watchdog. You cannot do this with any file handles open.
  96700. + * If your watchdog is set to continue ticking on close and you unload
  96701. + * it, well it keeps ticking. We won't get the interrupt but the board
  96702. + * will not touch PC memory so all is fine. You just have to load a new
  96703. + * module in 60 seconds or reboot.
  96704. + */
  96705. +
  96706. +static void __exit wdog_exit(void)
  96707. +{
  96708. + misc_deregister(&wdog_miscdev);
  96709. + unregister_reboot_notifier(&wdog_notifier);
  96710. +}
  96711. +
  96712. +static int __init wdog_init(void)
  96713. +{
  96714. + int ret;
  96715. +
  96716. + /* Check that the heartbeat value is within it's range;
  96717. + if not reset to the default */
  96718. + if (wdog_set_heartbeat(heartbeat)) {
  96719. + wdog_set_heartbeat(WD_TIMO);
  96720. + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
  96721. + "0 < heartbeat < %d, using %d\n",
  96722. + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  96723. + WD_TIMO);
  96724. + }
  96725. +
  96726. + ret = register_reboot_notifier(&wdog_notifier);
  96727. + if (ret) {
  96728. + printk(KERN_ERR
  96729. + "wdt: cannot register reboot notifier (err=%d)\n", ret);
  96730. + goto out_reboot;
  96731. + }
  96732. +
  96733. + ret = misc_register(&wdog_miscdev);
  96734. + if (ret) {
  96735. + printk(KERN_ERR
  96736. + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  96737. + WATCHDOG_MINOR, ret);
  96738. + goto out_misc;
  96739. + }
  96740. +
  96741. + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
  96742. + heartbeat, nowayout);
  96743. + return 0;
  96744. +
  96745. +out_misc:
  96746. + unregister_reboot_notifier(&wdog_notifier);
  96747. +out_reboot:
  96748. + return ret;
  96749. +}
  96750. +
  96751. +module_init(wdog_init);
  96752. +module_exit(wdog_exit);
  96753. +
  96754. +MODULE_AUTHOR("Luke Diamand");
  96755. +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
  96756. +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  96757. +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  96758. +MODULE_LICENSE("GPL");
  96759. diff -Nur linux-3.12.13/drivers/watchdog/Kconfig linux-raspberry-pi/drivers/watchdog/Kconfig
  96760. --- linux-3.12.13/drivers/watchdog/Kconfig 2014-02-22 22:32:50.000000000 +0100
  96761. +++ linux-raspberry-pi/drivers/watchdog/Kconfig 2014-03-11 17:51:32.000000000 +0100
  96762. @@ -392,6 +392,12 @@
  96763. To compile this driver as a module, choose M here: the
  96764. module will be called retu_wdt.
  96765. +config BCM2708_WDT
  96766. + tristate "BCM2708 Watchdog"
  96767. + depends on ARCH_BCM2708
  96768. + help
  96769. + Enables BCM2708 watchdog support.
  96770. +
  96771. # AVR32 Architecture
  96772. config AT32AP700X_WDT
  96773. diff -Nur linux-3.12.13/drivers/watchdog/Makefile linux-raspberry-pi/drivers/watchdog/Makefile
  96774. --- linux-3.12.13/drivers/watchdog/Makefile 2014-02-22 22:32:50.000000000 +0100
  96775. +++ linux-raspberry-pi/drivers/watchdog/Makefile 2014-03-11 17:51:32.000000000 +0100
  96776. @@ -54,6 +54,7 @@
  96777. obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
  96778. obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
  96779. obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
  96780. +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
  96781. obj-$(CONFIG_BCM2835_WDT) += bcm2835_wdt.o
  96782. # AVR32 Architecture
  96783. diff -Nur linux-3.12.13/include/linux/broadcom/vc_cma.h linux-raspberry-pi/include/linux/broadcom/vc_cma.h
  96784. --- linux-3.12.13/include/linux/broadcom/vc_cma.h 1970-01-01 01:00:00.000000000 +0100
  96785. +++ linux-raspberry-pi/include/linux/broadcom/vc_cma.h 2014-03-11 17:51:38.000000000 +0100
  96786. @@ -0,0 +1,29 @@
  96787. +/*****************************************************************************
  96788. +* Copyright 2012 Broadcom Corporation. All rights reserved.
  96789. +*
  96790. +* Unless you and Broadcom execute a separate written software license
  96791. +* agreement governing use of this software, this software is licensed to you
  96792. +* under the terms of the GNU General Public License version 2, available at
  96793. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96794. +*
  96795. +* Notwithstanding the above, under no circumstances may you combine this
  96796. +* software in any way with any other Broadcom software provided under a
  96797. +* license other than the GPL, without Broadcom's express prior written
  96798. +* consent.
  96799. +*****************************************************************************/
  96800. +
  96801. +#if !defined( VC_CMA_H )
  96802. +#define VC_CMA_H
  96803. +
  96804. +#include <linux/ioctl.h>
  96805. +
  96806. +#define VC_CMA_IOC_MAGIC 0xc5
  96807. +
  96808. +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
  96809. +
  96810. +#ifdef __KERNEL__
  96811. +extern void __init vc_cma_early_init(void);
  96812. +extern void __init vc_cma_reserve(void);
  96813. +#endif
  96814. +
  96815. +#endif /* VC_CMA_H */
  96816. diff -Nur linux-3.12.13/include/linux/mmc/host.h linux-raspberry-pi/include/linux/mmc/host.h
  96817. --- linux-3.12.13/include/linux/mmc/host.h 2014-02-22 22:32:50.000000000 +0100
  96818. +++ linux-raspberry-pi/include/linux/mmc/host.h 2014-03-11 17:51:40.000000000 +0100
  96819. @@ -281,6 +281,7 @@
  96820. MMC_CAP2_PACKED_WR)
  96821. #define MMC_CAP2_NO_PRESCAN_POWERUP (1 << 14) /* Don't power up before scan */
  96822. #define MMC_CAP2_SANITIZE (1 << 15) /* Support Sanitize */
  96823. +#define MMC_CAP2_FORCE_MULTIBLOCK (1 << 31) /* Always use multiblock transfers */
  96824. mmc_pm_flag_t pm_caps; /* supported pm features */
  96825. diff -Nur linux-3.12.13/include/linux/mmc/sdhci.h linux-raspberry-pi/include/linux/mmc/sdhci.h
  96826. --- linux-3.12.13/include/linux/mmc/sdhci.h 2014-02-22 22:32:50.000000000 +0100
  96827. +++ linux-raspberry-pi/include/linux/mmc/sdhci.h 2014-03-11 17:51:40.000000000 +0100
  96828. @@ -100,6 +100,7 @@
  96829. #define SDHCI_QUIRK2_BROKEN_HOST_CONTROL (1<<5)
  96830. int irq; /* Device IRQ */
  96831. + int second_irq; /* Additional IRQ to disable/enable in low-latency mode */
  96832. void __iomem *ioaddr; /* Mapped address */
  96833. const struct sdhci_ops *ops; /* Low level hw interface */
  96834. @@ -131,6 +132,7 @@
  96835. #define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  96836. #define SDHCI_SDR104_NEEDS_TUNING (1<<10) /* SDR104/HS200 needs tuning */
  96837. #define SDHCI_USING_RETUNING_TIMER (1<<11) /* Host is using a retuning timer for the card */
  96838. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  96839. unsigned int version; /* SDHCI spec. version */
  96840. @@ -146,6 +148,7 @@
  96841. struct mmc_request *mrq; /* Current request */
  96842. struct mmc_command *cmd; /* Current command */
  96843. + int last_cmdop; /* Opcode of last cmd sent */
  96844. struct mmc_data *data; /* Current data request */
  96845. unsigned int data_early:1; /* Data finished before cmd */
  96846. diff -Nur linux-3.12.13/include/sound/soc-dai.h linux-raspberry-pi/include/sound/soc-dai.h
  96847. --- linux-3.12.13/include/sound/soc-dai.h 2014-02-22 22:32:50.000000000 +0100
  96848. +++ linux-raspberry-pi/include/sound/soc-dai.h 2014-03-11 17:33:22.000000000 +0100
  96849. @@ -105,6 +105,8 @@
  96850. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  96851. int pll_id, int source, unsigned int freq_in, unsigned int freq_out);
  96852. +int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio);
  96853. +
  96854. /* Digital Audio interface formatting */
  96855. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt);
  96856. @@ -131,6 +133,7 @@
  96857. int (*set_pll)(struct snd_soc_dai *dai, int pll_id, int source,
  96858. unsigned int freq_in, unsigned int freq_out);
  96859. int (*set_clkdiv)(struct snd_soc_dai *dai, int div_id, int div);
  96860. + int (*set_bclk_ratio)(struct snd_soc_dai *dai, unsigned int ratio);
  96861. /*
  96862. * DAI format configuration
  96863. diff -Nur linux-3.12.13/include/uapi/linux/fb.h linux-raspberry-pi/include/uapi/linux/fb.h
  96864. --- linux-3.12.13/include/uapi/linux/fb.h 2014-02-22 22:32:50.000000000 +0100
  96865. +++ linux-raspberry-pi/include/uapi/linux/fb.h 2014-03-11 17:33:22.000000000 +0100
  96866. @@ -34,6 +34,11 @@
  96867. #define FBIOPUT_MODEINFO 0x4617
  96868. #define FBIOGET_DISPINFO 0x4618
  96869. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  96870. +/*
  96871. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  96872. + * be concurrently added to the mainline kernel
  96873. + */
  96874. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  96875. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  96876. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  96877. diff -Nur linux-3.12.13/sound/arm/bcm2835.c linux-raspberry-pi/sound/arm/bcm2835.c
  96878. --- linux-3.12.13/sound/arm/bcm2835.c 1970-01-01 01:00:00.000000000 +0100
  96879. +++ linux-raspberry-pi/sound/arm/bcm2835.c 2014-03-11 17:51:48.000000000 +0100
  96880. @@ -0,0 +1,413 @@
  96881. +/*****************************************************************************
  96882. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  96883. +*
  96884. +* Unless you and Broadcom execute a separate written software license
  96885. +* agreement governing use of this software, this software is licensed to you
  96886. +* under the terms of the GNU General Public License version 2, available at
  96887. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96888. +*
  96889. +* Notwithstanding the above, under no circumstances may you combine this
  96890. +* software in any way with any other Broadcom software provided under a
  96891. +* license other than the GPL, without Broadcom's express prior written
  96892. +* consent.
  96893. +*****************************************************************************/
  96894. +
  96895. +#include <linux/platform_device.h>
  96896. +
  96897. +#include <linux/init.h>
  96898. +#include <linux/slab.h>
  96899. +#include <linux/module.h>
  96900. +
  96901. +#include "bcm2835.h"
  96902. +
  96903. +/* module parameters (see "Module Parameters") */
  96904. +/* SNDRV_CARDS: maximum number of cards supported by this module */
  96905. +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
  96906. +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
  96907. +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
  96908. +
  96909. +/* HACKY global pointers needed for successive probes to work : ssp
  96910. + * But compared against the changes we will have to do in VC audio_ipc code
  96911. + * to export 8 audio_ipc devices as a single IPC device and then monitor all
  96912. + * four devices in a thread, this gets things done quickly and should be easier
  96913. + * to debug if we run into issues
  96914. + */
  96915. +
  96916. +static struct snd_card *g_card = NULL;
  96917. +static bcm2835_chip_t *g_chip = NULL;
  96918. +
  96919. +static int snd_bcm2835_free(bcm2835_chip_t * chip)
  96920. +{
  96921. + kfree(chip);
  96922. + return 0;
  96923. +}
  96924. +
  96925. +/* component-destructor
  96926. + * (see "Management of Cards and Components")
  96927. + */
  96928. +static int snd_bcm2835_dev_free(struct snd_device *device)
  96929. +{
  96930. + return snd_bcm2835_free(device->device_data);
  96931. +}
  96932. +
  96933. +/* chip-specific constructor
  96934. + * (see "Management of Cards and Components")
  96935. + */
  96936. +static int snd_bcm2835_create(struct snd_card *card,
  96937. + struct platform_device *pdev,
  96938. + bcm2835_chip_t ** rchip)
  96939. +{
  96940. + bcm2835_chip_t *chip;
  96941. + int err;
  96942. + static struct snd_device_ops ops = {
  96943. + .dev_free = snd_bcm2835_dev_free,
  96944. + };
  96945. +
  96946. + *rchip = NULL;
  96947. +
  96948. + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  96949. + if (chip == NULL)
  96950. + return -ENOMEM;
  96951. +
  96952. + chip->card = card;
  96953. +
  96954. + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  96955. + if (err < 0) {
  96956. + snd_bcm2835_free(chip);
  96957. + return err;
  96958. + }
  96959. +
  96960. + *rchip = chip;
  96961. + return 0;
  96962. +}
  96963. +
  96964. +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
  96965. +{
  96966. + static int dev;
  96967. + bcm2835_chip_t *chip;
  96968. + struct snd_card *card;
  96969. + int err;
  96970. +
  96971. + if (dev >= MAX_SUBSTREAMS)
  96972. + return -ENODEV;
  96973. +
  96974. + if (!enable[dev]) {
  96975. + dev++;
  96976. + return -ENOENT;
  96977. + }
  96978. +
  96979. + if (dev > 0)
  96980. + goto add_register_map;
  96981. +
  96982. + err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &g_card);
  96983. + if (err < 0)
  96984. + goto out;
  96985. +
  96986. + snd_card_set_dev(g_card, &pdev->dev);
  96987. + strcpy(g_card->driver, "BRCM bcm2835 ALSA Driver");
  96988. + strcpy(g_card->shortname, "bcm2835 ALSA");
  96989. + sprintf(g_card->longname, "%s", g_card->shortname);
  96990. +
  96991. + err = snd_bcm2835_create(g_card, pdev, &chip);
  96992. + if (err < 0) {
  96993. + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
  96994. + goto out_bcm2835_create;
  96995. + }
  96996. +
  96997. + g_chip = chip;
  96998. + err = snd_bcm2835_new_pcm(chip);
  96999. + if (err < 0) {
  97000. + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
  97001. + goto out_bcm2835_new_pcm;
  97002. + }
  97003. +
  97004. + err = snd_bcm2835_new_ctl(chip);
  97005. + if (err < 0) {
  97006. + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
  97007. + goto out_bcm2835_new_ctl;
  97008. + }
  97009. +
  97010. +add_register_map:
  97011. + card = g_card;
  97012. + chip = g_chip;
  97013. +
  97014. + BUG_ON(!(card && chip));
  97015. +
  97016. + chip->avail_substreams |= (1 << dev);
  97017. + chip->pdev[dev] = pdev;
  97018. +
  97019. + if (dev == 0) {
  97020. + err = snd_card_register(card);
  97021. + if (err < 0) {
  97022. + dev_err(&pdev->dev,
  97023. + "Failed to register bcm2835 ALSA card \n");
  97024. + goto out_card_register;
  97025. + }
  97026. + platform_set_drvdata(pdev, card);
  97027. + audio_info("bcm2835 ALSA card created!\n");
  97028. + } else {
  97029. + audio_info("bcm2835 ALSA chip created!\n");
  97030. + platform_set_drvdata(pdev, (void *)dev);
  97031. + }
  97032. +
  97033. + dev++;
  97034. +
  97035. + return 0;
  97036. +
  97037. +out_card_register:
  97038. +out_bcm2835_new_ctl:
  97039. +out_bcm2835_new_pcm:
  97040. +out_bcm2835_create:
  97041. + BUG_ON(!g_card);
  97042. + if (snd_card_free(g_card))
  97043. + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
  97044. + g_card = NULL;
  97045. +out:
  97046. + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
  97047. + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
  97048. + return err;
  97049. +}
  97050. +
  97051. +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
  97052. +{
  97053. + uint32_t idx;
  97054. + void *drv_data;
  97055. +
  97056. + drv_data = platform_get_drvdata(pdev);
  97057. +
  97058. + if (drv_data == (void *)g_card) {
  97059. + /* This is the card device */
  97060. + snd_card_free((struct snd_card *)drv_data);
  97061. + g_card = NULL;
  97062. + g_chip = NULL;
  97063. + } else {
  97064. + idx = (uint32_t) drv_data;
  97065. + if (g_card != NULL) {
  97066. + BUG_ON(!g_chip);
  97067. + /* We pass chip device numbers in audio ipc devices
  97068. + * other than the one we registered our card with
  97069. + */
  97070. + idx = (uint32_t) drv_data;
  97071. + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
  97072. + g_chip->avail_substreams &= ~(1 << idx);
  97073. + /* There should be atleast one substream registered
  97074. + * after we are done here, as it wil be removed when
  97075. + * the *remove* is called for the card device
  97076. + */
  97077. + BUG_ON(!g_chip->avail_substreams);
  97078. + }
  97079. + }
  97080. +
  97081. + platform_set_drvdata(pdev, NULL);
  97082. +
  97083. + return 0;
  97084. +}
  97085. +
  97086. +#ifdef CONFIG_PM
  97087. +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
  97088. + pm_message_t state)
  97089. +{
  97090. + return 0;
  97091. +}
  97092. +
  97093. +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
  97094. +{
  97095. + return 0;
  97096. +}
  97097. +
  97098. +#endif
  97099. +
  97100. +static struct platform_driver bcm2835_alsa0_driver = {
  97101. + .probe = snd_bcm2835_alsa_probe,
  97102. + .remove = snd_bcm2835_alsa_remove,
  97103. +#ifdef CONFIG_PM
  97104. + .suspend = snd_bcm2835_alsa_suspend,
  97105. + .resume = snd_bcm2835_alsa_resume,
  97106. +#endif
  97107. + .driver = {
  97108. + .name = "bcm2835_AUD0",
  97109. + .owner = THIS_MODULE,
  97110. + },
  97111. +};
  97112. +
  97113. +static struct platform_driver bcm2835_alsa1_driver = {
  97114. + .probe = snd_bcm2835_alsa_probe,
  97115. + .remove = snd_bcm2835_alsa_remove,
  97116. +#ifdef CONFIG_PM
  97117. + .suspend = snd_bcm2835_alsa_suspend,
  97118. + .resume = snd_bcm2835_alsa_resume,
  97119. +#endif
  97120. + .driver = {
  97121. + .name = "bcm2835_AUD1",
  97122. + .owner = THIS_MODULE,
  97123. + },
  97124. +};
  97125. +
  97126. +static struct platform_driver bcm2835_alsa2_driver = {
  97127. + .probe = snd_bcm2835_alsa_probe,
  97128. + .remove = snd_bcm2835_alsa_remove,
  97129. +#ifdef CONFIG_PM
  97130. + .suspend = snd_bcm2835_alsa_suspend,
  97131. + .resume = snd_bcm2835_alsa_resume,
  97132. +#endif
  97133. + .driver = {
  97134. + .name = "bcm2835_AUD2",
  97135. + .owner = THIS_MODULE,
  97136. + },
  97137. +};
  97138. +
  97139. +static struct platform_driver bcm2835_alsa3_driver = {
  97140. + .probe = snd_bcm2835_alsa_probe,
  97141. + .remove = snd_bcm2835_alsa_remove,
  97142. +#ifdef CONFIG_PM
  97143. + .suspend = snd_bcm2835_alsa_suspend,
  97144. + .resume = snd_bcm2835_alsa_resume,
  97145. +#endif
  97146. + .driver = {
  97147. + .name = "bcm2835_AUD3",
  97148. + .owner = THIS_MODULE,
  97149. + },
  97150. +};
  97151. +
  97152. +static struct platform_driver bcm2835_alsa4_driver = {
  97153. + .probe = snd_bcm2835_alsa_probe,
  97154. + .remove = snd_bcm2835_alsa_remove,
  97155. +#ifdef CONFIG_PM
  97156. + .suspend = snd_bcm2835_alsa_suspend,
  97157. + .resume = snd_bcm2835_alsa_resume,
  97158. +#endif
  97159. + .driver = {
  97160. + .name = "bcm2835_AUD4",
  97161. + .owner = THIS_MODULE,
  97162. + },
  97163. +};
  97164. +
  97165. +static struct platform_driver bcm2835_alsa5_driver = {
  97166. + .probe = snd_bcm2835_alsa_probe,
  97167. + .remove = snd_bcm2835_alsa_remove,
  97168. +#ifdef CONFIG_PM
  97169. + .suspend = snd_bcm2835_alsa_suspend,
  97170. + .resume = snd_bcm2835_alsa_resume,
  97171. +#endif
  97172. + .driver = {
  97173. + .name = "bcm2835_AUD5",
  97174. + .owner = THIS_MODULE,
  97175. + },
  97176. +};
  97177. +
  97178. +static struct platform_driver bcm2835_alsa6_driver = {
  97179. + .probe = snd_bcm2835_alsa_probe,
  97180. + .remove = snd_bcm2835_alsa_remove,
  97181. +#ifdef CONFIG_PM
  97182. + .suspend = snd_bcm2835_alsa_suspend,
  97183. + .resume = snd_bcm2835_alsa_resume,
  97184. +#endif
  97185. + .driver = {
  97186. + .name = "bcm2835_AUD6",
  97187. + .owner = THIS_MODULE,
  97188. + },
  97189. +};
  97190. +
  97191. +static struct platform_driver bcm2835_alsa7_driver = {
  97192. + .probe = snd_bcm2835_alsa_probe,
  97193. + .remove = snd_bcm2835_alsa_remove,
  97194. +#ifdef CONFIG_PM
  97195. + .suspend = snd_bcm2835_alsa_suspend,
  97196. + .resume = snd_bcm2835_alsa_resume,
  97197. +#endif
  97198. + .driver = {
  97199. + .name = "bcm2835_AUD7",
  97200. + .owner = THIS_MODULE,
  97201. + },
  97202. +};
  97203. +
  97204. +static int bcm2835_alsa_device_init(void)
  97205. +{
  97206. + int err;
  97207. + err = platform_driver_register(&bcm2835_alsa0_driver);
  97208. + if (err) {
  97209. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97210. + goto out;
  97211. + }
  97212. +
  97213. + err = platform_driver_register(&bcm2835_alsa1_driver);
  97214. + if (err) {
  97215. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97216. + goto unregister_0;
  97217. + }
  97218. +
  97219. + err = platform_driver_register(&bcm2835_alsa2_driver);
  97220. + if (err) {
  97221. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97222. + goto unregister_1;
  97223. + }
  97224. +
  97225. + err = platform_driver_register(&bcm2835_alsa3_driver);
  97226. + if (err) {
  97227. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97228. + goto unregister_2;
  97229. + }
  97230. +
  97231. + err = platform_driver_register(&bcm2835_alsa4_driver);
  97232. + if (err) {
  97233. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97234. + goto unregister_3;
  97235. + }
  97236. +
  97237. + err = platform_driver_register(&bcm2835_alsa5_driver);
  97238. + if (err) {
  97239. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97240. + goto unregister_4;
  97241. + }
  97242. +
  97243. + err = platform_driver_register(&bcm2835_alsa6_driver);
  97244. + if (err) {
  97245. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97246. + goto unregister_5;
  97247. + }
  97248. +
  97249. + err = platform_driver_register(&bcm2835_alsa7_driver);
  97250. + if (err) {
  97251. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97252. + goto unregister_6;
  97253. + }
  97254. +
  97255. + return 0;
  97256. +
  97257. +unregister_6:
  97258. + platform_driver_unregister(&bcm2835_alsa6_driver);
  97259. +unregister_5:
  97260. + platform_driver_unregister(&bcm2835_alsa5_driver);
  97261. +unregister_4:
  97262. + platform_driver_unregister(&bcm2835_alsa4_driver);
  97263. +unregister_3:
  97264. + platform_driver_unregister(&bcm2835_alsa3_driver);
  97265. +unregister_2:
  97266. + platform_driver_unregister(&bcm2835_alsa2_driver);
  97267. +unregister_1:
  97268. + platform_driver_unregister(&bcm2835_alsa1_driver);
  97269. +unregister_0:
  97270. + platform_driver_unregister(&bcm2835_alsa0_driver);
  97271. +out:
  97272. + return err;
  97273. +}
  97274. +
  97275. +static void bcm2835_alsa_device_exit(void)
  97276. +{
  97277. + platform_driver_unregister(&bcm2835_alsa0_driver);
  97278. + platform_driver_unregister(&bcm2835_alsa1_driver);
  97279. + platform_driver_unregister(&bcm2835_alsa2_driver);
  97280. + platform_driver_unregister(&bcm2835_alsa3_driver);
  97281. + platform_driver_unregister(&bcm2835_alsa4_driver);
  97282. + platform_driver_unregister(&bcm2835_alsa5_driver);
  97283. + platform_driver_unregister(&bcm2835_alsa6_driver);
  97284. + platform_driver_unregister(&bcm2835_alsa7_driver);
  97285. +}
  97286. +
  97287. +late_initcall(bcm2835_alsa_device_init);
  97288. +module_exit(bcm2835_alsa_device_exit);
  97289. +
  97290. +MODULE_AUTHOR("Dom Cobley");
  97291. +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
  97292. +MODULE_LICENSE("GPL");
  97293. +MODULE_ALIAS("platform:bcm2835_alsa");
  97294. diff -Nur linux-3.12.13/sound/arm/bcm2835-ctl.c linux-raspberry-pi/sound/arm/bcm2835-ctl.c
  97295. --- linux-3.12.13/sound/arm/bcm2835-ctl.c 1970-01-01 01:00:00.000000000 +0100
  97296. +++ linux-raspberry-pi/sound/arm/bcm2835-ctl.c 2014-03-11 17:51:48.000000000 +0100
  97297. @@ -0,0 +1,200 @@
  97298. +/*****************************************************************************
  97299. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97300. +*
  97301. +* Unless you and Broadcom execute a separate written software license
  97302. +* agreement governing use of this software, this software is licensed to you
  97303. +* under the terms of the GNU General Public License version 2, available at
  97304. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97305. +*
  97306. +* Notwithstanding the above, under no circumstances may you combine this
  97307. +* software in any way with any other Broadcom software provided under a
  97308. +* license other than the GPL, without Broadcom's express prior written
  97309. +* consent.
  97310. +*****************************************************************************/
  97311. +
  97312. +#include <linux/platform_device.h>
  97313. +#include <linux/init.h>
  97314. +#include <linux/io.h>
  97315. +#include <linux/jiffies.h>
  97316. +#include <linux/slab.h>
  97317. +#include <linux/time.h>
  97318. +#include <linux/wait.h>
  97319. +#include <linux/delay.h>
  97320. +#include <linux/moduleparam.h>
  97321. +#include <linux/sched.h>
  97322. +
  97323. +#include <sound/core.h>
  97324. +#include <sound/control.h>
  97325. +#include <sound/pcm.h>
  97326. +#include <sound/pcm_params.h>
  97327. +#include <sound/rawmidi.h>
  97328. +#include <sound/initval.h>
  97329. +#include <sound/tlv.h>
  97330. +
  97331. +#include "bcm2835.h"
  97332. +
  97333. +/* volume maximum and minimum in terms of 0.01dB */
  97334. +#define CTRL_VOL_MAX 400
  97335. +#define CTRL_VOL_MIN -10239 /* originally -10240 */
  97336. +
  97337. +
  97338. +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
  97339. + struct snd_ctl_elem_info *uinfo)
  97340. +{
  97341. + audio_info(" ... IN\n");
  97342. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  97343. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  97344. + uinfo->count = 1;
  97345. + uinfo->value.integer.min = CTRL_VOL_MIN;
  97346. + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
  97347. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  97348. + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  97349. + uinfo->count = 1;
  97350. + uinfo->value.integer.min = 0;
  97351. + uinfo->value.integer.max = 1;
  97352. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  97353. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  97354. + uinfo->count = 1;
  97355. + uinfo->value.integer.min = 0;
  97356. + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
  97357. + }
  97358. + audio_info(" ... OUT\n");
  97359. + return 0;
  97360. +}
  97361. +
  97362. +/* toggles mute on or off depending on the value of nmute, and returns
  97363. + * 1 if the mute value was changed, otherwise 0
  97364. + */
  97365. +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
  97366. +{
  97367. + /* if settings are ok, just return 0 */
  97368. + if(chip->mute == nmute)
  97369. + return 0;
  97370. +
  97371. + /* if the sound is muted then we need to unmute */
  97372. + if(chip->mute == CTRL_VOL_MUTE)
  97373. + {
  97374. + chip->volume = chip->old_volume; /* copy the old volume back */
  97375. + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  97376. + }
  97377. + else /* otherwise we mute */
  97378. + {
  97379. + chip->old_volume = chip->volume;
  97380. + chip->volume = 26214; /* set volume to minimum level AKA mute */
  97381. + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  97382. + }
  97383. +
  97384. + chip->mute = nmute;
  97385. + return 1;
  97386. +}
  97387. +
  97388. +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
  97389. + struct snd_ctl_elem_value *ucontrol)
  97390. +{
  97391. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  97392. +
  97393. + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
  97394. +
  97395. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
  97396. + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
  97397. + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
  97398. + ucontrol->value.integer.value[0] = chip->mute;
  97399. + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
  97400. + ucontrol->value.integer.value[0] = chip->dest;
  97401. +
  97402. + return 0;
  97403. +}
  97404. +
  97405. +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
  97406. + struct snd_ctl_elem_value *ucontrol)
  97407. +{
  97408. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  97409. + int changed = 0;
  97410. +
  97411. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  97412. + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
  97413. + if (chip->mute == CTRL_VOL_MUTE) {
  97414. + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
  97415. + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
  97416. + }
  97417. + if (changed
  97418. + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
  97419. +
  97420. + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
  97421. + changed = 1;
  97422. + }
  97423. +
  97424. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  97425. + /* Now implemented */
  97426. + audio_info(" Mute attempted\n");
  97427. + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
  97428. +
  97429. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  97430. + if (ucontrol->value.integer.value[0] != chip->dest) {
  97431. + chip->dest = ucontrol->value.integer.value[0];
  97432. + changed = 1;
  97433. + }
  97434. + }
  97435. +
  97436. + if (changed) {
  97437. + if (bcm2835_audio_set_ctls(chip))
  97438. + printk(KERN_ERR "Failed to set ALSA controls..\n");
  97439. + }
  97440. +
  97441. + return changed;
  97442. +}
  97443. +
  97444. +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
  97445. +
  97446. +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
  97447. + {
  97448. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  97449. + .name = "PCM Playback Volume",
  97450. + .index = 0,
  97451. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  97452. + .private_value = PCM_PLAYBACK_VOLUME,
  97453. + .info = snd_bcm2835_ctl_info,
  97454. + .get = snd_bcm2835_ctl_get,
  97455. + .put = snd_bcm2835_ctl_put,
  97456. + .count = 1,
  97457. + .tlv = {.p = snd_bcm2835_db_scale}
  97458. + },
  97459. + {
  97460. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  97461. + .name = "PCM Playback Switch",
  97462. + .index = 0,
  97463. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  97464. + .private_value = PCM_PLAYBACK_MUTE,
  97465. + .info = snd_bcm2835_ctl_info,
  97466. + .get = snd_bcm2835_ctl_get,
  97467. + .put = snd_bcm2835_ctl_put,
  97468. + .count = 1,
  97469. + },
  97470. + {
  97471. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  97472. + .name = "PCM Playback Route",
  97473. + .index = 0,
  97474. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  97475. + .private_value = PCM_PLAYBACK_DEVICE,
  97476. + .info = snd_bcm2835_ctl_info,
  97477. + .get = snd_bcm2835_ctl_get,
  97478. + .put = snd_bcm2835_ctl_put,
  97479. + .count = 1,
  97480. + },
  97481. +};
  97482. +
  97483. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
  97484. +{
  97485. + int err;
  97486. + unsigned int idx;
  97487. +
  97488. + strcpy(chip->card->mixername, "Broadcom Mixer");
  97489. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
  97490. + err =
  97491. + snd_ctl_add(chip->card,
  97492. + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
  97493. + if (err < 0)
  97494. + return err;
  97495. + }
  97496. + return 0;
  97497. +}
  97498. diff -Nur linux-3.12.13/sound/arm/bcm2835.h linux-raspberry-pi/sound/arm/bcm2835.h
  97499. --- linux-3.12.13/sound/arm/bcm2835.h 1970-01-01 01:00:00.000000000 +0100
  97500. +++ linux-raspberry-pi/sound/arm/bcm2835.h 2014-03-11 17:51:48.000000000 +0100
  97501. @@ -0,0 +1,157 @@
  97502. +/*****************************************************************************
  97503. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97504. +*
  97505. +* Unless you and Broadcom execute a separate written software license
  97506. +* agreement governing use of this software, this software is licensed to you
  97507. +* under the terms of the GNU General Public License version 2, available at
  97508. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97509. +*
  97510. +* Notwithstanding the above, under no circumstances may you combine this
  97511. +* software in any way with any other Broadcom software provided under a
  97512. +* license other than the GPL, without Broadcom's express prior written
  97513. +* consent.
  97514. +*****************************************************************************/
  97515. +
  97516. +#ifndef __SOUND_ARM_BCM2835_H
  97517. +#define __SOUND_ARM_BCM2835_H
  97518. +
  97519. +#include <linux/device.h>
  97520. +#include <linux/list.h>
  97521. +#include <linux/interrupt.h>
  97522. +#include <linux/wait.h>
  97523. +#include <sound/core.h>
  97524. +#include <sound/initval.h>
  97525. +#include <sound/pcm.h>
  97526. +#include <sound/pcm_params.h>
  97527. +#include <sound/pcm-indirect.h>
  97528. +#include <linux/workqueue.h>
  97529. +
  97530. +/*
  97531. +#define AUDIO_DEBUG_ENABLE
  97532. +#define AUDIO_VERBOSE_DEBUG_ENABLE
  97533. +*/
  97534. +
  97535. +/* Debug macros */
  97536. +
  97537. +#ifdef AUDIO_DEBUG_ENABLE
  97538. +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
  97539. +
  97540. +#define audio_debug(fmt, arg...) \
  97541. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  97542. +
  97543. +#define audio_info(fmt, arg...) \
  97544. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  97545. +
  97546. +#else
  97547. +
  97548. +#define audio_debug(fmt, arg...)
  97549. +
  97550. +#define audio_info(fmt, arg...)
  97551. +
  97552. +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
  97553. +
  97554. +#else
  97555. +
  97556. +#define audio_debug(fmt, arg...)
  97557. +
  97558. +#define audio_info(fmt, arg...)
  97559. +
  97560. +#endif /* AUDIO_DEBUG_ENABLE */
  97561. +
  97562. +#define audio_error(fmt, arg...) \
  97563. + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
  97564. +
  97565. +#define audio_warning(fmt, arg...) \
  97566. + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
  97567. +
  97568. +#define audio_alert(fmt, arg...) \
  97569. + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
  97570. +
  97571. +#define MAX_SUBSTREAMS (8)
  97572. +#define AVAIL_SUBSTREAMS_MASK (0xff)
  97573. +enum {
  97574. + CTRL_VOL_MUTE,
  97575. + CTRL_VOL_UNMUTE
  97576. +};
  97577. +
  97578. +/* macros for alsa2chip and chip2alsa, instead of functions */
  97579. +
  97580. +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
  97581. +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
  97582. +
  97583. +/* Some constants for values .. */
  97584. +typedef enum {
  97585. + AUDIO_DEST_AUTO = 0,
  97586. + AUDIO_DEST_HEADPHONES = 1,
  97587. + AUDIO_DEST_HDMI = 2,
  97588. + AUDIO_DEST_MAX,
  97589. +} SND_BCM2835_ROUTE_T;
  97590. +
  97591. +typedef enum {
  97592. + PCM_PLAYBACK_VOLUME,
  97593. + PCM_PLAYBACK_MUTE,
  97594. + PCM_PLAYBACK_DEVICE,
  97595. +} SND_BCM2835_CTRL_T;
  97596. +
  97597. +/* definition of the chip-specific record */
  97598. +typedef struct bcm2835_chip {
  97599. + struct snd_card *card;
  97600. + struct snd_pcm *pcm;
  97601. + /* Bitmat for valid reg_base and irq numbers */
  97602. + uint32_t avail_substreams;
  97603. + struct platform_device *pdev[MAX_SUBSTREAMS];
  97604. + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
  97605. +
  97606. + int volume;
  97607. + int old_volume; /* stores the volume value whist muted */
  97608. + int dest;
  97609. + int mute;
  97610. +} bcm2835_chip_t;
  97611. +
  97612. +typedef struct bcm2835_alsa_stream {
  97613. + bcm2835_chip_t *chip;
  97614. + struct snd_pcm_substream *substream;
  97615. + struct snd_pcm_indirect pcm_indirect;
  97616. +
  97617. + struct semaphore buffers_update_sem;
  97618. + struct semaphore control_sem;
  97619. + spinlock_t lock;
  97620. + volatile uint32_t control;
  97621. + volatile uint32_t status;
  97622. +
  97623. + int open;
  97624. + int running;
  97625. + int draining;
  97626. +
  97627. + unsigned int pos;
  97628. + unsigned int buffer_size;
  97629. + unsigned int period_size;
  97630. +
  97631. + uint32_t enable_fifo_irq;
  97632. + irq_handler_t fifo_irq_handler;
  97633. +
  97634. + atomic_t retrieved;
  97635. + struct opaque_AUDIO_INSTANCE_T *instance;
  97636. + struct workqueue_struct *my_wq;
  97637. + int idx;
  97638. +} bcm2835_alsa_stream_t;
  97639. +
  97640. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
  97641. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
  97642. +
  97643. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
  97644. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
  97645. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  97646. + uint32_t channels, uint32_t samplerate,
  97647. + uint32_t bps);
  97648. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
  97649. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
  97650. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
  97651. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
  97652. +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
  97653. + void *src);
  97654. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97655. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97656. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97657. +
  97658. +#endif /* __SOUND_ARM_BCM2835_H */
  97659. diff -Nur linux-3.12.13/sound/arm/bcm2835-pcm.c linux-raspberry-pi/sound/arm/bcm2835-pcm.c
  97660. --- linux-3.12.13/sound/arm/bcm2835-pcm.c 1970-01-01 01:00:00.000000000 +0100
  97661. +++ linux-raspberry-pi/sound/arm/bcm2835-pcm.c 2014-03-11 17:51:48.000000000 +0100
  97662. @@ -0,0 +1,426 @@
  97663. +/*****************************************************************************
  97664. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97665. +*
  97666. +* Unless you and Broadcom execute a separate written software license
  97667. +* agreement governing use of this software, this software is licensed to you
  97668. +* under the terms of the GNU General Public License version 2, available at
  97669. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97670. +*
  97671. +* Notwithstanding the above, under no circumstances may you combine this
  97672. +* software in any way with any other Broadcom software provided under a
  97673. +* license other than the GPL, without Broadcom's express prior written
  97674. +* consent.
  97675. +*****************************************************************************/
  97676. +
  97677. +#include <linux/interrupt.h>
  97678. +#include <linux/slab.h>
  97679. +
  97680. +#include "bcm2835.h"
  97681. +
  97682. +/* hardware definition */
  97683. +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
  97684. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  97685. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  97686. + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  97687. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  97688. + .rate_min = 8000,
  97689. + .rate_max = 48000,
  97690. + .channels_min = 1,
  97691. + .channels_max = 2,
  97692. + .buffer_bytes_max = 128 * 1024,
  97693. + .period_bytes_min = 1 * 1024,
  97694. + .period_bytes_max = 128 * 1024,
  97695. + .periods_min = 1,
  97696. + .periods_max = 128,
  97697. +};
  97698. +
  97699. +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
  97700. +{
  97701. + audio_info("Freeing up alsa stream here ..\n");
  97702. + if (runtime->private_data)
  97703. + kfree(runtime->private_data);
  97704. + runtime->private_data = NULL;
  97705. +}
  97706. +
  97707. +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
  97708. +{
  97709. + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
  97710. + uint32_t consumed = 0;
  97711. + int new_period = 0;
  97712. +
  97713. + audio_info(" .. IN\n");
  97714. +
  97715. + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
  97716. + alsa_stream ? alsa_stream->substream : 0);
  97717. +
  97718. + if (alsa_stream->open)
  97719. + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
  97720. +
  97721. + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
  97722. + * each iteration are the buffers that have been played out already
  97723. + */
  97724. +
  97725. + if (alsa_stream->period_size) {
  97726. + if ((alsa_stream->pos / alsa_stream->period_size) !=
  97727. + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
  97728. + new_period = 1;
  97729. + }
  97730. + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
  97731. + alsa_stream->pos,
  97732. + consumed,
  97733. + alsa_stream->buffer_size,
  97734. + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
  97735. + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
  97736. + new_period);
  97737. + if (alsa_stream->buffer_size) {
  97738. + alsa_stream->pos += consumed &~ (1<<30);
  97739. + alsa_stream->pos %= alsa_stream->buffer_size;
  97740. + }
  97741. +
  97742. + if (alsa_stream->substream) {
  97743. + if (new_period)
  97744. + snd_pcm_period_elapsed(alsa_stream->substream);
  97745. + } else {
  97746. + audio_warning(" unexpected NULL substream\n");
  97747. + }
  97748. + audio_info(" .. OUT\n");
  97749. +
  97750. + return IRQ_HANDLED;
  97751. +}
  97752. +
  97753. +/* open callback */
  97754. +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
  97755. +{
  97756. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  97757. + struct snd_pcm_runtime *runtime = substream->runtime;
  97758. + bcm2835_alsa_stream_t *alsa_stream;
  97759. + int idx;
  97760. + int err;
  97761. +
  97762. + audio_info(" .. IN (%d)\n", substream->number);
  97763. +
  97764. + audio_info("Alsa open (%d)\n", substream->number);
  97765. + idx = substream->number;
  97766. +
  97767. + if (idx > MAX_SUBSTREAMS) {
  97768. + audio_error
  97769. + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
  97770. + idx, MAX_SUBSTREAMS);
  97771. + err = -ENODEV;
  97772. + goto out;
  97773. + }
  97774. +
  97775. + /* Check if we are ready */
  97776. + if (!(chip->avail_substreams & (1 << idx))) {
  97777. + /* We are not ready yet */
  97778. + audio_error("substream(%d) device is not ready yet\n", idx);
  97779. + err = -EAGAIN;
  97780. + goto out;
  97781. + }
  97782. +
  97783. + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
  97784. + if (alsa_stream == NULL) {
  97785. + return -ENOMEM;
  97786. + }
  97787. +
  97788. + /* Initialise alsa_stream */
  97789. + alsa_stream->chip = chip;
  97790. + alsa_stream->substream = substream;
  97791. + alsa_stream->idx = idx;
  97792. + chip->alsa_stream[idx] = alsa_stream;
  97793. +
  97794. + sema_init(&alsa_stream->buffers_update_sem, 0);
  97795. + sema_init(&alsa_stream->control_sem, 0);
  97796. + spin_lock_init(&alsa_stream->lock);
  97797. +
  97798. + /* Enabled in start trigger, called on each "fifo irq" after that */
  97799. + alsa_stream->enable_fifo_irq = 0;
  97800. + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
  97801. +
  97802. + runtime->private_data = alsa_stream;
  97803. + runtime->private_free = snd_bcm2835_playback_free;
  97804. + runtime->hw = snd_bcm2835_playback_hw;
  97805. + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
  97806. + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  97807. + 16);
  97808. +
  97809. + err = bcm2835_audio_open(alsa_stream);
  97810. + if (err != 0) {
  97811. + kfree(alsa_stream);
  97812. + return err;
  97813. + }
  97814. +
  97815. + alsa_stream->open = 1;
  97816. + alsa_stream->draining = 1;
  97817. +
  97818. +out:
  97819. + audio_info(" .. OUT =%d\n", err);
  97820. +
  97821. + return err;
  97822. +}
  97823. +
  97824. +/* close callback */
  97825. +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
  97826. +{
  97827. + /* the hardware-specific codes will be here */
  97828. +
  97829. + struct snd_pcm_runtime *runtime = substream->runtime;
  97830. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97831. +
  97832. + audio_info(" .. IN\n");
  97833. + audio_info("Alsa close\n");
  97834. +
  97835. + /*
  97836. + * Call stop if it's still running. This happens when app
  97837. + * is force killed and we don't get a stop trigger.
  97838. + */
  97839. + if (alsa_stream->running) {
  97840. + int err;
  97841. + err = bcm2835_audio_stop(alsa_stream);
  97842. + alsa_stream->running = 0;
  97843. + if (err != 0)
  97844. + audio_error(" Failed to STOP alsa device\n");
  97845. + }
  97846. +
  97847. + alsa_stream->period_size = 0;
  97848. + alsa_stream->buffer_size = 0;
  97849. +
  97850. + if (alsa_stream->open) {
  97851. + alsa_stream->open = 0;
  97852. + bcm2835_audio_close(alsa_stream);
  97853. + }
  97854. + if (alsa_stream->chip)
  97855. + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
  97856. + /*
  97857. + * Do not free up alsa_stream here, it will be freed up by
  97858. + * runtime->private_free callback we registered in *_open above
  97859. + */
  97860. +
  97861. + audio_info(" .. OUT\n");
  97862. +
  97863. + return 0;
  97864. +}
  97865. +
  97866. +/* hw_params callback */
  97867. +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
  97868. + struct snd_pcm_hw_params *params)
  97869. +{
  97870. + int err;
  97871. + struct snd_pcm_runtime *runtime = substream->runtime;
  97872. + bcm2835_alsa_stream_t *alsa_stream =
  97873. + (bcm2835_alsa_stream_t *) runtime->private_data;
  97874. +
  97875. + audio_info(" .. IN\n");
  97876. +
  97877. + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  97878. + if (err < 0) {
  97879. + audio_error
  97880. + (" pcm_lib_malloc failed to allocated pages for buffers\n");
  97881. + return err;
  97882. + }
  97883. +
  97884. + err = bcm2835_audio_set_params(alsa_stream, params_channels(params),
  97885. + params_rate(params),
  97886. + snd_pcm_format_width(params_format
  97887. + (params)));
  97888. + if (err < 0) {
  97889. + audio_error(" error setting hw params\n");
  97890. + }
  97891. +
  97892. + bcm2835_audio_setup(alsa_stream);
  97893. +
  97894. + /* in preparation of the stream, set the controls (volume level) of the stream */
  97895. + bcm2835_audio_set_ctls(alsa_stream->chip);
  97896. +
  97897. + audio_info(" .. OUT\n");
  97898. +
  97899. + return err;
  97900. +}
  97901. +
  97902. +/* hw_free callback */
  97903. +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
  97904. +{
  97905. + audio_info(" .. IN\n");
  97906. + return snd_pcm_lib_free_pages(substream);
  97907. +}
  97908. +
  97909. +/* prepare callback */
  97910. +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
  97911. +{
  97912. + struct snd_pcm_runtime *runtime = substream->runtime;
  97913. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97914. +
  97915. + audio_info(" .. IN\n");
  97916. +
  97917. + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
  97918. +
  97919. + alsa_stream->pcm_indirect.hw_buffer_size =
  97920. + alsa_stream->pcm_indirect.sw_buffer_size =
  97921. + snd_pcm_lib_buffer_bytes(substream);
  97922. +
  97923. + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
  97924. + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
  97925. + alsa_stream->pos = 0;
  97926. +
  97927. + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
  97928. + alsa_stream->buffer_size, alsa_stream->period_size,
  97929. + alsa_stream->pos, runtime->frame_bits);
  97930. +
  97931. + audio_info(" .. OUT\n");
  97932. + return 0;
  97933. +}
  97934. +
  97935. +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
  97936. + struct snd_pcm_indirect *rec, size_t bytes)
  97937. +{
  97938. + struct snd_pcm_runtime *runtime = substream->runtime;
  97939. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97940. + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
  97941. + int err;
  97942. +
  97943. + err = bcm2835_audio_write(alsa_stream, bytes, src);
  97944. + if (err)
  97945. + audio_error(" Failed to transfer to alsa device (%d)\n", err);
  97946. +
  97947. +}
  97948. +
  97949. +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
  97950. +{
  97951. + struct snd_pcm_runtime *runtime = substream->runtime;
  97952. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97953. + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
  97954. +
  97955. + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
  97956. + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
  97957. + snd_bcm2835_pcm_transfer);
  97958. + return 0;
  97959. +}
  97960. +
  97961. +/* trigger callback */
  97962. +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  97963. +{
  97964. + struct snd_pcm_runtime *runtime = substream->runtime;
  97965. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97966. + int err = 0;
  97967. +
  97968. + audio_info(" .. IN\n");
  97969. +
  97970. + switch (cmd) {
  97971. + case SNDRV_PCM_TRIGGER_START:
  97972. + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
  97973. + alsa_stream->running);
  97974. + if (!alsa_stream->running) {
  97975. + err = bcm2835_audio_start(alsa_stream);
  97976. + if (err == 0) {
  97977. + alsa_stream->pcm_indirect.hw_io =
  97978. + alsa_stream->pcm_indirect.hw_data =
  97979. + bytes_to_frames(runtime,
  97980. + alsa_stream->pos);
  97981. + substream->ops->ack(substream);
  97982. + alsa_stream->running = 1;
  97983. + alsa_stream->draining = 1;
  97984. + } else {
  97985. + audio_error(" Failed to START alsa device (%d)\n", err);
  97986. + }
  97987. + }
  97988. + break;
  97989. + case SNDRV_PCM_TRIGGER_STOP:
  97990. + audio_debug
  97991. + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
  97992. + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
  97993. + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  97994. + audio_info("DRAINING\n");
  97995. + alsa_stream->draining = 1;
  97996. + } else {
  97997. + audio_info("DROPPING\n");
  97998. + alsa_stream->draining = 0;
  97999. + }
  98000. + if (alsa_stream->running) {
  98001. + err = bcm2835_audio_stop(alsa_stream);
  98002. + if (err != 0)
  98003. + audio_error(" Failed to STOP alsa device (%d)\n", err);
  98004. + alsa_stream->running = 0;
  98005. + }
  98006. + break;
  98007. + default:
  98008. + err = -EINVAL;
  98009. + }
  98010. +
  98011. + audio_info(" .. OUT\n");
  98012. + return err;
  98013. +}
  98014. +
  98015. +/* pointer callback */
  98016. +static snd_pcm_uframes_t
  98017. +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
  98018. +{
  98019. + struct snd_pcm_runtime *runtime = substream->runtime;
  98020. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  98021. +
  98022. + audio_info(" .. IN\n");
  98023. +
  98024. + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
  98025. + frames_to_bytes(runtime, runtime->status->hw_ptr),
  98026. + frames_to_bytes(runtime, runtime->control->appl_ptr),
  98027. + alsa_stream->pos);
  98028. +
  98029. + audio_info(" .. OUT\n");
  98030. + return snd_pcm_indirect_playback_pointer(substream,
  98031. + &alsa_stream->pcm_indirect,
  98032. + alsa_stream->pos);
  98033. +}
  98034. +
  98035. +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  98036. + unsigned int cmd, void *arg)
  98037. +{
  98038. + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  98039. + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
  98040. + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
  98041. + return ret;
  98042. +}
  98043. +
  98044. +/* operators */
  98045. +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
  98046. + .open = snd_bcm2835_playback_open,
  98047. + .close = snd_bcm2835_playback_close,
  98048. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  98049. + .hw_params = snd_bcm2835_pcm_hw_params,
  98050. + .hw_free = snd_bcm2835_pcm_hw_free,
  98051. + .prepare = snd_bcm2835_pcm_prepare,
  98052. + .trigger = snd_bcm2835_pcm_trigger,
  98053. + .pointer = snd_bcm2835_pcm_pointer,
  98054. + .ack = snd_bcm2835_pcm_ack,
  98055. +};
  98056. +
  98057. +/* create a pcm device */
  98058. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
  98059. +{
  98060. + struct snd_pcm *pcm;
  98061. + int err;
  98062. +
  98063. + audio_info(" .. IN\n");
  98064. + err =
  98065. + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
  98066. + if (err < 0)
  98067. + return err;
  98068. + pcm->private_data = chip;
  98069. + strcpy(pcm->name, "bcm2835 ALSA");
  98070. + chip->pcm = pcm;
  98071. + chip->dest = AUDIO_DEST_AUTO;
  98072. + chip->volume = alsa2chip(0);
  98073. + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
  98074. + /* set operators */
  98075. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  98076. + &snd_bcm2835_playback_ops);
  98077. +
  98078. + /* pre-allocation of buffers */
  98079. + /* NOTE: this may fail */
  98080. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  98081. + snd_dma_continuous_data
  98082. + (GFP_KERNEL), 64 * 1024,
  98083. + 64 * 1024);
  98084. +
  98085. + audio_info(" .. OUT\n");
  98086. +
  98087. + return 0;
  98088. +}
  98089. diff -Nur linux-3.12.13/sound/arm/bcm2835-vchiq.c linux-raspberry-pi/sound/arm/bcm2835-vchiq.c
  98090. --- linux-3.12.13/sound/arm/bcm2835-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  98091. +++ linux-raspberry-pi/sound/arm/bcm2835-vchiq.c 2014-03-11 17:51:48.000000000 +0100
  98092. @@ -0,0 +1,879 @@
  98093. +/*****************************************************************************
  98094. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  98095. +*
  98096. +* Unless you and Broadcom execute a separate written software license
  98097. +* agreement governing use of this software, this software is licensed to you
  98098. +* under the terms of the GNU General Public License version 2, available at
  98099. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  98100. +*
  98101. +* Notwithstanding the above, under no circumstances may you combine this
  98102. +* software in any way with any other Broadcom software provided under a
  98103. +* license other than the GPL, without Broadcom's express prior written
  98104. +* consent.
  98105. +*****************************************************************************/
  98106. +
  98107. +#include <linux/device.h>
  98108. +#include <sound/core.h>
  98109. +#include <sound/initval.h>
  98110. +#include <sound/pcm.h>
  98111. +#include <linux/io.h>
  98112. +#include <linux/interrupt.h>
  98113. +#include <linux/fs.h>
  98114. +#include <linux/file.h>
  98115. +#include <linux/mm.h>
  98116. +#include <linux/syscalls.h>
  98117. +#include <asm/uaccess.h>
  98118. +#include <linux/slab.h>
  98119. +#include <linux/delay.h>
  98120. +#include <linux/atomic.h>
  98121. +#include <linux/module.h>
  98122. +#include <linux/completion.h>
  98123. +
  98124. +#include "bcm2835.h"
  98125. +
  98126. +/* ---- Include Files -------------------------------------------------------- */
  98127. +
  98128. +#include "interface/vchi/vchi.h"
  98129. +#include "vc_vchi_audioserv_defs.h"
  98130. +
  98131. +/* ---- Private Constants and Types ------------------------------------------ */
  98132. +
  98133. +#define BCM2835_AUDIO_STOP 0
  98134. +#define BCM2835_AUDIO_START 1
  98135. +#define BCM2835_AUDIO_WRITE 2
  98136. +
  98137. +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
  98138. +#ifdef AUDIO_DEBUG_ENABLE
  98139. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98140. + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98141. + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98142. + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98143. +#else
  98144. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98145. + #define LOG_WARN( fmt, arg... )
  98146. + #define LOG_INFO( fmt, arg... )
  98147. + #define LOG_DBG( fmt, arg... )
  98148. +#endif
  98149. +
  98150. +typedef struct opaque_AUDIO_INSTANCE_T {
  98151. + uint32_t num_connections;
  98152. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  98153. + struct completion msg_avail_comp;
  98154. + struct mutex vchi_mutex;
  98155. + bcm2835_alsa_stream_t *alsa_stream;
  98156. + int32_t result;
  98157. + short peer_version;
  98158. +} AUDIO_INSTANCE_T;
  98159. +
  98160. +bool force_bulk = false;
  98161. +
  98162. +/* ---- Private Variables ---------------------------------------------------- */
  98163. +
  98164. +/* ---- Private Function Prototypes ------------------------------------------ */
  98165. +
  98166. +/* ---- Private Functions ---------------------------------------------------- */
  98167. +
  98168. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
  98169. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
  98170. +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  98171. + uint32_t count, void *src);
  98172. +
  98173. +typedef struct {
  98174. + struct work_struct my_work;
  98175. + bcm2835_alsa_stream_t *alsa_stream;
  98176. + int cmd;
  98177. + void *src;
  98178. + uint32_t count;
  98179. +} my_work_t;
  98180. +
  98181. +static void my_wq_function(struct work_struct *work)
  98182. +{
  98183. + my_work_t *w = (my_work_t *) work;
  98184. + int ret = -9;
  98185. + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
  98186. + switch (w->cmd) {
  98187. + case BCM2835_AUDIO_START:
  98188. + ret = bcm2835_audio_start_worker(w->alsa_stream);
  98189. + break;
  98190. + case BCM2835_AUDIO_STOP:
  98191. + ret = bcm2835_audio_stop_worker(w->alsa_stream);
  98192. + break;
  98193. + case BCM2835_AUDIO_WRITE:
  98194. + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
  98195. + w->src);
  98196. + break;
  98197. + default:
  98198. + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
  98199. + break;
  98200. + }
  98201. + kfree((void *)work);
  98202. + LOG_DBG(" .. OUT %d\n", ret);
  98203. +}
  98204. +
  98205. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
  98206. +{
  98207. + int ret = -1;
  98208. + LOG_DBG(" .. IN\n");
  98209. + if (alsa_stream->my_wq) {
  98210. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  98211. + /*--- Queue some work (item 1) ---*/
  98212. + if (work) {
  98213. + INIT_WORK((struct work_struct *)work, my_wq_function);
  98214. + work->alsa_stream = alsa_stream;
  98215. + work->cmd = BCM2835_AUDIO_START;
  98216. + if (queue_work
  98217. + (alsa_stream->my_wq, (struct work_struct *)work))
  98218. + ret = 0;
  98219. + } else
  98220. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  98221. + }
  98222. + LOG_DBG(" .. OUT %d\n", ret);
  98223. + return ret;
  98224. +}
  98225. +
  98226. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
  98227. +{
  98228. + int ret = -1;
  98229. + LOG_DBG(" .. IN\n");
  98230. + if (alsa_stream->my_wq) {
  98231. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  98232. + /*--- Queue some work (item 1) ---*/
  98233. + if (work) {
  98234. + INIT_WORK((struct work_struct *)work, my_wq_function);
  98235. + work->alsa_stream = alsa_stream;
  98236. + work->cmd = BCM2835_AUDIO_STOP;
  98237. + if (queue_work
  98238. + (alsa_stream->my_wq, (struct work_struct *)work))
  98239. + ret = 0;
  98240. + } else
  98241. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  98242. + }
  98243. + LOG_DBG(" .. OUT %d\n", ret);
  98244. + return ret;
  98245. +}
  98246. +
  98247. +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
  98248. + uint32_t count, void *src)
  98249. +{
  98250. + int ret = -1;
  98251. + LOG_DBG(" .. IN\n");
  98252. + if (alsa_stream->my_wq) {
  98253. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  98254. + /*--- Queue some work (item 1) ---*/
  98255. + if (work) {
  98256. + INIT_WORK((struct work_struct *)work, my_wq_function);
  98257. + work->alsa_stream = alsa_stream;
  98258. + work->cmd = BCM2835_AUDIO_WRITE;
  98259. + work->src = src;
  98260. + work->count = count;
  98261. + if (queue_work
  98262. + (alsa_stream->my_wq, (struct work_struct *)work))
  98263. + ret = 0;
  98264. + } else
  98265. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  98266. + }
  98267. + LOG_DBG(" .. OUT %d\n", ret);
  98268. + return ret;
  98269. +}
  98270. +
  98271. +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
  98272. +{
  98273. + alsa_stream->my_wq = create_workqueue("my_queue");
  98274. + return;
  98275. +}
  98276. +
  98277. +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
  98278. +{
  98279. + if (alsa_stream->my_wq) {
  98280. + flush_workqueue(alsa_stream->my_wq);
  98281. + destroy_workqueue(alsa_stream->my_wq);
  98282. + alsa_stream->my_wq = NULL;
  98283. + }
  98284. + return;
  98285. +}
  98286. +
  98287. +static void audio_vchi_callback(void *param,
  98288. + const VCHI_CALLBACK_REASON_T reason,
  98289. + void *msg_handle)
  98290. +{
  98291. + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
  98292. + int32_t status;
  98293. + int32_t msg_len;
  98294. + VC_AUDIO_MSG_T m;
  98295. + bcm2835_alsa_stream_t *alsa_stream = 0;
  98296. + LOG_DBG(" .. IN instance=%p, param=%p, reason=%d, handle=%p\n",
  98297. + instance, param, reason, msg_handle);
  98298. +
  98299. + if (!instance || reason != VCHI_CALLBACK_MSG_AVAILABLE) {
  98300. + return;
  98301. + }
  98302. + alsa_stream = instance->alsa_stream;
  98303. + status = vchi_msg_dequeue(instance->vchi_handle[0],
  98304. + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
  98305. + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
  98306. + LOG_DBG
  98307. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
  98308. + instance, m.u.result.success);
  98309. + instance->result = m.u.result.success;
  98310. + complete(&instance->msg_avail_comp);
  98311. + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
  98312. + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
  98313. + LOG_DBG
  98314. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
  98315. + instance, m.u.complete.count);
  98316. + if (alsa_stream && callback) {
  98317. + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
  98318. + callback(0, alsa_stream);
  98319. + } else {
  98320. + LOG_DBG(" .. unexpected alsa_stream=%p, callback=%p\n",
  98321. + alsa_stream, callback);
  98322. + }
  98323. + } else {
  98324. + LOG_DBG(" .. unexpected m.type=%d\n", m.type);
  98325. + }
  98326. + LOG_DBG(" .. OUT\n");
  98327. +}
  98328. +
  98329. +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
  98330. + VCHI_CONNECTION_T **
  98331. + vchi_connections,
  98332. + uint32_t num_connections)
  98333. +{
  98334. + uint32_t i;
  98335. + AUDIO_INSTANCE_T *instance;
  98336. + int status;
  98337. +
  98338. + LOG_DBG("%s: start", __func__);
  98339. +
  98340. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  98341. + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
  98342. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  98343. +
  98344. + return NULL;
  98345. + }
  98346. + /* Allocate memory for this instance */
  98347. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  98348. +
  98349. + memset(instance, 0, sizeof(*instance));
  98350. + instance->num_connections = num_connections;
  98351. +
  98352. + /* Create a lock for exclusive, serialized VCHI connection access */
  98353. + mutex_init(&instance->vchi_mutex);
  98354. + /* Open the VCHI service connections */
  98355. + for (i = 0; i < num_connections; i++) {
  98356. + SERVICE_CREATION_T params = {
  98357. + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
  98358. + VC_AUDIO_SERVER_NAME, // 4cc service code
  98359. + vchi_connections[i], // passed in fn pointers
  98360. + 0, // rx fifo size (unused)
  98361. + 0, // tx fifo size (unused)
  98362. + audio_vchi_callback, // service callback
  98363. + instance, // service callback parameter
  98364. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
  98365. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
  98366. + 0 // want crc check on bulk transfers
  98367. + };
  98368. +
  98369. + status = vchi_service_open(vchi_instance, &params,
  98370. + &instance->vchi_handle[i]);
  98371. + if (status) {
  98372. + LOG_ERR
  98373. + ("%s: failed to open VCHI service connection (status=%d)\n",
  98374. + __func__, status);
  98375. +
  98376. + goto err_close_services;
  98377. + }
  98378. + /* Finished with the service for now */
  98379. + vchi_service_release(instance->vchi_handle[i]);
  98380. + }
  98381. +
  98382. + return instance;
  98383. +
  98384. +err_close_services:
  98385. + for (i = 0; i < instance->num_connections; i++) {
  98386. + vchi_service_close(instance->vchi_handle[i]);
  98387. + }
  98388. +
  98389. + kfree(instance);
  98390. +
  98391. + return NULL;
  98392. +}
  98393. +
  98394. +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
  98395. +{
  98396. + uint32_t i;
  98397. +
  98398. + LOG_DBG(" .. IN\n");
  98399. +
  98400. + if (instance == NULL) {
  98401. + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
  98402. +
  98403. + return -1;
  98404. + }
  98405. +
  98406. + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
  98407. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98408. + {
  98409. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98410. + return -EINTR;
  98411. + }
  98412. +
  98413. + /* Close all VCHI service connections */
  98414. + for (i = 0; i < instance->num_connections; i++) {
  98415. + int32_t success;
  98416. + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
  98417. + vchi_service_use(instance->vchi_handle[i]);
  98418. +
  98419. + success = vchi_service_close(instance->vchi_handle[i]);
  98420. + if (success != 0) {
  98421. + LOG_ERR
  98422. + ("%s: failed to close VCHI service connection (status=%d)\n",
  98423. + __func__, success);
  98424. + }
  98425. + }
  98426. +
  98427. + mutex_unlock(&instance->vchi_mutex);
  98428. +
  98429. + kfree(instance);
  98430. +
  98431. + LOG_DBG(" .. OUT\n");
  98432. +
  98433. + return 0;
  98434. +}
  98435. +
  98436. +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
  98437. +{
  98438. + static VCHI_INSTANCE_T vchi_instance;
  98439. + static VCHI_CONNECTION_T *vchi_connection;
  98440. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98441. + int ret;
  98442. + LOG_DBG(" .. IN\n");
  98443. +
  98444. + LOG_INFO("%s: start", __func__);
  98445. + //BUG_ON(instance);
  98446. + if (instance) {
  98447. + LOG_ERR("%s: VCHI instance already open (%p)\n",
  98448. + __func__, instance);
  98449. + instance->alsa_stream = alsa_stream;
  98450. + alsa_stream->instance = instance;
  98451. + ret = 0; // xxx todo -1;
  98452. + goto err_free_mem;
  98453. + }
  98454. +
  98455. + /* Initialize and create a VCHI connection */
  98456. + ret = vchi_initialise(&vchi_instance);
  98457. + if (ret != 0) {
  98458. + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
  98459. + __func__, ret);
  98460. +
  98461. + ret = -EIO;
  98462. + goto err_free_mem;
  98463. + }
  98464. + ret = vchi_connect(NULL, 0, vchi_instance);
  98465. + if (ret != 0) {
  98466. + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
  98467. + __func__, ret);
  98468. +
  98469. + ret = -EIO;
  98470. + goto err_free_mem;
  98471. + }
  98472. +
  98473. + /* Initialize an instance of the audio service */
  98474. + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
  98475. +
  98476. + if (instance == NULL /*|| audio_handle != instance */ ) {
  98477. + LOG_ERR("%s: failed to initialize audio service\n", __func__);
  98478. +
  98479. + ret = -EPERM;
  98480. + goto err_free_mem;
  98481. + }
  98482. +
  98483. + instance->alsa_stream = alsa_stream;
  98484. + alsa_stream->instance = instance;
  98485. +
  98486. + LOG_DBG(" success !\n");
  98487. +err_free_mem:
  98488. + LOG_DBG(" .. OUT\n");
  98489. +
  98490. + return ret;
  98491. +}
  98492. +
  98493. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
  98494. +{
  98495. + AUDIO_INSTANCE_T *instance;
  98496. + VC_AUDIO_MSG_T m;
  98497. + int32_t success;
  98498. + int ret;
  98499. + LOG_DBG(" .. IN\n");
  98500. +
  98501. + my_workqueue_init(alsa_stream);
  98502. +
  98503. + ret = bcm2835_audio_open_connection(alsa_stream);
  98504. + if (ret != 0) {
  98505. + ret = -1;
  98506. + goto exit;
  98507. + }
  98508. + instance = alsa_stream->instance;
  98509. +
  98510. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98511. + {
  98512. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98513. + return -EINTR;
  98514. + }
  98515. + vchi_service_use(instance->vchi_handle[0]);
  98516. +
  98517. + m.type = VC_AUDIO_MSG_TYPE_OPEN;
  98518. +
  98519. + /* Send the message to the videocore */
  98520. + success = vchi_msg_queue(instance->vchi_handle[0],
  98521. + &m, sizeof m,
  98522. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98523. +
  98524. + if (success != 0) {
  98525. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  98526. + __func__, success);
  98527. +
  98528. + ret = -1;
  98529. + goto unlock;
  98530. + }
  98531. +
  98532. + ret = 0;
  98533. +
  98534. +unlock:
  98535. + vchi_service_release(instance->vchi_handle[0]);
  98536. + mutex_unlock(&instance->vchi_mutex);
  98537. +exit:
  98538. + LOG_DBG(" .. OUT\n");
  98539. + return ret;
  98540. +}
  98541. +
  98542. +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
  98543. + bcm2835_chip_t * chip)
  98544. +{
  98545. + VC_AUDIO_MSG_T m;
  98546. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98547. + int32_t success;
  98548. + int ret;
  98549. + LOG_DBG(" .. IN\n");
  98550. +
  98551. + LOG_INFO
  98552. + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  98553. +
  98554. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98555. + {
  98556. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98557. + return -EINTR;
  98558. + }
  98559. + vchi_service_use(instance->vchi_handle[0]);
  98560. +
  98561. + instance->result = -1;
  98562. +
  98563. + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
  98564. + m.u.control.dest = chip->dest;
  98565. + m.u.control.volume = chip->volume;
  98566. +
  98567. + /* Create the message available completion */
  98568. + init_completion(&instance->msg_avail_comp);
  98569. +
  98570. + /* Send the message to the videocore */
  98571. + success = vchi_msg_queue(instance->vchi_handle[0],
  98572. + &m, sizeof m,
  98573. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98574. +
  98575. + if (success != 0) {
  98576. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  98577. + __func__, success);
  98578. +
  98579. + ret = -1;
  98580. + goto unlock;
  98581. + }
  98582. +
  98583. + /* We are expecting a reply from the videocore */
  98584. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98585. + if (ret) {
  98586. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  98587. + __func__, success);
  98588. + goto unlock;
  98589. + }
  98590. +
  98591. + if (instance->result != 0) {
  98592. + LOG_ERR("%s: result=%d\n", __func__, instance->result);
  98593. +
  98594. + ret = -1;
  98595. + goto unlock;
  98596. + }
  98597. +
  98598. + ret = 0;
  98599. +
  98600. +unlock:
  98601. + vchi_service_release(instance->vchi_handle[0]);
  98602. + mutex_unlock(&instance->vchi_mutex);
  98603. +
  98604. + LOG_DBG(" .. OUT\n");
  98605. + return ret;
  98606. +}
  98607. +
  98608. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
  98609. +{
  98610. + int i;
  98611. + int ret = 0;
  98612. + LOG_DBG(" .. IN\n");
  98613. +
  98614. + /* change ctls for all substreams */
  98615. + for (i = 0; i < MAX_SUBSTREAMS; i++) {
  98616. + if (chip->avail_substreams & (1 << i)) {
  98617. + if (!chip->alsa_stream[i])
  98618. + {
  98619. + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
  98620. + ret = 0;
  98621. + }
  98622. + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
  98623. + (chip->alsa_stream[i], chip) != 0)
  98624. + {
  98625. + LOG_DBG("Couldn't set the controls for stream %d\n", i);
  98626. + ret = -1;
  98627. + }
  98628. + else LOG_DBG(" Controls set for stream %d\n", i);
  98629. + }
  98630. + }
  98631. + LOG_DBG(" .. OUT ret=%d\n", ret);
  98632. + return ret;
  98633. +}
  98634. +
  98635. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  98636. + uint32_t channels, uint32_t samplerate,
  98637. + uint32_t bps)
  98638. +{
  98639. + VC_AUDIO_MSG_T m;
  98640. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98641. + int32_t success;
  98642. + int ret;
  98643. + LOG_DBG(" .. IN\n");
  98644. +
  98645. + LOG_INFO
  98646. + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
  98647. + channels, samplerate, bps);
  98648. +
  98649. + /* resend ctls - alsa_stream may not have been open when first send */
  98650. + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
  98651. + if (ret != 0) {
  98652. + LOG_ERR(" Alsa controls not supported\n");
  98653. + return -EINVAL;
  98654. + }
  98655. +
  98656. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98657. + {
  98658. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98659. + return -EINTR;
  98660. + }
  98661. + vchi_service_use(instance->vchi_handle[0]);
  98662. +
  98663. + instance->result = -1;
  98664. +
  98665. + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
  98666. + m.u.config.channels = channels;
  98667. + m.u.config.samplerate = samplerate;
  98668. + m.u.config.bps = bps;
  98669. +
  98670. + /* Create the message available completion */
  98671. + init_completion(&instance->msg_avail_comp);
  98672. +
  98673. + /* Send the message to the videocore */
  98674. + success = vchi_msg_queue(instance->vchi_handle[0],
  98675. + &m, sizeof m,
  98676. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98677. +
  98678. + if (success != 0) {
  98679. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  98680. + __func__, success);
  98681. +
  98682. + ret = -1;
  98683. + goto unlock;
  98684. + }
  98685. +
  98686. + /* We are expecting a reply from the videocore */
  98687. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98688. + if (ret) {
  98689. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  98690. + __func__, success);
  98691. + goto unlock;
  98692. + }
  98693. +
  98694. + if (instance->result != 0) {
  98695. + LOG_ERR("%s: result=%d", __func__, instance->result);
  98696. +
  98697. + ret = -1;
  98698. + goto unlock;
  98699. + }
  98700. +
  98701. + ret = 0;
  98702. +
  98703. +unlock:
  98704. + vchi_service_release(instance->vchi_handle[0]);
  98705. + mutex_unlock(&instance->vchi_mutex);
  98706. +
  98707. + LOG_DBG(" .. OUT\n");
  98708. + return ret;
  98709. +}
  98710. +
  98711. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
  98712. +{
  98713. + LOG_DBG(" .. IN\n");
  98714. +
  98715. + LOG_DBG(" .. OUT\n");
  98716. +
  98717. + return 0;
  98718. +}
  98719. +
  98720. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
  98721. +{
  98722. + VC_AUDIO_MSG_T m;
  98723. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98724. + int32_t success;
  98725. + int ret;
  98726. + LOG_DBG(" .. IN\n");
  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. + m.type = VC_AUDIO_MSG_TYPE_START;
  98736. +
  98737. + /* Send the message to the videocore */
  98738. + success = vchi_msg_queue(instance->vchi_handle[0],
  98739. + &m, sizeof m,
  98740. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98741. +
  98742. + if (success != 0) {
  98743. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98744. + __func__, success);
  98745. +
  98746. + ret = -1;
  98747. + goto unlock;
  98748. + }
  98749. +
  98750. + ret = 0;
  98751. +
  98752. +unlock:
  98753. + vchi_service_release(instance->vchi_handle[0]);
  98754. + mutex_unlock(&instance->vchi_mutex);
  98755. + LOG_DBG(" .. OUT\n");
  98756. + return ret;
  98757. +}
  98758. +
  98759. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
  98760. +{
  98761. + VC_AUDIO_MSG_T m;
  98762. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98763. + int32_t success;
  98764. + int ret;
  98765. + LOG_DBG(" .. IN\n");
  98766. +
  98767. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98768. + {
  98769. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98770. + return -EINTR;
  98771. + }
  98772. + vchi_service_use(instance->vchi_handle[0]);
  98773. +
  98774. + m.type = VC_AUDIO_MSG_TYPE_STOP;
  98775. + m.u.stop.draining = alsa_stream->draining;
  98776. +
  98777. + /* Send the message to the videocore */
  98778. + success = vchi_msg_queue(instance->vchi_handle[0],
  98779. + &m, sizeof m,
  98780. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98781. +
  98782. + if (success != 0) {
  98783. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98784. + __func__, success);
  98785. +
  98786. + ret = -1;
  98787. + goto unlock;
  98788. + }
  98789. +
  98790. + ret = 0;
  98791. +
  98792. +unlock:
  98793. + vchi_service_release(instance->vchi_handle[0]);
  98794. + mutex_unlock(&instance->vchi_mutex);
  98795. + LOG_DBG(" .. OUT\n");
  98796. + return ret;
  98797. +}
  98798. +
  98799. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
  98800. +{
  98801. + VC_AUDIO_MSG_T m;
  98802. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98803. + int32_t success;
  98804. + int ret;
  98805. + LOG_DBG(" .. IN\n");
  98806. +
  98807. + my_workqueue_quit(alsa_stream);
  98808. +
  98809. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98810. + {
  98811. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98812. + return -EINTR;
  98813. + }
  98814. + vchi_service_use(instance->vchi_handle[0]);
  98815. +
  98816. + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
  98817. +
  98818. + /* Create the message available completion */
  98819. + init_completion(&instance->msg_avail_comp);
  98820. +
  98821. + /* Send the message to the videocore */
  98822. + success = vchi_msg_queue(instance->vchi_handle[0],
  98823. + &m, sizeof m,
  98824. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98825. +
  98826. + if (success != 0) {
  98827. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98828. + __func__, success);
  98829. + ret = -1;
  98830. + goto unlock;
  98831. + }
  98832. +
  98833. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98834. + if (ret) {
  98835. + LOG_ERR("%s: failed on waiting for event (status=%d)",
  98836. + __func__, success);
  98837. + goto unlock;
  98838. + }
  98839. + if (instance->result != 0) {
  98840. + LOG_ERR("%s: failed result (status=%d)",
  98841. + __func__, instance->result);
  98842. +
  98843. + ret = -1;
  98844. + goto unlock;
  98845. + }
  98846. +
  98847. + ret = 0;
  98848. +
  98849. +unlock:
  98850. + vchi_service_release(instance->vchi_handle[0]);
  98851. + mutex_unlock(&instance->vchi_mutex);
  98852. +
  98853. + /* Stop the audio service */
  98854. + if (instance) {
  98855. + vc_vchi_audio_deinit(instance);
  98856. + alsa_stream->instance = NULL;
  98857. + }
  98858. + LOG_DBG(" .. OUT\n");
  98859. + return ret;
  98860. +}
  98861. +
  98862. +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  98863. + uint32_t count, void *src)
  98864. +{
  98865. + VC_AUDIO_MSG_T m;
  98866. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98867. + int32_t success;
  98868. + int ret;
  98869. +
  98870. + LOG_DBG(" .. IN\n");
  98871. +
  98872. + LOG_INFO(" Writing %d bytes from %p\n", count, src);
  98873. +
  98874. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98875. + {
  98876. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98877. + return -EINTR;
  98878. + }
  98879. + vchi_service_use(instance->vchi_handle[0]);
  98880. +
  98881. + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
  98882. + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
  98883. + }
  98884. + m.type = VC_AUDIO_MSG_TYPE_WRITE;
  98885. + m.u.write.count = count;
  98886. + // old version uses bulk, new version uses control
  98887. + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
  98888. + m.u.write.callback = alsa_stream->fifo_irq_handler;
  98889. + m.u.write.cookie = alsa_stream;
  98890. + m.u.write.silence = src == NULL;
  98891. +
  98892. + /* Send the message to the videocore */
  98893. + success = vchi_msg_queue(instance->vchi_handle[0],
  98894. + &m, sizeof m,
  98895. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98896. +
  98897. + if (success != 0) {
  98898. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98899. + __func__, success);
  98900. +
  98901. + ret = -1;
  98902. + goto unlock;
  98903. + }
  98904. + if (!m.u.write.silence) {
  98905. + if (m.u.write.max_packet == 0) {
  98906. + /* Send the message to the videocore */
  98907. + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
  98908. + src, count,
  98909. + 0 *
  98910. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
  98911. + +
  98912. + 1 *
  98913. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
  98914. + NULL);
  98915. + } else {
  98916. + while (count > 0) {
  98917. + int bytes = min((int)m.u.write.max_packet, (int)count);
  98918. + success = vchi_msg_queue(instance->vchi_handle[0],
  98919. + src, bytes,
  98920. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98921. + src = (char *)src + bytes;
  98922. + count -= bytes;
  98923. + }
  98924. + }
  98925. + if (success != 0) {
  98926. + LOG_ERR
  98927. + ("%s: failed on vchi_bulk_queue_transmit (status=%d)",
  98928. + __func__, success);
  98929. +
  98930. + ret = -1;
  98931. + goto unlock;
  98932. + }
  98933. + }
  98934. + ret = 0;
  98935. +
  98936. +unlock:
  98937. + vchi_service_release(instance->vchi_handle[0]);
  98938. + mutex_unlock(&instance->vchi_mutex);
  98939. + LOG_DBG(" .. OUT\n");
  98940. + return ret;
  98941. +}
  98942. +
  98943. +/**
  98944. + * Returns all buffers from arm->vc
  98945. + */
  98946. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
  98947. +{
  98948. + LOG_DBG(" .. IN\n");
  98949. + LOG_DBG(" .. OUT\n");
  98950. + return;
  98951. +}
  98952. +
  98953. +/**
  98954. + * Forces VC to flush(drop) its filled playback buffers and
  98955. + * return them the us. (VC->ARM)
  98956. + */
  98957. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
  98958. +{
  98959. + LOG_DBG(" .. IN\n");
  98960. + LOG_DBG(" .. OUT\n");
  98961. +}
  98962. +
  98963. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
  98964. +{
  98965. + uint32_t count = atomic_read(&alsa_stream->retrieved);
  98966. + atomic_sub(count, &alsa_stream->retrieved);
  98967. + return count;
  98968. +}
  98969. +
  98970. +module_param(force_bulk, bool, 0444);
  98971. +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
  98972. diff -Nur linux-3.12.13/sound/arm/Kconfig linux-raspberry-pi/sound/arm/Kconfig
  98973. --- linux-3.12.13/sound/arm/Kconfig 2014-02-22 22:32:50.000000000 +0100
  98974. +++ linux-raspberry-pi/sound/arm/Kconfig 2014-03-11 17:33:39.000000000 +0100
  98975. @@ -39,5 +39,12 @@
  98976. Say Y or M if you want to support any AC97 codec attached to
  98977. the PXA2xx AC97 interface.
  98978. +config SND_BCM2835
  98979. + tristate "BCM2835 ALSA driver"
  98980. + depends on ARCH_BCM2708 && BCM2708_VCHIQ && SND
  98981. + select SND_PCM
  98982. + help
  98983. + Say Y or M if you want to support BCM2835 Alsa pcm card driver
  98984. +
  98985. endif # SND_ARM
  98986. diff -Nur linux-3.12.13/sound/arm/Makefile linux-raspberry-pi/sound/arm/Makefile
  98987. --- linux-3.12.13/sound/arm/Makefile 2014-02-22 22:32:50.000000000 +0100
  98988. +++ linux-raspberry-pi/sound/arm/Makefile 2014-03-11 17:51:47.000000000 +0100
  98989. @@ -14,3 +14,8 @@
  98990. obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
  98991. snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
  98992. +
  98993. +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
  98994. +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
  98995. +
  98996. +EXTRA_CFLAGS += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  98997. diff -Nur linux-3.12.13/sound/arm/vc_vchi_audioserv_defs.h linux-raspberry-pi/sound/arm/vc_vchi_audioserv_defs.h
  98998. --- linux-3.12.13/sound/arm/vc_vchi_audioserv_defs.h 1970-01-01 01:00:00.000000000 +0100
  98999. +++ linux-raspberry-pi/sound/arm/vc_vchi_audioserv_defs.h 2014-03-11 17:33:39.000000000 +0100
  99000. @@ -0,0 +1,116 @@
  99001. +/*****************************************************************************
  99002. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  99003. +*
  99004. +* Unless you and Broadcom execute a separate written software license
  99005. +* agreement governing use of this software, this software is licensed to you
  99006. +* under the terms of the GNU General Public License version 2, available at
  99007. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  99008. +*
  99009. +* Notwithstanding the above, under no circumstances may you combine this
  99010. +* software in any way with any other Broadcom software provided under a
  99011. +* license other than the GPL, without Broadcom's express prior written
  99012. +* consent.
  99013. +*****************************************************************************/
  99014. +
  99015. +#ifndef _VC_AUDIO_DEFS_H_
  99016. +#define _VC_AUDIO_DEFS_H_
  99017. +
  99018. +#define VC_AUDIOSERV_MIN_VER 1
  99019. +#define VC_AUDIOSERV_VER 2
  99020. +
  99021. +// FourCC code used for VCHI connection
  99022. +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
  99023. +
  99024. +// Maximum message length
  99025. +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
  99026. +
  99027. +// List of screens that are currently supported
  99028. +// All message types supported for HOST->VC direction
  99029. +typedef enum {
  99030. + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
  99031. + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
  99032. + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
  99033. + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
  99034. + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
  99035. + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
  99036. + VC_AUDIO_MSG_TYPE_START, // Configure audio
  99037. + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
  99038. + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
  99039. + VC_AUDIO_MSG_TYPE_MAX
  99040. +} VC_AUDIO_MSG_TYPE;
  99041. +
  99042. +// configure the audio
  99043. +typedef struct {
  99044. + uint32_t channels;
  99045. + uint32_t samplerate;
  99046. + uint32_t bps;
  99047. +
  99048. +} VC_AUDIO_CONFIG_T;
  99049. +
  99050. +typedef struct {
  99051. + uint32_t volume;
  99052. + uint32_t dest;
  99053. +
  99054. +} VC_AUDIO_CONTROL_T;
  99055. +
  99056. +// audio
  99057. +typedef struct {
  99058. + uint32_t dummy;
  99059. +
  99060. +} VC_AUDIO_OPEN_T;
  99061. +
  99062. +// audio
  99063. +typedef struct {
  99064. + uint32_t dummy;
  99065. +
  99066. +} VC_AUDIO_CLOSE_T;
  99067. +// audio
  99068. +typedef struct {
  99069. + uint32_t dummy;
  99070. +
  99071. +} VC_AUDIO_START_T;
  99072. +// audio
  99073. +typedef struct {
  99074. + uint32_t draining;
  99075. +
  99076. +} VC_AUDIO_STOP_T;
  99077. +
  99078. +// configure the write audio samples
  99079. +typedef struct {
  99080. + uint32_t count; // in bytes
  99081. + void *callback;
  99082. + void *cookie;
  99083. + uint16_t silence;
  99084. + uint16_t max_packet;
  99085. +} VC_AUDIO_WRITE_T;
  99086. +
  99087. +// Generic result for a request (VC->HOST)
  99088. +typedef struct {
  99089. + int32_t success; // Success value
  99090. +
  99091. +} VC_AUDIO_RESULT_T;
  99092. +
  99093. +// Generic result for a request (VC->HOST)
  99094. +typedef struct {
  99095. + int32_t count; // Success value
  99096. + void *callback;
  99097. + void *cookie;
  99098. +} VC_AUDIO_COMPLETE_T;
  99099. +
  99100. +// Message header for all messages in HOST->VC direction
  99101. +typedef struct {
  99102. + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
  99103. + union {
  99104. + VC_AUDIO_CONFIG_T config;
  99105. + VC_AUDIO_CONTROL_T control;
  99106. + VC_AUDIO_OPEN_T open;
  99107. + VC_AUDIO_CLOSE_T close;
  99108. + VC_AUDIO_START_T start;
  99109. + VC_AUDIO_STOP_T stop;
  99110. + VC_AUDIO_WRITE_T write;
  99111. + VC_AUDIO_RESULT_T result;
  99112. + VC_AUDIO_COMPLETE_T complete;
  99113. + } u;
  99114. +} VC_AUDIO_MSG_T;
  99115. +
  99116. +#endif // _VC_AUDIO_DEFS_H_
  99117. diff -Nur linux-3.12.13/sound/soc/bcm/bcm2708-i2s.c linux-raspberry-pi/sound/soc/bcm/bcm2708-i2s.c
  99118. --- linux-3.12.13/sound/soc/bcm/bcm2708-i2s.c 1970-01-01 01:00:00.000000000 +0100
  99119. +++ linux-raspberry-pi/sound/soc/bcm/bcm2708-i2s.c 2014-03-11 17:33:43.000000000 +0100
  99120. @@ -0,0 +1,945 @@
  99121. +/*
  99122. + * ALSA SoC I2S Audio Layer for Broadcom BCM2708 SoC
  99123. + *
  99124. + * Author: Florian Meier <florian.meier@koalo.de>
  99125. + * Copyright 2013
  99126. + *
  99127. + * Based on
  99128. + * Raspberry Pi PCM I2S ALSA Driver
  99129. + * Copyright (c) by Phil Poole 2013
  99130. + *
  99131. + * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  99132. + * Vladimir Barinov, <vbarinov@embeddedalley.com>
  99133. + * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  99134. + *
  99135. + * OMAP ALSA SoC DAI driver using McBSP port
  99136. + * Copyright (C) 2008 Nokia Corporation
  99137. + * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  99138. + * Peter Ujfalusi <peter.ujfalusi@ti.com>
  99139. + *
  99140. + * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  99141. + * Author: Timur Tabi <timur@freescale.com>
  99142. + * Copyright 2007-2010 Freescale Semiconductor, Inc.
  99143. + *
  99144. + * This program is free software; you can redistribute it and/or
  99145. + * modify it under the terms of the GNU General Public License
  99146. + * version 2 as published by the Free Software Foundation.
  99147. + *
  99148. + * This program is distributed in the hope that it will be useful, but
  99149. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  99150. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  99151. + * General Public License for more details.
  99152. + */
  99153. +
  99154. +#include <linux/init.h>
  99155. +#include <linux/module.h>
  99156. +#include <linux/device.h>
  99157. +#include <linux/slab.h>
  99158. +#include <linux/delay.h>
  99159. +#include <linux/io.h>
  99160. +#include <linux/clk.h>
  99161. +
  99162. +#include <sound/core.h>
  99163. +#include <sound/pcm.h>
  99164. +#include <sound/pcm_params.h>
  99165. +#include <sound/initval.h>
  99166. +#include <sound/soc.h>
  99167. +#include <sound/dmaengine_pcm.h>
  99168. +
  99169. +/* Clock registers */
  99170. +#define BCM2708_CLK_PCMCTL_REG 0x00
  99171. +#define BCM2708_CLK_PCMDIV_REG 0x04
  99172. +
  99173. +/* Clock register settings */
  99174. +#define BCM2708_CLK_PASSWD (0x5a000000)
  99175. +#define BCM2708_CLK_PASSWD_MASK (0xff000000)
  99176. +#define BCM2708_CLK_MASH(v) ((v) << 9)
  99177. +#define BCM2708_CLK_FLIP BIT(8)
  99178. +#define BCM2708_CLK_BUSY BIT(7)
  99179. +#define BCM2708_CLK_KILL BIT(5)
  99180. +#define BCM2708_CLK_ENAB BIT(4)
  99181. +#define BCM2708_CLK_SRC(v) (v)
  99182. +
  99183. +#define BCM2708_CLK_SHIFT (12)
  99184. +#define BCM2708_CLK_DIVI(v) ((v) << BCM2708_CLK_SHIFT)
  99185. +#define BCM2708_CLK_DIVF(v) (v)
  99186. +#define BCM2708_CLK_DIVF_MASK (0xFFF)
  99187. +
  99188. +enum {
  99189. + BCM2708_CLK_MASH_0 = 0,
  99190. + BCM2708_CLK_MASH_1,
  99191. + BCM2708_CLK_MASH_2,
  99192. + BCM2708_CLK_MASH_3,
  99193. +};
  99194. +
  99195. +enum {
  99196. + BCM2708_CLK_SRC_GND = 0,
  99197. + BCM2708_CLK_SRC_OSC,
  99198. + BCM2708_CLK_SRC_DBG0,
  99199. + BCM2708_CLK_SRC_DBG1,
  99200. + BCM2708_CLK_SRC_PLLA,
  99201. + BCM2708_CLK_SRC_PLLC,
  99202. + BCM2708_CLK_SRC_PLLD,
  99203. + BCM2708_CLK_SRC_HDMI,
  99204. +};
  99205. +
  99206. +/* Most clocks are not useable (freq = 0) */
  99207. +static const unsigned int bcm2708_clk_freq[BCM2708_CLK_SRC_HDMI+1] = {
  99208. + [BCM2708_CLK_SRC_GND] = 0,
  99209. + [BCM2708_CLK_SRC_OSC] = 19200000,
  99210. + [BCM2708_CLK_SRC_DBG0] = 0,
  99211. + [BCM2708_CLK_SRC_DBG1] = 0,
  99212. + [BCM2708_CLK_SRC_PLLA] = 0,
  99213. + [BCM2708_CLK_SRC_PLLC] = 0,
  99214. + [BCM2708_CLK_SRC_PLLD] = 500000000,
  99215. + [BCM2708_CLK_SRC_HDMI] = 0,
  99216. +};
  99217. +
  99218. +/* I2S registers */
  99219. +#define BCM2708_I2S_CS_A_REG 0x00
  99220. +#define BCM2708_I2S_FIFO_A_REG 0x04
  99221. +#define BCM2708_I2S_MODE_A_REG 0x08
  99222. +#define BCM2708_I2S_RXC_A_REG 0x0c
  99223. +#define BCM2708_I2S_TXC_A_REG 0x10
  99224. +#define BCM2708_I2S_DREQ_A_REG 0x14
  99225. +#define BCM2708_I2S_INTEN_A_REG 0x18
  99226. +#define BCM2708_I2S_INTSTC_A_REG 0x1c
  99227. +#define BCM2708_I2S_GRAY_REG 0x20
  99228. +
  99229. +/* I2S register settings */
  99230. +#define BCM2708_I2S_STBY BIT(25)
  99231. +#define BCM2708_I2S_SYNC BIT(24)
  99232. +#define BCM2708_I2S_RXSEX BIT(23)
  99233. +#define BCM2708_I2S_RXF BIT(22)
  99234. +#define BCM2708_I2S_TXE BIT(21)
  99235. +#define BCM2708_I2S_RXD BIT(20)
  99236. +#define BCM2708_I2S_TXD BIT(19)
  99237. +#define BCM2708_I2S_RXR BIT(18)
  99238. +#define BCM2708_I2S_TXW BIT(17)
  99239. +#define BCM2708_I2S_CS_RXERR BIT(16)
  99240. +#define BCM2708_I2S_CS_TXERR BIT(15)
  99241. +#define BCM2708_I2S_RXSYNC BIT(14)
  99242. +#define BCM2708_I2S_TXSYNC BIT(13)
  99243. +#define BCM2708_I2S_DMAEN BIT(9)
  99244. +#define BCM2708_I2S_RXTHR(v) ((v) << 7)
  99245. +#define BCM2708_I2S_TXTHR(v) ((v) << 5)
  99246. +#define BCM2708_I2S_RXCLR BIT(4)
  99247. +#define BCM2708_I2S_TXCLR BIT(3)
  99248. +#define BCM2708_I2S_TXON BIT(2)
  99249. +#define BCM2708_I2S_RXON BIT(1)
  99250. +#define BCM2708_I2S_EN (1)
  99251. +
  99252. +#define BCM2708_I2S_CLKDIS BIT(28)
  99253. +#define BCM2708_I2S_PDMN BIT(27)
  99254. +#define BCM2708_I2S_PDME BIT(26)
  99255. +#define BCM2708_I2S_FRXP BIT(25)
  99256. +#define BCM2708_I2S_FTXP BIT(24)
  99257. +#define BCM2708_I2S_CLKM BIT(23)
  99258. +#define BCM2708_I2S_CLKI BIT(22)
  99259. +#define BCM2708_I2S_FSM BIT(21)
  99260. +#define BCM2708_I2S_FSI BIT(20)
  99261. +#define BCM2708_I2S_FLEN(v) ((v) << 10)
  99262. +#define BCM2708_I2S_FSLEN(v) (v)
  99263. +
  99264. +#define BCM2708_I2S_CHWEX BIT(15)
  99265. +#define BCM2708_I2S_CHEN BIT(14)
  99266. +#define BCM2708_I2S_CHPOS(v) ((v) << 4)
  99267. +#define BCM2708_I2S_CHWID(v) (v)
  99268. +#define BCM2708_I2S_CH1(v) ((v) << 16)
  99269. +#define BCM2708_I2S_CH2(v) (v)
  99270. +
  99271. +#define BCM2708_I2S_TX_PANIC(v) ((v) << 24)
  99272. +#define BCM2708_I2S_RX_PANIC(v) ((v) << 16)
  99273. +#define BCM2708_I2S_TX(v) ((v) << 8)
  99274. +#define BCM2708_I2S_RX(v) (v)
  99275. +
  99276. +#define BCM2708_I2S_INT_RXERR BIT(3)
  99277. +#define BCM2708_I2S_INT_TXERR BIT(2)
  99278. +#define BCM2708_I2S_INT_RXR BIT(1)
  99279. +#define BCM2708_I2S_INT_TXW BIT(0)
  99280. +
  99281. +/* I2S DMA interface */
  99282. +#define BCM2708_I2S_FIFO_PHYSICAL_ADDR 0x7E203004
  99283. +#define BCM2708_DMA_DREQ_PCM_TX 2
  99284. +#define BCM2708_DMA_DREQ_PCM_RX 3
  99285. +
  99286. +/* General device struct */
  99287. +struct bcm2708_i2s_dev {
  99288. + struct device *dev;
  99289. + struct snd_dmaengine_dai_dma_data dma_data[2];
  99290. + unsigned int fmt;
  99291. + unsigned int bclk_ratio;
  99292. +
  99293. + struct regmap *i2s_regmap;
  99294. + struct regmap *clk_regmap;
  99295. +};
  99296. +
  99297. +static void bcm2708_i2s_start_clock(struct bcm2708_i2s_dev *dev)
  99298. +{
  99299. + /* Start the clock if in master mode */
  99300. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  99301. +
  99302. + switch (master) {
  99303. + case SND_SOC_DAIFMT_CBS_CFS:
  99304. + case SND_SOC_DAIFMT_CBS_CFM:
  99305. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99306. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  99307. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  99308. + break;
  99309. + default:
  99310. + break;
  99311. + }
  99312. +}
  99313. +
  99314. +static void bcm2708_i2s_stop_clock(struct bcm2708_i2s_dev *dev)
  99315. +{
  99316. + uint32_t clkreg;
  99317. + int timeout = 1000;
  99318. +
  99319. + /* Stop clock */
  99320. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99321. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  99322. + BCM2708_CLK_PASSWD);
  99323. +
  99324. + /* Wait for the BUSY flag going down */
  99325. + while (--timeout) {
  99326. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  99327. + if (!(clkreg & BCM2708_CLK_BUSY))
  99328. + break;
  99329. + }
  99330. +
  99331. + if (!timeout) {
  99332. + /* KILL the clock */
  99333. + dev_err(dev->dev, "I2S clock didn't stop. Kill the clock!\n");
  99334. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99335. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD_MASK,
  99336. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD);
  99337. + }
  99338. +}
  99339. +
  99340. +static void bcm2708_i2s_clear_fifos(struct bcm2708_i2s_dev *dev,
  99341. + bool tx, bool rx)
  99342. +{
  99343. + int timeout = 1000;
  99344. + uint32_t syncval;
  99345. + uint32_t csreg;
  99346. + uint32_t i2s_active_state;
  99347. + uint32_t clkreg;
  99348. + uint32_t clk_active_state;
  99349. + uint32_t off;
  99350. + uint32_t clr;
  99351. +
  99352. + off = tx ? BCM2708_I2S_TXON : 0;
  99353. + off |= rx ? BCM2708_I2S_RXON : 0;
  99354. +
  99355. + clr = tx ? BCM2708_I2S_TXCLR : 0;
  99356. + clr |= rx ? BCM2708_I2S_RXCLR : 0;
  99357. +
  99358. + /* Backup the current state */
  99359. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  99360. + i2s_active_state = csreg & (BCM2708_I2S_RXON | BCM2708_I2S_TXON);
  99361. +
  99362. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  99363. + clk_active_state = clkreg & BCM2708_CLK_ENAB;
  99364. +
  99365. + /* Start clock if not running */
  99366. + if (!clk_active_state) {
  99367. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99368. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  99369. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  99370. + }
  99371. +
  99372. + /* Stop I2S module */
  99373. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, off, 0);
  99374. +
  99375. + /*
  99376. + * Clear the FIFOs
  99377. + * Requires at least 2 PCM clock cycles to take effect
  99378. + */
  99379. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, clr, clr);
  99380. +
  99381. + /* Wait for 2 PCM clock cycles */
  99382. +
  99383. + /*
  99384. + * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
  99385. + * FIXME: This does not seem to work for slave mode!
  99386. + */
  99387. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &syncval);
  99388. + syncval &= BCM2708_I2S_SYNC;
  99389. +
  99390. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99391. + BCM2708_I2S_SYNC, ~syncval);
  99392. +
  99393. + /* Wait for the SYNC flag changing it's state */
  99394. + while (--timeout) {
  99395. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  99396. + if ((csreg & BCM2708_I2S_SYNC) != syncval)
  99397. + break;
  99398. + }
  99399. +
  99400. + if (!timeout)
  99401. + dev_err(dev->dev, "I2S SYNC error!\n");
  99402. +
  99403. + /* Stop clock if it was not running before */
  99404. + if (!clk_active_state)
  99405. + bcm2708_i2s_stop_clock(dev);
  99406. +
  99407. + /* Restore I2S state */
  99408. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99409. + BCM2708_I2S_RXON | BCM2708_I2S_TXON, i2s_active_state);
  99410. +}
  99411. +
  99412. +static int bcm2708_i2s_set_dai_fmt(struct snd_soc_dai *dai,
  99413. + unsigned int fmt)
  99414. +{
  99415. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99416. + dev->fmt = fmt;
  99417. + return 0;
  99418. +}
  99419. +
  99420. +static int bcm2708_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  99421. + unsigned int ratio)
  99422. +{
  99423. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99424. + dev->bclk_ratio = ratio;
  99425. + return 0;
  99426. +}
  99427. +
  99428. +static int bcm2708_i2s_hw_params(struct snd_pcm_substream *substream,
  99429. + struct snd_pcm_hw_params *params,
  99430. + struct snd_soc_dai *dai)
  99431. +{
  99432. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99433. +
  99434. + unsigned int sampling_rate = params_rate(params);
  99435. + unsigned int data_length, data_delay, bclk_ratio;
  99436. + unsigned int ch1pos, ch2pos, mode, format;
  99437. + unsigned int mash = BCM2708_CLK_MASH_1;
  99438. + unsigned int divi, divf, target_frequency;
  99439. + int clk_src = -1;
  99440. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  99441. + bool bit_master = (master == SND_SOC_DAIFMT_CBS_CFS
  99442. + || master == SND_SOC_DAIFMT_CBS_CFM);
  99443. +
  99444. + bool frame_master = (master == SND_SOC_DAIFMT_CBS_CFS
  99445. + || master == SND_SOC_DAIFMT_CBM_CFS);
  99446. + uint32_t csreg;
  99447. +
  99448. + /*
  99449. + * If a stream is already enabled,
  99450. + * the registers are already set properly.
  99451. + */
  99452. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  99453. +
  99454. + if (csreg & (BCM2708_I2S_TXON | BCM2708_I2S_RXON))
  99455. + return 0;
  99456. +
  99457. + /*
  99458. + * Adjust the data length according to the format.
  99459. + * We prefill the half frame length with an integer
  99460. + * divider of 2400 as explained at the clock settings.
  99461. + * Maybe it is overwritten there, if the Integer mode
  99462. + * does not apply.
  99463. + */
  99464. + switch (params_format(params)) {
  99465. + case SNDRV_PCM_FORMAT_S16_LE:
  99466. + data_length = 16;
  99467. + bclk_ratio = 40;
  99468. + break;
  99469. + case SNDRV_PCM_FORMAT_S24_LE:
  99470. + data_length = 24;
  99471. + bclk_ratio = 40;
  99472. + break;
  99473. + case SNDRV_PCM_FORMAT_S32_LE:
  99474. + data_length = 32;
  99475. + bclk_ratio = 80;
  99476. + break;
  99477. + default:
  99478. + return -EINVAL;
  99479. + }
  99480. +
  99481. + /* If bclk_ratio already set, use that one. */
  99482. + if (dev->bclk_ratio)
  99483. + bclk_ratio = dev->bclk_ratio;
  99484. +
  99485. + /*
  99486. + * Clock Settings
  99487. + *
  99488. + * The target frequency of the bit clock is
  99489. + * sampling rate * frame length
  99490. + *
  99491. + * Integer mode:
  99492. + * Sampling rates that are multiples of 8000 kHz
  99493. + * can be driven by the oscillator of 19.2 MHz
  99494. + * with an integer divider as long as the frame length
  99495. + * is an integer divider of 19200000/8000=2400 as set up above.
  99496. + * This is no longer possible if the sampling rate
  99497. + * is too high (e.g. 192 kHz), because the oscillator is too slow.
  99498. + *
  99499. + * MASH mode:
  99500. + * For all other sampling rates, it is not possible to
  99501. + * have an integer divider. Approximate the clock
  99502. + * with the MASH module that induces a slight frequency
  99503. + * variance. To minimize that it is best to have the fastest
  99504. + * clock here. That is PLLD with 500 MHz.
  99505. + */
  99506. + target_frequency = sampling_rate * bclk_ratio;
  99507. + clk_src = BCM2708_CLK_SRC_OSC;
  99508. + mash = BCM2708_CLK_MASH_0;
  99509. +
  99510. + if (bcm2708_clk_freq[clk_src] % target_frequency == 0
  99511. + && bit_master && frame_master) {
  99512. + divi = bcm2708_clk_freq[clk_src] / target_frequency;
  99513. + divf = 0;
  99514. + } else {
  99515. + uint64_t dividend;
  99516. +
  99517. + if (!dev->bclk_ratio) {
  99518. + /*
  99519. + * Overwrite bclk_ratio, because the
  99520. + * above trick is not needed or can
  99521. + * not be used.
  99522. + */
  99523. + bclk_ratio = 2 * data_length;
  99524. + }
  99525. +
  99526. + target_frequency = sampling_rate * bclk_ratio;
  99527. +
  99528. + clk_src = BCM2708_CLK_SRC_PLLD;
  99529. + mash = BCM2708_CLK_MASH_1;
  99530. +
  99531. + dividend = bcm2708_clk_freq[clk_src];
  99532. + dividend <<= BCM2708_CLK_SHIFT;
  99533. + do_div(dividend, target_frequency);
  99534. + divi = dividend >> BCM2708_CLK_SHIFT;
  99535. + divf = dividend & BCM2708_CLK_DIVF_MASK;
  99536. + }
  99537. +
  99538. + /* Set clock divider */
  99539. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMDIV_REG, BCM2708_CLK_PASSWD
  99540. + | BCM2708_CLK_DIVI(divi)
  99541. + | BCM2708_CLK_DIVF(divf));
  99542. +
  99543. + /* Setup clock, but don't start it yet */
  99544. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, BCM2708_CLK_PASSWD
  99545. + | BCM2708_CLK_MASH(mash)
  99546. + | BCM2708_CLK_SRC(clk_src));
  99547. +
  99548. + /* Setup the frame format */
  99549. + format = BCM2708_I2S_CHEN;
  99550. +
  99551. + if (data_length >= 24)
  99552. + format |= BCM2708_I2S_CHWEX;
  99553. +
  99554. + format |= BCM2708_I2S_CHWID((data_length-8)&0xf);
  99555. +
  99556. + switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  99557. + case SND_SOC_DAIFMT_I2S:
  99558. + data_delay = 1;
  99559. + break;
  99560. + default:
  99561. + /*
  99562. + * TODO
  99563. + * Others are possible but are not implemented at the moment.
  99564. + */
  99565. + dev_err(dev->dev, "%s:bad format\n", __func__);
  99566. + return -EINVAL;
  99567. + }
  99568. +
  99569. + ch1pos = data_delay;
  99570. + ch2pos = bclk_ratio / 2 + data_delay;
  99571. +
  99572. + switch (params_channels(params)) {
  99573. + case 2:
  99574. + format = BCM2708_I2S_CH1(format) | BCM2708_I2S_CH2(format);
  99575. + format |= BCM2708_I2S_CH1(BCM2708_I2S_CHPOS(ch1pos));
  99576. + format |= BCM2708_I2S_CH2(BCM2708_I2S_CHPOS(ch2pos));
  99577. + break;
  99578. + default:
  99579. + return -EINVAL;
  99580. + }
  99581. +
  99582. + /*
  99583. + * Set format for both streams.
  99584. + * We cannot set another frame length
  99585. + * (and therefore word length) anyway,
  99586. + * so the format will be the same.
  99587. + */
  99588. + regmap_write(dev->i2s_regmap, BCM2708_I2S_RXC_A_REG, format);
  99589. + regmap_write(dev->i2s_regmap, BCM2708_I2S_TXC_A_REG, format);
  99590. +
  99591. + /* Setup the I2S mode */
  99592. + mode = 0;
  99593. +
  99594. + if (data_length <= 16) {
  99595. + /*
  99596. + * Use frame packed mode (2 channels per 32 bit word)
  99597. + * We cannot set another frame length in the second stream
  99598. + * (and therefore word length) anyway,
  99599. + * so the format will be the same.
  99600. + */
  99601. + mode |= BCM2708_I2S_FTXP | BCM2708_I2S_FRXP;
  99602. + }
  99603. +
  99604. + mode |= BCM2708_I2S_FLEN(bclk_ratio - 1);
  99605. + mode |= BCM2708_I2S_FSLEN(bclk_ratio / 2);
  99606. +
  99607. + /* Master or slave? */
  99608. + switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  99609. + case SND_SOC_DAIFMT_CBS_CFS:
  99610. + /* CPU is master */
  99611. + break;
  99612. + case SND_SOC_DAIFMT_CBM_CFS:
  99613. + /*
  99614. + * CODEC is bit clock master
  99615. + * CPU is frame master
  99616. + */
  99617. + mode |= BCM2708_I2S_CLKM;
  99618. + break;
  99619. + case SND_SOC_DAIFMT_CBS_CFM:
  99620. + /*
  99621. + * CODEC is frame master
  99622. + * CPU is bit clock master
  99623. + */
  99624. + mode |= BCM2708_I2S_FSM;
  99625. + break;
  99626. + case SND_SOC_DAIFMT_CBM_CFM:
  99627. + /* CODEC is master */
  99628. + mode |= BCM2708_I2S_CLKM;
  99629. + mode |= BCM2708_I2S_FSM;
  99630. + break;
  99631. + default:
  99632. + dev_err(dev->dev, "%s:bad master\n", __func__);
  99633. + return -EINVAL;
  99634. + }
  99635. +
  99636. + /*
  99637. + * Invert clocks?
  99638. + *
  99639. + * The BCM approach seems to be inverted to the classical I2S approach.
  99640. + */
  99641. + switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
  99642. + case SND_SOC_DAIFMT_NB_NF:
  99643. + /* None. Therefore, both for BCM */
  99644. + mode |= BCM2708_I2S_CLKI;
  99645. + mode |= BCM2708_I2S_FSI;
  99646. + break;
  99647. + case SND_SOC_DAIFMT_IB_IF:
  99648. + /* Both. Therefore, none for BCM */
  99649. + break;
  99650. + case SND_SOC_DAIFMT_NB_IF:
  99651. + /*
  99652. + * Invert only frame sync. Therefore,
  99653. + * invert only bit clock for BCM
  99654. + */
  99655. + mode |= BCM2708_I2S_CLKI;
  99656. + break;
  99657. + case SND_SOC_DAIFMT_IB_NF:
  99658. + /*
  99659. + * Invert only bit clock. Therefore,
  99660. + * invert only frame sync for BCM
  99661. + */
  99662. + mode |= BCM2708_I2S_FSI;
  99663. + break;
  99664. + default:
  99665. + return -EINVAL;
  99666. + }
  99667. +
  99668. + regmap_write(dev->i2s_regmap, BCM2708_I2S_MODE_A_REG, mode);
  99669. +
  99670. + /* Setup the DMA parameters */
  99671. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99672. + BCM2708_I2S_RXTHR(1)
  99673. + | BCM2708_I2S_TXTHR(1)
  99674. + | BCM2708_I2S_DMAEN, 0xffffffff);
  99675. +
  99676. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_DREQ_A_REG,
  99677. + BCM2708_I2S_TX_PANIC(0x10)
  99678. + | BCM2708_I2S_RX_PANIC(0x30)
  99679. + | BCM2708_I2S_TX(0x30)
  99680. + | BCM2708_I2S_RX(0x20), 0xffffffff);
  99681. +
  99682. + /* Clear FIFOs */
  99683. + bcm2708_i2s_clear_fifos(dev, true, true);
  99684. +
  99685. + return 0;
  99686. +}
  99687. +
  99688. +static int bcm2708_i2s_prepare(struct snd_pcm_substream *substream,
  99689. + struct snd_soc_dai *dai)
  99690. +{
  99691. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99692. + uint32_t cs_reg;
  99693. +
  99694. + bcm2708_i2s_start_clock(dev);
  99695. +
  99696. + /*
  99697. + * Clear both FIFOs if the one that should be started
  99698. + * is not empty at the moment. This should only happen
  99699. + * after overrun. Otherwise, hw_params would have cleared
  99700. + * the FIFO.
  99701. + */
  99702. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &cs_reg);
  99703. +
  99704. + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  99705. + && !(cs_reg & BCM2708_I2S_TXE))
  99706. + bcm2708_i2s_clear_fifos(dev, true, false);
  99707. + else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
  99708. + && (cs_reg & BCM2708_I2S_RXD))
  99709. + bcm2708_i2s_clear_fifos(dev, false, true);
  99710. +
  99711. + return 0;
  99712. +}
  99713. +
  99714. +static void bcm2708_i2s_stop(struct bcm2708_i2s_dev *dev,
  99715. + struct snd_pcm_substream *substream,
  99716. + struct snd_soc_dai *dai)
  99717. +{
  99718. + uint32_t mask;
  99719. +
  99720. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  99721. + mask = BCM2708_I2S_RXON;
  99722. + else
  99723. + mask = BCM2708_I2S_TXON;
  99724. +
  99725. + regmap_update_bits(dev->i2s_regmap,
  99726. + BCM2708_I2S_CS_A_REG, mask, 0);
  99727. +
  99728. + /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
  99729. + if (!dai->active && !(dev->fmt & SND_SOC_DAIFMT_CONT))
  99730. + bcm2708_i2s_stop_clock(dev);
  99731. +}
  99732. +
  99733. +static int bcm2708_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  99734. + struct snd_soc_dai *dai)
  99735. +{
  99736. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99737. + uint32_t mask;
  99738. +
  99739. + switch (cmd) {
  99740. + case SNDRV_PCM_TRIGGER_START:
  99741. + case SNDRV_PCM_TRIGGER_RESUME:
  99742. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  99743. + bcm2708_i2s_start_clock(dev);
  99744. +
  99745. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  99746. + mask = BCM2708_I2S_RXON;
  99747. + else
  99748. + mask = BCM2708_I2S_TXON;
  99749. +
  99750. + regmap_update_bits(dev->i2s_regmap,
  99751. + BCM2708_I2S_CS_A_REG, mask, mask);
  99752. + break;
  99753. +
  99754. + case SNDRV_PCM_TRIGGER_STOP:
  99755. + case SNDRV_PCM_TRIGGER_SUSPEND:
  99756. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  99757. + bcm2708_i2s_stop(dev, substream, dai);
  99758. + break;
  99759. + default:
  99760. + return -EINVAL;
  99761. + }
  99762. +
  99763. + return 0;
  99764. +}
  99765. +
  99766. +static int bcm2708_i2s_startup(struct snd_pcm_substream *substream,
  99767. + struct snd_soc_dai *dai)
  99768. +{
  99769. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99770. +
  99771. + if (dai->active)
  99772. + return 0;
  99773. +
  99774. + /* Should this still be running stop it */
  99775. + bcm2708_i2s_stop_clock(dev);
  99776. +
  99777. + /* Enable PCM block */
  99778. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99779. + BCM2708_I2S_EN, BCM2708_I2S_EN);
  99780. +
  99781. + /*
  99782. + * Disable STBY.
  99783. + * Requires at least 4 PCM clock cycles to take effect.
  99784. + */
  99785. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99786. + BCM2708_I2S_STBY, BCM2708_I2S_STBY);
  99787. +
  99788. + return 0;
  99789. +}
  99790. +
  99791. +static void bcm2708_i2s_shutdown(struct snd_pcm_substream *substream,
  99792. + struct snd_soc_dai *dai)
  99793. +{
  99794. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99795. +
  99796. + bcm2708_i2s_stop(dev, substream, dai);
  99797. +
  99798. + /* If both streams are stopped, disable module and clock */
  99799. + if (dai->active)
  99800. + return;
  99801. +
  99802. + /* Disable the module */
  99803. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99804. + BCM2708_I2S_EN, 0);
  99805. +
  99806. + /*
  99807. + * Stopping clock is necessary, because stop does
  99808. + * not stop the clock when SND_SOC_DAIFMT_CONT
  99809. + */
  99810. + bcm2708_i2s_stop_clock(dev);
  99811. +}
  99812. +
  99813. +static const struct snd_soc_dai_ops bcm2708_i2s_dai_ops = {
  99814. + .startup = bcm2708_i2s_startup,
  99815. + .shutdown = bcm2708_i2s_shutdown,
  99816. + .prepare = bcm2708_i2s_prepare,
  99817. + .trigger = bcm2708_i2s_trigger,
  99818. + .hw_params = bcm2708_i2s_hw_params,
  99819. + .set_fmt = bcm2708_i2s_set_dai_fmt,
  99820. + .set_bclk_ratio = bcm2708_i2s_set_dai_bclk_ratio
  99821. +};
  99822. +
  99823. +static int bcm2708_i2s_dai_probe(struct snd_soc_dai *dai)
  99824. +{
  99825. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99826. +
  99827. + dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  99828. + dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  99829. +
  99830. + return 0;
  99831. +}
  99832. +
  99833. +static struct snd_soc_dai_driver bcm2708_i2s_dai = {
  99834. + .name = "bcm2708-i2s",
  99835. + .probe = bcm2708_i2s_dai_probe,
  99836. + .playback = {
  99837. + .channels_min = 2,
  99838. + .channels_max = 2,
  99839. + .rates = SNDRV_PCM_RATE_8000_192000,
  99840. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  99841. + | SNDRV_PCM_FMTBIT_S24_LE
  99842. + | SNDRV_PCM_FMTBIT_S32_LE
  99843. + },
  99844. + .capture = {
  99845. + .channels_min = 2,
  99846. + .channels_max = 2,
  99847. + .rates = SNDRV_PCM_RATE_8000_192000,
  99848. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  99849. + | SNDRV_PCM_FMTBIT_S24_LE
  99850. + | SNDRV_PCM_FMTBIT_S32_LE
  99851. + },
  99852. + .ops = &bcm2708_i2s_dai_ops,
  99853. + .symmetric_rates = 1
  99854. +};
  99855. +
  99856. +static bool bcm2708_i2s_volatile_reg(struct device *dev, unsigned int reg)
  99857. +{
  99858. + switch (reg) {
  99859. + case BCM2708_I2S_CS_A_REG:
  99860. + case BCM2708_I2S_FIFO_A_REG:
  99861. + case BCM2708_I2S_INTSTC_A_REG:
  99862. + case BCM2708_I2S_GRAY_REG:
  99863. + return true;
  99864. + default:
  99865. + return false;
  99866. + };
  99867. +}
  99868. +
  99869. +static bool bcm2708_i2s_precious_reg(struct device *dev, unsigned int reg)
  99870. +{
  99871. + switch (reg) {
  99872. + case BCM2708_I2S_FIFO_A_REG:
  99873. + return true;
  99874. + default:
  99875. + return false;
  99876. + };
  99877. +}
  99878. +
  99879. +static bool bcm2708_clk_volatile_reg(struct device *dev, unsigned int reg)
  99880. +{
  99881. + switch (reg) {
  99882. + case BCM2708_CLK_PCMCTL_REG:
  99883. + return true;
  99884. + default:
  99885. + return false;
  99886. + };
  99887. +}
  99888. +
  99889. +static const struct regmap_config bcm2708_regmap_config[] = {
  99890. + {
  99891. + .reg_bits = 32,
  99892. + .reg_stride = 4,
  99893. + .val_bits = 32,
  99894. + .max_register = BCM2708_I2S_GRAY_REG,
  99895. + .precious_reg = bcm2708_i2s_precious_reg,
  99896. + .volatile_reg = bcm2708_i2s_volatile_reg,
  99897. + .cache_type = REGCACHE_RBTREE,
  99898. + },
  99899. + {
  99900. + .reg_bits = 32,
  99901. + .reg_stride = 4,
  99902. + .val_bits = 32,
  99903. + .max_register = BCM2708_CLK_PCMDIV_REG,
  99904. + .volatile_reg = bcm2708_clk_volatile_reg,
  99905. + .cache_type = REGCACHE_RBTREE,
  99906. + },
  99907. +};
  99908. +
  99909. +static const struct snd_soc_component_driver bcm2708_i2s_component = {
  99910. + .name = "bcm2708-i2s-comp",
  99911. +};
  99912. +
  99913. +
  99914. +static void bcm2708_i2s_setup_gpio(void)
  99915. +{
  99916. + /*
  99917. + * This is the common way to handle the GPIO pins for
  99918. + * the Raspberry Pi.
  99919. + * TODO Better way would be to handle
  99920. + * this in the device tree!
  99921. + */
  99922. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  99923. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  99924. +
  99925. + unsigned int *gpio;
  99926. + int pin;
  99927. + gpio = ioremap(GPIO_BASE, SZ_16K);
  99928. +
  99929. + /* SPI is on GPIO 7..11 */
  99930. + for (pin = 28; pin <= 31; pin++) {
  99931. + INP_GPIO(pin); /* set mode to GPIO input first */
  99932. + SET_GPIO_ALT(pin, 2); /* set mode to ALT 0 */
  99933. + }
  99934. +#undef INP_GPIO
  99935. +#undef SET_GPIO_ALT
  99936. +}
  99937. +
  99938. +static const struct snd_pcm_hardware bcm2708_pcm_hardware = {
  99939. + .info = SNDRV_PCM_INFO_INTERLEAVED |
  99940. + SNDRV_PCM_INFO_JOINT_DUPLEX,
  99941. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  99942. + SNDRV_PCM_FMTBIT_S24_LE |
  99943. + SNDRV_PCM_FMTBIT_S32_LE,
  99944. + .period_bytes_min = 32,
  99945. + .period_bytes_max = 64 * PAGE_SIZE,
  99946. + .periods_min = 2,
  99947. + .periods_max = 255,
  99948. + .buffer_bytes_max = 128 * PAGE_SIZE,
  99949. +};
  99950. +
  99951. +static const struct snd_dmaengine_pcm_config bcm2708_dmaengine_pcm_config = {
  99952. + .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  99953. + .pcm_hardware = &bcm2708_pcm_hardware,
  99954. + .prealloc_buffer_size = 256 * PAGE_SIZE,
  99955. +};
  99956. +
  99957. +
  99958. +static int bcm2708_i2s_probe(struct platform_device *pdev)
  99959. +{
  99960. + struct bcm2708_i2s_dev *dev;
  99961. + int i;
  99962. + int ret;
  99963. + struct regmap *regmap[2];
  99964. + struct resource *mem[2];
  99965. +
  99966. + /* Request both ioareas */
  99967. + for (i = 0; i <= 1; i++) {
  99968. + void __iomem *base;
  99969. +
  99970. + mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
  99971. + base = devm_ioremap_resource(&pdev->dev, mem[i]);
  99972. + if (IS_ERR(base))
  99973. + return PTR_ERR(base);
  99974. +
  99975. + regmap[i] = devm_regmap_init_mmio(&pdev->dev, base,
  99976. + &bcm2708_regmap_config[i]);
  99977. + if (IS_ERR(regmap[i])) {
  99978. + dev_err(&pdev->dev, "I2S probe: regmap init failed\n");
  99979. + return PTR_ERR(regmap[i]);
  99980. + }
  99981. + }
  99982. +
  99983. + dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
  99984. + GFP_KERNEL);
  99985. + if (IS_ERR(dev))
  99986. + return PTR_ERR(dev);
  99987. +
  99988. + bcm2708_i2s_setup_gpio();
  99989. +
  99990. + dev->i2s_regmap = regmap[0];
  99991. + dev->clk_regmap = regmap[1];
  99992. +
  99993. + /* Set the DMA address */
  99994. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  99995. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  99996. +
  99997. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  99998. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  99999. +
  100000. + /* Set the DREQ */
  100001. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].slave_id =
  100002. + BCM2708_DMA_DREQ_PCM_TX;
  100003. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].slave_id =
  100004. + BCM2708_DMA_DREQ_PCM_RX;
  100005. +
  100006. + /* Set the bus width */
  100007. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
  100008. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  100009. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
  100010. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  100011. +
  100012. + /* Set burst */
  100013. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
  100014. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
  100015. +
  100016. + /* BCLK ratio - use default */
  100017. + dev->bclk_ratio = 0;
  100018. +
  100019. + /* Store the pdev */
  100020. + dev->dev = &pdev->dev;
  100021. + dev_set_drvdata(&pdev->dev, dev);
  100022. +
  100023. + ret = snd_soc_register_component(&pdev->dev,
  100024. + &bcm2708_i2s_component, &bcm2708_i2s_dai, 1);
  100025. +
  100026. + if (ret) {
  100027. + dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  100028. + ret = -ENOMEM;
  100029. + return ret;
  100030. + }
  100031. +
  100032. + ret = snd_dmaengine_pcm_register(&pdev->dev,
  100033. + &bcm2708_dmaengine_pcm_config,
  100034. + SND_DMAENGINE_PCM_FLAG_COMPAT);
  100035. + if (ret) {
  100036. + dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  100037. + snd_soc_unregister_component(&pdev->dev);
  100038. + return ret;
  100039. + }
  100040. +
  100041. + return 0;
  100042. +}
  100043. +
  100044. +static int bcm2708_i2s_remove(struct platform_device *pdev)
  100045. +{
  100046. + snd_dmaengine_pcm_unregister(&pdev->dev);
  100047. + snd_soc_unregister_component(&pdev->dev);
  100048. + return 0;
  100049. +}
  100050. +
  100051. +static struct platform_driver bcm2708_i2s_driver = {
  100052. + .probe = bcm2708_i2s_probe,
  100053. + .remove = bcm2708_i2s_remove,
  100054. + .driver = {
  100055. + .name = "bcm2708-i2s",
  100056. + .owner = THIS_MODULE,
  100057. + },
  100058. +};
  100059. +
  100060. +module_platform_driver(bcm2708_i2s_driver);
  100061. +
  100062. +MODULE_ALIAS("platform:bcm2708-i2s");
  100063. +MODULE_DESCRIPTION("BCM2708 I2S interface");
  100064. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100065. +MODULE_LICENSE("GPL v2");
  100066. diff -Nur linux-3.12.13/sound/soc/bcm/hifiberry_dac.c linux-raspberry-pi/sound/soc/bcm/hifiberry_dac.c
  100067. --- linux-3.12.13/sound/soc/bcm/hifiberry_dac.c 1970-01-01 01:00:00.000000000 +0100
  100068. +++ linux-raspberry-pi/sound/soc/bcm/hifiberry_dac.c 2014-03-11 17:33:43.000000000 +0100
  100069. @@ -0,0 +1,100 @@
  100070. +/*
  100071. + * ASoC Driver for HifiBerry DAC
  100072. + *
  100073. + * Author: Florian Meier <florian.meier@koalo.de>
  100074. + * Copyright 2013
  100075. + *
  100076. + * This program is free software; you can redistribute it and/or
  100077. + * modify it under the terms of the GNU General Public License
  100078. + * version 2 as published by the Free Software Foundation.
  100079. + *
  100080. + * This program is distributed in the hope that it will be useful, but
  100081. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100082. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100083. + * General Public License for more details.
  100084. + */
  100085. +
  100086. +#include <linux/module.h>
  100087. +#include <linux/platform_device.h>
  100088. +
  100089. +#include <sound/core.h>
  100090. +#include <sound/pcm.h>
  100091. +#include <sound/pcm_params.h>
  100092. +#include <sound/soc.h>
  100093. +#include <sound/jack.h>
  100094. +
  100095. +static int snd_rpi_hifiberry_dac_init(struct snd_soc_pcm_runtime *rtd)
  100096. +{
  100097. + return 0;
  100098. +}
  100099. +
  100100. +static int snd_rpi_hifiberry_dac_hw_params(struct snd_pcm_substream *substream,
  100101. + struct snd_pcm_hw_params *params)
  100102. +{
  100103. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100104. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100105. +
  100106. + unsigned int sample_bits =
  100107. + snd_pcm_format_physical_width(params_format(params));
  100108. +
  100109. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  100110. +}
  100111. +
  100112. +/* machine stream operations */
  100113. +static struct snd_soc_ops snd_rpi_hifiberry_dac_ops = {
  100114. + .hw_params = snd_rpi_hifiberry_dac_hw_params,
  100115. +};
  100116. +
  100117. +static struct snd_soc_dai_link snd_rpi_hifiberry_dac_dai[] = {
  100118. +{
  100119. + .name = "HifiBerry DAC",
  100120. + .stream_name = "HifiBerry DAC HiFi",
  100121. + .cpu_dai_name = "bcm2708-i2s.0",
  100122. + .codec_dai_name = "pcm5102a-hifi",
  100123. + .platform_name = "bcm2708-i2s.0",
  100124. + .codec_name = "pcm5102a-codec",
  100125. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100126. + SND_SOC_DAIFMT_CBS_CFS,
  100127. + .ops = &snd_rpi_hifiberry_dac_ops,
  100128. + .init = snd_rpi_hifiberry_dac_init,
  100129. +},
  100130. +};
  100131. +
  100132. +/* audio machine driver */
  100133. +static struct snd_soc_card snd_rpi_hifiberry_dac = {
  100134. + .name = "snd_rpi_hifiberry_dac",
  100135. + .dai_link = snd_rpi_hifiberry_dac_dai,
  100136. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dac_dai),
  100137. +};
  100138. +
  100139. +static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
  100140. +{
  100141. + int ret = 0;
  100142. +
  100143. + snd_rpi_hifiberry_dac.dev = &pdev->dev;
  100144. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dac);
  100145. + if (ret)
  100146. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100147. +
  100148. + return ret;
  100149. +}
  100150. +
  100151. +static int snd_rpi_hifiberry_dac_remove(struct platform_device *pdev)
  100152. +{
  100153. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dac);
  100154. +}
  100155. +
  100156. +static struct platform_driver snd_rpi_hifiberry_dac_driver = {
  100157. + .driver = {
  100158. + .name = "snd-hifiberry-dac",
  100159. + .owner = THIS_MODULE,
  100160. + },
  100161. + .probe = snd_rpi_hifiberry_dac_probe,
  100162. + .remove = snd_rpi_hifiberry_dac_remove,
  100163. +};
  100164. +
  100165. +module_platform_driver(snd_rpi_hifiberry_dac_driver);
  100166. +
  100167. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100168. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry DAC");
  100169. +MODULE_LICENSE("GPL v2");
  100170. diff -Nur linux-3.12.13/sound/soc/bcm/hifiberry_digi.c linux-raspberry-pi/sound/soc/bcm/hifiberry_digi.c
  100171. --- linux-3.12.13/sound/soc/bcm/hifiberry_digi.c 1970-01-01 01:00:00.000000000 +0100
  100172. +++ linux-raspberry-pi/sound/soc/bcm/hifiberry_digi.c 2014-03-11 17:51:48.000000000 +0100
  100173. @@ -0,0 +1,153 @@
  100174. +/*
  100175. + * ASoC Driver for HifiBerry Digi
  100176. + *
  100177. + * Author: Daniel Matuschek <info@crazy-audio.com>
  100178. + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
  100179. + * Copyright 2013
  100180. + *
  100181. + * This program is free software; you can redistribute it and/or
  100182. + * modify it under the terms of the GNU General Public License
  100183. + * version 2 as published by the Free Software Foundation.
  100184. + *
  100185. + * This program is distributed in the hope that it will be useful, but
  100186. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100187. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100188. + * General Public License for more details.
  100189. + */
  100190. +
  100191. +#include <linux/module.h>
  100192. +#include <linux/platform_device.h>
  100193. +
  100194. +#include <sound/core.h>
  100195. +#include <sound/pcm.h>
  100196. +#include <sound/pcm_params.h>
  100197. +#include <sound/soc.h>
  100198. +#include <sound/jack.h>
  100199. +
  100200. +#include "../codecs/wm8804.h"
  100201. +
  100202. +static int samplerate=44100;
  100203. +
  100204. +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
  100205. +{
  100206. + struct snd_soc_codec *codec = rtd->codec;
  100207. +
  100208. + /* enable TX output */
  100209. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  100210. +
  100211. + return 0;
  100212. +}
  100213. +
  100214. +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
  100215. + struct snd_pcm_hw_params *params)
  100216. +{
  100217. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100218. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  100219. + struct snd_soc_codec *codec = rtd->codec;
  100220. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100221. +
  100222. + int sysclk = 27000000; /* This is fixed on this board */
  100223. +
  100224. + long mclk_freq=0;
  100225. + int mclk_div=1;
  100226. +
  100227. + int ret;
  100228. +
  100229. + samplerate = params_rate(params);
  100230. +
  100231. + switch (samplerate) {
  100232. + case 44100:
  100233. + case 48000:
  100234. + case 88200:
  100235. + case 96000:
  100236. + mclk_freq=samplerate*256;
  100237. + mclk_div=WM8804_MCLKDIV_256FS;
  100238. + break;
  100239. + case 176400:
  100240. + case 192000:
  100241. + mclk_freq=samplerate*128;
  100242. + mclk_div=WM8804_MCLKDIV_128FS;
  100243. + break;
  100244. + default:
  100245. + dev_err(substream->pcm->dev,
  100246. + "Failed to set WM8804 SYSCLK, unsupported samplerate\n");
  100247. + }
  100248. +
  100249. + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  100250. + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  100251. +
  100252. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  100253. + sysclk, SND_SOC_CLOCK_OUT);
  100254. + if (ret < 0) {
  100255. + dev_err(substream->pcm->dev,
  100256. + "Failed to set WM8804 SYSCLK: %d\n", ret);
  100257. + return ret;
  100258. + }
  100259. +
  100260. + /* Enable TX output */
  100261. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  100262. +
  100263. + /* Power on */
  100264. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
  100265. +
  100266. + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
  100267. +}
  100268. +
  100269. +/* machine stream operations */
  100270. +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
  100271. + .hw_params = snd_rpi_hifiberry_digi_hw_params,
  100272. +};
  100273. +
  100274. +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
  100275. +{
  100276. + .name = "HifiBerry Digi",
  100277. + .stream_name = "HifiBerry Digi HiFi",
  100278. + .cpu_dai_name = "bcm2708-i2s.0",
  100279. + .codec_dai_name = "wm8804-spdif",
  100280. + .platform_name = "bcm2708-i2s.0",
  100281. + .codec_name = "wm8804.1-003b",
  100282. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100283. + SND_SOC_DAIFMT_CBM_CFM,
  100284. + .ops = &snd_rpi_hifiberry_digi_ops,
  100285. + .init = snd_rpi_hifiberry_digi_init,
  100286. +},
  100287. +};
  100288. +
  100289. +/* audio machine driver */
  100290. +static struct snd_soc_card snd_rpi_hifiberry_digi = {
  100291. + .name = "snd_rpi_hifiberry_digi",
  100292. + .dai_link = snd_rpi_hifiberry_digi_dai,
  100293. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
  100294. +};
  100295. +
  100296. +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
  100297. +{
  100298. + int ret = 0;
  100299. +
  100300. + snd_rpi_hifiberry_digi.dev = &pdev->dev;
  100301. + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
  100302. + if (ret)
  100303. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100304. +
  100305. + return ret;
  100306. +}
  100307. +
  100308. +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
  100309. +{
  100310. + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
  100311. +}
  100312. +
  100313. +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
  100314. + .driver = {
  100315. + .name = "snd-hifiberry-digi",
  100316. + .owner = THIS_MODULE,
  100317. + },
  100318. + .probe = snd_rpi_hifiberry_digi_probe,
  100319. + .remove = snd_rpi_hifiberry_digi_remove,
  100320. +};
  100321. +
  100322. +module_platform_driver(snd_rpi_hifiberry_digi_driver);
  100323. +
  100324. +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
  100325. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
  100326. +MODULE_LICENSE("GPL v2");
  100327. diff -Nur linux-3.12.13/sound/soc/bcm/Kconfig linux-raspberry-pi/sound/soc/bcm/Kconfig
  100328. --- linux-3.12.13/sound/soc/bcm/Kconfig 1970-01-01 01:00:00.000000000 +0100
  100329. +++ linux-raspberry-pi/sound/soc/bcm/Kconfig 2014-03-11 17:33:43.000000000 +0100
  100330. @@ -0,0 +1,31 @@
  100331. +config SND_BCM2708_SOC_I2S
  100332. + tristate "SoC Audio support for the Broadcom BCM2708 I2S module"
  100333. + depends on MACH_BCM2708
  100334. + select REGMAP_MMIO
  100335. + select SND_SOC_DMAENGINE_PCM
  100336. + select SND_SOC_GENERIC_DMAENGINE_PCM
  100337. + help
  100338. + Say Y or M if you want to add support for codecs attached to
  100339. + the BCM2708 I2S interface. You will also need
  100340. + to select the audio interfaces to support below.
  100341. +
  100342. +config SND_BCM2708_SOC_HIFIBERRY_DAC
  100343. + tristate "Support for HifiBerry DAC"
  100344. + depends on SND_BCM2708_SOC_I2S
  100345. + select SND_SOC_PCM5102A
  100346. + help
  100347. + Say Y or M if you want to add support for HifiBerry DAC.
  100348. +
  100349. +config SND_BCM2708_SOC_HIFIBERRY_DIGI
  100350. + tristate "Support for HifiBerry Digi"
  100351. + depends on SND_BCM2708_SOC_I2S
  100352. + select SND_SOC_WM8804
  100353. + help
  100354. + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
  100355. +
  100356. +config SND_BCM2708_SOC_RPI_DAC
  100357. + tristate "Support for RPi-DAC"
  100358. + depends on SND_BCM2708_SOC_I2S
  100359. + select SND_SOC_PCM1794A
  100360. + help
  100361. + Say Y or M if you want to add support for RPi-DAC.
  100362. diff -Nur linux-3.12.13/sound/soc/bcm/Makefile linux-raspberry-pi/sound/soc/bcm/Makefile
  100363. --- linux-3.12.13/sound/soc/bcm/Makefile 1970-01-01 01:00:00.000000000 +0100
  100364. +++ linux-raspberry-pi/sound/soc/bcm/Makefile 2014-03-11 17:33:43.000000000 +0100
  100365. @@ -0,0 +1,13 @@
  100366. +# BCM2708 Platform Support
  100367. +snd-soc-bcm2708-i2s-objs := bcm2708-i2s.o
  100368. +
  100369. +obj-$(CONFIG_SND_BCM2708_SOC_I2S) += snd-soc-bcm2708-i2s.o
  100370. +
  100371. +# BCM2708 Machine Support
  100372. +snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  100373. +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  100374. +snd-soc-rpi-dac-objs := rpi-dac.o
  100375. +
  100376. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  100377. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  100378. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  100379. diff -Nur linux-3.12.13/sound/soc/bcm/rpi-dac.c linux-raspberry-pi/sound/soc/bcm/rpi-dac.c
  100380. --- linux-3.12.13/sound/soc/bcm/rpi-dac.c 1970-01-01 01:00:00.000000000 +0100
  100381. +++ linux-raspberry-pi/sound/soc/bcm/rpi-dac.c 2014-03-11 17:33:43.000000000 +0100
  100382. @@ -0,0 +1,97 @@
  100383. +/*
  100384. + * ASoC Driver for RPi-DAC.
  100385. + *
  100386. + * Author: Florian Meier <florian.meier@koalo.de>
  100387. + * Copyright 2013
  100388. + *
  100389. + * This program is free software; you can redistribute it and/or
  100390. + * modify it under the terms of the GNU General Public License
  100391. + * version 2 as published by the Free Software Foundation.
  100392. + *
  100393. + * This program is distributed in the hope that it will be useful, but
  100394. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100395. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100396. + * General Public License for more details.
  100397. + */
  100398. +
  100399. +#include <linux/module.h>
  100400. +#include <linux/platform_device.h>
  100401. +
  100402. +#include <sound/core.h>
  100403. +#include <sound/pcm.h>
  100404. +#include <sound/pcm_params.h>
  100405. +#include <sound/soc.h>
  100406. +#include <sound/jack.h>
  100407. +
  100408. +static int snd_rpi_rpi_dac_init(struct snd_soc_pcm_runtime *rtd)
  100409. +{
  100410. + return 0;
  100411. +}
  100412. +
  100413. +static int snd_rpi_rpi_dac_hw_params(struct snd_pcm_substream *substream,
  100414. + struct snd_pcm_hw_params *params)
  100415. +{
  100416. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100417. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100418. +
  100419. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 32*2);
  100420. +}
  100421. +
  100422. +/* machine stream operations */
  100423. +static struct snd_soc_ops snd_rpi_rpi_dac_ops = {
  100424. + .hw_params = snd_rpi_rpi_dac_hw_params,
  100425. +};
  100426. +
  100427. +static struct snd_soc_dai_link snd_rpi_rpi_dac_dai[] = {
  100428. +{
  100429. + .name = "HifiBerry Mini",
  100430. + .stream_name = "HifiBerry Mini HiFi",
  100431. + .cpu_dai_name = "bcm2708-i2s.0",
  100432. + .codec_dai_name = "pcm1794a-hifi",
  100433. + .platform_name = "bcm2708-i2s.0",
  100434. + .codec_name = "pcm1794a-codec",
  100435. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100436. + SND_SOC_DAIFMT_CBS_CFS,
  100437. + .ops = &snd_rpi_rpi_dac_ops,
  100438. + .init = snd_rpi_rpi_dac_init,
  100439. +},
  100440. +};
  100441. +
  100442. +/* audio machine driver */
  100443. +static struct snd_soc_card snd_rpi_rpi_dac = {
  100444. + .name = "snd_rpi_rpi_dac",
  100445. + .dai_link = snd_rpi_rpi_dac_dai,
  100446. + .num_links = ARRAY_SIZE(snd_rpi_rpi_dac_dai),
  100447. +};
  100448. +
  100449. +static int snd_rpi_rpi_dac_probe(struct platform_device *pdev)
  100450. +{
  100451. + int ret = 0;
  100452. +
  100453. + snd_rpi_rpi_dac.dev = &pdev->dev;
  100454. + ret = snd_soc_register_card(&snd_rpi_rpi_dac);
  100455. + if (ret)
  100456. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100457. +
  100458. + return ret;
  100459. +}
  100460. +
  100461. +static int snd_rpi_rpi_dac_remove(struct platform_device *pdev)
  100462. +{
  100463. + return snd_soc_unregister_card(&snd_rpi_rpi_dac);
  100464. +}
  100465. +
  100466. +static struct platform_driver snd_rpi_rpi_dac_driver = {
  100467. + .driver = {
  100468. + .name = "snd-rpi-dac",
  100469. + .owner = THIS_MODULE,
  100470. + },
  100471. + .probe = snd_rpi_rpi_dac_probe,
  100472. + .remove = snd_rpi_rpi_dac_remove,
  100473. +};
  100474. +
  100475. +module_platform_driver(snd_rpi_rpi_dac_driver);
  100476. +
  100477. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100478. +MODULE_DESCRIPTION("ASoC Driver for RPi-DAC");
  100479. +MODULE_LICENSE("GPL v2");
  100480. diff -Nur linux-3.12.13/sound/soc/codecs/Kconfig linux-raspberry-pi/sound/soc/codecs/Kconfig
  100481. --- linux-3.12.13/sound/soc/codecs/Kconfig 2014-02-22 22:32:50.000000000 +0100
  100482. +++ linux-raspberry-pi/sound/soc/codecs/Kconfig 2014-03-11 17:51:48.000000000 +0100
  100483. @@ -59,6 +59,8 @@
  100484. select SND_SOC_PCM1681 if I2C
  100485. select SND_SOC_PCM1792A if SPI_MASTER
  100486. select SND_SOC_PCM3008
  100487. + select SND_SOC_PCM1794A
  100488. + select SND_SOC_PCM5102A
  100489. select SND_SOC_RT5631 if I2C
  100490. select SND_SOC_RT5640 if I2C
  100491. select SND_SOC_SGTL5000 if I2C
  100492. @@ -311,6 +313,12 @@
  100493. config SND_SOC_PCM3008
  100494. tristate
  100495. +config SND_SOC_PCM1794A
  100496. + tristate
  100497. +
  100498. +config SND_SOC_PCM5102A
  100499. + tristate
  100500. +
  100501. config SND_SOC_RT5631
  100502. tristate
  100503. diff -Nur linux-3.12.13/sound/soc/codecs/Makefile linux-raspberry-pi/sound/soc/codecs/Makefile
  100504. --- linux-3.12.13/sound/soc/codecs/Makefile 2014-02-22 22:32:50.000000000 +0100
  100505. +++ linux-raspberry-pi/sound/soc/codecs/Makefile 2014-03-11 17:51:48.000000000 +0100
  100506. @@ -46,6 +46,8 @@
  100507. snd-soc-pcm1681-objs := pcm1681.o
  100508. snd-soc-pcm1792a-codec-objs := pcm1792a.o
  100509. snd-soc-pcm3008-objs := pcm3008.o
  100510. +snd-soc-pcm1794a-objs := pcm1794a.o
  100511. +snd-soc-pcm5102a-objs := pcm5102a.o
  100512. snd-soc-rt5631-objs := rt5631.o
  100513. snd-soc-rt5640-objs := rt5640.o
  100514. snd-soc-sgtl5000-objs := sgtl5000.o
  100515. @@ -179,6 +181,8 @@
  100516. obj-$(CONFIG_SND_SOC_PCM1681) += snd-soc-pcm1681.o
  100517. obj-$(CONFIG_SND_SOC_PCM1792A) += snd-soc-pcm1792a-codec.o
  100518. obj-$(CONFIG_SND_SOC_PCM3008) += snd-soc-pcm3008.o
  100519. +obj-$(CONFIG_SND_SOC_PCM1794A) += snd-soc-pcm1794a.o
  100520. +obj-$(CONFIG_SND_SOC_PCM5102A) += snd-soc-pcm5102a.o
  100521. obj-$(CONFIG_SND_SOC_RT5631) += snd-soc-rt5631.o
  100522. obj-$(CONFIG_SND_SOC_RT5640) += snd-soc-rt5640.o
  100523. obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o
  100524. diff -Nur linux-3.12.13/sound/soc/codecs/pcm1794a.c linux-raspberry-pi/sound/soc/codecs/pcm1794a.c
  100525. --- linux-3.12.13/sound/soc/codecs/pcm1794a.c 1970-01-01 01:00:00.000000000 +0100
  100526. +++ linux-raspberry-pi/sound/soc/codecs/pcm1794a.c 2014-03-11 17:33:43.000000000 +0100
  100527. @@ -0,0 +1,62 @@
  100528. +/*
  100529. + * Driver for the PCM1794A codec
  100530. + *
  100531. + * Author: Florian Meier <florian.meier@koalo.de>
  100532. + * Copyright 2013
  100533. + *
  100534. + * This program is free software; you can redistribute it and/or
  100535. + * modify it under the terms of the GNU General Public License
  100536. + * version 2 as published by the Free Software Foundation.
  100537. + *
  100538. + * This program is distributed in the hope that it will be useful, but
  100539. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100540. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100541. + * General Public License for more details.
  100542. + */
  100543. +
  100544. +
  100545. +#include <linux/init.h>
  100546. +#include <linux/module.h>
  100547. +#include <linux/platform_device.h>
  100548. +
  100549. +#include <sound/soc.h>
  100550. +
  100551. +static struct snd_soc_dai_driver pcm1794a_dai = {
  100552. + .name = "pcm1794a-hifi",
  100553. + .playback = {
  100554. + .channels_min = 2,
  100555. + .channels_max = 2,
  100556. + .rates = SNDRV_PCM_RATE_8000_192000,
  100557. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  100558. + SNDRV_PCM_FMTBIT_S24_LE
  100559. + },
  100560. +};
  100561. +
  100562. +static struct snd_soc_codec_driver soc_codec_dev_pcm1794a;
  100563. +
  100564. +static int pcm1794a_probe(struct platform_device *pdev)
  100565. +{
  100566. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm1794a,
  100567. + &pcm1794a_dai, 1);
  100568. +}
  100569. +
  100570. +static int pcm1794a_remove(struct platform_device *pdev)
  100571. +{
  100572. + snd_soc_unregister_codec(&pdev->dev);
  100573. + return 0;
  100574. +}
  100575. +
  100576. +static struct platform_driver pcm1794a_codec_driver = {
  100577. + .probe = pcm1794a_probe,
  100578. + .remove = pcm1794a_remove,
  100579. + .driver = {
  100580. + .name = "pcm1794a-codec",
  100581. + .owner = THIS_MODULE,
  100582. + },
  100583. +};
  100584. +
  100585. +module_platform_driver(pcm1794a_codec_driver);
  100586. +
  100587. +MODULE_DESCRIPTION("ASoC PCM1794A codec driver");
  100588. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100589. +MODULE_LICENSE("GPL v2");
  100590. diff -Nur linux-3.12.13/sound/soc/codecs/pcm5102a.c linux-raspberry-pi/sound/soc/codecs/pcm5102a.c
  100591. --- linux-3.12.13/sound/soc/codecs/pcm5102a.c 1970-01-01 01:00:00.000000000 +0100
  100592. +++ linux-raspberry-pi/sound/soc/codecs/pcm5102a.c 2014-03-11 17:51:49.000000000 +0100
  100593. @@ -0,0 +1,63 @@
  100594. +/*
  100595. + * Driver for the PCM5102A codec
  100596. + *
  100597. + * Author: Florian Meier <florian.meier@koalo.de>
  100598. + * Copyright 2013
  100599. + *
  100600. + * This program is free software; you can redistribute it and/or
  100601. + * modify it under the terms of the GNU General Public License
  100602. + * version 2 as published by the Free Software Foundation.
  100603. + *
  100604. + * This program is distributed in the hope that it will be useful, but
  100605. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100606. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100607. + * General Public License for more details.
  100608. + */
  100609. +
  100610. +
  100611. +#include <linux/init.h>
  100612. +#include <linux/module.h>
  100613. +#include <linux/platform_device.h>
  100614. +
  100615. +#include <sound/soc.h>
  100616. +
  100617. +static struct snd_soc_dai_driver pcm5102a_dai = {
  100618. + .name = "pcm5102a-hifi",
  100619. + .playback = {
  100620. + .channels_min = 2,
  100621. + .channels_max = 2,
  100622. + .rates = SNDRV_PCM_RATE_8000_192000,
  100623. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  100624. + SNDRV_PCM_FMTBIT_S24_LE |
  100625. + SNDRV_PCM_FMTBIT_S32_LE
  100626. + },
  100627. +};
  100628. +
  100629. +static struct snd_soc_codec_driver soc_codec_dev_pcm5102a;
  100630. +
  100631. +static int pcm5102a_probe(struct platform_device *pdev)
  100632. +{
  100633. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm5102a,
  100634. + &pcm5102a_dai, 1);
  100635. +}
  100636. +
  100637. +static int pcm5102a_remove(struct platform_device *pdev)
  100638. +{
  100639. + snd_soc_unregister_codec(&pdev->dev);
  100640. + return 0;
  100641. +}
  100642. +
  100643. +static struct platform_driver pcm5102a_codec_driver = {
  100644. + .probe = pcm5102a_probe,
  100645. + .remove = pcm5102a_remove,
  100646. + .driver = {
  100647. + .name = "pcm5102a-codec",
  100648. + .owner = THIS_MODULE,
  100649. + },
  100650. +};
  100651. +
  100652. +module_platform_driver(pcm5102a_codec_driver);
  100653. +
  100654. +MODULE_DESCRIPTION("ASoC PCM5102A codec driver");
  100655. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100656. +MODULE_LICENSE("GPL v2");
  100657. diff -Nur linux-3.12.13/sound/soc/codecs/wm8804.c linux-raspberry-pi/sound/soc/codecs/wm8804.c
  100658. --- linux-3.12.13/sound/soc/codecs/wm8804.c 2014-02-22 22:32:50.000000000 +0100
  100659. +++ linux-raspberry-pi/sound/soc/codecs/wm8804.c 2014-03-11 17:33:43.000000000 +0100
  100660. @@ -63,6 +63,7 @@
  100661. struct regmap *regmap;
  100662. struct regulator_bulk_data supplies[WM8804_NUM_SUPPLIES];
  100663. struct notifier_block disable_nb[WM8804_NUM_SUPPLIES];
  100664. + int mclk_div;
  100665. };
  100666. static int txsrc_get(struct snd_kcontrol *kcontrol,
  100667. @@ -277,6 +278,7 @@
  100668. blen = 0x1;
  100669. break;
  100670. case SNDRV_PCM_FORMAT_S24_LE:
  100671. + case SNDRV_PCM_FORMAT_S32_LE:
  100672. blen = 0x2;
  100673. break;
  100674. default:
  100675. @@ -318,7 +320,7 @@
  100676. #define FIXED_PLL_SIZE ((1ULL << 22) * 10)
  100677. static int pll_factors(struct pll_div *pll_div, unsigned int target,
  100678. - unsigned int source)
  100679. + unsigned int source, unsigned int mclk_div)
  100680. {
  100681. u64 Kpart;
  100682. unsigned long int K, Ndiv, Nmod, tmp;
  100683. @@ -330,7 +332,8 @@
  100684. */
  100685. for (i = 0; i < ARRAY_SIZE(post_table); i++) {
  100686. tmp = target * post_table[i].div;
  100687. - if (tmp >= 90000000 && tmp <= 100000000) {
  100688. + if ((tmp >= 90000000 && tmp <= 100000000) &&
  100689. + (mclk_div == post_table[i].mclkdiv)) {
  100690. pll_div->freqmode = post_table[i].freqmode;
  100691. pll_div->mclkdiv = post_table[i].mclkdiv;
  100692. target *= post_table[i].div;
  100693. @@ -387,8 +390,11 @@
  100694. } else {
  100695. int ret;
  100696. struct pll_div pll_div;
  100697. + struct wm8804_priv *wm8804;
  100698. - ret = pll_factors(&pll_div, freq_out, freq_in);
  100699. + wm8804 = snd_soc_codec_get_drvdata(codec);
  100700. +
  100701. + ret = pll_factors(&pll_div, freq_out, freq_in, wm8804->mclk_div);
  100702. if (ret)
  100703. return ret;
  100704. @@ -452,6 +458,7 @@
  100705. int div_id, int div)
  100706. {
  100707. struct snd_soc_codec *codec;
  100708. + struct wm8804_priv *wm8804;
  100709. codec = dai->codec;
  100710. switch (div_id) {
  100711. @@ -459,6 +466,10 @@
  100712. snd_soc_update_bits(codec, WM8804_PLL5, 0x30,
  100713. (div & 0x3) << 4);
  100714. break;
  100715. + case WM8804_MCLK_DIV:
  100716. + wm8804 = snd_soc_codec_get_drvdata(codec);
  100717. + wm8804->mclk_div = div;
  100718. + break;
  100719. default:
  100720. dev_err(dai->dev, "Unknown clock divider: %d\n", div_id);
  100721. return -EINVAL;
  100722. @@ -641,7 +652,7 @@
  100723. };
  100724. #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  100725. - SNDRV_PCM_FMTBIT_S24_LE)
  100726. + SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  100727. #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  100728. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  100729. @@ -674,7 +685,7 @@
  100730. .suspend = wm8804_suspend,
  100731. .resume = wm8804_resume,
  100732. .set_bias_level = wm8804_set_bias_level,
  100733. - .idle_bias_off = true,
  100734. + .idle_bias_off = false,
  100735. .controls = wm8804_snd_controls,
  100736. .num_controls = ARRAY_SIZE(wm8804_snd_controls),
  100737. diff -Nur linux-3.12.13/sound/soc/codecs/wm8804.h linux-raspberry-pi/sound/soc/codecs/wm8804.h
  100738. --- linux-3.12.13/sound/soc/codecs/wm8804.h 2014-02-22 22:32:50.000000000 +0100
  100739. +++ linux-raspberry-pi/sound/soc/codecs/wm8804.h 2014-03-11 17:33:43.000000000 +0100
  100740. @@ -57,5 +57,9 @@
  100741. #define WM8804_CLKOUT_SRC_OSCCLK 4
  100742. #define WM8804_CLKOUT_DIV 1
  100743. +#define WM8804_MCLK_DIV 2
  100744. +
  100745. +#define WM8804_MCLKDIV_256FS 0
  100746. +#define WM8804_MCLKDIV_128FS 1
  100747. #endif /* _WM8804_H */
  100748. diff -Nur linux-3.12.13/sound/soc/Kconfig linux-raspberry-pi/sound/soc/Kconfig
  100749. --- linux-3.12.13/sound/soc/Kconfig 2014-02-22 22:32:50.000000000 +0100
  100750. +++ linux-raspberry-pi/sound/soc/Kconfig 2014-03-11 17:51:48.000000000 +0100
  100751. @@ -33,6 +33,7 @@
  100752. # All the supported SoCs
  100753. source "sound/soc/atmel/Kconfig"
  100754. source "sound/soc/au1x/Kconfig"
  100755. +source "sound/soc/bcm/Kconfig"
  100756. source "sound/soc/blackfin/Kconfig"
  100757. source "sound/soc/cirrus/Kconfig"
  100758. source "sound/soc/davinci/Kconfig"
  100759. diff -Nur linux-3.12.13/sound/soc/Makefile linux-raspberry-pi/sound/soc/Makefile
  100760. --- linux-3.12.13/sound/soc/Makefile 2014-02-22 22:32:50.000000000 +0100
  100761. +++ linux-raspberry-pi/sound/soc/Makefile 2014-03-11 17:51:48.000000000 +0100
  100762. @@ -10,6 +10,7 @@
  100763. obj-$(CONFIG_SND_SOC) += generic/
  100764. obj-$(CONFIG_SND_SOC) += atmel/
  100765. obj-$(CONFIG_SND_SOC) += au1x/
  100766. +obj-$(CONFIG_SND_SOC) += bcm/
  100767. obj-$(CONFIG_SND_SOC) += blackfin/
  100768. obj-$(CONFIG_SND_SOC) += cirrus/
  100769. obj-$(CONFIG_SND_SOC) += davinci/
  100770. diff -Nur linux-3.12.13/sound/soc/soc-core.c linux-raspberry-pi/sound/soc/soc-core.c
  100771. --- linux-3.12.13/sound/soc/soc-core.c 2014-02-22 22:32:50.000000000 +0100
  100772. +++ linux-raspberry-pi/sound/soc/soc-core.c 2014-03-11 17:51:55.000000000 +0100
  100773. @@ -3576,6 +3576,22 @@
  100774. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  100775. /**
  100776. + * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
  100777. + * @dai: DAI
  100778. + * @ratio Ratio of BCLK to Sample rate.
  100779. + *
  100780. + * Configures the DAI for a preset BCLK to sample rate ratio.
  100781. + */
  100782. +int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
  100783. +{
  100784. + if (dai->driver && dai->driver->ops->set_bclk_ratio)
  100785. + return dai->driver->ops->set_bclk_ratio(dai, ratio);
  100786. + else
  100787. + return -EINVAL;
  100788. +}
  100789. +EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
  100790. +
  100791. +/**
  100792. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  100793. * @dai: DAI
  100794. * @fmt: SND_SOC_DAIFMT_ format value.